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