1use 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 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
72pub 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 Safety::Unsafe
79 };
80 let is_in_list = match tcx.item_name(intrinsic_id.into()) {
81 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::const_eval_select => hir::Safety::Safe,
144 _ => hir::Safety::Unsafe,
145 };
146
147 if has_safe_attr != is_in_list {
148 tcx.dcx().struct_span_err(
149 tcx.def_span(intrinsic_id),
150 DiagMessage::from(format!(
151 "intrinsic safety mismatch between list of intrinsics within the compiler and core library intrinsics for intrinsic `{}`",
152 tcx.item_name(intrinsic_id.into())
153 )
154 )).emit();
155 }
156
157 is_in_list
158}
159
160pub fn check_intrinsic_type(
163 tcx: TyCtxt<'_>,
164 intrinsic_id: LocalDefId,
165 span: Span,
166 intrinsic_name: Symbol,
167 abi: ExternAbi,
168) {
169 let generics = tcx.generics_of(intrinsic_id);
170 let param = |n| {
171 if let &ty::GenericParamDef { name, kind: ty::GenericParamDefKind::Type { .. }, .. } =
172 generics.param_at(n as usize, tcx)
173 {
174 Ty::new_param(tcx, n, name)
175 } else {
176 Ty::new_error_with_message(tcx, span, "expected param")
177 }
178 };
179 let name_str = intrinsic_name.as_str();
180
181 let bound_vars = tcx.mk_bound_variable_kinds(&[
182 ty::BoundVariableKind::Region(ty::BoundRegionKind::Anon),
183 ty::BoundVariableKind::Region(ty::BoundRegionKind::Anon),
184 ty::BoundVariableKind::Region(ty::BoundRegionKind::ClosureEnv),
185 ]);
186 let mk_va_list_ty = |mutbl| {
187 tcx.lang_items().va_list().map(|did| {
188 let region = ty::Region::new_bound(
189 tcx,
190 ty::INNERMOST,
191 ty::BoundRegion { var: ty::BoundVar::ZERO, kind: ty::BoundRegionKind::Anon },
192 );
193 let env_region = ty::Region::new_bound(
194 tcx,
195 ty::INNERMOST,
196 ty::BoundRegion {
197 var: ty::BoundVar::from_u32(2),
198 kind: ty::BoundRegionKind::ClosureEnv,
199 },
200 );
201 let va_list_ty = tcx.type_of(did).instantiate(tcx, &[region.into()]);
202 (Ty::new_ref(tcx, env_region, va_list_ty, mutbl), va_list_ty)
203 })
204 };
205
206 let (n_tps, n_lts, n_cts, inputs, output, safety) = if name_str.starts_with("atomic_") {
207 let split: Vec<&str> = name_str.split('_').collect();
208 assert!(split.len() >= 2, "Atomic intrinsic in an incorrect format");
209
210 let (n_tps, inputs, output) = match split[1] {
213 "cxchg" | "cxchgweak" => (
214 1,
215 vec![Ty::new_mut_ptr(tcx, param(0)), param(0), param(0)],
216 Ty::new_tup(tcx, &[param(0), tcx.types.bool]),
217 ),
218 "load" => (1, vec![Ty::new_imm_ptr(tcx, param(0))], param(0)),
219 "store" => (1, vec![Ty::new_mut_ptr(tcx, param(0)), param(0)], tcx.types.unit),
220
221 "xchg" | "xadd" | "xsub" | "and" | "nand" | "or" | "xor" | "max" | "min" | "umax"
222 | "umin" => (1, vec![Ty::new_mut_ptr(tcx, param(0)), param(0)], param(0)),
223 "fence" | "singlethreadfence" => (0, Vec::new(), tcx.types.unit),
224 op => {
225 tcx.dcx().emit_err(UnrecognizedAtomicOperation { span, op });
226 return;
227 }
228 };
229 (n_tps, 0, 0, inputs, output, hir::Safety::Unsafe)
230 } else if intrinsic_name == sym::contract_check_ensures {
231 let p = generics.param_at(0, tcx);
237 let r = ty::Region::new_early_param(tcx, p.to_early_bound_region_data());
238 let ref_ret = Ty::new_imm_ref(tcx, r, param(1));
239 (2, 1, 0, vec![ref_ret, param(2)], tcx.types.unit, hir::Safety::Safe)
240 } else {
241 let safety = intrinsic_operation_unsafety(tcx, intrinsic_id);
242 let (n_tps, n_cts, inputs, output) = match intrinsic_name {
243 sym::abort => (0, 0, vec![], tcx.types.never),
244 sym::unreachable => (0, 0, vec![], tcx.types.never),
245 sym::breakpoint => (0, 0, vec![], tcx.types.unit),
246 sym::size_of | sym::pref_align_of | sym::min_align_of | sym::variant_count => {
247 (1, 0, vec![], tcx.types.usize)
248 }
249 sym::size_of_val | sym::min_align_of_val => {
250 (1, 0, vec![Ty::new_imm_ptr(tcx, param(0))], tcx.types.usize)
251 }
252 sym::rustc_peek => (1, 0, vec![param(0)], param(0)),
253 sym::caller_location => (0, 0, vec![], tcx.caller_location_ty()),
254 sym::assert_inhabited
255 | sym::assert_zero_valid
256 | sym::assert_mem_uninitialized_valid => (1, 0, vec![], tcx.types.unit),
257 sym::forget => (1, 0, vec![param(0)], tcx.types.unit),
258 sym::transmute | sym::transmute_unchecked => (2, 0, vec![param(0)], param(1)),
259 sym::prefetch_read_data
260 | sym::prefetch_write_data
261 | sym::prefetch_read_instruction
262 | sym::prefetch_write_instruction => {
263 (1, 0, vec![Ty::new_imm_ptr(tcx, param(0)), tcx.types.i32], tcx.types.unit)
264 }
265 sym::needs_drop => (1, 0, vec![], tcx.types.bool),
266
267 sym::type_name => (1, 0, vec![], Ty::new_static_str(tcx)),
268 sym::type_id => (1, 0, vec![], tcx.types.u128),
269 sym::offset => (2, 0, vec![param(0), param(1)], param(0)),
270 sym::arith_offset => (
271 1,
272 0,
273 vec![Ty::new_imm_ptr(tcx, param(0)), tcx.types.isize],
274 Ty::new_imm_ptr(tcx, param(0)),
275 ),
276 sym::ptr_mask => (
277 1,
278 0,
279 vec![Ty::new_imm_ptr(tcx, param(0)), tcx.types.usize],
280 Ty::new_imm_ptr(tcx, param(0)),
281 ),
282
283 sym::copy | sym::copy_nonoverlapping => (
284 1,
285 0,
286 vec![
287 Ty::new_imm_ptr(tcx, param(0)),
288 Ty::new_mut_ptr(tcx, param(0)),
289 tcx.types.usize,
290 ],
291 tcx.types.unit,
292 ),
293 sym::volatile_copy_memory | sym::volatile_copy_nonoverlapping_memory => (
294 1,
295 0,
296 vec![
297 Ty::new_mut_ptr(tcx, param(0)),
298 Ty::new_imm_ptr(tcx, param(0)),
299 tcx.types.usize,
300 ],
301 tcx.types.unit,
302 ),
303 sym::compare_bytes => {
304 let byte_ptr = Ty::new_imm_ptr(tcx, tcx.types.u8);
305 (0, 0, vec![byte_ptr, byte_ptr, tcx.types.usize], tcx.types.i32)
306 }
307 sym::write_bytes | sym::volatile_set_memory => (
308 1,
309 0,
310 vec![Ty::new_mut_ptr(tcx, param(0)), tcx.types.u8, tcx.types.usize],
311 tcx.types.unit,
312 ),
313
314 sym::sqrtf16 => (0, 0, vec![tcx.types.f16], tcx.types.f16),
315 sym::sqrtf32 => (0, 0, vec![tcx.types.f32], tcx.types.f32),
316 sym::sqrtf64 => (0, 0, vec![tcx.types.f64], tcx.types.f64),
317 sym::sqrtf128 => (0, 0, vec![tcx.types.f128], tcx.types.f128),
318
319 sym::powif16 => (0, 0, vec![tcx.types.f16, tcx.types.i32], tcx.types.f16),
320 sym::powif32 => (0, 0, vec![tcx.types.f32, tcx.types.i32], tcx.types.f32),
321 sym::powif64 => (0, 0, vec![tcx.types.f64, tcx.types.i32], tcx.types.f64),
322 sym::powif128 => (0, 0, vec![tcx.types.f128, tcx.types.i32], tcx.types.f128),
323
324 sym::sinf16 => (0, 0, vec![tcx.types.f16], tcx.types.f16),
325 sym::sinf32 => (0, 0, vec![tcx.types.f32], tcx.types.f32),
326 sym::sinf64 => (0, 0, vec![tcx.types.f64], tcx.types.f64),
327 sym::sinf128 => (0, 0, vec![tcx.types.f128], tcx.types.f128),
328
329 sym::cosf16 => (0, 0, vec![tcx.types.f16], tcx.types.f16),
330 sym::cosf32 => (0, 0, vec![tcx.types.f32], tcx.types.f32),
331 sym::cosf64 => (0, 0, vec![tcx.types.f64], tcx.types.f64),
332 sym::cosf128 => (0, 0, vec![tcx.types.f128], tcx.types.f128),
333
334 sym::powf16 => (0, 0, vec![tcx.types.f16, tcx.types.f16], tcx.types.f16),
335 sym::powf32 => (0, 0, vec![tcx.types.f32, tcx.types.f32], tcx.types.f32),
336 sym::powf64 => (0, 0, vec![tcx.types.f64, tcx.types.f64], tcx.types.f64),
337 sym::powf128 => (0, 0, vec![tcx.types.f128, tcx.types.f128], tcx.types.f128),
338
339 sym::expf16 => (0, 0, vec![tcx.types.f16], tcx.types.f16),
340 sym::expf32 => (0, 0, vec![tcx.types.f32], tcx.types.f32),
341 sym::expf64 => (0, 0, vec![tcx.types.f64], tcx.types.f64),
342 sym::expf128 => (0, 0, vec![tcx.types.f128], tcx.types.f128),
343
344 sym::exp2f16 => (0, 0, vec![tcx.types.f16], tcx.types.f16),
345 sym::exp2f32 => (0, 0, vec![tcx.types.f32], tcx.types.f32),
346 sym::exp2f64 => (0, 0, vec![tcx.types.f64], tcx.types.f64),
347 sym::exp2f128 => (0, 0, vec![tcx.types.f128], tcx.types.f128),
348
349 sym::logf16 => (0, 0, vec![tcx.types.f16], tcx.types.f16),
350 sym::logf32 => (0, 0, vec![tcx.types.f32], tcx.types.f32),
351 sym::logf64 => (0, 0, vec![tcx.types.f64], tcx.types.f64),
352 sym::logf128 => (0, 0, vec![tcx.types.f128], tcx.types.f128),
353
354 sym::log10f16 => (0, 0, vec![tcx.types.f16], tcx.types.f16),
355 sym::log10f32 => (0, 0, vec![tcx.types.f32], tcx.types.f32),
356 sym::log10f64 => (0, 0, vec![tcx.types.f64], tcx.types.f64),
357 sym::log10f128 => (0, 0, vec![tcx.types.f128], tcx.types.f128),
358
359 sym::log2f16 => (0, 0, vec![tcx.types.f16], tcx.types.f16),
360 sym::log2f32 => (0, 0, vec![tcx.types.f32], tcx.types.f32),
361 sym::log2f64 => (0, 0, vec![tcx.types.f64], tcx.types.f64),
362 sym::log2f128 => (0, 0, vec![tcx.types.f128], tcx.types.f128),
363
364 sym::fmaf16 => (0, 0, vec![tcx.types.f16, tcx.types.f16, tcx.types.f16], tcx.types.f16),
365 sym::fmaf32 => (0, 0, vec![tcx.types.f32, tcx.types.f32, tcx.types.f32], tcx.types.f32),
366 sym::fmaf64 => (0, 0, vec![tcx.types.f64, tcx.types.f64, tcx.types.f64], tcx.types.f64),
367 sym::fmaf128 => {
368 (0, 0, vec![tcx.types.f128, tcx.types.f128, tcx.types.f128], tcx.types.f128)
369 }
370
371 sym::fmuladdf16 => {
372 (0, 0, vec![tcx.types.f16, tcx.types.f16, tcx.types.f16], tcx.types.f16)
373 }
374 sym::fmuladdf32 => {
375 (0, 0, vec![tcx.types.f32, tcx.types.f32, tcx.types.f32], tcx.types.f32)
376 }
377 sym::fmuladdf64 => {
378 (0, 0, vec![tcx.types.f64, tcx.types.f64, tcx.types.f64], tcx.types.f64)
379 }
380 sym::fmuladdf128 => {
381 (0, 0, vec![tcx.types.f128, tcx.types.f128, tcx.types.f128], tcx.types.f128)
382 }
383
384 sym::fabsf16 => (0, 0, vec![tcx.types.f16], tcx.types.f16),
385 sym::fabsf32 => (0, 0, vec![tcx.types.f32], tcx.types.f32),
386 sym::fabsf64 => (0, 0, vec![tcx.types.f64], tcx.types.f64),
387 sym::fabsf128 => (0, 0, vec![tcx.types.f128], tcx.types.f128),
388
389 sym::minnumf16 => (0, 0, vec![tcx.types.f16, tcx.types.f16], tcx.types.f16),
390 sym::minnumf32 => (0, 0, vec![tcx.types.f32, tcx.types.f32], tcx.types.f32),
391 sym::minnumf64 => (0, 0, vec![tcx.types.f64, tcx.types.f64], tcx.types.f64),
392 sym::minnumf128 => (0, 0, vec![tcx.types.f128, tcx.types.f128], tcx.types.f128),
393
394 sym::maxnumf16 => (0, 0, vec![tcx.types.f16, tcx.types.f16], tcx.types.f16),
395 sym::maxnumf32 => (0, 0, vec![tcx.types.f32, tcx.types.f32], tcx.types.f32),
396 sym::maxnumf64 => (0, 0, vec![tcx.types.f64, tcx.types.f64], tcx.types.f64),
397 sym::maxnumf128 => (0, 0, vec![tcx.types.f128, tcx.types.f128], tcx.types.f128),
398
399 sym::copysignf16 => (0, 0, vec![tcx.types.f16, tcx.types.f16], tcx.types.f16),
400 sym::copysignf32 => (0, 0, vec![tcx.types.f32, tcx.types.f32], tcx.types.f32),
401 sym::copysignf64 => (0, 0, vec![tcx.types.f64, tcx.types.f64], tcx.types.f64),
402 sym::copysignf128 => (0, 0, vec![tcx.types.f128, tcx.types.f128], tcx.types.f128),
403
404 sym::floorf16 => (0, 0, vec![tcx.types.f16], tcx.types.f16),
405 sym::floorf32 => (0, 0, vec![tcx.types.f32], tcx.types.f32),
406 sym::floorf64 => (0, 0, vec![tcx.types.f64], tcx.types.f64),
407 sym::floorf128 => (0, 0, vec![tcx.types.f128], tcx.types.f128),
408
409 sym::ceilf16 => (0, 0, vec![tcx.types.f16], tcx.types.f16),
410 sym::ceilf32 => (0, 0, vec![tcx.types.f32], tcx.types.f32),
411 sym::ceilf64 => (0, 0, vec![tcx.types.f64], tcx.types.f64),
412 sym::ceilf128 => (0, 0, vec![tcx.types.f128], tcx.types.f128),
413
414 sym::truncf16 => (0, 0, vec![tcx.types.f16], tcx.types.f16),
415 sym::truncf32 => (0, 0, vec![tcx.types.f32], tcx.types.f32),
416 sym::truncf64 => (0, 0, vec![tcx.types.f64], tcx.types.f64),
417 sym::truncf128 => (0, 0, vec![tcx.types.f128], tcx.types.f128),
418
419 sym::rintf16 => (0, 0, vec![tcx.types.f16], tcx.types.f16),
420 sym::rintf32 => (0, 0, vec![tcx.types.f32], tcx.types.f32),
421 sym::rintf64 => (0, 0, vec![tcx.types.f64], tcx.types.f64),
422 sym::rintf128 => (0, 0, vec![tcx.types.f128], tcx.types.f128),
423
424 sym::nearbyintf16 => (0, 0, vec![tcx.types.f16], tcx.types.f16),
425 sym::nearbyintf32 => (0, 0, vec![tcx.types.f32], tcx.types.f32),
426 sym::nearbyintf64 => (0, 0, vec![tcx.types.f64], tcx.types.f64),
427 sym::nearbyintf128 => (0, 0, vec![tcx.types.f128], tcx.types.f128),
428
429 sym::roundf16 => (0, 0, vec![tcx.types.f16], tcx.types.f16),
430 sym::roundf32 => (0, 0, vec![tcx.types.f32], tcx.types.f32),
431 sym::roundf64 => (0, 0, vec![tcx.types.f64], tcx.types.f64),
432 sym::roundf128 => (0, 0, vec![tcx.types.f128], tcx.types.f128),
433
434 sym::roundevenf16 => (0, 0, vec![tcx.types.f16], tcx.types.f16),
435 sym::roundevenf32 => (0, 0, vec![tcx.types.f32], tcx.types.f32),
436 sym::roundevenf64 => (0, 0, vec![tcx.types.f64], tcx.types.f64),
437 sym::roundevenf128 => (0, 0, vec![tcx.types.f128], tcx.types.f128),
438
439 sym::volatile_load | sym::unaligned_volatile_load => {
440 (1, 0, vec![Ty::new_imm_ptr(tcx, param(0))], param(0))
441 }
442 sym::volatile_store | sym::unaligned_volatile_store => {
443 (1, 0, vec![Ty::new_mut_ptr(tcx, param(0)), param(0)], tcx.types.unit)
444 }
445
446 sym::ctpop | sym::ctlz | sym::ctlz_nonzero | sym::cttz | sym::cttz_nonzero => {
447 (1, 0, vec![param(0)], tcx.types.u32)
448 }
449
450 sym::bswap | sym::bitreverse => (1, 0, vec![param(0)], param(0)),
451
452 sym::three_way_compare => {
453 (1, 0, vec![param(0), param(0)], tcx.ty_ordering_enum(Some(span)))
454 }
455
456 sym::add_with_overflow | sym::sub_with_overflow | sym::mul_with_overflow => {
457 (1, 0, vec![param(0), param(0)], Ty::new_tup(tcx, &[param(0), tcx.types.bool]))
458 }
459
460 sym::carrying_mul_add => {
461 (2, 0, vec![param(0); 4], Ty::new_tup(tcx, &[param(1), param(0)]))
462 }
463
464 sym::ptr_guaranteed_cmp => (
465 1,
466 0,
467 vec![Ty::new_imm_ptr(tcx, param(0)), Ty::new_imm_ptr(tcx, param(0))],
468 tcx.types.u8,
469 ),
470
471 sym::const_allocate => {
472 (0, 0, vec![tcx.types.usize, tcx.types.usize], Ty::new_mut_ptr(tcx, tcx.types.u8))
473 }
474 sym::const_deallocate => (
475 0,
476 0,
477 vec![Ty::new_mut_ptr(tcx, tcx.types.u8), tcx.types.usize, tcx.types.usize],
478 tcx.types.unit,
479 ),
480
481 sym::ptr_offset_from => (
482 1,
483 0,
484 vec![Ty::new_imm_ptr(tcx, param(0)), Ty::new_imm_ptr(tcx, param(0))],
485 tcx.types.isize,
486 ),
487 sym::ptr_offset_from_unsigned => (
488 1,
489 0,
490 vec![Ty::new_imm_ptr(tcx, param(0)), Ty::new_imm_ptr(tcx, param(0))],
491 tcx.types.usize,
492 ),
493 sym::unchecked_div | sym::unchecked_rem | sym::exact_div | sym::disjoint_bitor => {
494 (1, 0, vec![param(0), param(0)], param(0))
495 }
496 sym::unchecked_shl | sym::unchecked_shr => (2, 0, vec![param(0), param(1)], param(0)),
497 sym::rotate_left | sym::rotate_right => (1, 0, vec![param(0), tcx.types.u32], param(0)),
498 sym::unchecked_add | sym::unchecked_sub | sym::unchecked_mul => {
499 (1, 0, vec![param(0), param(0)], param(0))
500 }
501 sym::wrapping_add | sym::wrapping_sub | sym::wrapping_mul => {
502 (1, 0, vec![param(0), param(0)], param(0))
503 }
504 sym::saturating_add | sym::saturating_sub => (1, 0, vec![param(0), param(0)], param(0)),
505 sym::fadd_fast | sym::fsub_fast | sym::fmul_fast | sym::fdiv_fast | sym::frem_fast => {
506 (1, 0, vec![param(0), param(0)], param(0))
507 }
508 sym::fadd_algebraic
509 | sym::fsub_algebraic
510 | sym::fmul_algebraic
511 | sym::fdiv_algebraic
512 | sym::frem_algebraic => (1, 0, vec![param(0), param(0)], param(0)),
513 sym::float_to_int_unchecked => (2, 0, vec![param(0)], param(1)),
514
515 sym::assume => (0, 0, vec![tcx.types.bool], tcx.types.unit),
516 sym::select_unpredictable => (1, 0, vec![tcx.types.bool, param(0), param(0)], param(0)),
517 sym::cold_path => (0, 0, vec![], tcx.types.unit),
518
519 sym::read_via_copy => (1, 0, vec![Ty::new_imm_ptr(tcx, param(0))], param(0)),
520 sym::write_via_move => {
521 (1, 0, vec![Ty::new_mut_ptr(tcx, param(0)), param(0)], tcx.types.unit)
522 }
523
524 sym::typed_swap_nonoverlapping => {
525 (1, 0, vec![Ty::new_mut_ptr(tcx, param(0)); 2], tcx.types.unit)
526 }
527
528 sym::discriminant_value => {
529 let assoc_items = tcx.associated_item_def_ids(
530 tcx.require_lang_item(hir::LangItem::DiscriminantKind, None),
531 );
532 let discriminant_def_id = assoc_items[0];
533
534 let br =
535 ty::BoundRegion { var: ty::BoundVar::ZERO, kind: ty::BoundRegionKind::Anon };
536 (
537 1,
538 0,
539 vec![Ty::new_imm_ref(
540 tcx,
541 ty::Region::new_bound(tcx, ty::INNERMOST, br),
542 param(0),
543 )],
544 Ty::new_projection_from_args(
545 tcx,
546 discriminant_def_id,
547 tcx.mk_args(&[param(0).into()]),
548 ),
549 )
550 }
551
552 sym::catch_unwind => {
553 let mut_u8 = Ty::new_mut_ptr(tcx, tcx.types.u8);
554 let try_fn_ty = ty::Binder::dummy(tcx.mk_fn_sig(
555 [mut_u8],
556 tcx.types.unit,
557 false,
558 hir::Safety::Safe,
559 ExternAbi::Rust,
560 ));
561 let catch_fn_ty = ty::Binder::dummy(tcx.mk_fn_sig(
562 [mut_u8, mut_u8],
563 tcx.types.unit,
564 false,
565 hir::Safety::Safe,
566 ExternAbi::Rust,
567 ));
568 (
569 0,
570 0,
571 vec![Ty::new_fn_ptr(tcx, try_fn_ty), mut_u8, Ty::new_fn_ptr(tcx, catch_fn_ty)],
572 tcx.types.i32,
573 )
574 }
575
576 sym::va_start | sym::va_end => match mk_va_list_ty(hir::Mutability::Mut) {
577 Some((va_list_ref_ty, _)) => (0, 0, vec![va_list_ref_ty], tcx.types.unit),
578 None => bug!("`va_list` lang item needed for C-variadic intrinsics"),
579 },
580
581 sym::va_copy => match mk_va_list_ty(hir::Mutability::Not) {
582 Some((va_list_ref_ty, va_list_ty)) => {
583 let va_list_ptr_ty = Ty::new_mut_ptr(tcx, va_list_ty);
584 (0, 0, vec![va_list_ptr_ty, va_list_ref_ty], tcx.types.unit)
585 }
586 None => bug!("`va_list` lang item needed for C-variadic intrinsics"),
587 },
588
589 sym::va_arg => match mk_va_list_ty(hir::Mutability::Mut) {
590 Some((va_list_ref_ty, _)) => (1, 0, vec![va_list_ref_ty], param(0)),
591 None => bug!("`va_list` lang item needed for C-variadic intrinsics"),
592 },
593
594 sym::nontemporal_store => {
595 (1, 0, vec![Ty::new_mut_ptr(tcx, param(0)), param(0)], tcx.types.unit)
596 }
597
598 sym::raw_eq => {
599 let br =
600 ty::BoundRegion { var: ty::BoundVar::ZERO, kind: ty::BoundRegionKind::Anon };
601 let param_ty_lhs =
602 Ty::new_imm_ref(tcx, ty::Region::new_bound(tcx, ty::INNERMOST, br), param(0));
603 let br = ty::BoundRegion {
604 var: ty::BoundVar::from_u32(1),
605 kind: ty::BoundRegionKind::Anon,
606 };
607 let param_ty_rhs =
608 Ty::new_imm_ref(tcx, ty::Region::new_bound(tcx, ty::INNERMOST, br), param(0));
609 (1, 0, vec![param_ty_lhs, param_ty_rhs], tcx.types.bool)
610 }
611
612 sym::black_box => (1, 0, vec![param(0)], param(0)),
613
614 sym::is_val_statically_known => (1, 0, vec![param(0)], tcx.types.bool),
615
616 sym::const_eval_select => (4, 0, vec![param(0), param(1), param(2)], param(3)),
617
618 sym::vtable_size | sym::vtable_align => {
619 (0, 0, vec![Ty::new_imm_ptr(tcx, tcx.types.unit)], tcx.types.usize)
620 }
621
622 sym::aggregate_raw_ptr => (3, 0, vec![param(1), param(2)], param(0)),
625 sym::ptr_metadata => (2, 0, vec![Ty::new_imm_ptr(tcx, param(0))], param(1)),
626
627 sym::ub_checks => (0, 0, Vec::new(), tcx.types.bool),
628
629 sym::box_new => (1, 0, vec![param(0)], Ty::new_box(tcx, param(0))),
630
631 sym::contract_checks => (0, 0, Vec::new(), tcx.types.bool),
633 sym::contract_check_requires => (1, 0, vec![param(0)], tcx.types.unit),
635
636 sym::simd_eq
637 | sym::simd_ne
638 | sym::simd_lt
639 | sym::simd_le
640 | sym::simd_gt
641 | sym::simd_ge => (2, 0, vec![param(0), param(0)], param(1)),
642 sym::simd_add
643 | sym::simd_sub
644 | sym::simd_mul
645 | sym::simd_rem
646 | sym::simd_div
647 | sym::simd_shl
648 | sym::simd_shr
649 | sym::simd_and
650 | sym::simd_or
651 | sym::simd_xor
652 | sym::simd_fmin
653 | sym::simd_fmax
654 | sym::simd_fpow
655 | sym::simd_saturating_add
656 | sym::simd_saturating_sub => (1, 0, vec![param(0), param(0)], param(0)),
657 sym::simd_arith_offset => (2, 0, vec![param(0), param(1)], param(0)),
658 sym::simd_neg
659 | sym::simd_bswap
660 | sym::simd_bitreverse
661 | sym::simd_ctlz
662 | sym::simd_cttz
663 | sym::simd_ctpop
664 | sym::simd_fsqrt
665 | sym::simd_fsin
666 | sym::simd_fcos
667 | sym::simd_fexp
668 | sym::simd_fexp2
669 | sym::simd_flog2
670 | sym::simd_flog10
671 | sym::simd_flog
672 | sym::simd_fabs
673 | sym::simd_ceil
674 | sym::simd_floor
675 | sym::simd_round
676 | sym::simd_trunc => (1, 0, vec![param(0)], param(0)),
677 sym::simd_fpowi => (1, 0, vec![param(0), tcx.types.i32], param(0)),
678 sym::simd_fma | sym::simd_relaxed_fma => {
679 (1, 0, vec![param(0), param(0), param(0)], param(0))
680 }
681 sym::simd_gather => (3, 0, vec![param(0), param(1), param(2)], param(0)),
682 sym::simd_masked_load => (3, 0, vec![param(0), param(1), param(2)], param(2)),
683 sym::simd_masked_store => (3, 0, vec![param(0), param(1), param(2)], tcx.types.unit),
684 sym::simd_scatter => (3, 0, vec![param(0), param(1), param(2)], tcx.types.unit),
685 sym::simd_insert => (2, 0, vec![param(0), tcx.types.u32, param(1)], param(0)),
686 sym::simd_extract => (2, 0, vec![param(0), tcx.types.u32], param(1)),
687 sym::simd_cast
688 | sym::simd_as
689 | sym::simd_cast_ptr
690 | sym::simd_expose_provenance
691 | sym::simd_with_exposed_provenance => (2, 0, vec![param(0)], param(1)),
692 sym::simd_bitmask => (2, 0, vec![param(0)], param(1)),
693 sym::simd_select | sym::simd_select_bitmask => {
694 (2, 0, vec![param(0), param(1), param(1)], param(1))
695 }
696 sym::simd_reduce_all | sym::simd_reduce_any => (1, 0, vec![param(0)], tcx.types.bool),
697 sym::simd_reduce_add_ordered | sym::simd_reduce_mul_ordered => {
698 (2, 0, vec![param(0), param(1)], param(1))
699 }
700 sym::simd_reduce_add_unordered
701 | sym::simd_reduce_mul_unordered
702 | sym::simd_reduce_and
703 | sym::simd_reduce_or
704 | sym::simd_reduce_xor
705 | sym::simd_reduce_min
706 | sym::simd_reduce_max => (2, 0, vec![param(0)], param(1)),
707 sym::simd_shuffle => (3, 0, vec![param(0), param(0), param(1)], param(2)),
708 sym::simd_shuffle_generic => (2, 1, vec![param(0), param(0)], param(1)),
709
710 other => {
711 tcx.dcx().emit_err(UnrecognizedIntrinsicFunction { span, name: other });
712 return;
713 }
714 };
715 (n_tps, 0, n_cts, inputs, output, safety)
716 };
717 let sig = tcx.mk_fn_sig(inputs, output, false, safety, abi);
718 let sig = ty::Binder::bind_with_vars(sig, bound_vars);
719 equate_intrinsic_type(tcx, span, intrinsic_id, n_tps, n_lts, n_cts, sig)
720}