]> git.lizzy.rs Git - rust.git/blob - src/librustc_typeck/check/intrinsic.rs
Auto merge of #67770 - Centril:reduce-diversity-2, r=petrochenkov
[rust.git] / src / librustc_typeck / check / intrinsic.rs
1 //! Type-checking for the rust-intrinsic and platform-intrinsic
2 //! intrinsics that the compiler exposes.
3
4 use crate::require_same_types;
5
6 use errors::struct_span_err;
7 use rustc::traits::{ObligationCause, ObligationCauseCode};
8 use rustc::ty::subst::Subst;
9 use rustc::ty::{self, Ty, TyCtxt};
10 use rustc_error_codes::*;
11 use rustc_hir as hir;
12 use rustc_span::symbol::Symbol;
13 use rustc_target::spec::abi::Abi;
14
15 use std::iter;
16
17 fn equate_intrinsic_type<'tcx>(
18     tcx: TyCtxt<'tcx>,
19     it: &hir::ForeignItem<'_>,
20     n_tps: usize,
21     abi: Abi,
22     safety: hir::Unsafety,
23     inputs: Vec<Ty<'tcx>>,
24     output: Ty<'tcx>,
25 ) {
26     let def_id = tcx.hir().local_def_id(it.hir_id);
27
28     match it.kind {
29         hir::ForeignItemKind::Fn(..) => {}
30         _ => {
31             struct_span_err!(tcx.sess, it.span, E0622, "intrinsic must be a function")
32                 .span_label(it.span, "expected a function")
33                 .emit();
34             return;
35         }
36     }
37
38     let i_n_tps = tcx.generics_of(def_id).own_counts().types;
39     if i_n_tps != n_tps {
40         let span = match it.kind {
41             hir::ForeignItemKind::Fn(_, _, ref generics) => generics.span,
42             _ => bug!(),
43         };
44
45         struct_span_err!(
46             tcx.sess,
47             span,
48             E0094,
49             "intrinsic has wrong number of type \
50                          parameters: found {}, expected {}",
51             i_n_tps,
52             n_tps
53         )
54         .span_label(span, format!("expected {} type parameter", n_tps))
55         .emit();
56         return;
57     }
58
59     let fty = tcx.mk_fn_ptr(ty::Binder::bind(tcx.mk_fn_sig(
60         inputs.into_iter(),
61         output,
62         false,
63         safety,
64         abi,
65     )));
66     let cause = ObligationCause::new(it.span, it.hir_id, ObligationCauseCode::IntrinsicType);
67     require_same_types(tcx, &cause, tcx.mk_fn_ptr(tcx.fn_sig(def_id)), fty);
68 }
69
70 /// Returns `true` if the given intrinsic is unsafe to call or not.
71 pub fn intrinsic_operation_unsafety(intrinsic: &str) -> hir::Unsafety {
72     match intrinsic {
73         "size_of" | "min_align_of" | "needs_drop" | "caller_location" | "size_of_val"
74         | "min_align_of_val" | "add_with_overflow" | "sub_with_overflow" | "mul_with_overflow"
75         | "wrapping_add" | "wrapping_sub" | "wrapping_mul" | "saturating_add"
76         | "saturating_sub" | "rotate_left" | "rotate_right" | "ctpop" | "ctlz" | "cttz"
77         | "bswap" | "bitreverse" | "discriminant_value" | "type_id" | "likely" | "unlikely"
78         | "minnumf32" | "minnumf64" | "maxnumf32" | "maxnumf64" | "type_name" => {
79             hir::Unsafety::Normal
80         }
81         _ => hir::Unsafety::Unsafe,
82     }
83 }
84
85 /// Remember to add all intrinsics here, in librustc_codegen_llvm/intrinsic.rs,
86 /// and in libcore/intrinsics.rs
87 pub fn check_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem<'_>) {
88     let param = |n| tcx.mk_ty_param(n, Symbol::intern(&format!("P{}", n)));
89     let name = it.ident.as_str();
90
91     let mk_va_list_ty = |mutbl| {
92         tcx.lang_items().va_list().map(|did| {
93             let region = tcx.mk_region(ty::ReLateBound(ty::INNERMOST, ty::BrAnon(0)));
94             let env_region = ty::ReLateBound(ty::INNERMOST, ty::BrEnv);
95             let va_list_ty = tcx.type_of(did).subst(tcx, &[region.into()]);
96             (
97                 tcx.mk_ref(tcx.mk_region(env_region), ty::TypeAndMut { ty: va_list_ty, mutbl }),
98                 va_list_ty,
99             )
100         })
101     };
102
103     let (n_tps, inputs, output, unsafety) = if name.starts_with("atomic_") {
104         let split: Vec<&str> = name.split('_').collect();
105         assert!(split.len() >= 2, "Atomic intrinsic in an incorrect format");
106
107         //We only care about the operation here
108         let (n_tps, inputs, output) = match split[1] {
109             "cxchg" | "cxchgweak" => (
110                 1,
111                 vec![tcx.mk_mut_ptr(param(0)), param(0), param(0)],
112                 tcx.intern_tup(&[param(0), tcx.types.bool]),
113             ),
114             "load" => (1, vec![tcx.mk_imm_ptr(param(0))], param(0)),
115             "store" => (1, vec![tcx.mk_mut_ptr(param(0)), param(0)], tcx.mk_unit()),
116
117             "xchg" | "xadd" | "xsub" | "and" | "nand" | "or" | "xor" | "max" | "min" | "umax"
118             | "umin" => (1, vec![tcx.mk_mut_ptr(param(0)), param(0)], param(0)),
119             "fence" | "singlethreadfence" => (0, Vec::new(), tcx.mk_unit()),
120             op => {
121                 struct_span_err!(
122                     tcx.sess,
123                     it.span,
124                     E0092,
125                     "unrecognized atomic operation function: `{}`",
126                     op
127                 )
128                 .span_label(it.span, "unrecognized atomic operation")
129                 .emit();
130                 return;
131             }
132         };
133         (n_tps, inputs, output, hir::Unsafety::Unsafe)
134     } else if &name[..] == "abort" || &name[..] == "unreachable" {
135         (0, Vec::new(), tcx.types.never, hir::Unsafety::Unsafe)
136     } else {
137         let unsafety = intrinsic_operation_unsafety(&name[..]);
138         let (n_tps, inputs, output) = match &name[..] {
139             "breakpoint" => (0, Vec::new(), tcx.mk_unit()),
140             "size_of" | "pref_align_of" | "min_align_of" => (1, Vec::new(), tcx.types.usize),
141             "size_of_val" | "min_align_of_val" => (
142                 1,
143                 vec![tcx.mk_imm_ref(
144                     tcx.mk_region(ty::ReLateBound(ty::INNERMOST, ty::BrAnon(0))),
145                     param(0),
146                 )],
147                 tcx.types.usize,
148             ),
149             "rustc_peek" => (1, vec![param(0)], param(0)),
150             "caller_location" => (0, vec![], tcx.caller_location_ty()),
151             "panic_if_uninhabited" => (1, Vec::new(), tcx.mk_unit()),
152             "init" => (1, Vec::new(), param(0)),
153             "uninit" => (1, Vec::new(), param(0)),
154             "forget" => (1, vec![param(0)], tcx.mk_unit()),
155             "transmute" => (2, vec![param(0)], param(1)),
156             "move_val_init" => (1, vec![tcx.mk_mut_ptr(param(0)), param(0)], tcx.mk_unit()),
157             "prefetch_read_data"
158             | "prefetch_write_data"
159             | "prefetch_read_instruction"
160             | "prefetch_write_instruction" => (
161                 1,
162                 vec![
163                     tcx.mk_ptr(ty::TypeAndMut { ty: param(0), mutbl: hir::Mutability::Not }),
164                     tcx.types.i32,
165                 ],
166                 tcx.mk_unit(),
167             ),
168             "drop_in_place" => (1, vec![tcx.mk_mut_ptr(param(0))], tcx.mk_unit()),
169             "needs_drop" => (1, Vec::new(), tcx.types.bool),
170
171             "type_name" => (1, Vec::new(), tcx.mk_static_str()),
172             "type_id" => (1, Vec::new(), tcx.types.u64),
173             "offset" | "arith_offset" => (
174                 1,
175                 vec![
176                     tcx.mk_ptr(ty::TypeAndMut { ty: param(0), mutbl: hir::Mutability::Not }),
177                     tcx.types.isize,
178                 ],
179                 tcx.mk_ptr(ty::TypeAndMut { ty: param(0), mutbl: hir::Mutability::Not }),
180             ),
181             "copy" | "copy_nonoverlapping" => (
182                 1,
183                 vec![
184                     tcx.mk_ptr(ty::TypeAndMut { ty: param(0), mutbl: hir::Mutability::Not }),
185                     tcx.mk_ptr(ty::TypeAndMut { ty: param(0), mutbl: hir::Mutability::Mut }),
186                     tcx.types.usize,
187                 ],
188                 tcx.mk_unit(),
189             ),
190             "volatile_copy_memory" | "volatile_copy_nonoverlapping_memory" => (
191                 1,
192                 vec![
193                     tcx.mk_ptr(ty::TypeAndMut { ty: param(0), mutbl: hir::Mutability::Mut }),
194                     tcx.mk_ptr(ty::TypeAndMut { ty: param(0), mutbl: hir::Mutability::Not }),
195                     tcx.types.usize,
196                 ],
197                 tcx.mk_unit(),
198             ),
199             "write_bytes" | "volatile_set_memory" => (
200                 1,
201                 vec![
202                     tcx.mk_ptr(ty::TypeAndMut { ty: param(0), mutbl: hir::Mutability::Mut }),
203                     tcx.types.u8,
204                     tcx.types.usize,
205                 ],
206                 tcx.mk_unit(),
207             ),
208             "sqrtf32" => (0, vec![tcx.types.f32], tcx.types.f32),
209             "sqrtf64" => (0, vec![tcx.types.f64], tcx.types.f64),
210             "powif32" => (0, vec![tcx.types.f32, tcx.types.i32], tcx.types.f32),
211             "powif64" => (0, vec![tcx.types.f64, tcx.types.i32], tcx.types.f64),
212             "sinf32" => (0, vec![tcx.types.f32], tcx.types.f32),
213             "sinf64" => (0, vec![tcx.types.f64], tcx.types.f64),
214             "cosf32" => (0, vec![tcx.types.f32], tcx.types.f32),
215             "cosf64" => (0, vec![tcx.types.f64], tcx.types.f64),
216             "powf32" => (0, vec![tcx.types.f32, tcx.types.f32], tcx.types.f32),
217             "powf64" => (0, vec![tcx.types.f64, tcx.types.f64], tcx.types.f64),
218             "expf32" => (0, vec![tcx.types.f32], tcx.types.f32),
219             "expf64" => (0, vec![tcx.types.f64], tcx.types.f64),
220             "exp2f32" => (0, vec![tcx.types.f32], tcx.types.f32),
221             "exp2f64" => (0, vec![tcx.types.f64], tcx.types.f64),
222             "logf32" => (0, vec![tcx.types.f32], tcx.types.f32),
223             "logf64" => (0, vec![tcx.types.f64], tcx.types.f64),
224             "log10f32" => (0, vec![tcx.types.f32], tcx.types.f32),
225             "log10f64" => (0, vec![tcx.types.f64], tcx.types.f64),
226             "log2f32" => (0, vec![tcx.types.f32], tcx.types.f32),
227             "log2f64" => (0, vec![tcx.types.f64], tcx.types.f64),
228             "fmaf32" => (0, vec![tcx.types.f32, tcx.types.f32, tcx.types.f32], tcx.types.f32),
229             "fmaf64" => (0, vec![tcx.types.f64, tcx.types.f64, tcx.types.f64], tcx.types.f64),
230             "fabsf32" => (0, vec![tcx.types.f32], tcx.types.f32),
231             "fabsf64" => (0, vec![tcx.types.f64], tcx.types.f64),
232             "minnumf32" => (0, vec![tcx.types.f32, tcx.types.f32], tcx.types.f32),
233             "minnumf64" => (0, vec![tcx.types.f64, tcx.types.f64], tcx.types.f64),
234             "maxnumf32" => (0, vec![tcx.types.f32, tcx.types.f32], tcx.types.f32),
235             "maxnumf64" => (0, vec![tcx.types.f64, tcx.types.f64], tcx.types.f64),
236             "copysignf32" => (0, vec![tcx.types.f32, tcx.types.f32], tcx.types.f32),
237             "copysignf64" => (0, vec![tcx.types.f64, tcx.types.f64], tcx.types.f64),
238             "floorf32" => (0, vec![tcx.types.f32], tcx.types.f32),
239             "floorf64" => (0, vec![tcx.types.f64], tcx.types.f64),
240             "ceilf32" => (0, vec![tcx.types.f32], tcx.types.f32),
241             "ceilf64" => (0, vec![tcx.types.f64], tcx.types.f64),
242             "truncf32" => (0, vec![tcx.types.f32], tcx.types.f32),
243             "truncf64" => (0, vec![tcx.types.f64], tcx.types.f64),
244             "rintf32" => (0, vec![tcx.types.f32], tcx.types.f32),
245             "rintf64" => (0, vec![tcx.types.f64], tcx.types.f64),
246             "nearbyintf32" => (0, vec![tcx.types.f32], tcx.types.f32),
247             "nearbyintf64" => (0, vec![tcx.types.f64], tcx.types.f64),
248             "roundf32" => (0, vec![tcx.types.f32], tcx.types.f32),
249             "roundf64" => (0, vec![tcx.types.f64], tcx.types.f64),
250
251             "volatile_load" | "unaligned_volatile_load" => {
252                 (1, vec![tcx.mk_imm_ptr(param(0))], param(0))
253             }
254             "volatile_store" | "unaligned_volatile_store" => {
255                 (1, vec![tcx.mk_mut_ptr(param(0)), param(0)], tcx.mk_unit())
256             }
257
258             "ctpop" | "ctlz" | "ctlz_nonzero" | "cttz" | "cttz_nonzero" | "bswap"
259             | "bitreverse" => (1, vec![param(0)], param(0)),
260
261             "add_with_overflow" | "sub_with_overflow" | "mul_with_overflow" => {
262                 (1, vec![param(0), param(0)], tcx.intern_tup(&[param(0), tcx.types.bool]))
263             }
264
265             "ptr_offset_from" => {
266                 (1, vec![tcx.mk_imm_ptr(param(0)), tcx.mk_imm_ptr(param(0))], tcx.types.isize)
267             }
268             "unchecked_div" | "unchecked_rem" | "exact_div" => {
269                 (1, vec![param(0), param(0)], param(0))
270             }
271             "unchecked_shl" | "unchecked_shr" | "rotate_left" | "rotate_right" => {
272                 (1, vec![param(0), param(0)], param(0))
273             }
274             "unchecked_add" | "unchecked_sub" | "unchecked_mul" => {
275                 (1, vec![param(0), param(0)], param(0))
276             }
277             "wrapping_add" | "wrapping_sub" | "wrapping_mul" => {
278                 (1, vec![param(0), param(0)], param(0))
279             }
280             "saturating_add" | "saturating_sub" => (1, vec![param(0), param(0)], param(0)),
281             "fadd_fast" | "fsub_fast" | "fmul_fast" | "fdiv_fast" | "frem_fast" => {
282                 (1, vec![param(0), param(0)], param(0))
283             }
284             "float_to_int_approx_unchecked" => (2, vec![param(0)], param(1)),
285
286             "assume" => (0, vec![tcx.types.bool], tcx.mk_unit()),
287             "likely" => (0, vec![tcx.types.bool], tcx.types.bool),
288             "unlikely" => (0, vec![tcx.types.bool], tcx.types.bool),
289
290             "discriminant_value" => (
291                 1,
292                 vec![tcx.mk_imm_ref(
293                     tcx.mk_region(ty::ReLateBound(ty::INNERMOST, ty::BrAnon(0))),
294                     param(0),
295                 )],
296                 tcx.types.u64,
297             ),
298
299             "try" => {
300                 let mut_u8 = tcx.mk_mut_ptr(tcx.types.u8);
301                 let fn_ty = ty::Binder::bind(tcx.mk_fn_sig(
302                     iter::once(mut_u8),
303                     tcx.mk_unit(),
304                     false,
305                     hir::Unsafety::Normal,
306                     Abi::Rust,
307                 ));
308                 (0, vec![tcx.mk_fn_ptr(fn_ty), mut_u8, mut_u8], tcx.types.i32)
309             }
310
311             "va_start" | "va_end" => match mk_va_list_ty(hir::Mutability::Mut) {
312                 Some((va_list_ref_ty, _)) => (0, vec![va_list_ref_ty], tcx.mk_unit()),
313                 None => bug!("`va_list` language item needed for C-variadic intrinsics"),
314             },
315
316             "va_copy" => match mk_va_list_ty(hir::Mutability::Not) {
317                 Some((va_list_ref_ty, va_list_ty)) => {
318                     let va_list_ptr_ty = tcx.mk_mut_ptr(va_list_ty);
319                     (0, vec![va_list_ptr_ty, va_list_ref_ty], tcx.mk_unit())
320                 }
321                 None => bug!("`va_list` language item needed for C-variadic intrinsics"),
322             },
323
324             "va_arg" => match mk_va_list_ty(hir::Mutability::Mut) {
325                 Some((va_list_ref_ty, _)) => (1, vec![va_list_ref_ty], param(0)),
326                 None => bug!("`va_list` language item needed for C-variadic intrinsics"),
327             },
328
329             "nontemporal_store" => (1, vec![tcx.mk_mut_ptr(param(0)), param(0)], tcx.mk_unit()),
330
331             "miri_start_panic" => {
332                 // FIXME - the relevant types aren't lang items,
333                 // so it's not trivial to check this
334                 return;
335             }
336
337             ref other => {
338                 struct_span_err!(
339                     tcx.sess,
340                     it.span,
341                     E0093,
342                     "unrecognized intrinsic function: `{}`",
343                     *other
344                 )
345                 .span_label(it.span, "unrecognized intrinsic")
346                 .emit();
347                 return;
348             }
349         };
350         (n_tps, inputs, output, unsafety)
351     };
352     equate_intrinsic_type(tcx, it, n_tps, Abi::RustIntrinsic, unsafety, inputs, output)
353 }
354
355 /// Type-check `extern "platform-intrinsic" { ... }` functions.
356 pub fn check_platform_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem<'_>) {
357     let param = |n| {
358         let name = Symbol::intern(&format!("P{}", n));
359         tcx.mk_ty_param(n, name)
360     };
361
362     let name = it.ident.as_str();
363
364     let (n_tps, inputs, output) = match &*name {
365         "simd_eq" | "simd_ne" | "simd_lt" | "simd_le" | "simd_gt" | "simd_ge" => {
366             (2, vec![param(0), param(0)], param(1))
367         }
368         "simd_add"
369         | "simd_sub"
370         | "simd_mul"
371         | "simd_rem"
372         | "simd_div"
373         | "simd_shl"
374         | "simd_shr"
375         | "simd_and"
376         | "simd_or"
377         | "simd_xor"
378         | "simd_fmin"
379         | "simd_fmax"
380         | "simd_fpow"
381         | "simd_saturating_add"
382         | "simd_saturating_sub" => (1, vec![param(0), param(0)], param(0)),
383         "simd_fsqrt" | "simd_fsin" | "simd_fcos" | "simd_fexp" | "simd_fexp2" | "simd_flog2"
384         | "simd_flog10" | "simd_flog" | "simd_fabs" | "simd_floor" | "simd_ceil" => {
385             (1, vec![param(0)], param(0))
386         }
387         "simd_fpowi" => (1, vec![param(0), tcx.types.i32], param(0)),
388         "simd_fma" => (1, vec![param(0), param(0), param(0)], param(0)),
389         "simd_gather" => (3, vec![param(0), param(1), param(2)], param(0)),
390         "simd_scatter" => (3, vec![param(0), param(1), param(2)], tcx.mk_unit()),
391         "simd_insert" => (2, vec![param(0), tcx.types.u32, param(1)], param(0)),
392         "simd_extract" => (2, vec![param(0), tcx.types.u32], param(1)),
393         "simd_cast" => (2, vec![param(0)], param(1)),
394         "simd_bitmask" => (2, vec![param(0)], param(1)),
395         "simd_select" | "simd_select_bitmask" => (2, vec![param(0), param(1), param(1)], param(1)),
396         "simd_reduce_all" | "simd_reduce_any" => (1, vec![param(0)], tcx.types.bool),
397         "simd_reduce_add_ordered" | "simd_reduce_mul_ordered" => {
398             (2, vec![param(0), param(1)], param(1))
399         }
400         "simd_reduce_add_unordered"
401         | "simd_reduce_mul_unordered"
402         | "simd_reduce_and"
403         | "simd_reduce_or"
404         | "simd_reduce_xor"
405         | "simd_reduce_min"
406         | "simd_reduce_max"
407         | "simd_reduce_min_nanless"
408         | "simd_reduce_max_nanless" => (2, vec![param(0)], param(1)),
409         name if name.starts_with("simd_shuffle") => match name["simd_shuffle".len()..].parse() {
410             Ok(n) => {
411                 let params = vec![param(0), param(0), tcx.mk_array(tcx.types.u32, n)];
412                 (2, params, param(1))
413             }
414             Err(_) => {
415                 struct_span_err!(
416                     tcx.sess,
417                     it.span,
418                     E0439,
419                     "invalid `simd_shuffle`, needs length: `{}`",
420                     name
421                 )
422                 .emit();
423                 return;
424             }
425         },
426         _ => {
427             let msg = format!("unrecognized platform-specific intrinsic function: `{}`", name);
428             tcx.sess.struct_span_err(it.span, &msg).emit();
429             return;
430         }
431     };
432
433     equate_intrinsic_type(
434         tcx,
435         it,
436         n_tps,
437         Abi::PlatformIntrinsic,
438         hir::Unsafety::Unsafe,
439         inputs,
440         output,
441     )
442 }