rustc_hir_analysis/check/
intrinsic.rs

1//! Type-checking for the rust-intrinsic intrinsics that the compiler exposes.
2
3use rustc_abi::ExternAbi;
4use rustc_errors::codes::*;
5use rustc_errors::{DiagMessage, struct_span_code_err};
6use rustc_hir::{self as hir, Safety};
7use rustc_middle::bug;
8use rustc_middle::traits::{ObligationCause, ObligationCauseCode};
9use rustc_middle::ty::{self, Ty, TyCtxt};
10use rustc_span::def_id::LocalDefId;
11use rustc_span::{Span, Symbol, sym};
12
13use crate::check::check_function_signature;
14use crate::errors::{
15    UnrecognizedAtomicOperation, UnrecognizedIntrinsicFunction,
16    WrongNumberOfGenericArgumentsToIntrinsic,
17};
18
19fn equate_intrinsic_type<'tcx>(
20    tcx: TyCtxt<'tcx>,
21    span: Span,
22    def_id: LocalDefId,
23    n_tps: usize,
24    n_lts: usize,
25    n_cts: usize,
26    sig: ty::PolyFnSig<'tcx>,
27) {
28    let (generics, span) = match tcx.hir_node_by_def_id(def_id) {
29        hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn { generics, .. }, .. })
30        | hir::Node::ForeignItem(hir::ForeignItem {
31            kind: hir::ForeignItemKind::Fn(_, _, generics),
32            ..
33        }) => (tcx.generics_of(def_id), generics.span),
34        _ => {
35            struct_span_code_err!(tcx.dcx(), span, E0622, "intrinsic must be a function")
36                .with_span_label(span, "expected a function")
37                .emit();
38            return;
39        }
40    };
41    let own_counts = generics.own_counts();
42
43    let gen_count_ok = |found: usize, expected: usize, descr: &str| -> bool {
44        if found != expected {
45            tcx.dcx().emit_err(WrongNumberOfGenericArgumentsToIntrinsic {
46                span,
47                found,
48                expected,
49                descr,
50            });
51            false
52        } else {
53            true
54        }
55    };
56
57    // the host effect param should be invisible as it shouldn't matter
58    // whether effects is enabled for the intrinsic provider crate.
59    if gen_count_ok(own_counts.lifetimes, n_lts, "lifetime")
60        && gen_count_ok(own_counts.types, n_tps, "type")
61        && gen_count_ok(own_counts.consts, n_cts, "const")
62    {
63        let _ = check_function_signature(
64            tcx,
65            ObligationCause::new(span, def_id, ObligationCauseCode::IntrinsicType),
66            def_id.into(),
67            sig,
68        );
69    }
70}
71
72/// Returns the unsafety of the given intrinsic.
73pub fn intrinsic_operation_unsafety(tcx: TyCtxt<'_>, intrinsic_id: LocalDefId) -> hir::Safety {
74    let has_safe_attr = if tcx.has_attr(intrinsic_id, sym::rustc_intrinsic) {
75        tcx.fn_sig(intrinsic_id).skip_binder().safety()
76    } else {
77        // Old-style intrinsics are never safe
78        Safety::Unsafe
79    };
80    let is_in_list = match tcx.item_name(intrinsic_id.into()) {
81        // When adding a new intrinsic to this list,
82        // it's usually worth updating that intrinsic's documentation
83        // to note that it's safe to call, since
84        // safe extern fns are otherwise unprecedented.
85        sym::abort
86        | sym::assert_inhabited
87        | sym::assert_zero_valid
88        | sym::assert_mem_uninitialized_valid
89        | sym::box_new
90        | sym::breakpoint
91        | sym::size_of
92        | sym::min_align_of
93        | sym::needs_drop
94        | sym::caller_location
95        | sym::add_with_overflow
96        | sym::sub_with_overflow
97        | sym::mul_with_overflow
98        | sym::carrying_mul_add
99        | sym::wrapping_add
100        | sym::wrapping_sub
101        | sym::wrapping_mul
102        | sym::saturating_add
103        | sym::saturating_sub
104        | sym::rotate_left
105        | sym::rotate_right
106        | sym::ctpop
107        | sym::ctlz
108        | sym::cttz
109        | sym::bswap
110        | sym::bitreverse
111        | sym::three_way_compare
112        | sym::discriminant_value
113        | sym::type_id
114        | sym::select_unpredictable
115        | sym::cold_path
116        | sym::ptr_guaranteed_cmp
117        | sym::minnumf16
118        | sym::minnumf32
119        | sym::minnumf64
120        | sym::minnumf128
121        | sym::maxnumf16
122        | sym::maxnumf32
123        | sym::maxnumf64
124        | sym::maxnumf128
125        | sym::rustc_peek
126        | sym::type_name
127        | sym::forget
128        | sym::black_box
129        | sym::variant_count
130        | sym::is_val_statically_known
131        | sym::ptr_mask
132        | sym::aggregate_raw_ptr
133        | sym::ptr_metadata
134        | sym::ub_checks
135        | sym::contract_checks
136        | sym::contract_check_requires
137        | sym::contract_check_ensures
138        | sym::fadd_algebraic
139        | sym::fsub_algebraic
140        | sym::fmul_algebraic
141        | sym::fdiv_algebraic
142        | sym::frem_algebraic
143        | sym::round_ties_even_f16
144        | sym::round_ties_even_f32
145        | sym::round_ties_even_f64
146        | sym::round_ties_even_f128
147        | sym::const_eval_select => hir::Safety::Safe,
148        _ => hir::Safety::Unsafe,
149    };
150
151    if has_safe_attr != is_in_list {
152        tcx.dcx().struct_span_err(
153            tcx.def_span(intrinsic_id),
154            DiagMessage::from(format!(
155                "intrinsic safety mismatch between list of intrinsics within the compiler and core library intrinsics for intrinsic `{}`",
156                tcx.item_name(intrinsic_id.into())
157            )
158        )).emit();
159    }
160
161    is_in_list
162}
163
164/// Remember to add all intrinsics here, in `compiler/rustc_codegen_llvm/src/intrinsic.rs`,
165/// and in `library/core/src/intrinsics.rs`.
166pub fn check_intrinsic_type(
167    tcx: TyCtxt<'_>,
168    intrinsic_id: LocalDefId,
169    span: Span,
170    intrinsic_name: Symbol,
171    abi: ExternAbi,
172) {
173    let generics = tcx.generics_of(intrinsic_id);
174    let param = |n| {
175        if let &ty::GenericParamDef { name, kind: ty::GenericParamDefKind::Type { .. }, .. } =
176            generics.param_at(n as usize, tcx)
177        {
178            Ty::new_param(tcx, n, name)
179        } else {
180            Ty::new_error_with_message(tcx, span, "expected param")
181        }
182    };
183    let name_str = intrinsic_name.as_str();
184
185    let bound_vars = tcx.mk_bound_variable_kinds(&[
186        ty::BoundVariableKind::Region(ty::BoundRegionKind::Anon),
187        ty::BoundVariableKind::Region(ty::BoundRegionKind::Anon),
188        ty::BoundVariableKind::Region(ty::BoundRegionKind::ClosureEnv),
189    ]);
190    let mk_va_list_ty = |mutbl| {
191        tcx.lang_items().va_list().map(|did| {
192            let region = ty::Region::new_bound(
193                tcx,
194                ty::INNERMOST,
195                ty::BoundRegion { var: ty::BoundVar::ZERO, kind: ty::BoundRegionKind::Anon },
196            );
197            let env_region = ty::Region::new_bound(
198                tcx,
199                ty::INNERMOST,
200                ty::BoundRegion {
201                    var: ty::BoundVar::from_u32(2),
202                    kind: ty::BoundRegionKind::ClosureEnv,
203                },
204            );
205            let va_list_ty = tcx.type_of(did).instantiate(tcx, &[region.into()]);
206            (Ty::new_ref(tcx, env_region, va_list_ty, mutbl), va_list_ty)
207        })
208    };
209
210    let (n_tps, n_lts, n_cts, inputs, output, safety) = if name_str.starts_with("atomic_") {
211        let split: Vec<&str> = name_str.split('_').collect();
212        assert!(split.len() >= 2, "Atomic intrinsic in an incorrect format");
213
214        // Each atomic op has variants with different suffixes (`_seq_cst`, `_acquire`, etc.). Use
215        // string ops to strip the suffixes, because the variants all get the same treatment here.
216        let (n_tps, inputs, output) = match split[1] {
217            "cxchg" | "cxchgweak" => (
218                1,
219                vec![Ty::new_mut_ptr(tcx, param(0)), param(0), param(0)],
220                Ty::new_tup(tcx, &[param(0), tcx.types.bool]),
221            ),
222            "load" => (1, vec![Ty::new_imm_ptr(tcx, param(0))], param(0)),
223            "store" => (1, vec![Ty::new_mut_ptr(tcx, param(0)), param(0)], tcx.types.unit),
224
225            "xchg" | "xadd" | "xsub" | "and" | "nand" | "or" | "xor" | "max" | "min" | "umax"
226            | "umin" => (1, vec![Ty::new_mut_ptr(tcx, param(0)), param(0)], param(0)),
227            "fence" | "singlethreadfence" => (0, Vec::new(), tcx.types.unit),
228            op => {
229                tcx.dcx().emit_err(UnrecognizedAtomicOperation { span, op });
230                return;
231            }
232        };
233        (n_tps, 0, 0, inputs, output, hir::Safety::Unsafe)
234    } else if intrinsic_name == sym::contract_check_ensures {
235        // contract_check_ensures::<'a, Ret, C>(&'a Ret, C)
236        // where C: impl Fn(&'a Ret) -> bool,
237        //
238        // so: two type params, one lifetime param, 0 const params, two inputs, no return
239
240        let p = generics.param_at(0, tcx);
241        let r = ty::Region::new_early_param(tcx, p.to_early_bound_region_data());
242        let ref_ret = Ty::new_imm_ref(tcx, r, param(1));
243        (2, 1, 0, vec![ref_ret, param(2)], tcx.types.unit, hir::Safety::Safe)
244    } else {
245        let safety = intrinsic_operation_unsafety(tcx, intrinsic_id);
246        let (n_tps, n_cts, inputs, output) = match intrinsic_name {
247            sym::abort => (0, 0, vec![], tcx.types.never),
248            sym::unreachable => (0, 0, vec![], tcx.types.never),
249            sym::breakpoint => (0, 0, vec![], tcx.types.unit),
250            sym::size_of | sym::pref_align_of | sym::min_align_of | sym::variant_count => {
251                (1, 0, vec![], tcx.types.usize)
252            }
253            sym::size_of_val | sym::min_align_of_val => {
254                (1, 0, vec![Ty::new_imm_ptr(tcx, param(0))], tcx.types.usize)
255            }
256            sym::rustc_peek => (1, 0, vec![param(0)], param(0)),
257            sym::caller_location => (0, 0, vec![], tcx.caller_location_ty()),
258            sym::assert_inhabited
259            | sym::assert_zero_valid
260            | sym::assert_mem_uninitialized_valid => (1, 0, vec![], tcx.types.unit),
261            sym::forget => (1, 0, vec![param(0)], tcx.types.unit),
262            sym::transmute | sym::transmute_unchecked => (2, 0, vec![param(0)], param(1)),
263            sym::prefetch_read_data
264            | sym::prefetch_write_data
265            | sym::prefetch_read_instruction
266            | sym::prefetch_write_instruction => {
267                (1, 0, vec![Ty::new_imm_ptr(tcx, param(0)), tcx.types.i32], tcx.types.unit)
268            }
269            sym::needs_drop => (1, 0, vec![], tcx.types.bool),
270
271            sym::type_name => (1, 0, vec![], Ty::new_static_str(tcx)),
272            sym::type_id => (1, 0, vec![], tcx.types.u128),
273            sym::offset => (2, 0, vec![param(0), param(1)], param(0)),
274            sym::arith_offset => (
275                1,
276                0,
277                vec![Ty::new_imm_ptr(tcx, param(0)), tcx.types.isize],
278                Ty::new_imm_ptr(tcx, param(0)),
279            ),
280            sym::ptr_mask => (
281                1,
282                0,
283                vec![Ty::new_imm_ptr(tcx, param(0)), tcx.types.usize],
284                Ty::new_imm_ptr(tcx, param(0)),
285            ),
286
287            sym::copy | sym::copy_nonoverlapping => (
288                1,
289                0,
290                vec![
291                    Ty::new_imm_ptr(tcx, param(0)),
292                    Ty::new_mut_ptr(tcx, param(0)),
293                    tcx.types.usize,
294                ],
295                tcx.types.unit,
296            ),
297            sym::volatile_copy_memory | sym::volatile_copy_nonoverlapping_memory => (
298                1,
299                0,
300                vec![
301                    Ty::new_mut_ptr(tcx, param(0)),
302                    Ty::new_imm_ptr(tcx, param(0)),
303                    tcx.types.usize,
304                ],
305                tcx.types.unit,
306            ),
307            sym::compare_bytes => {
308                let byte_ptr = Ty::new_imm_ptr(tcx, tcx.types.u8);
309                (0, 0, vec![byte_ptr, byte_ptr, tcx.types.usize], tcx.types.i32)
310            }
311            sym::write_bytes | sym::volatile_set_memory => (
312                1,
313                0,
314                vec![Ty::new_mut_ptr(tcx, param(0)), tcx.types.u8, tcx.types.usize],
315                tcx.types.unit,
316            ),
317
318            sym::sqrtf16 => (0, 0, vec![tcx.types.f16], tcx.types.f16),
319            sym::sqrtf32 => (0, 0, vec![tcx.types.f32], tcx.types.f32),
320            sym::sqrtf64 => (0, 0, vec![tcx.types.f64], tcx.types.f64),
321            sym::sqrtf128 => (0, 0, vec![tcx.types.f128], tcx.types.f128),
322
323            sym::powif16 => (0, 0, vec![tcx.types.f16, tcx.types.i32], tcx.types.f16),
324            sym::powif32 => (0, 0, vec![tcx.types.f32, tcx.types.i32], tcx.types.f32),
325            sym::powif64 => (0, 0, vec![tcx.types.f64, tcx.types.i32], tcx.types.f64),
326            sym::powif128 => (0, 0, vec![tcx.types.f128, tcx.types.i32], tcx.types.f128),
327
328            sym::sinf16 => (0, 0, vec![tcx.types.f16], tcx.types.f16),
329            sym::sinf32 => (0, 0, vec![tcx.types.f32], tcx.types.f32),
330            sym::sinf64 => (0, 0, vec![tcx.types.f64], tcx.types.f64),
331            sym::sinf128 => (0, 0, vec![tcx.types.f128], tcx.types.f128),
332
333            sym::cosf16 => (0, 0, vec![tcx.types.f16], tcx.types.f16),
334            sym::cosf32 => (0, 0, vec![tcx.types.f32], tcx.types.f32),
335            sym::cosf64 => (0, 0, vec![tcx.types.f64], tcx.types.f64),
336            sym::cosf128 => (0, 0, vec![tcx.types.f128], tcx.types.f128),
337
338            sym::powf16 => (0, 0, vec![tcx.types.f16, tcx.types.f16], tcx.types.f16),
339            sym::powf32 => (0, 0, vec![tcx.types.f32, tcx.types.f32], tcx.types.f32),
340            sym::powf64 => (0, 0, vec![tcx.types.f64, tcx.types.f64], tcx.types.f64),
341            sym::powf128 => (0, 0, vec![tcx.types.f128, tcx.types.f128], tcx.types.f128),
342
343            sym::expf16 => (0, 0, vec![tcx.types.f16], tcx.types.f16),
344            sym::expf32 => (0, 0, vec![tcx.types.f32], tcx.types.f32),
345            sym::expf64 => (0, 0, vec![tcx.types.f64], tcx.types.f64),
346            sym::expf128 => (0, 0, vec![tcx.types.f128], tcx.types.f128),
347
348            sym::exp2f16 => (0, 0, vec![tcx.types.f16], tcx.types.f16),
349            sym::exp2f32 => (0, 0, vec![tcx.types.f32], tcx.types.f32),
350            sym::exp2f64 => (0, 0, vec![tcx.types.f64], tcx.types.f64),
351            sym::exp2f128 => (0, 0, vec![tcx.types.f128], tcx.types.f128),
352
353            sym::logf16 => (0, 0, vec![tcx.types.f16], tcx.types.f16),
354            sym::logf32 => (0, 0, vec![tcx.types.f32], tcx.types.f32),
355            sym::logf64 => (0, 0, vec![tcx.types.f64], tcx.types.f64),
356            sym::logf128 => (0, 0, vec![tcx.types.f128], tcx.types.f128),
357
358            sym::log10f16 => (0, 0, vec![tcx.types.f16], tcx.types.f16),
359            sym::log10f32 => (0, 0, vec![tcx.types.f32], tcx.types.f32),
360            sym::log10f64 => (0, 0, vec![tcx.types.f64], tcx.types.f64),
361            sym::log10f128 => (0, 0, vec![tcx.types.f128], tcx.types.f128),
362
363            sym::log2f16 => (0, 0, vec![tcx.types.f16], tcx.types.f16),
364            sym::log2f32 => (0, 0, vec![tcx.types.f32], tcx.types.f32),
365            sym::log2f64 => (0, 0, vec![tcx.types.f64], tcx.types.f64),
366            sym::log2f128 => (0, 0, vec![tcx.types.f128], tcx.types.f128),
367
368            sym::fmaf16 => (0, 0, vec![tcx.types.f16, tcx.types.f16, tcx.types.f16], tcx.types.f16),
369            sym::fmaf32 => (0, 0, vec![tcx.types.f32, tcx.types.f32, tcx.types.f32], tcx.types.f32),
370            sym::fmaf64 => (0, 0, vec![tcx.types.f64, tcx.types.f64, tcx.types.f64], tcx.types.f64),
371            sym::fmaf128 => {
372                (0, 0, vec![tcx.types.f128, tcx.types.f128, tcx.types.f128], tcx.types.f128)
373            }
374
375            sym::fmuladdf16 => {
376                (0, 0, vec![tcx.types.f16, tcx.types.f16, tcx.types.f16], tcx.types.f16)
377            }
378            sym::fmuladdf32 => {
379                (0, 0, vec![tcx.types.f32, tcx.types.f32, tcx.types.f32], tcx.types.f32)
380            }
381            sym::fmuladdf64 => {
382                (0, 0, vec![tcx.types.f64, tcx.types.f64, tcx.types.f64], tcx.types.f64)
383            }
384            sym::fmuladdf128 => {
385                (0, 0, vec![tcx.types.f128, tcx.types.f128, tcx.types.f128], tcx.types.f128)
386            }
387
388            sym::fabsf16 => (0, 0, vec![tcx.types.f16], tcx.types.f16),
389            sym::fabsf32 => (0, 0, vec![tcx.types.f32], tcx.types.f32),
390            sym::fabsf64 => (0, 0, vec![tcx.types.f64], tcx.types.f64),
391            sym::fabsf128 => (0, 0, vec![tcx.types.f128], tcx.types.f128),
392
393            sym::minnumf16 => (0, 0, vec![tcx.types.f16, tcx.types.f16], tcx.types.f16),
394            sym::minnumf32 => (0, 0, vec![tcx.types.f32, tcx.types.f32], tcx.types.f32),
395            sym::minnumf64 => (0, 0, vec![tcx.types.f64, tcx.types.f64], tcx.types.f64),
396            sym::minnumf128 => (0, 0, vec![tcx.types.f128, tcx.types.f128], tcx.types.f128),
397
398            sym::maxnumf16 => (0, 0, vec![tcx.types.f16, tcx.types.f16], tcx.types.f16),
399            sym::maxnumf32 => (0, 0, vec![tcx.types.f32, tcx.types.f32], tcx.types.f32),
400            sym::maxnumf64 => (0, 0, vec![tcx.types.f64, tcx.types.f64], tcx.types.f64),
401            sym::maxnumf128 => (0, 0, vec![tcx.types.f128, tcx.types.f128], tcx.types.f128),
402
403            sym::copysignf16 => (0, 0, vec![tcx.types.f16, tcx.types.f16], tcx.types.f16),
404            sym::copysignf32 => (0, 0, vec![tcx.types.f32, tcx.types.f32], tcx.types.f32),
405            sym::copysignf64 => (0, 0, vec![tcx.types.f64, tcx.types.f64], tcx.types.f64),
406            sym::copysignf128 => (0, 0, vec![tcx.types.f128, tcx.types.f128], tcx.types.f128),
407
408            sym::floorf16 => (0, 0, vec![tcx.types.f16], tcx.types.f16),
409            sym::floorf32 => (0, 0, vec![tcx.types.f32], tcx.types.f32),
410            sym::floorf64 => (0, 0, vec![tcx.types.f64], tcx.types.f64),
411            sym::floorf128 => (0, 0, vec![tcx.types.f128], tcx.types.f128),
412
413            sym::ceilf16 => (0, 0, vec![tcx.types.f16], tcx.types.f16),
414            sym::ceilf32 => (0, 0, vec![tcx.types.f32], tcx.types.f32),
415            sym::ceilf64 => (0, 0, vec![tcx.types.f64], tcx.types.f64),
416            sym::ceilf128 => (0, 0, vec![tcx.types.f128], tcx.types.f128),
417
418            sym::truncf16 => (0, 0, vec![tcx.types.f16], tcx.types.f16),
419            sym::truncf32 => (0, 0, vec![tcx.types.f32], tcx.types.f32),
420            sym::truncf64 => (0, 0, vec![tcx.types.f64], tcx.types.f64),
421            sym::truncf128 => (0, 0, vec![tcx.types.f128], tcx.types.f128),
422
423            sym::round_ties_even_f16 => (0, 0, vec![tcx.types.f16], tcx.types.f16),
424            sym::round_ties_even_f32 => (0, 0, vec![tcx.types.f32], tcx.types.f32),
425            sym::round_ties_even_f64 => (0, 0, vec![tcx.types.f64], tcx.types.f64),
426            sym::round_ties_even_f128 => (0, 0, vec![tcx.types.f128], tcx.types.f128),
427
428            sym::roundf16 => (0, 0, vec![tcx.types.f16], tcx.types.f16),
429            sym::roundf32 => (0, 0, vec![tcx.types.f32], tcx.types.f32),
430            sym::roundf64 => (0, 0, vec![tcx.types.f64], tcx.types.f64),
431            sym::roundf128 => (0, 0, vec![tcx.types.f128], tcx.types.f128),
432
433            sym::volatile_load | sym::unaligned_volatile_load => {
434                (1, 0, vec![Ty::new_imm_ptr(tcx, param(0))], param(0))
435            }
436            sym::volatile_store | sym::unaligned_volatile_store => {
437                (1, 0, vec![Ty::new_mut_ptr(tcx, param(0)), param(0)], tcx.types.unit)
438            }
439
440            sym::ctpop | sym::ctlz | sym::ctlz_nonzero | sym::cttz | sym::cttz_nonzero => {
441                (1, 0, vec![param(0)], tcx.types.u32)
442            }
443
444            sym::bswap | sym::bitreverse => (1, 0, vec![param(0)], param(0)),
445
446            sym::three_way_compare => {
447                (1, 0, vec![param(0), param(0)], tcx.ty_ordering_enum(Some(span)))
448            }
449
450            sym::add_with_overflow | sym::sub_with_overflow | sym::mul_with_overflow => {
451                (1, 0, vec![param(0), param(0)], Ty::new_tup(tcx, &[param(0), tcx.types.bool]))
452            }
453
454            sym::carrying_mul_add => {
455                (2, 0, vec![param(0); 4], Ty::new_tup(tcx, &[param(1), param(0)]))
456            }
457
458            sym::ptr_guaranteed_cmp => (
459                1,
460                0,
461                vec![Ty::new_imm_ptr(tcx, param(0)), Ty::new_imm_ptr(tcx, param(0))],
462                tcx.types.u8,
463            ),
464
465            sym::const_allocate => {
466                (0, 0, vec![tcx.types.usize, tcx.types.usize], Ty::new_mut_ptr(tcx, tcx.types.u8))
467            }
468            sym::const_deallocate => (
469                0,
470                0,
471                vec![Ty::new_mut_ptr(tcx, tcx.types.u8), tcx.types.usize, tcx.types.usize],
472                tcx.types.unit,
473            ),
474
475            sym::ptr_offset_from => (
476                1,
477                0,
478                vec![Ty::new_imm_ptr(tcx, param(0)), Ty::new_imm_ptr(tcx, param(0))],
479                tcx.types.isize,
480            ),
481            sym::ptr_offset_from_unsigned => (
482                1,
483                0,
484                vec![Ty::new_imm_ptr(tcx, param(0)), Ty::new_imm_ptr(tcx, param(0))],
485                tcx.types.usize,
486            ),
487            sym::unchecked_div | sym::unchecked_rem | sym::exact_div | sym::disjoint_bitor => {
488                (1, 0, vec![param(0), param(0)], param(0))
489            }
490            sym::unchecked_shl | sym::unchecked_shr => (2, 0, vec![param(0), param(1)], param(0)),
491            sym::rotate_left | sym::rotate_right => (1, 0, vec![param(0), tcx.types.u32], param(0)),
492            sym::unchecked_add | sym::unchecked_sub | sym::unchecked_mul => {
493                (1, 0, vec![param(0), param(0)], param(0))
494            }
495            sym::wrapping_add | sym::wrapping_sub | sym::wrapping_mul => {
496                (1, 0, vec![param(0), param(0)], param(0))
497            }
498            sym::saturating_add | sym::saturating_sub => (1, 0, vec![param(0), param(0)], param(0)),
499            sym::fadd_fast | sym::fsub_fast | sym::fmul_fast | sym::fdiv_fast | sym::frem_fast => {
500                (1, 0, vec![param(0), param(0)], param(0))
501            }
502            sym::fadd_algebraic
503            | sym::fsub_algebraic
504            | sym::fmul_algebraic
505            | sym::fdiv_algebraic
506            | sym::frem_algebraic => (1, 0, vec![param(0), param(0)], param(0)),
507            sym::float_to_int_unchecked => (2, 0, vec![param(0)], param(1)),
508
509            sym::assume => (0, 0, vec![tcx.types.bool], tcx.types.unit),
510            sym::select_unpredictable => (1, 0, vec![tcx.types.bool, param(0), param(0)], param(0)),
511            sym::cold_path => (0, 0, vec![], tcx.types.unit),
512
513            sym::read_via_copy => (1, 0, vec![Ty::new_imm_ptr(tcx, param(0))], param(0)),
514            sym::write_via_move => {
515                (1, 0, vec![Ty::new_mut_ptr(tcx, param(0)), param(0)], tcx.types.unit)
516            }
517
518            sym::typed_swap_nonoverlapping => {
519                (1, 0, vec![Ty::new_mut_ptr(tcx, param(0)); 2], tcx.types.unit)
520            }
521
522            sym::discriminant_value => {
523                let assoc_items = tcx.associated_item_def_ids(
524                    tcx.require_lang_item(hir::LangItem::DiscriminantKind, None),
525                );
526                let discriminant_def_id = assoc_items[0];
527
528                let br =
529                    ty::BoundRegion { var: ty::BoundVar::ZERO, kind: ty::BoundRegionKind::Anon };
530                (
531                    1,
532                    0,
533                    vec![Ty::new_imm_ref(
534                        tcx,
535                        ty::Region::new_bound(tcx, ty::INNERMOST, br),
536                        param(0),
537                    )],
538                    Ty::new_projection_from_args(
539                        tcx,
540                        discriminant_def_id,
541                        tcx.mk_args(&[param(0).into()]),
542                    ),
543                )
544            }
545
546            sym::catch_unwind => {
547                let mut_u8 = Ty::new_mut_ptr(tcx, tcx.types.u8);
548                let try_fn_ty = ty::Binder::dummy(tcx.mk_fn_sig(
549                    [mut_u8],
550                    tcx.types.unit,
551                    false,
552                    hir::Safety::Safe,
553                    ExternAbi::Rust,
554                ));
555                let catch_fn_ty = ty::Binder::dummy(tcx.mk_fn_sig(
556                    [mut_u8, mut_u8],
557                    tcx.types.unit,
558                    false,
559                    hir::Safety::Safe,
560                    ExternAbi::Rust,
561                ));
562                (
563                    0,
564                    0,
565                    vec![Ty::new_fn_ptr(tcx, try_fn_ty), mut_u8, Ty::new_fn_ptr(tcx, catch_fn_ty)],
566                    tcx.types.i32,
567                )
568            }
569
570            sym::va_start | sym::va_end => match mk_va_list_ty(hir::Mutability::Mut) {
571                Some((va_list_ref_ty, _)) => (0, 0, vec![va_list_ref_ty], tcx.types.unit),
572                None => bug!("`va_list` lang item needed for C-variadic intrinsics"),
573            },
574
575            sym::va_copy => match mk_va_list_ty(hir::Mutability::Not) {
576                Some((va_list_ref_ty, va_list_ty)) => {
577                    let va_list_ptr_ty = Ty::new_mut_ptr(tcx, va_list_ty);
578                    (0, 0, vec![va_list_ptr_ty, va_list_ref_ty], tcx.types.unit)
579                }
580                None => bug!("`va_list` lang item needed for C-variadic intrinsics"),
581            },
582
583            sym::va_arg => match mk_va_list_ty(hir::Mutability::Mut) {
584                Some((va_list_ref_ty, _)) => (1, 0, vec![va_list_ref_ty], param(0)),
585                None => bug!("`va_list` lang item needed for C-variadic intrinsics"),
586            },
587
588            sym::nontemporal_store => {
589                (1, 0, vec![Ty::new_mut_ptr(tcx, param(0)), param(0)], tcx.types.unit)
590            }
591
592            sym::raw_eq => {
593                let br =
594                    ty::BoundRegion { var: ty::BoundVar::ZERO, kind: ty::BoundRegionKind::Anon };
595                let param_ty_lhs =
596                    Ty::new_imm_ref(tcx, ty::Region::new_bound(tcx, ty::INNERMOST, br), param(0));
597                let br = ty::BoundRegion {
598                    var: ty::BoundVar::from_u32(1),
599                    kind: ty::BoundRegionKind::Anon,
600                };
601                let param_ty_rhs =
602                    Ty::new_imm_ref(tcx, ty::Region::new_bound(tcx, ty::INNERMOST, br), param(0));
603                (1, 0, vec![param_ty_lhs, param_ty_rhs], tcx.types.bool)
604            }
605
606            sym::black_box => (1, 0, vec![param(0)], param(0)),
607
608            sym::is_val_statically_known => (1, 0, vec![param(0)], tcx.types.bool),
609
610            sym::const_eval_select => (4, 0, vec![param(0), param(1), param(2)], param(3)),
611
612            sym::vtable_size | sym::vtable_align => {
613                (0, 0, vec![Ty::new_imm_ptr(tcx, tcx.types.unit)], tcx.types.usize)
614            }
615
616            // This type check is not particularly useful, but the `where` bounds
617            // on the definition in `core` do the heavy lifting for checking it.
618            sym::aggregate_raw_ptr => (3, 0, vec![param(1), param(2)], param(0)),
619            sym::ptr_metadata => (2, 0, vec![Ty::new_imm_ptr(tcx, param(0))], param(1)),
620
621            sym::ub_checks => (0, 0, Vec::new(), tcx.types.bool),
622
623            sym::box_new => (1, 0, vec![param(0)], Ty::new_box(tcx, param(0))),
624
625            // contract_checks() -> bool
626            sym::contract_checks => (0, 0, Vec::new(), tcx.types.bool),
627            // contract_check_requires::<C>(C) -> bool, where C: impl Fn() -> bool
628            sym::contract_check_requires => (1, 0, vec![param(0)], tcx.types.unit),
629
630            sym::simd_eq
631            | sym::simd_ne
632            | sym::simd_lt
633            | sym::simd_le
634            | sym::simd_gt
635            | sym::simd_ge => (2, 0, vec![param(0), param(0)], param(1)),
636            sym::simd_add
637            | sym::simd_sub
638            | sym::simd_mul
639            | sym::simd_rem
640            | sym::simd_div
641            | sym::simd_shl
642            | sym::simd_shr
643            | sym::simd_and
644            | sym::simd_or
645            | sym::simd_xor
646            | sym::simd_fmin
647            | sym::simd_fmax
648            | sym::simd_saturating_add
649            | sym::simd_saturating_sub => (1, 0, vec![param(0), param(0)], param(0)),
650            sym::simd_arith_offset => (2, 0, vec![param(0), param(1)], param(0)),
651            sym::simd_neg
652            | sym::simd_bswap
653            | sym::simd_bitreverse
654            | sym::simd_ctlz
655            | sym::simd_cttz
656            | sym::simd_ctpop
657            | sym::simd_fsqrt
658            | sym::simd_fsin
659            | sym::simd_fcos
660            | sym::simd_fexp
661            | sym::simd_fexp2
662            | sym::simd_flog2
663            | sym::simd_flog10
664            | sym::simd_flog
665            | sym::simd_fabs
666            | sym::simd_ceil
667            | sym::simd_floor
668            | sym::simd_round
669            | sym::simd_trunc => (1, 0, vec![param(0)], param(0)),
670            sym::simd_fma | sym::simd_relaxed_fma => {
671                (1, 0, vec![param(0), param(0), param(0)], param(0))
672            }
673            sym::simd_gather => (3, 0, vec![param(0), param(1), param(2)], param(0)),
674            sym::simd_masked_load => (3, 0, vec![param(0), param(1), param(2)], param(2)),
675            sym::simd_masked_store => (3, 0, vec![param(0), param(1), param(2)], tcx.types.unit),
676            sym::simd_scatter => (3, 0, vec![param(0), param(1), param(2)], tcx.types.unit),
677            sym::simd_insert => (2, 0, vec![param(0), tcx.types.u32, param(1)], param(0)),
678            sym::simd_extract => (2, 0, vec![param(0), tcx.types.u32], param(1)),
679            sym::simd_cast
680            | sym::simd_as
681            | sym::simd_cast_ptr
682            | sym::simd_expose_provenance
683            | sym::simd_with_exposed_provenance => (2, 0, vec![param(0)], param(1)),
684            sym::simd_bitmask => (2, 0, vec![param(0)], param(1)),
685            sym::simd_select | sym::simd_select_bitmask => {
686                (2, 0, vec![param(0), param(1), param(1)], param(1))
687            }
688            sym::simd_reduce_all | sym::simd_reduce_any => (1, 0, vec![param(0)], tcx.types.bool),
689            sym::simd_reduce_add_ordered | sym::simd_reduce_mul_ordered => {
690                (2, 0, vec![param(0), param(1)], param(1))
691            }
692            sym::simd_reduce_add_unordered
693            | sym::simd_reduce_mul_unordered
694            | sym::simd_reduce_and
695            | sym::simd_reduce_or
696            | sym::simd_reduce_xor
697            | sym::simd_reduce_min
698            | sym::simd_reduce_max => (2, 0, vec![param(0)], param(1)),
699            sym::simd_shuffle => (3, 0, vec![param(0), param(0), param(1)], param(2)),
700            sym::simd_shuffle_const_generic => (2, 1, vec![param(0), param(0)], param(1)),
701
702            other => {
703                tcx.dcx().emit_err(UnrecognizedIntrinsicFunction { span, name: other });
704                return;
705            }
706        };
707        (n_tps, 0, n_cts, inputs, output, safety)
708    };
709    let sig = tcx.mk_fn_sig(inputs, output, false, safety, abi);
710    let sig = ty::Binder::bind_with_vars(sig, bound_vars);
711    equate_intrinsic_type(tcx, span, intrinsic_id, n_tps, n_lts, n_cts, sig)
712}