1use std::{collections::HashMap, sync::OnceLock};
4
5use rustc_hir::def_id::DefId;
6use rustc_middle::{
7 mir::Operand,
8 ty::{Ty, TyCtxt, TyKind},
9};
10use serde::Deserialize;
11
12use super::{
13 BoundedArithmeticOp, CallEffect, CallEffectSummary, OverflowOp, SimdArithOp, SimdCmpOp,
14};
15use crate::def_ids::type_ids;
16use crate::{
17 analysis::dataflow::pointer_copy,
18 def_ids::{
19 intrinsic_api,
20 lang_api::{self, LangItem},
21 nomir_api, other_api,
22 },
23 util::{
24 mir_utils::{self, type_layout},
25 name::public_def_path,
26 },
27};
28
29pub(crate) struct EffCtx<'a, 'tcx> {
32 pub tcx: TyCtxt<'tcx>,
33 pub caller: DefId,
34 pub func: &'a Operand<'tcx>,
35}
36
37struct Entry {
40 matches: fn(TyCtxt<'_>, Option<DefId>) -> bool,
41 effects: EffectBuilder,
42}
43
44type EffectBuilder = for<'a, 'tcx> fn(&EffCtx<'a, 'tcx>) -> Vec<CallEffect>;
45
46macro_rules! ED {
48 ($m:expr, $e:ident) => {
49 Entry {
50 matches: $m,
51 effects: $e,
52 }
53 };
54}
55
56static REGISTRY: &[Entry] = &[
57 ED!(nomir_api::is_global_alloc_shim, eff_nullable_allocation),
59 ED!(nomir_api::is_global_alloc_zeroed_shim, eff_nullable_zeroed_allocation),
60 ED!(nomir_api::is_alloc_shim_marker, eff_no_effect),
61 ED!(nomir_api::is_convert_vec_to_vec, eff_vec_from_slice),
62];
63
64#[derive(Deserialize)]
65#[serde(deny_unknown_fields)]
66struct EffectModel {
67 effect: String,
68}
69
70fn load_effect_models(source: &'static str) -> HashMap<String, EffectModel> {
71 serde_json::from_str(source).expect("failed to parse call effect models")
72}
73
74fn intrinsic_effects() -> &'static HashMap<String, EffectModel> {
75 static MODELS: OnceLock<HashMap<String, EffectModel>> = OnceLock::new();
76 MODELS.get_or_init(|| load_effect_models(include_str!("assets/intrinsic-effects.json")))
77}
78
79fn ffi_effects() -> &'static HashMap<String, EffectModel> {
80 static MODELS: OnceLock<HashMap<String, EffectModel>> = OnceLock::new();
81 MODELS.get_or_init(|| load_effect_models(include_str!("assets/ffi-effects.json")))
82}
83
84fn effect_builder(name: &str) -> Option<EffectBuilder> {
85 match name {
86 "eff_alias_arg0" => Some(eff_alias_arg0),
87 "eff_align_of_val" => Some(eff_align_of_val),
88 "eff_arith_offset" => Some(eff_arith_offset),
89 "eff_atomic_rmw" => Some(eff_atomic_rmw),
90 "eff_cmp_min" => Some(eff_cmp_min),
91 "eff_compare_bytes" => Some(eff_compare_bytes),
92 "eff_copy_memory" => Some(eff_copy_memory),
93 "eff_exact_div" => Some(eff_exact_div),
94 "eff_float_to_int_unchecked" => Some(eff_float_to_int_unchecked),
95 "eff_fill_bytes" => Some(eff_fill_bytes),
96 "eff_layout_const" => Some(eff_layout_const),
97 "eff_leading_zeros" => Some(eff_leading_zeros),
98 "eff_load_memory" => Some(eff_load_memory),
99 "eff_no_effect" => Some(eff_no_effect),
100 "eff_opaque_return" => Some(eff_opaque_return),
101 "eff_ptr_mask" => Some(eff_ptr_mask),
102 "eff_ptr_offset_from" => Some(eff_ptr_offset_from),
103 "eff_raw_eq" => Some(eff_raw_eq),
104 "eff_return_add" => Some(eff_return_add),
105 "eff_return_arg_value" => Some(eff_return_arg_value),
106 "eff_return_max" => Some(eff_return_max),
107 "eff_return_mul" => Some(eff_return_mul),
108 "eff_return_nonzero_iff" => Some(eff_return_nonzero_iff),
109 "eff_saturating_add" => Some(eff_saturating_add),
110 "eff_saturating_sub" => Some(eff_saturating_sub),
111 "eff_select_unpredictable" => Some(eff_select_unpredictable),
112 "eff_simd_add" => Some(eff_simd_add),
113 "eff_simd_bitmask" => Some(eff_simd_bitmask),
114 "eff_simd_eq" => Some(eff_simd_eq),
115 "eff_simd_extract" => Some(eff_simd_extract),
116 "eff_simd_gather" => Some(eff_simd_gather),
117 "eff_simd_ge" => Some(eff_simd_ge),
118 "eff_simd_gt" => Some(eff_simd_gt),
119 "eff_simd_insert" => Some(eff_simd_insert),
120 "eff_simd_le" => Some(eff_simd_le),
121 "eff_simd_lt" => Some(eff_simd_lt),
122 "eff_simd_masked_load" => Some(eff_simd_masked_load),
123 "eff_simd_masked_store" => Some(eff_simd_masked_store),
124 "eff_simd_mul" => Some(eff_simd_mul),
125 "eff_simd_ne" => Some(eff_simd_ne),
126 "eff_simd_opaque" => Some(eff_simd_opaque),
127 "eff_simd_saturating_add" => Some(eff_simd_saturating_add),
128 "eff_simd_saturating_sub" => Some(eff_simd_saturating_sub),
129 "eff_simd_scatter" => Some(eff_simd_scatter),
130 "eff_simd_splat" => Some(eff_simd_splat),
131 "eff_simd_sub" => Some(eff_simd_sub),
132 "eff_size_of_val" => Some(eff_size_of_val),
133 "eff_store_memory" => Some(eff_store_memory),
134 "eff_swap_memory" => Some(eff_swap_memory),
135 "eff_trailing_zeros" => Some(eff_trailing_zeros),
136 "eff_volatile_copy_memory" => Some(eff_volatile_copy_memory),
137 "eff_with_overflow" => Some(eff_with_overflow),
138 "eff_wrapping_add" => Some(eff_wrapping_add),
139 "eff_wrapping_sub" => Some(eff_wrapping_sub),
140 _ => None,
141 }
142}
143
144fn model_builder(model: &EffectModel) -> EffectBuilder {
145 let name = model.effect.as_str();
146 effect_builder(name).unwrap_or_else(|| panic!("unknown call effect builder: {name}"))
147}
148
149fn validate_model_effects() {
150 static VALIDATED: OnceLock<()> = OnceLock::new();
151 VALIDATED.get_or_init(|| {
152 for model in intrinsic_effects()
153 .values()
154 .chain(ffi_effects().values())
155 {
156 model_builder(model);
157 }
158 });
159}
160
161fn lookup_builder(tcx: TyCtxt<'_>, callee: Option<DefId>) -> Option<EffectBuilder> {
162 let id = callee?;
163 if let Some(name) = intrinsic_api::intrinsic_name(tcx, Some(id)) {
164 validate_model_effects();
165 return intrinsic_effects()
166 .get(name.as_str())
167 .map(model_builder);
168 }
169 if tcx.is_foreign_item(id) {
170 validate_model_effects();
171 if let Some(builder) = ffi_effects()
172 .get(&public_def_path(tcx, id))
173 .map(model_builder)
174 {
175 return Some(builder);
176 }
177 }
178 if other_api::is_align_offset(tcx, id) {
179 return Some(eff_align_offset);
180 }
181 REGISTRY
182 .iter()
183 .find(|entry| (entry.matches)(tcx, callee))
184 .map(|entry| entry.effects)
185}
186
187pub(crate) fn has_registered_effect(tcx: TyCtxt<'_>, callee: Option<DefId>) -> bool {
188 lookup_builder(tcx, callee).is_some()
189}
190
191pub(crate) fn is_unmodeled_intrinsic(tcx: TyCtxt<'_>, callee: Option<DefId>) -> bool {
192 callee.is_some_and(|id| intrinsic_api::is_intrinsic(tcx, id))
193 && !has_registered_effect(tcx, callee)
194}
195
196pub(crate) fn lookup_effect<'tcx>(
197 tcx: TyCtxt<'tcx>,
198 caller: DefId,
199 callee: Option<DefId>,
200 func: &Operand<'tcx>,
201) -> Option<CallEffectSummary> {
202 if let Some(builder) = lookup_builder(tcx, callee) {
203 let ctx = EffCtx {
204 tcx,
205 caller,
206 func,
207 };
208 return Some(builder(&ctx));
209 }
210 if callee.is_some_and(|id| {
211 pointer_copy::direct_copy_flow(tcx, id).is_some() && !tcx.is_mir_available(id)
212 }) {
213 return Some(eff_copy_memory(&EffCtx {
214 tcx,
215 caller,
216 func,
217 }));
218 }
219 None
220}
221fn eff_return_arg_value(_: &EffCtx<'_, '_>) -> Vec<CallEffect> {
224 vec![CallEffect::ReturnArgValue { arg: 0 }]
225}
226
227fn eff_opaque_return(_: &EffCtx<'_, '_>) -> Vec<CallEffect> {
228 vec![CallEffect::ReturnOpaque]
229}
230
231fn eff_float_to_int_unchecked(_: &EffCtx<'_, '_>) -> Vec<CallEffect> {
232 vec![CallEffect::ReturnFloatToIntUnchecked]
233}
234
235fn eff_size_of_val(_: &EffCtx<'_, '_>) -> Vec<CallEffect> {
236 vec![CallEffect::ReturnSizeOfVal { arg: 0 }]
237}
238
239fn eff_align_of_val(_: &EffCtx<'_, '_>) -> Vec<CallEffect> {
240 vec![CallEffect::ReturnAlignOfVal { arg: 0 }]
241}
242
243fn eff_trailing_zeros(_: &EffCtx<'_, '_>) -> Vec<CallEffect> {
244 vec![CallEffect::ReturnTrailingZeros { arg: 0 }]
245}
246
247fn eff_load_memory(_: &EffCtx<'_, '_>) -> Vec<CallEffect> {
248 vec![CallEffect::LoadMemory { pointer_arg: 0 }]
249}
250
251fn eff_store_memory(_: &EffCtx<'_, '_>) -> Vec<CallEffect> {
252 vec![CallEffect::StoreMemory {
253 pointer_arg: 0,
254 value_arg: 1,
255 }]
256}
257
258fn eff_volatile_copy_memory(_: &EffCtx<'_, '_>) -> Vec<CallEffect> {
259 vec![CallEffect::CopyMemory {
260 source_arg: 1,
261 destination_arg: 0,
262 count_arg: 2,
263 }]
264}
265
266fn eff_compare_bytes(_: &EffCtx<'_, '_>) -> Vec<CallEffect> {
267 vec![CallEffect::ReturnCompareBytes {
268 left_arg: 0,
269 right_arg: 1,
270 count_arg: 2,
271 }]
272}
273
274fn eff_atomic_rmw(_: &EffCtx<'_, '_>) -> Vec<CallEffect> {
275 vec![CallEffect::AtomicRmw { pointer_arg: 0 }]
276}
277
278fn eff_ptr_mask(_: &EffCtx<'_, '_>) -> Vec<CallEffect> {
279 vec![CallEffect::ReturnMaskedPointer {
280 pointer_arg: 0,
281 mask_arg: 1,
282 }]
283}
284
285fn eff_simd_lt(_: &EffCtx<'_, '_>) -> Vec<CallEffect> {
286 vec![CallEffect::SimdCompare { lhs_arg: 0, rhs_arg: 1, op: SimdCmpOp::Lt }]
287}
288
289fn eff_simd_eq(_: &EffCtx<'_, '_>) -> Vec<CallEffect> {
290 vec![CallEffect::SimdCompare { lhs_arg: 0, rhs_arg: 1, op: SimdCmpOp::Eq }]
291}
292
293fn eff_simd_gt(_: &EffCtx<'_, '_>) -> Vec<CallEffect> {
294 vec![CallEffect::SimdCompare { lhs_arg: 0, rhs_arg: 1, op: SimdCmpOp::Gt }]
295}
296
297fn eff_simd_ne(_: &EffCtx<'_, '_>) -> Vec<CallEffect> {
298 vec![CallEffect::SimdCompare { lhs_arg: 0, rhs_arg: 1, op: SimdCmpOp::Ne }]
299}
300
301fn eff_simd_le(_: &EffCtx<'_, '_>) -> Vec<CallEffect> {
302 vec![CallEffect::SimdCompare { lhs_arg: 0, rhs_arg: 1, op: SimdCmpOp::Le }]
303}
304
305fn eff_simd_ge(_: &EffCtx<'_, '_>) -> Vec<CallEffect> {
306 vec![CallEffect::SimdCompare { lhs_arg: 0, rhs_arg: 1, op: SimdCmpOp::Ge }]
307}
308
309fn eff_simd_bitmask(_: &EffCtx<'_, '_>) -> Vec<CallEffect> {
310 vec![CallEffect::SimdBitmask { arg: 0 }]
311}
312
313fn eff_simd_opaque(_: &EffCtx<'_, '_>) -> Vec<CallEffect> {
314 vec![CallEffect::SimdOpaque]
315}
316
317fn eff_simd_splat(_: &EffCtx<'_, '_>) -> Vec<CallEffect> {
318 vec![CallEffect::SimdSplat { arg: 0 }]
319}
320
321fn eff_simd_extract(_: &EffCtx<'_, '_>) -> Vec<CallEffect> {
322 vec![CallEffect::SimdExtract { vector_arg: 0, index_arg: 1 }]
323}
324
325fn eff_simd_insert(_: &EffCtx<'_, '_>) -> Vec<CallEffect> {
326 vec![CallEffect::SimdInsert { vector_arg: 0, index_arg: 1, value_arg: 2 }]
327}
328
329fn eff_simd_scatter(_: &EffCtx<'_, '_>) -> Vec<CallEffect> {
330 vec![CallEffect::SimdHavocMemory { pointer_arg: 1, mask_arg: 2 }]
331}
332
333fn eff_simd_masked_store(_: &EffCtx<'_, '_>) -> Vec<CallEffect> {
334 vec![CallEffect::SimdHavocMemory { pointer_arg: 1, mask_arg: 0 }]
335}
336
337fn eff_simd_gather(_: &EffCtx<'_, '_>) -> Vec<CallEffect> {
338 vec![CallEffect::SimdMaskedFallback { mask_arg: 2, fallback_arg: 0 }]
339}
340
341fn eff_simd_masked_load(_: &EffCtx<'_, '_>) -> Vec<CallEffect> {
342 vec![CallEffect::SimdMaskedFallback { mask_arg: 0, fallback_arg: 2 }]
343}
344
345fn eff_simd_add(_: &EffCtx<'_, '_>) -> Vec<CallEffect> {
346 vec![CallEffect::SimdArithmetic { lhs_arg: 0, rhs_arg: 1, op: SimdArithOp::Add }]
347}
348
349fn eff_simd_sub(_: &EffCtx<'_, '_>) -> Vec<CallEffect> {
350 vec![CallEffect::SimdArithmetic { lhs_arg: 0, rhs_arg: 1, op: SimdArithOp::Sub }]
351}
352
353fn eff_simd_mul(_: &EffCtx<'_, '_>) -> Vec<CallEffect> {
354 vec![CallEffect::SimdArithmetic { lhs_arg: 0, rhs_arg: 1, op: SimdArithOp::Mul }]
355}
356
357fn eff_simd_saturating_add(_: &EffCtx<'_, '_>) -> Vec<CallEffect> {
358 vec![CallEffect::SimdArithmetic { lhs_arg: 0, rhs_arg: 1, op: SimdArithOp::SaturatingAdd }]
359}
360
361fn eff_simd_saturating_sub(_: &EffCtx<'_, '_>) -> Vec<CallEffect> {
362 vec![CallEffect::SimdArithmetic { lhs_arg: 0, rhs_arg: 1, op: SimdArithOp::SaturatingSub }]
363}
364
365fn eff_raw_eq(_: &EffCtx<'_, '_>) -> Vec<CallEffect> {
366 vec![CallEffect::ReturnRawEq {
367 left_arg: 0,
368 right_arg: 1,
369 }]
370}
371
372fn eff_exact_div(_: &EffCtx<'_, '_>) -> Vec<CallEffect> {
373 vec![CallEffect::ReturnExactDiv {
374 lhs_arg: 0,
375 rhs_arg: 1,
376 }]
377}
378
379fn eff_select_unpredictable(_: &EffCtx<'_, '_>) -> Vec<CallEffect> {
380 vec![CallEffect::SelectUnpredictable]
381}
382
383fn eff_alias_arg0(_: &EffCtx<'_, '_>) -> Vec<CallEffect> {
384 vec![CallEffect::ReturnAliasArg { arg: 0 }]
385}
386
387fn eff_ptr_offset_from(_: &EffCtx<'_, '_>) -> Vec<CallEffect> {
388 vec![CallEffect::ReturnPtrOffsetFrom {
389 ptr_arg: 0,
390 base_arg: 1,
391 }]
392}
393
394fn eff_arith_offset(_: &EffCtx<'_, '_>) -> Vec<CallEffect> {
395 vec![CallEffect::ReturnWrappingPointerOffset {
396 base_arg: 0,
397 offset_arg: 1,
398 }]
399}
400
401fn eff_copy_memory(_: &EffCtx<'_, '_>) -> Vec<CallEffect> {
402 vec![CallEffect::CopyMemory {
403 source_arg: 0,
404 destination_arg: 1,
405 count_arg: 2,
406 }]
407}
408
409fn eff_swap_memory(_: &EffCtx<'_, '_>) -> Vec<CallEffect> {
410 vec![CallEffect::SwapMemory {
411 left_arg: 0,
412 right_arg: 1,
413 }]
414}
415
416fn eff_fill_bytes(_: &EffCtx<'_, '_>) -> Vec<CallEffect> {
417 vec![CallEffect::FillBytes {
418 pointer_arg: 0,
419 byte_arg: 1,
420 count_arg: 2,
421 }]
422}
423
424fn eff_cmp_min(_: &EffCtx<'_, '_>) -> Vec<CallEffect> {
425 vec![CallEffect::ReturnMin {
426 lhs_arg: 0,
427 rhs_arg: 1,
428 }]
429}
430
431fn eff_return_nonzero_iff(_: &EffCtx<'_, '_>) -> Vec<CallEffect> {
432 vec![CallEffect::ReturnNonZeroIff { arg: 0 }]
433}
434
435fn eff_leading_zeros(_: &EffCtx<'_, '_>) -> Vec<CallEffect> {
436 vec![CallEffect::ReturnLeadingZeros { arg: 0 }]
437}
438
439fn eff_return_max(_: &EffCtx<'_, '_>) -> Vec<CallEffect> {
440 vec![CallEffect::ReturnMax {
441 lhs_arg: 0,
442 rhs_arg: 1,
443 }]
444}
445
446fn eff_return_add(_: &EffCtx<'_, '_>) -> Vec<CallEffect> {
447 vec![CallEffect::ReturnAdd {
448 lhs_arg: 0,
449 rhs_arg: 1,
450 }]
451}
452
453fn eff_return_mul(_: &EffCtx<'_, '_>) -> Vec<CallEffect> {
454 vec![CallEffect::ReturnMul {
455 lhs_arg: 0,
456 rhs_arg: 1,
457 }]
458}
459
460fn eff_saturating_add(_: &EffCtx<'_, '_>) -> Vec<CallEffect> {
461 vec![CallEffect::ReturnBoundedArithmetic {
462 lhs_arg: 0,
463 rhs_arg: 1,
464 op: BoundedArithmeticOp::SaturatingAdd,
465 }]
466}
467
468fn eff_saturating_sub(_: &EffCtx<'_, '_>) -> Vec<CallEffect> {
469 vec![CallEffect::ReturnBoundedArithmetic {
470 lhs_arg: 0,
471 rhs_arg: 1,
472 op: BoundedArithmeticOp::SaturatingSub,
473 }]
474}
475
476fn eff_wrapping_add(_: &EffCtx<'_, '_>) -> Vec<CallEffect> {
477 vec![CallEffect::ReturnBoundedArithmetic {
478 lhs_arg: 0,
479 rhs_arg: 1,
480 op: BoundedArithmeticOp::WrappingAdd,
481 }]
482}
483
484fn eff_wrapping_sub(_: &EffCtx<'_, '_>) -> Vec<CallEffect> {
485 vec![CallEffect::ReturnBoundedArithmetic {
486 lhs_arg: 0,
487 rhs_arg: 1,
488 op: BoundedArithmeticOp::WrappingSub,
489 }]
490}
491
492fn eff_with_overflow(ctx: &EffCtx<'_, '_>) -> Vec<CallEffect> {
493 let callee = mir_utils::dep_callee_def_id(ctx.func);
494 let intrinsic = intrinsic_api::intrinsic_name(ctx.tcx, callee);
495 let op = match intrinsic.as_ref().map(|name| name.as_str()) {
496 Some("add_with_overflow") => OverflowOp::Add,
497 Some("sub_with_overflow") => OverflowOp::Sub,
498 _ => OverflowOp::Mul,
499 };
500 vec![CallEffect::ReturnWithOverflow {
501 lhs_arg: 0,
502 rhs_arg: 1,
503 op,
504 }]
505}
506
507fn eff_align_offset(_: &EffCtx<'_, '_>) -> Vec<CallEffect> {
508 vec![CallEffect::ReturnAlignOffset {
509 ptr_arg: 0,
510 align_arg: 1,
511 }]
512}
513
514fn eff_nullable_allocation(_: &EffCtx<'_, '_>) -> Vec<CallEffect> {
515 vec![CallEffect::ReturnNullableAllocation {
516 size_arg: 0,
517 align_arg: 1,
518 zeroed: false,
519 }]
520}
521
522fn eff_nullable_zeroed_allocation(_: &EffCtx<'_, '_>) -> Vec<CallEffect> {
523 vec![CallEffect::ReturnNullableAllocation {
524 size_arg: 0,
525 align_arg: 1,
526 zeroed: true,
527 }]
528}
529
530fn eff_no_effect(_: &EffCtx<'_, '_>) -> Vec<CallEffect> {
531 Vec::new()
532}
533
534fn eff_vec_from_slice(_: &EffCtx<'_, '_>) -> Vec<CallEffect> {
535 vec![CallEffect::ReturnVecFromSlice]
536}
537
538fn eff_layout_const(ctx: &EffCtx<'_, '_>) -> Vec<CallEffect> {
539 layout_constant_effect(ctx.tcx, ctx.caller, ctx.func).map_or_else(Vec::new, |e| vec![e])
540}
541
542fn layout_call_ty<'tcx>(func: &Operand<'tcx>) -> Option<Ty<'tcx>> {
545 mir_utils::fn_def_first_type_arg(func)
546}
547
548fn layout_constant_effect<'tcx>(
549 tcx: TyCtxt<'tcx>,
550 caller: DefId,
551 func: &Operand<'tcx>,
552) -> Option<CallEffect> {
553 let ty = layout_call_ty(func)?;
554 let callee = mir_utils::dep_callee_def_id(func)?;
555 let intrinsic = intrinsic_api::intrinsic_name(tcx, Some(callee));
556 let intrinsic = intrinsic.as_ref().map(|name| name.as_str());
557 let is_align = tcx.is_diagnostic_item(rustc_span::Symbol::intern("mem_align_of"), callee)
558 || intrinsic == Some("align_of");
559 let is_size = tcx.is_diagnostic_item(rustc_span::Symbol::intern("mem_size_of"), callee)
560 || intrinsic == Some("size_of");
561 if !is_size && !is_align {
562 return None;
563 }
564 let (align, size) = type_layout(tcx, caller, ty)?;
565 if align == 0 && size == 0 {
570 return Some(CallEffect::ReturnLayoutSymbolic { is_size });
571 }
572 Some(CallEffect::ReturnConst {
573 value: if is_align { align } else { size },
574 })
575}
576
577pub(crate) fn effect_summary<'tcx>(
582 tcx: TyCtxt<'tcx>,
583 func: &Operand<'tcx>,
584) -> Option<CallEffectSummary> {
585 if let Some(peel) = transparent_deref_peel(tcx, func) {
586 return Some(vec![CallEffect::ReturnTransparentDeref { arg: 0, peel }]);
587 }
588 None
589}
590
591fn transparent_deref_peel<'tcx>(tcx: TyCtxt<'tcx>, func: &Operand<'tcx>) -> Option<usize> {
592 let self_ty = mir_utils::fn_def_first_type_arg(func)?;
593 let TyKind::Adt(adt_def, _) = self_ty.kind() else {
594 return None;
595 };
596 let did = adt_def.did();
597 if lang_api::matches(tcx, did, LangItem::ManuallyDrop) {
598 return Some(2);
599 }
600 if lang_api::matches(tcx, did, LangItem::MaybeDangling) {
601 return Some(1);
602 }
603 None
604}
605
606pub(crate) fn vec_elem_ty<'tcx>(ty: Ty<'tcx>) -> Option<Ty<'tcx>> {
610 if let TyKind::Adt(adt_def, substs) = ty.kind()
611 && type_ids::is_std_vec(adt_def.did())
612 {
613 return substs.first().and_then(|s| s.as_type());
614 }
615 None
616}