]> git.lizzy.rs Git - rust.git/blob - src/librustc_typeck/check/intrinsic.rs
5c0eef5b1f332818c60c5619379d7c6a1a0ac580
[rust.git] / src / librustc_typeck / check / intrinsic.rs
1 // Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! Type-checking for the rust-intrinsic and platform-intrinsic
12 //! intrinsics that the compiler exposes.
13
14 use intrinsics;
15 use rustc::traits::{ObligationCause, ObligationCauseCode};
16 use rustc::ty::{self, TyCtxt, Ty};
17 use rustc::ty::subst::Subst;
18 use rustc::util::nodemap::FxHashMap;
19 use require_same_types;
20
21 use rustc_target::spec::abi::Abi;
22 use syntax::ast;
23 use syntax::symbol::Symbol;
24 use syntax_pos::Span;
25
26 use rustc::hir;
27
28 use std::iter;
29
30 fn equate_intrinsic_type<'a, 'tcx>(
31     tcx: TyCtxt<'a, 'tcx, 'tcx>,
32     it: &hir::ForeignItem,
33     n_tps: usize,
34     abi: Abi,
35     safety: hir::Unsafety,
36     inputs: Vec<Ty<'tcx>>,
37     output: Ty<'tcx>,
38 ) {
39     let def_id = tcx.hir().local_def_id(it.id);
40
41     match it.node {
42         hir::ForeignItemKind::Fn(..) => {}
43         _ => {
44             struct_span_err!(tcx.sess, it.span, E0622,
45                              "intrinsic must be a function")
46                 .span_label(it.span, "expected a function")
47                 .emit();
48             return;
49         }
50     }
51
52     let i_n_tps = tcx.generics_of(def_id).own_counts().types;
53     if i_n_tps != n_tps {
54         let span = match it.node {
55             hir::ForeignItemKind::Fn(_, _, ref generics) => generics.span,
56             _ => bug!()
57         };
58
59         struct_span_err!(tcx.sess, span, E0094,
60                         "intrinsic has wrong number of type \
61                          parameters: found {}, expected {}",
62                         i_n_tps, n_tps)
63             .span_label(span, format!("expected {} type parameter", n_tps))
64             .emit();
65         return;
66     }
67
68     let fty = tcx.mk_fn_ptr(ty::Binder::bind(tcx.mk_fn_sig(
69         inputs.into_iter(),
70         output,
71         false,
72         safety,
73         abi
74     )));
75     let cause = ObligationCause::new(it.span, it.id, ObligationCauseCode::IntrinsicType);
76     require_same_types(tcx, &cause, tcx.mk_fn_ptr(tcx.fn_sig(def_id)), fty);
77 }
78
79 /// Remember to add all intrinsics here, in librustc_codegen_llvm/intrinsic.rs,
80 /// and in libcore/intrinsics.rs
81 pub fn check_intrinsic_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
82                                       it: &hir::ForeignItem) {
83     let param = |n| tcx.mk_ty_param(n, Symbol::intern(&format!("P{}", n)).as_interned_str());
84     let name = it.name.as_str();
85
86     let mk_va_list_ty = || {
87         tcx.lang_items().va_list().map(|did| {
88             let region = tcx.mk_region(ty::ReLateBound(ty::INNERMOST, ty::BrAnon(0)));
89             let env_region = ty::ReLateBound(ty::INNERMOST, ty::BrEnv);
90             let va_list_ty = tcx.type_of(did).subst(tcx, &[region.into()]);
91             tcx.mk_mut_ref(tcx.mk_region(env_region), va_list_ty)
92         })
93     };
94
95     let (n_tps, inputs, output, unsafety) = if name.starts_with("atomic_") {
96         let split : Vec<&str> = name.split('_').collect();
97         assert!(split.len() >= 2, "Atomic intrinsic in an incorrect format");
98
99         //We only care about the operation here
100         let (n_tps, inputs, output) = match split[1] {
101             "cxchg" | "cxchgweak" => (1, vec![tcx.mk_mut_ptr(param(0)),
102                                               param(0),
103                                               param(0)],
104                                       tcx.intern_tup(&[param(0), tcx.types.bool])),
105             "load" => (1, vec![tcx.mk_imm_ptr(param(0))],
106                        param(0)),
107             "store" => (1, vec![tcx.mk_mut_ptr(param(0)), param(0)],
108                         tcx.mk_unit()),
109
110             "xchg" | "xadd" | "xsub" | "and"  | "nand" | "or" | "xor" | "max" |
111             "min"  | "umax" | "umin" => {
112                 (1, vec![tcx.mk_mut_ptr(param(0)), param(0)],
113                  param(0))
114             }
115             "fence" | "singlethreadfence" => {
116                 (0, Vec::new(), tcx.mk_unit())
117             }
118             op => {
119                 struct_span_err!(tcx.sess, it.span, E0092,
120                       "unrecognized atomic operation function: `{}`", op)
121                   .span_label(it.span, "unrecognized atomic operation")
122                   .emit();
123                 return;
124             }
125         };
126         (n_tps, inputs, output, hir::Unsafety::Unsafe)
127     } else if &name[..] == "abort" || &name[..] == "unreachable" {
128         (0, Vec::new(), tcx.types.never, hir::Unsafety::Unsafe)
129     } else {
130         let unsafety = match &name[..] {
131             "size_of" | "min_align_of" | "needs_drop" => hir::Unsafety::Normal,
132             _ => hir::Unsafety::Unsafe,
133         };
134         let (n_tps, inputs, output) = match &name[..] {
135             "breakpoint" => (0, Vec::new(), tcx.mk_unit()),
136             "size_of" |
137             "pref_align_of" | "min_align_of" => (1, Vec::new(), tcx.types.usize),
138             "size_of_val" |  "min_align_of_val" => {
139                 (1, vec![
140                     tcx.mk_imm_ref(tcx.mk_region(ty::ReLateBound(ty::INNERMOST,
141                                                                  ty::BrAnon(0))),
142                                    param(0))
143                  ], tcx.types.usize)
144             }
145             "rustc_peek" => (1, vec![param(0)], param(0)),
146             "init" => (1, Vec::new(), param(0)),
147             "uninit" => (1, Vec::new(), param(0)),
148             "forget" => (1, vec![param(0)], tcx.mk_unit()),
149             "transmute" => (2, vec![ param(0) ], param(1)),
150             "move_val_init" => {
151                 (1,
152                  vec![
153                     tcx.mk_mut_ptr(param(0)),
154                     param(0)
155                   ],
156                tcx.mk_unit())
157             }
158             "prefetch_read_data" | "prefetch_write_data" |
159             "prefetch_read_instruction" | "prefetch_write_instruction" => {
160                 (1, vec![tcx.mk_ptr(ty::TypeAndMut {
161                           ty: param(0),
162                           mutbl: hir::MutImmutable
163                          }), tcx.types.i32],
164                     tcx.mk_unit())
165             }
166             "drop_in_place" => {
167                 (1, vec![tcx.mk_mut_ptr(param(0))], tcx.mk_unit())
168             }
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 {
177                       ty: param(0),
178                       mutbl: hir::MutImmutable
179                   }),
180                   tcx.types.isize
181                ],
182                tcx.mk_ptr(ty::TypeAndMut {
183                    ty: param(0),
184                    mutbl: hir::MutImmutable
185                }))
186             }
187             "copy" | "copy_nonoverlapping" => {
188               (1,
189                vec![
190                   tcx.mk_ptr(ty::TypeAndMut {
191                       ty: param(0),
192                       mutbl: hir::MutImmutable
193                   }),
194                   tcx.mk_ptr(ty::TypeAndMut {
195                       ty: param(0),
196                       mutbl: hir::MutMutable
197                   }),
198                   tcx.types.usize,
199                ],
200                tcx.mk_unit())
201             }
202             "volatile_copy_memory" | "volatile_copy_nonoverlapping_memory" => {
203               (1,
204                vec![
205                   tcx.mk_ptr(ty::TypeAndMut {
206                       ty: param(0),
207                       mutbl: hir::MutMutable
208                   }),
209                   tcx.mk_ptr(ty::TypeAndMut {
210                       ty: param(0),
211                       mutbl: hir::MutImmutable
212                   }),
213                   tcx.types.usize,
214                ],
215                tcx.mk_unit())
216             }
217             "write_bytes" | "volatile_set_memory" => {
218               (1,
219                vec![
220                   tcx.mk_ptr(ty::TypeAndMut {
221                       ty: param(0),
222                       mutbl: hir::MutMutable
223                   }),
224                   tcx.types.u8,
225                   tcx.types.usize,
226                ],
227                tcx.mk_unit())
228             }
229             "sqrtf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
230             "sqrtf64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
231             "powif32" => {
232                (0,
233                 vec![ tcx.types.f32, tcx.types.i32 ],
234                 tcx.types.f32)
235             }
236             "powif64" => {
237                (0,
238                 vec![ tcx.types.f64, tcx.types.i32 ],
239                 tcx.types.f64)
240             }
241             "sinf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
242             "sinf64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
243             "cosf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
244             "cosf64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
245             "powf32" => {
246                (0,
247                 vec![ tcx.types.f32, tcx.types.f32 ],
248                 tcx.types.f32)
249             }
250             "powf64" => {
251                (0,
252                 vec![ tcx.types.f64, tcx.types.f64 ],
253                 tcx.types.f64)
254             }
255             "expf32"   => (0, vec![ tcx.types.f32 ], tcx.types.f32),
256             "expf64"   => (0, vec![ tcx.types.f64 ], tcx.types.f64),
257             "exp2f32"  => (0, vec![ tcx.types.f32 ], tcx.types.f32),
258             "exp2f64"  => (0, vec![ tcx.types.f64 ], tcx.types.f64),
259             "logf32"   => (0, vec![ tcx.types.f32 ], tcx.types.f32),
260             "logf64"   => (0, vec![ tcx.types.f64 ], tcx.types.f64),
261             "log10f32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
262             "log10f64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
263             "log2f32"  => (0, vec![ tcx.types.f32 ], tcx.types.f32),
264             "log2f64"  => (0, vec![ tcx.types.f64 ], tcx.types.f64),
265             "fmaf32" => {
266                 (0,
267                  vec![ tcx.types.f32, tcx.types.f32, tcx.types.f32 ],
268                  tcx.types.f32)
269             }
270             "fmaf64" => {
271                 (0,
272                  vec![ tcx.types.f64, tcx.types.f64, tcx.types.f64 ],
273                  tcx.types.f64)
274             }
275             "fabsf32"      => (0, vec![ tcx.types.f32 ], tcx.types.f32),
276             "fabsf64"      => (0, vec![ tcx.types.f64 ], tcx.types.f64),
277             "copysignf32"  => (0, vec![ tcx.types.f32, tcx.types.f32 ], tcx.types.f32),
278             "copysignf64"  => (0, vec![ tcx.types.f64, tcx.types.f64 ], tcx.types.f64),
279             "floorf32"     => (0, vec![ tcx.types.f32 ], tcx.types.f32),
280             "floorf64"     => (0, vec![ tcx.types.f64 ], tcx.types.f64),
281             "ceilf32"      => (0, vec![ tcx.types.f32 ], tcx.types.f32),
282             "ceilf64"      => (0, vec![ tcx.types.f64 ], tcx.types.f64),
283             "truncf32"     => (0, vec![ tcx.types.f32 ], tcx.types.f32),
284             "truncf64"     => (0, vec![ tcx.types.f64 ], tcx.types.f64),
285             "rintf32"      => (0, vec![ tcx.types.f32 ], tcx.types.f32),
286             "rintf64"      => (0, vec![ tcx.types.f64 ], tcx.types.f64),
287             "nearbyintf32" => (0, vec![ tcx.types.f32 ], tcx.types.f32),
288             "nearbyintf64" => (0, vec![ tcx.types.f64 ], tcx.types.f64),
289             "roundf32"     => (0, vec![ tcx.types.f32 ], tcx.types.f32),
290             "roundf64"     => (0, vec![ tcx.types.f64 ], tcx.types.f64),
291
292             "volatile_load" | "unaligned_volatile_load" =>
293                 (1, vec![ tcx.mk_imm_ptr(param(0)) ], param(0)),
294             "volatile_store" | "unaligned_volatile_store" =>
295                 (1, vec![ tcx.mk_mut_ptr(param(0)), param(0) ], tcx.mk_unit()),
296
297             "ctpop" | "ctlz" | "ctlz_nonzero" | "cttz" | "cttz_nonzero" |
298             "bswap" | "bitreverse" =>
299                 (1, vec![param(0)], param(0)),
300
301             "add_with_overflow" | "sub_with_overflow"  | "mul_with_overflow" =>
302                 (1, vec![param(0), param(0)],
303                 tcx.intern_tup(&[param(0), tcx.types.bool])),
304
305             "unchecked_div" | "unchecked_rem" | "exact_div" =>
306                 (1, vec![param(0), param(0)], param(0)),
307             "unchecked_shl" | "unchecked_shr" |
308             "rotate_left" | "rotate_right" =>
309                 (1, vec![param(0), param(0)], param(0)),
310
311             "overflowing_add" | "overflowing_sub" | "overflowing_mul" =>
312                 (1, vec![param(0), param(0)], param(0)),
313             "fadd_fast" | "fsub_fast" | "fmul_fast" | "fdiv_fast" | "frem_fast" =>
314                 (1, vec![param(0), param(0)], param(0)),
315
316             "assume" => (0, vec![tcx.types.bool], tcx.mk_unit()),
317             "likely" => (0, vec![tcx.types.bool], tcx.types.bool),
318             "unlikely" => (0, vec![tcx.types.bool], tcx.types.bool),
319
320             "discriminant_value" => (1, vec![
321                     tcx.mk_imm_ref(tcx.mk_region(ty::ReLateBound(ty::INNERMOST,
322                                                                  ty::BrAnon(0))),
323                                    param(0))], tcx.types.u64),
324
325             "try" => {
326                 let mut_u8 = tcx.mk_mut_ptr(tcx.types.u8);
327                 let fn_ty = ty::Binder::bind(tcx.mk_fn_sig(
328                     iter::once(mut_u8),
329                     tcx.mk_unit(),
330                     false,
331                     hir::Unsafety::Normal,
332                     Abi::Rust,
333                 ));
334                 (0, vec![tcx.mk_fn_ptr(fn_ty), mut_u8, mut_u8], tcx.types.i32)
335             }
336
337             "va_start" | "va_end" => {
338                 match mk_va_list_ty() {
339                     Some(va_list_ty) => (0, vec![va_list_ty], tcx.mk_unit()),
340                     None => bug!("va_list lang_item must be defined to use va_list intrinsics")
341                 }
342             }
343
344             "va_copy" => {
345                 match tcx.lang_items().va_list() {
346                     Some(did) => {
347                         let region = tcx.mk_region(ty::ReLateBound(ty::INNERMOST, ty::BrAnon(0)));
348                         let env_region = ty::ReLateBound(ty::INNERMOST, ty::BrEnv);
349                         let va_list_ty = tcx.type_of(did).subst(tcx, &[region.into()]);
350                         let ret_ty = match va_list_ty.sty {
351                             ty::Adt(def, _) if def.is_struct() => {
352                                 let fields = &def.non_enum_variant().fields;
353                                 match tcx.type_of(fields[0].did).subst(tcx, &[region.into()]).sty {
354                                     ty::Ref(_, element_ty, _) => match element_ty.sty {
355                                         ty::Adt(..) => element_ty,
356                                         _ => va_list_ty
357                                     }
358                                     _ => bug!("va_list structure is invalid")
359                                 }
360                             }
361                             _ => {
362                                 bug!("va_list structure is invalid")
363                             }
364                         };
365                         (0, vec![tcx.mk_imm_ref(tcx.mk_region(env_region), va_list_ty)], ret_ty)
366                     }
367                     None => bug!("va_list lang_item must be defined to use va_list intrinsics")
368                 }
369             }
370
371             "va_arg" => {
372                 match mk_va_list_ty() {
373                     Some(va_list_ty) => (1, vec![va_list_ty], param(0)),
374                     None => bug!("va_list lang_item must be defined to use va_list intrinsics")
375                 }
376             }
377
378             "nontemporal_store" => {
379                 (1, vec![ tcx.mk_mut_ptr(param(0)), param(0) ], tcx.mk_unit())
380             }
381
382             ref other => {
383                 struct_span_err!(tcx.sess, it.span, E0093,
384                                  "unrecognized intrinsic function: `{}`",
385                                  *other)
386                                  .span_label(it.span, "unrecognized intrinsic")
387                                  .emit();
388                 return;
389             }
390         };
391         (n_tps, inputs, output, unsafety)
392     };
393     equate_intrinsic_type(tcx, it, n_tps, Abi::RustIntrinsic, unsafety, inputs, output)
394 }
395
396 /// Type-check `extern "platform-intrinsic" { ... }` functions.
397 pub fn check_platform_intrinsic_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
398                                                it: &hir::ForeignItem) {
399     let param = |n| {
400         let name = Symbol::intern(&format!("P{}", n)).as_interned_str();
401         tcx.mk_ty_param(n, name)
402     };
403
404     let def_id = tcx.hir().local_def_id(it.id);
405     let i_n_tps = tcx.generics_of(def_id).own_counts().types;
406     let name = it.name.as_str();
407
408     let (n_tps, inputs, output) = match &*name {
409         "simd_eq" | "simd_ne" | "simd_lt" | "simd_le" | "simd_gt" | "simd_ge" => {
410             (2, vec![param(0), param(0)], param(1))
411         }
412         "simd_add" | "simd_sub" | "simd_mul" | "simd_rem" |
413         "simd_div" | "simd_shl" | "simd_shr" |
414         "simd_and" | "simd_or" | "simd_xor" |
415         "simd_fmin" | "simd_fmax" | "simd_fpow" => {
416             (1, vec![param(0), param(0)], param(0))
417         }
418         "simd_fsqrt" | "simd_fsin" | "simd_fcos" | "simd_fexp" | "simd_fexp2" |
419         "simd_flog2" | "simd_flog10" | "simd_flog" |
420         "simd_fabs" | "simd_floor" | "simd_ceil" => {
421             (1, vec![param(0)], param(0))
422         }
423         "simd_fpowi" => {
424             (1, vec![param(0), tcx.types.i32], param(0))
425         }
426         "simd_fma" => {
427             (1, vec![param(0), param(0), param(0)], param(0))
428         }
429         "simd_gather" => {
430             (3, vec![param(0), param(1), param(2)], param(0))
431         }
432         "simd_scatter" => {
433             (3, vec![param(0), param(1), param(2)], tcx.mk_unit())
434         }
435         "simd_insert" => (2, vec![param(0), tcx.types.u32, param(1)], param(0)),
436         "simd_extract" => (2, vec![param(0), tcx.types.u32], param(1)),
437         "simd_cast" => (2, vec![param(0)], param(1)),
438         "simd_select" => (2, vec![param(0), param(1), param(1)], param(1)),
439         "simd_reduce_all" | "simd_reduce_any" => (1, vec![param(0)], tcx.types.bool),
440         "simd_reduce_add_ordered" | "simd_reduce_mul_ordered"
441             => (2, vec![param(0), param(1)], param(1)),
442         "simd_reduce_add_unordered" | "simd_reduce_mul_unordered" |
443         "simd_reduce_and" | "simd_reduce_or"  | "simd_reduce_xor" |
444         "simd_reduce_min" | "simd_reduce_max" |
445         "simd_reduce_min_nanless" | "simd_reduce_max_nanless"
446             => (2, vec![param(0)], param(1)),
447         name if name.starts_with("simd_shuffle") => {
448             match name["simd_shuffle".len()..].parse() {
449                 Ok(n) => {
450                     let params = vec![param(0), param(0),
451                                       tcx.mk_array(tcx.types.u32, n)];
452                     (2, params, param(1))
453                 }
454                 Err(_) => {
455                     span_err!(tcx.sess, it.span, E0439,
456                               "invalid `simd_shuffle`, needs length: `{}`", name);
457                     return
458                 }
459             }
460         }
461         _ => {
462             match intrinsics::Intrinsic::find(&name) {
463                 Some(intr) => {
464                     // this function is a platform specific intrinsic
465                     if i_n_tps != 0 {
466                         span_err!(tcx.sess, it.span, E0440,
467                                   "platform-specific intrinsic has wrong number of type \
468                                    parameters: found {}, expected 0",
469                                   i_n_tps);
470                         return
471                     }
472
473                     let mut structural_to_nomimal = FxHashMap::default();
474
475                     let sig = tcx.fn_sig(def_id);
476                     let sig = sig.no_bound_vars().unwrap();
477                     if intr.inputs.len() != sig.inputs().len() {
478                         span_err!(tcx.sess, it.span, E0444,
479                                   "platform-specific intrinsic has invalid number of \
480                                    arguments: found {}, expected {}",
481                                   sig.inputs().len(), intr.inputs.len());
482                         return
483                     }
484                     let input_pairs = intr.inputs.iter().zip(sig.inputs());
485                     for (i, (expected_arg, arg)) in input_pairs.enumerate() {
486                         match_intrinsic_type_to_type(tcx, &format!("argument {}", i + 1), it.span,
487                                                      &mut structural_to_nomimal, expected_arg, arg);
488                     }
489                     match_intrinsic_type_to_type(tcx, "return value", it.span,
490                                                  &mut structural_to_nomimal,
491                                                  &intr.output, sig.output());
492                     return
493                 }
494                 None => {
495                     span_err!(tcx.sess, it.span, E0441,
496                               "unrecognized platform-specific intrinsic function: `{}`", name);
497                     return;
498                 }
499             }
500         }
501     };
502
503     equate_intrinsic_type(tcx, it, n_tps, Abi::PlatformIntrinsic, hir::Unsafety::Unsafe,
504                           inputs, output)
505 }
506
507 // walk the expected type and the actual type in lock step, checking they're
508 // the same, in a kinda-structural way, i.e., `Vector`s have to be simd structs with
509 // exactly the right element type
510 fn match_intrinsic_type_to_type<'a, 'tcx>(
511         tcx: TyCtxt<'a, 'tcx, 'tcx>,
512         position: &str,
513         span: Span,
514         structural_to_nominal: &mut FxHashMap<&'a intrinsics::Type, Ty<'tcx>>,
515         expected: &'a intrinsics::Type, t: Ty<'tcx>)
516 {
517     use intrinsics::Type::*;
518
519     let simple_error = |real: &str, expected: &str| {
520         span_err!(tcx.sess, span, E0442,
521                   "intrinsic {} has wrong type: found {}, expected {}",
522                   position, real, expected)
523     };
524
525     match *expected {
526         Void => match t.sty {
527             ty::Tuple(ref v) if v.is_empty() => {},
528             _ => simple_error(&format!("`{}`", t), "()"),
529         },
530         // (The width we pass to LLVM doesn't concern the type checker.)
531         Integer(signed, bits, _llvm_width) => match (signed, bits, &t.sty) {
532             (true,  8,  &ty::Int(ast::IntTy::I8)) |
533             (false, 8,  &ty::Uint(ast::UintTy::U8)) |
534             (true,  16, &ty::Int(ast::IntTy::I16)) |
535             (false, 16, &ty::Uint(ast::UintTy::U16)) |
536             (true,  32, &ty::Int(ast::IntTy::I32)) |
537             (false, 32, &ty::Uint(ast::UintTy::U32)) |
538             (true,  64, &ty::Int(ast::IntTy::I64)) |
539             (false, 64, &ty::Uint(ast::UintTy::U64)) |
540             (true,  128, &ty::Int(ast::IntTy::I128)) |
541             (false, 128, &ty::Uint(ast::UintTy::U128)) => {},
542             _ => simple_error(&format!("`{}`", t),
543                               &format!("`{}{n}`",
544                                        if signed {"i"} else {"u"},
545                                        n = bits)),
546         },
547         Float(bits) => match (bits, &t.sty) {
548             (32, &ty::Float(ast::FloatTy::F32)) |
549             (64, &ty::Float(ast::FloatTy::F64)) => {},
550             _ => simple_error(&format!("`{}`", t),
551                               &format!("`f{n}`", n = bits)),
552         },
553         Pointer(ref inner_expected, ref _llvm_type, const_) => {
554             match t.sty {
555                 ty::RawPtr(ty::TypeAndMut { ty, mutbl }) => {
556                     if (mutbl == hir::MutImmutable) != const_ {
557                         simple_error(&format!("`{}`", t),
558                                      if const_ {"const pointer"} else {"mut pointer"})
559                     }
560                     match_intrinsic_type_to_type(tcx, position, span, structural_to_nominal,
561                                                  inner_expected, ty)
562                 }
563                 _ => simple_error(&format!("`{}`", t), "raw pointer"),
564             }
565         }
566         Vector(ref inner_expected, ref _llvm_type, len) => {
567             if !t.is_simd() {
568                 simple_error(&format!("non-simd type `{}`", t), "simd type");
569                 return;
570             }
571             let t_len = t.simd_size(tcx);
572             if len as usize != t_len {
573                 simple_error(&format!("vector with length {}", t_len),
574                              &format!("length {}", len));
575                 return;
576             }
577             let t_ty = t.simd_type(tcx);
578             {
579                 // check that a given structural type always has the same an intrinsic definition
580                 let previous = structural_to_nominal.entry(expected).or_insert(t);
581                 if *previous != t {
582                     // this gets its own error code because it is non-trivial
583                     span_err!(tcx.sess, span, E0443,
584                               "intrinsic {} has wrong type: found `{}`, expected `{}` which \
585                                was used for this vector type previously in this signature",
586                               position,
587                               t,
588                               *previous);
589                     return;
590                 }
591             }
592             match_intrinsic_type_to_type(tcx,
593                                          position,
594                                          span,
595                                          structural_to_nominal,
596                                          inner_expected,
597                                          t_ty)
598         }
599         Aggregate(_flatten, ref expected_contents) => {
600             match t.sty {
601                 ty::Tuple(contents) => {
602                     if contents.len() != expected_contents.len() {
603                         simple_error(&format!("tuple with length {}", contents.len()),
604                                      &format!("tuple with length {}", expected_contents.len()));
605                         return
606                     }
607                     for (e, c) in expected_contents.iter().zip(contents) {
608                         match_intrinsic_type_to_type(tcx, position, span, structural_to_nominal,
609                                                      e, c)
610                     }
611                 }
612                 _ => simple_error(&format!("`{}`", t),
613                                   "tuple"),
614             }
615         }
616     }
617 }