rustc_hir_analysis/check/
intrinsic.rs

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