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