• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use crate::error::UnsupportedFnAbi;
2 use crate::middle::codegen_fn_attrs::CodegenFnAttrFlags;
3 use crate::query::TyCtxtAt;
4 use crate::ty::normalize_erasing_regions::NormalizationError;
5 use crate::ty::{self, ConstKind, ReprOptions, Ty, TyCtxt, TypeVisitableExt};
6 use rustc_error_messages::DiagnosticMessage;
7 use rustc_errors::{DiagnosticBuilder, Handler, IntoDiagnostic};
8 use rustc_hir as hir;
9 use rustc_hir::def_id::DefId;
10 use rustc_index::IndexVec;
11 use rustc_session::config::OptLevel;
12 use rustc_span::symbol::{sym, Symbol};
13 use rustc_span::{Span, DUMMY_SP};
14 use rustc_target::abi::call::FnAbi;
15 use rustc_target::abi::*;
16 use rustc_target::spec::{abi::Abi as SpecAbi, HasTargetSpec, PanicStrategy, Target};
17 
18 use std::cmp;
19 use std::fmt;
20 use std::num::NonZeroUsize;
21 use std::ops::Bound;
22 
23 pub trait IntegerExt {
to_ty<'tcx>(&self, tcx: TyCtxt<'tcx>, signed: bool) -> Ty<'tcx>24     fn to_ty<'tcx>(&self, tcx: TyCtxt<'tcx>, signed: bool) -> Ty<'tcx>;
from_int_ty<C: HasDataLayout>(cx: &C, ity: ty::IntTy) -> Integer25     fn from_int_ty<C: HasDataLayout>(cx: &C, ity: ty::IntTy) -> Integer;
from_uint_ty<C: HasDataLayout>(cx: &C, uty: ty::UintTy) -> Integer26     fn from_uint_ty<C: HasDataLayout>(cx: &C, uty: ty::UintTy) -> Integer;
repr_discr<'tcx>( tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, repr: &ReprOptions, min: i128, max: i128, ) -> (Integer, bool)27     fn repr_discr<'tcx>(
28         tcx: TyCtxt<'tcx>,
29         ty: Ty<'tcx>,
30         repr: &ReprOptions,
31         min: i128,
32         max: i128,
33     ) -> (Integer, bool);
34 }
35 
36 impl IntegerExt for Integer {
37     #[inline]
to_ty<'tcx>(&self, tcx: TyCtxt<'tcx>, signed: bool) -> Ty<'tcx>38     fn to_ty<'tcx>(&self, tcx: TyCtxt<'tcx>, signed: bool) -> Ty<'tcx> {
39         match (*self, signed) {
40             (I8, false) => tcx.types.u8,
41             (I16, false) => tcx.types.u16,
42             (I32, false) => tcx.types.u32,
43             (I64, false) => tcx.types.u64,
44             (I128, false) => tcx.types.u128,
45             (I8, true) => tcx.types.i8,
46             (I16, true) => tcx.types.i16,
47             (I32, true) => tcx.types.i32,
48             (I64, true) => tcx.types.i64,
49             (I128, true) => tcx.types.i128,
50         }
51     }
52 
from_int_ty<C: HasDataLayout>(cx: &C, ity: ty::IntTy) -> Integer53     fn from_int_ty<C: HasDataLayout>(cx: &C, ity: ty::IntTy) -> Integer {
54         match ity {
55             ty::IntTy::I8 => I8,
56             ty::IntTy::I16 => I16,
57             ty::IntTy::I32 => I32,
58             ty::IntTy::I64 => I64,
59             ty::IntTy::I128 => I128,
60             ty::IntTy::Isize => cx.data_layout().ptr_sized_integer(),
61         }
62     }
from_uint_ty<C: HasDataLayout>(cx: &C, ity: ty::UintTy) -> Integer63     fn from_uint_ty<C: HasDataLayout>(cx: &C, ity: ty::UintTy) -> Integer {
64         match ity {
65             ty::UintTy::U8 => I8,
66             ty::UintTy::U16 => I16,
67             ty::UintTy::U32 => I32,
68             ty::UintTy::U64 => I64,
69             ty::UintTy::U128 => I128,
70             ty::UintTy::Usize => cx.data_layout().ptr_sized_integer(),
71         }
72     }
73 
74     /// Finds the appropriate Integer type and signedness for the given
75     /// signed discriminant range and `#[repr]` attribute.
76     /// N.B.: `u128` values above `i128::MAX` will be treated as signed, but
77     /// that shouldn't affect anything, other than maybe debuginfo.
repr_discr<'tcx>( tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, repr: &ReprOptions, min: i128, max: i128, ) -> (Integer, bool)78     fn repr_discr<'tcx>(
79         tcx: TyCtxt<'tcx>,
80         ty: Ty<'tcx>,
81         repr: &ReprOptions,
82         min: i128,
83         max: i128,
84     ) -> (Integer, bool) {
85         // Theoretically, negative values could be larger in unsigned representation
86         // than the unsigned representation of the signed minimum. However, if there
87         // are any negative values, the only valid unsigned representation is u128
88         // which can fit all i128 values, so the result remains unaffected.
89         let unsigned_fit = Integer::fit_unsigned(cmp::max(min as u128, max as u128));
90         let signed_fit = cmp::max(Integer::fit_signed(min), Integer::fit_signed(max));
91 
92         if let Some(ity) = repr.int {
93             let discr = Integer::from_attr(&tcx, ity);
94             let fit = if ity.is_signed() { signed_fit } else { unsigned_fit };
95             if discr < fit {
96                 bug!(
97                     "Integer::repr_discr: `#[repr]` hint too small for \
98                       discriminant range of enum `{}`",
99                     ty
100                 )
101             }
102             return (discr, ity.is_signed());
103         }
104 
105         let at_least = if repr.c() {
106             // This is usually I32, however it can be different on some platforms,
107             // notably hexagon and arm-none/thumb-none
108             tcx.data_layout().c_enum_min_size
109         } else {
110             // repr(Rust) enums try to be as small as possible
111             I8
112         };
113 
114         // If there are no negative values, we can use the unsigned fit.
115         if min >= 0 {
116             (cmp::max(unsigned_fit, at_least), false)
117         } else {
118             (cmp::max(signed_fit, at_least), true)
119         }
120     }
121 }
122 
123 pub trait PrimitiveExt {
to_ty<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx>124     fn to_ty<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx>;
to_int_ty<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx>125     fn to_int_ty<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx>;
126 }
127 
128 impl PrimitiveExt for Primitive {
129     #[inline]
to_ty<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx>130     fn to_ty<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
131         match *self {
132             Int(i, signed) => i.to_ty(tcx, signed),
133             F32 => tcx.types.f32,
134             F64 => tcx.types.f64,
135             // FIXME(erikdesjardins): handle non-default addrspace ptr sizes
136             Pointer(_) => Ty::new_mut_ptr(tcx, Ty::new_unit(tcx)),
137         }
138     }
139 
140     /// Return an *integer* type matching this primitive.
141     /// Useful in particular when dealing with enum discriminants.
142     #[inline]
to_int_ty<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx>143     fn to_int_ty<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
144         match *self {
145             Int(i, signed) => i.to_ty(tcx, signed),
146             // FIXME(erikdesjardins): handle non-default addrspace ptr sizes
147             Pointer(_) => {
148                 let signed = false;
149                 tcx.data_layout().ptr_sized_integer().to_ty(tcx, signed)
150             }
151             F32 | F64 => bug!("floats do not have an int type"),
152         }
153     }
154 }
155 
156 /// The first half of a fat pointer.
157 ///
158 /// - For a trait object, this is the address of the box.
159 /// - For a slice, this is the base address.
160 pub const FAT_PTR_ADDR: usize = 0;
161 
162 /// The second half of a fat pointer.
163 ///
164 /// - For a trait object, this is the address of the vtable.
165 /// - For a slice, this is the length.
166 pub const FAT_PTR_EXTRA: usize = 1;
167 
168 /// The maximum supported number of lanes in a SIMD vector.
169 ///
170 /// This value is selected based on backend support:
171 /// * LLVM does not appear to have a vector width limit.
172 /// * Cranelift stores the base-2 log of the lane count in a 4 bit integer.
173 pub const MAX_SIMD_LANES: u64 = 1 << 0xF;
174 
175 /// Used in `check_validity_requirement` to indicate the kind of initialization
176 /// that is checked to be valid
177 #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, HashStable)]
178 pub enum ValidityRequirement {
179     Inhabited,
180     Zero,
181     /// The return value of mem::uninitialized, 0x01
182     /// (unless -Zstrict-init-checks is on, in which case it's the same as Uninit).
183     UninitMitigated0x01Fill,
184     /// True uninitialized memory.
185     Uninit,
186 }
187 
188 impl ValidityRequirement {
from_intrinsic(intrinsic: Symbol) -> Option<Self>189     pub fn from_intrinsic(intrinsic: Symbol) -> Option<Self> {
190         match intrinsic {
191             sym::assert_inhabited => Some(Self::Inhabited),
192             sym::assert_zero_valid => Some(Self::Zero),
193             sym::assert_mem_uninitialized_valid => Some(Self::UninitMitigated0x01Fill),
194             _ => None,
195         }
196     }
197 }
198 
199 impl fmt::Display for ValidityRequirement {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result200     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
201         match self {
202             Self::Inhabited => f.write_str("is inhabited"),
203             Self::Zero => f.write_str("allows being left zeroed"),
204             Self::UninitMitigated0x01Fill => f.write_str("allows being filled with 0x01"),
205             Self::Uninit => f.write_str("allows being left uninitialized"),
206         }
207     }
208 }
209 
210 #[derive(Copy, Clone, Debug, HashStable, TyEncodable, TyDecodable)]
211 pub enum LayoutError<'tcx> {
212     Unknown(Ty<'tcx>),
213     SizeOverflow(Ty<'tcx>),
214     NormalizationFailure(Ty<'tcx>, NormalizationError<'tcx>),
215     Cycle,
216 }
217 
218 impl<'tcx> LayoutError<'tcx> {
diagnostic_message(&self) -> DiagnosticMessage219     pub fn diagnostic_message(&self) -> DiagnosticMessage {
220         use crate::fluent_generated::*;
221         use LayoutError::*;
222         match self {
223             Unknown(_) => middle_unknown_layout,
224             SizeOverflow(_) => middle_values_too_big,
225             NormalizationFailure(_, _) => middle_cannot_be_normalized,
226             Cycle => middle_cycle,
227         }
228     }
229 
into_diagnostic(self) -> crate::error::LayoutError<'tcx>230     pub fn into_diagnostic(self) -> crate::error::LayoutError<'tcx> {
231         use crate::error::LayoutError as E;
232         use LayoutError::*;
233         match self {
234             Unknown(ty) => E::Unknown { ty },
235             SizeOverflow(ty) => E::Overflow { ty },
236             NormalizationFailure(ty, e) => {
237                 E::NormalizationFailure { ty, failure_ty: e.get_type_for_failure() }
238             }
239             Cycle => E::Cycle,
240         }
241     }
242 }
243 
244 // FIXME: Once the other errors that embed this error have been converted to translatable
245 // diagnostics, this Display impl should be removed.
246 impl<'tcx> fmt::Display for LayoutError<'tcx> {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result247     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
248         match *self {
249             LayoutError::Unknown(ty) => write!(f, "the type `{}` has an unknown layout", ty),
250             LayoutError::SizeOverflow(ty) => {
251                 write!(f, "values of the type `{}` are too big for the current architecture", ty)
252             }
253             LayoutError::NormalizationFailure(t, e) => write!(
254                 f,
255                 "unable to determine layout for `{}` because `{}` cannot be normalized",
256                 t,
257                 e.get_type_for_failure()
258             ),
259             LayoutError::Cycle => write!(f, "a cycle occurred during layout computation"),
260         }
261     }
262 }
263 
264 #[derive(Clone, Copy)]
265 pub struct LayoutCx<'tcx, C> {
266     pub tcx: C,
267     pub param_env: ty::ParamEnv<'tcx>,
268 }
269 
270 impl<'tcx> LayoutCalculator for LayoutCx<'tcx, TyCtxt<'tcx>> {
271     type TargetDataLayoutRef = &'tcx TargetDataLayout;
272 
delay_bug(&self, txt: String)273     fn delay_bug(&self, txt: String) {
274         self.tcx.sess.delay_span_bug(DUMMY_SP, txt);
275     }
276 
current_data_layout(&self) -> Self::TargetDataLayoutRef277     fn current_data_layout(&self) -> Self::TargetDataLayoutRef {
278         &self.tcx.data_layout
279     }
280 }
281 
282 /// Type size "skeleton", i.e., the only information determining a type's size.
283 /// While this is conservative, (aside from constant sizes, only pointers,
284 /// newtypes thereof and null pointer optimized enums are allowed), it is
285 /// enough to statically check common use cases of transmute.
286 #[derive(Copy, Clone, Debug)]
287 pub enum SizeSkeleton<'tcx> {
288     /// Any statically computable Layout.
289     Known(Size),
290 
291     /// This is a generic const expression (i.e. N * 2), which may contain some parameters.
292     /// It must be of type usize, and represents the size of a type in bytes.
293     /// It is not required to be evaluatable to a concrete value, but can be used to check
294     /// that another SizeSkeleton is of equal size.
295     Generic(ty::Const<'tcx>),
296 
297     /// A potentially-fat pointer.
298     Pointer {
299         /// If true, this pointer is never null.
300         non_zero: bool,
301         /// The type which determines the unsized metadata, if any,
302         /// of this pointer. Either a type parameter or a projection
303         /// depending on one, with regions erased.
304         tail: Ty<'tcx>,
305     },
306 }
307 
308 impl<'tcx> SizeSkeleton<'tcx> {
compute( ty: Ty<'tcx>, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>, ) -> Result<SizeSkeleton<'tcx>, &'tcx LayoutError<'tcx>>309     pub fn compute(
310         ty: Ty<'tcx>,
311         tcx: TyCtxt<'tcx>,
312         param_env: ty::ParamEnv<'tcx>,
313     ) -> Result<SizeSkeleton<'tcx>, &'tcx LayoutError<'tcx>> {
314         debug_assert!(!ty.has_non_region_infer());
315 
316         // First try computing a static layout.
317         let err = match tcx.layout_of(param_env.and(ty)) {
318             Ok(layout) => {
319                 return Ok(SizeSkeleton::Known(layout.size));
320             }
321             Err(err @ LayoutError::Unknown(_)) => err,
322             // We can't extract SizeSkeleton info from other layout errors
323             Err(
324                 e @ LayoutError::Cycle
325                 | e @ LayoutError::SizeOverflow(_)
326                 | e @ LayoutError::NormalizationFailure(..),
327             ) => return Err(e),
328         };
329 
330         match *ty.kind() {
331             ty::Ref(_, pointee, _) | ty::RawPtr(ty::TypeAndMut { ty: pointee, .. }) => {
332                 let non_zero = !ty.is_unsafe_ptr();
333                 let tail = tcx.struct_tail_erasing_lifetimes(pointee, param_env);
334                 match tail.kind() {
335                     ty::Param(_) | ty::Alias(ty::Projection | ty::Inherent, _) => {
336                         debug_assert!(tail.has_non_region_param());
337                         Ok(SizeSkeleton::Pointer { non_zero, tail: tcx.erase_regions(tail) })
338                     }
339                     _ => bug!(
340                         "SizeSkeleton::compute({ty}): layout errored ({err:?}), yet \
341                               tail `{tail}` is not a type parameter or a projection",
342                     ),
343                 }
344             }
345             ty::Array(inner, len)
346                 if len.ty() == tcx.types.usize && tcx.features().transmute_generic_consts =>
347             {
348                 match SizeSkeleton::compute(inner, tcx, param_env)? {
349                     // This may succeed because the multiplication of two types may overflow
350                     // but a single size of a nested array will not.
351                     SizeSkeleton::Known(s) => {
352                         if let Some(c) = len.try_eval_target_usize(tcx, param_env) {
353                             let size = s
354                                 .bytes()
355                                 .checked_mul(c)
356                                 .ok_or_else(|| &*tcx.arena.alloc(LayoutError::SizeOverflow(ty)))?;
357                             return Ok(SizeSkeleton::Known(Size::from_bytes(size)));
358                         }
359                         let len = tcx.expand_abstract_consts(len);
360                         let prev = ty::Const::from_target_usize(tcx, s.bytes());
361                         let Some(gen_size) = mul_sorted_consts(tcx, param_env, len, prev) else {
362                             return Err(tcx.arena.alloc(LayoutError::SizeOverflow(ty)));
363                         };
364                         Ok(SizeSkeleton::Generic(gen_size))
365                     }
366                     SizeSkeleton::Pointer { .. } => Err(err),
367                     SizeSkeleton::Generic(g) => {
368                         let len = tcx.expand_abstract_consts(len);
369                         let Some(gen_size) = mul_sorted_consts(tcx, param_env, len, g) else {
370                             return Err(tcx.arena.alloc(LayoutError::SizeOverflow(ty)));
371                         };
372                         Ok(SizeSkeleton::Generic(gen_size))
373                     }
374                 }
375             }
376 
377             ty::Adt(def, substs) => {
378                 // Only newtypes and enums w/ nullable pointer optimization.
379                 if def.is_union() || def.variants().is_empty() || def.variants().len() > 2 {
380                     return Err(err);
381                 }
382 
383                 // Get a zero-sized variant or a pointer newtype.
384                 let zero_or_ptr_variant = |i| {
385                     let i = VariantIdx::from_usize(i);
386                     let fields =
387                         def.variant(i).fields.iter().map(|field| {
388                             SizeSkeleton::compute(field.ty(tcx, substs), tcx, param_env)
389                         });
390                     let mut ptr = None;
391                     for field in fields {
392                         let field = field?;
393                         match field {
394                             SizeSkeleton::Known(size) => {
395                                 if size.bytes() > 0 {
396                                     return Err(err);
397                                 }
398                             }
399                             SizeSkeleton::Pointer { .. } => {
400                                 if ptr.is_some() {
401                                     return Err(err);
402                                 }
403                                 ptr = Some(field);
404                             }
405                             SizeSkeleton::Generic(_) => {
406                                 return Err(err);
407                             }
408                         }
409                     }
410                     Ok(ptr)
411                 };
412 
413                 let v0 = zero_or_ptr_variant(0)?;
414                 // Newtype.
415                 if def.variants().len() == 1 {
416                     if let Some(SizeSkeleton::Pointer { non_zero, tail }) = v0 {
417                         return Ok(SizeSkeleton::Pointer {
418                             non_zero: non_zero
419                                 || match tcx.layout_scalar_valid_range(def.did()) {
420                                     (Bound::Included(start), Bound::Unbounded) => start > 0,
421                                     (Bound::Included(start), Bound::Included(end)) => {
422                                         0 < start && start < end
423                                     }
424                                     _ => false,
425                                 },
426                             tail,
427                         });
428                     } else {
429                         return Err(err);
430                     }
431                 }
432 
433                 let v1 = zero_or_ptr_variant(1)?;
434                 // Nullable pointer enum optimization.
435                 match (v0, v1) {
436                     (Some(SizeSkeleton::Pointer { non_zero: true, tail }), None)
437                     | (None, Some(SizeSkeleton::Pointer { non_zero: true, tail })) => {
438                         Ok(SizeSkeleton::Pointer { non_zero: false, tail })
439                     }
440                     _ => Err(err),
441                 }
442             }
443 
444             ty::Alias(..) => {
445                 let normalized = tcx.normalize_erasing_regions(param_env, ty);
446                 if ty == normalized {
447                     Err(err)
448                 } else {
449                     SizeSkeleton::compute(normalized, tcx, param_env)
450                 }
451             }
452 
453             _ => Err(err),
454         }
455     }
456 
same_size(self, other: SizeSkeleton<'tcx>) -> bool457     pub fn same_size(self, other: SizeSkeleton<'tcx>) -> bool {
458         match (self, other) {
459             (SizeSkeleton::Known(a), SizeSkeleton::Known(b)) => a == b,
460             (SizeSkeleton::Pointer { tail: a, .. }, SizeSkeleton::Pointer { tail: b, .. }) => {
461                 a == b
462             }
463             // constants are always pre-normalized into a canonical form so this
464             // only needs to check if their pointers are identical.
465             (SizeSkeleton::Generic(a), SizeSkeleton::Generic(b)) => a == b,
466             _ => false,
467         }
468     }
469 }
470 
471 /// When creating the layout for types with abstract consts in their size (i.e. [usize; 4 * N]),
472 /// to ensure that they have a canonical order and can be compared directly we combine all
473 /// constants, and sort the other terms. This allows comparison of expressions of sizes,
474 /// allowing for things like transmuting between types that depend on generic consts.
475 /// This returns `None` if multiplication of constants overflows.
mul_sorted_consts<'tcx>( tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>, a: ty::Const<'tcx>, b: ty::Const<'tcx>, ) -> Option<ty::Const<'tcx>>476 fn mul_sorted_consts<'tcx>(
477     tcx: TyCtxt<'tcx>,
478     param_env: ty::ParamEnv<'tcx>,
479     a: ty::Const<'tcx>,
480     b: ty::Const<'tcx>,
481 ) -> Option<ty::Const<'tcx>> {
482     use crate::mir::BinOp::Mul;
483 
484     let mut work = vec![a, b];
485     let mut done = vec![];
486     while let Some(n) = work.pop() {
487         if let ConstKind::Expr(ty::Expr::Binop(Mul, l, r)) = n.kind() {
488             work.push(l);
489             work.push(r)
490         } else {
491             done.push(n);
492         }
493     }
494     let mut k = 1;
495     let mut overflow = false;
496     done.retain(|c| {
497         let Some(c) = c.try_eval_target_usize(tcx, param_env) else {
498             return true;
499         };
500         let Some(next) = c.checked_mul(k) else {
501             overflow = true;
502             return false;
503         };
504         k = next;
505         false
506     });
507     if overflow {
508         return None;
509     }
510     if k != 1 {
511         done.push(ty::Const::from_target_usize(tcx, k));
512     } else if k == 0 {
513         return Some(ty::Const::from_target_usize(tcx, 0));
514     }
515     done.sort_unstable();
516 
517     // create a single tree from the buffer
518     done.into_iter().reduce(|acc, n| ty::Const::new_expr(tcx, ty::Expr::Binop(Mul, n, acc), n.ty()))
519 }
520 
521 pub trait HasTyCtxt<'tcx>: HasDataLayout {
tcx(&self) -> TyCtxt<'tcx>522     fn tcx(&self) -> TyCtxt<'tcx>;
523 }
524 
525 pub trait HasParamEnv<'tcx> {
param_env(&self) -> ty::ParamEnv<'tcx>526     fn param_env(&self) -> ty::ParamEnv<'tcx>;
527 }
528 
529 impl<'tcx> HasDataLayout for TyCtxt<'tcx> {
530     #[inline]
data_layout(&self) -> &TargetDataLayout531     fn data_layout(&self) -> &TargetDataLayout {
532         &self.data_layout
533     }
534 }
535 
536 impl<'tcx> HasTargetSpec for TyCtxt<'tcx> {
target_spec(&self) -> &Target537     fn target_spec(&self) -> &Target {
538         &self.sess.target
539     }
540 }
541 
542 impl<'tcx> HasTyCtxt<'tcx> for TyCtxt<'tcx> {
543     #[inline]
tcx(&self) -> TyCtxt<'tcx>544     fn tcx(&self) -> TyCtxt<'tcx> {
545         *self
546     }
547 }
548 
549 impl<'tcx> HasDataLayout for TyCtxtAt<'tcx> {
550     #[inline]
data_layout(&self) -> &TargetDataLayout551     fn data_layout(&self) -> &TargetDataLayout {
552         &self.data_layout
553     }
554 }
555 
556 impl<'tcx> HasTargetSpec for TyCtxtAt<'tcx> {
target_spec(&self) -> &Target557     fn target_spec(&self) -> &Target {
558         &self.sess.target
559     }
560 }
561 
562 impl<'tcx> HasTyCtxt<'tcx> for TyCtxtAt<'tcx> {
563     #[inline]
tcx(&self) -> TyCtxt<'tcx>564     fn tcx(&self) -> TyCtxt<'tcx> {
565         **self
566     }
567 }
568 
569 impl<'tcx, C> HasParamEnv<'tcx> for LayoutCx<'tcx, C> {
param_env(&self) -> ty::ParamEnv<'tcx>570     fn param_env(&self) -> ty::ParamEnv<'tcx> {
571         self.param_env
572     }
573 }
574 
575 impl<'tcx, T: HasDataLayout> HasDataLayout for LayoutCx<'tcx, T> {
data_layout(&self) -> &TargetDataLayout576     fn data_layout(&self) -> &TargetDataLayout {
577         self.tcx.data_layout()
578     }
579 }
580 
581 impl<'tcx, T: HasTargetSpec> HasTargetSpec for LayoutCx<'tcx, T> {
target_spec(&self) -> &Target582     fn target_spec(&self) -> &Target {
583         self.tcx.target_spec()
584     }
585 }
586 
587 impl<'tcx, T: HasTyCtxt<'tcx>> HasTyCtxt<'tcx> for LayoutCx<'tcx, T> {
tcx(&self) -> TyCtxt<'tcx>588     fn tcx(&self) -> TyCtxt<'tcx> {
589         self.tcx.tcx()
590     }
591 }
592 
593 pub trait MaybeResult<T> {
594     type Error;
595 
from(x: Result<T, Self::Error>) -> Self596     fn from(x: Result<T, Self::Error>) -> Self;
to_result(self) -> Result<T, Self::Error>597     fn to_result(self) -> Result<T, Self::Error>;
598 }
599 
600 impl<T> MaybeResult<T> for T {
601     type Error = !;
602 
from(Ok(x): Result<T, Self::Error>) -> Self603     fn from(Ok(x): Result<T, Self::Error>) -> Self {
604         x
605     }
to_result(self) -> Result<T, Self::Error>606     fn to_result(self) -> Result<T, Self::Error> {
607         Ok(self)
608     }
609 }
610 
611 impl<T, E> MaybeResult<T> for Result<T, E> {
612     type Error = E;
613 
from(x: Result<T, Self::Error>) -> Self614     fn from(x: Result<T, Self::Error>) -> Self {
615         x
616     }
to_result(self) -> Result<T, Self::Error>617     fn to_result(self) -> Result<T, Self::Error> {
618         self
619     }
620 }
621 
622 pub type TyAndLayout<'tcx> = rustc_target::abi::TyAndLayout<'tcx, Ty<'tcx>>;
623 
624 /// Trait for contexts that want to be able to compute layouts of types.
625 /// This automatically gives access to `LayoutOf`, through a blanket `impl`.
626 pub trait LayoutOfHelpers<'tcx>: HasDataLayout + HasTyCtxt<'tcx> + HasParamEnv<'tcx> {
627     /// The `TyAndLayout`-wrapping type (or `TyAndLayout` itself), which will be
628     /// returned from `layout_of` (see also `handle_layout_err`).
629     type LayoutOfResult: MaybeResult<TyAndLayout<'tcx>>;
630 
631     /// `Span` to use for `tcx.at(span)`, from `layout_of`.
632     // FIXME(eddyb) perhaps make this mandatory to get contexts to track it better?
633     #[inline]
layout_tcx_at_span(&self) -> Span634     fn layout_tcx_at_span(&self) -> Span {
635         DUMMY_SP
636     }
637 
638     /// Helper used for `layout_of`, to adapt `tcx.layout_of(...)` into a
639     /// `Self::LayoutOfResult` (which does not need to be a `Result<...>`).
640     ///
641     /// Most `impl`s, which propagate `LayoutError`s, should simply return `err`,
642     /// but this hook allows e.g. codegen to return only `TyAndLayout` from its
643     /// `cx.layout_of(...)`, without any `Result<...>` around it to deal with
644     /// (and any `LayoutError`s are turned into fatal errors or ICEs).
handle_layout_err( &self, err: LayoutError<'tcx>, span: Span, ty: Ty<'tcx>, ) -> <Self::LayoutOfResult as MaybeResult<TyAndLayout<'tcx>>>::Error645     fn handle_layout_err(
646         &self,
647         err: LayoutError<'tcx>,
648         span: Span,
649         ty: Ty<'tcx>,
650     ) -> <Self::LayoutOfResult as MaybeResult<TyAndLayout<'tcx>>>::Error;
651 }
652 
653 /// Blanket extension trait for contexts that can compute layouts of types.
654 pub trait LayoutOf<'tcx>: LayoutOfHelpers<'tcx> {
655     /// Computes the layout of a type. Note that this implicitly
656     /// executes in "reveal all" mode, and will normalize the input type.
657     #[inline]
layout_of(&self, ty: Ty<'tcx>) -> Self::LayoutOfResult658     fn layout_of(&self, ty: Ty<'tcx>) -> Self::LayoutOfResult {
659         self.spanned_layout_of(ty, DUMMY_SP)
660     }
661 
662     /// Computes the layout of a type, at `span`. Note that this implicitly
663     /// executes in "reveal all" mode, and will normalize the input type.
664     // FIXME(eddyb) avoid passing information like this, and instead add more
665     // `TyCtxt::at`-like APIs to be able to do e.g. `cx.at(span).layout_of(ty)`.
666     #[inline]
spanned_layout_of(&self, ty: Ty<'tcx>, span: Span) -> Self::LayoutOfResult667     fn spanned_layout_of(&self, ty: Ty<'tcx>, span: Span) -> Self::LayoutOfResult {
668         let span = if !span.is_dummy() { span } else { self.layout_tcx_at_span() };
669         let tcx = self.tcx().at(span);
670 
671         MaybeResult::from(
672             tcx.layout_of(self.param_env().and(ty))
673                 .map_err(|err| self.handle_layout_err(*err, span, ty)),
674         )
675     }
676 }
677 
678 impl<'tcx, C: LayoutOfHelpers<'tcx>> LayoutOf<'tcx> for C {}
679 
680 impl<'tcx> LayoutOfHelpers<'tcx> for LayoutCx<'tcx, TyCtxt<'tcx>> {
681     type LayoutOfResult = Result<TyAndLayout<'tcx>, &'tcx LayoutError<'tcx>>;
682 
683     #[inline]
handle_layout_err( &self, err: LayoutError<'tcx>, _: Span, _: Ty<'tcx>, ) -> &'tcx LayoutError<'tcx>684     fn handle_layout_err(
685         &self,
686         err: LayoutError<'tcx>,
687         _: Span,
688         _: Ty<'tcx>,
689     ) -> &'tcx LayoutError<'tcx> {
690         self.tcx.arena.alloc(err)
691     }
692 }
693 
694 impl<'tcx> LayoutOfHelpers<'tcx> for LayoutCx<'tcx, TyCtxtAt<'tcx>> {
695     type LayoutOfResult = Result<TyAndLayout<'tcx>, &'tcx LayoutError<'tcx>>;
696 
697     #[inline]
layout_tcx_at_span(&self) -> Span698     fn layout_tcx_at_span(&self) -> Span {
699         self.tcx.span
700     }
701 
702     #[inline]
handle_layout_err( &self, err: LayoutError<'tcx>, _: Span, _: Ty<'tcx>, ) -> &'tcx LayoutError<'tcx>703     fn handle_layout_err(
704         &self,
705         err: LayoutError<'tcx>,
706         _: Span,
707         _: Ty<'tcx>,
708     ) -> &'tcx LayoutError<'tcx> {
709         self.tcx.arena.alloc(err)
710     }
711 }
712 
713 impl<'tcx, C> TyAbiInterface<'tcx, C> for Ty<'tcx>
714 where
715     C: HasTyCtxt<'tcx> + HasParamEnv<'tcx>,
716 {
ty_and_layout_for_variant( this: TyAndLayout<'tcx>, cx: &C, variant_index: VariantIdx, ) -> TyAndLayout<'tcx>717     fn ty_and_layout_for_variant(
718         this: TyAndLayout<'tcx>,
719         cx: &C,
720         variant_index: VariantIdx,
721     ) -> TyAndLayout<'tcx> {
722         let layout = match this.variants {
723             Variants::Single { index }
724                 // If all variants but one are uninhabited, the variant layout is the enum layout.
725                 if index == variant_index &&
726                 // Don't confuse variants of uninhabited enums with the enum itself.
727                 // For more details see https://github.com/rust-lang/rust/issues/69763.
728                 this.fields != FieldsShape::Primitive =>
729             {
730                 this.layout
731             }
732 
733             Variants::Single { index } => {
734                 let tcx = cx.tcx();
735                 let param_env = cx.param_env();
736 
737                 // Deny calling for_variant more than once for non-Single enums.
738                 if let Ok(original_layout) = tcx.layout_of(param_env.and(this.ty)) {
739                     assert_eq!(original_layout.variants, Variants::Single { index });
740                 }
741 
742                 let fields = match this.ty.kind() {
743                     ty::Adt(def, _) if def.variants().is_empty() =>
744                         bug!("for_variant called on zero-variant enum"),
745                     ty::Adt(def, _) => def.variant(variant_index).fields.len(),
746                     _ => bug!(),
747                 };
748                 tcx.mk_layout(LayoutS {
749                     variants: Variants::Single { index: variant_index },
750                     fields: match NonZeroUsize::new(fields) {
751                         Some(fields) => FieldsShape::Union(fields),
752                         None => FieldsShape::Arbitrary { offsets: IndexVec::new(), memory_index: IndexVec::new() },
753                     },
754                     abi: Abi::Uninhabited,
755                     largest_niche: None,
756                     align: tcx.data_layout.i8_align,
757                     size: Size::ZERO,
758                 })
759             }
760 
761             Variants::Multiple { ref variants, .. } => cx.tcx().mk_layout(variants[variant_index].clone()),
762         };
763 
764         assert_eq!(*layout.variants(), Variants::Single { index: variant_index });
765 
766         TyAndLayout { ty: this.ty, layout }
767     }
768 
ty_and_layout_field(this: TyAndLayout<'tcx>, cx: &C, i: usize) -> TyAndLayout<'tcx>769     fn ty_and_layout_field(this: TyAndLayout<'tcx>, cx: &C, i: usize) -> TyAndLayout<'tcx> {
770         enum TyMaybeWithLayout<'tcx> {
771             Ty(Ty<'tcx>),
772             TyAndLayout(TyAndLayout<'tcx>),
773         }
774 
775         fn field_ty_or_layout<'tcx>(
776             this: TyAndLayout<'tcx>,
777             cx: &(impl HasTyCtxt<'tcx> + HasParamEnv<'tcx>),
778             i: usize,
779         ) -> TyMaybeWithLayout<'tcx> {
780             let tcx = cx.tcx();
781             let tag_layout = |tag: Scalar| -> TyAndLayout<'tcx> {
782                 TyAndLayout {
783                     layout: tcx.mk_layout(LayoutS::scalar(cx, tag)),
784                     ty: tag.primitive().to_ty(tcx),
785                 }
786             };
787 
788             match *this.ty.kind() {
789                 ty::Bool
790                 | ty::Char
791                 | ty::Int(_)
792                 | ty::Uint(_)
793                 | ty::Float(_)
794                 | ty::FnPtr(_)
795                 | ty::Never
796                 | ty::FnDef(..)
797                 | ty::GeneratorWitness(..)
798                 | ty::GeneratorWitnessMIR(..)
799                 | ty::Foreign(..)
800                 | ty::Dynamic(_, _, ty::Dyn) => {
801                     bug!("TyAndLayout::field({:?}): not applicable", this)
802                 }
803 
804                 // Potentially-fat pointers.
805                 ty::Ref(_, pointee, _) | ty::RawPtr(ty::TypeAndMut { ty: pointee, .. }) => {
806                     assert!(i < this.fields.count());
807 
808                     // Reuse the fat `*T` type as its own thin pointer data field.
809                     // This provides information about, e.g., DST struct pointees
810                     // (which may have no non-DST form), and will work as long
811                     // as the `Abi` or `FieldsShape` is checked by users.
812                     if i == 0 {
813                         let nil = Ty::new_unit(tcx);
814                         let unit_ptr_ty = if this.ty.is_unsafe_ptr() {
815                             Ty::new_mut_ptr(tcx, nil)
816                         } else {
817                             Ty::new_mut_ref(tcx, tcx.lifetimes.re_static, nil)
818                         };
819 
820                         // NOTE(eddyb) using an empty `ParamEnv`, and `unwrap`-ing
821                         // the `Result` should always work because the type is
822                         // always either `*mut ()` or `&'static mut ()`.
823                         return TyMaybeWithLayout::TyAndLayout(TyAndLayout {
824                             ty: this.ty,
825                             ..tcx.layout_of(ty::ParamEnv::reveal_all().and(unit_ptr_ty)).unwrap()
826                         });
827                     }
828 
829                     let mk_dyn_vtable = || {
830                         Ty::new_imm_ref(
831                             tcx,
832                             tcx.lifetimes.re_static,
833                             Ty::new_array(tcx, tcx.types.usize, 3),
834                         )
835                         /* FIXME: use actual fn pointers
836                         Warning: naively computing the number of entries in the
837                         vtable by counting the methods on the trait + methods on
838                         all parent traits does not work, because some methods can
839                         be not object safe and thus excluded from the vtable.
840                         Increase this counter if you tried to implement this but
841                         failed to do it without duplicating a lot of code from
842                         other places in the compiler: 2
843                         Ty::new_tup(tcx,&[
844                             Ty::new_array(tcx,tcx.types.usize, 3),
845                             Ty::new_array(tcx,Option<fn()>),
846                         ])
847                         */
848                     };
849 
850                     let metadata = if let Some(metadata_def_id) = tcx.lang_items().metadata_type()
851                         // Projection eagerly bails out when the pointee references errors,
852                         // fall back to structurally deducing metadata.
853                         && !pointee.references_error()
854                     {
855                         let metadata = tcx.normalize_erasing_regions(
856                             cx.param_env(),
857                             Ty::new_projection(tcx,metadata_def_id, [pointee]),
858                         );
859 
860                         // Map `Metadata = DynMetadata<dyn Trait>` back to a vtable, since it
861                         // offers better information than `std::ptr::metadata::VTable`,
862                         // and we rely on this layout information to trigger a panic in
863                         // `std::mem::uninitialized::<&dyn Trait>()`, for example.
864                         if let ty::Adt(def, substs) = metadata.kind()
865                             && Some(def.did()) == tcx.lang_items().dyn_metadata()
866                             && substs.type_at(0).is_trait()
867                         {
868                             mk_dyn_vtable()
869                         } else {
870                             metadata
871                         }
872                     } else {
873                         match tcx.struct_tail_erasing_lifetimes(pointee, cx.param_env()).kind() {
874                             ty::Slice(_) | ty::Str => tcx.types.usize,
875                             ty::Dynamic(_, _, ty::Dyn) => mk_dyn_vtable(),
876                             _ => bug!("TyAndLayout::field({:?}): not applicable", this),
877                         }
878                     };
879 
880                     TyMaybeWithLayout::Ty(metadata)
881                 }
882 
883                 // Arrays and slices.
884                 ty::Array(element, _) | ty::Slice(element) => TyMaybeWithLayout::Ty(element),
885                 ty::Str => TyMaybeWithLayout::Ty(tcx.types.u8),
886 
887                 // Tuples, generators and closures.
888                 ty::Closure(_, ref substs) => field_ty_or_layout(
889                     TyAndLayout { ty: substs.as_closure().tupled_upvars_ty(), ..this },
890                     cx,
891                     i,
892                 ),
893 
894                 ty::Generator(def_id, ref substs, _) => match this.variants {
895                     Variants::Single { index } => TyMaybeWithLayout::Ty(
896                         substs
897                             .as_generator()
898                             .state_tys(def_id, tcx)
899                             .nth(index.as_usize())
900                             .unwrap()
901                             .nth(i)
902                             .unwrap(),
903                     ),
904                     Variants::Multiple { tag, tag_field, .. } => {
905                         if i == tag_field {
906                             return TyMaybeWithLayout::TyAndLayout(tag_layout(tag));
907                         }
908                         TyMaybeWithLayout::Ty(substs.as_generator().prefix_tys().nth(i).unwrap())
909                     }
910                 },
911 
912                 ty::Tuple(tys) => TyMaybeWithLayout::Ty(tys[i]),
913 
914                 // ADTs.
915                 ty::Adt(def, substs) => {
916                     match this.variants {
917                         Variants::Single { index } => {
918                             let field = &def.variant(index).fields[FieldIdx::from_usize(i)];
919                             TyMaybeWithLayout::Ty(field.ty(tcx, substs))
920                         }
921 
922                         // Discriminant field for enums (where applicable).
923                         Variants::Multiple { tag, .. } => {
924                             assert_eq!(i, 0);
925                             return TyMaybeWithLayout::TyAndLayout(tag_layout(tag));
926                         }
927                     }
928                 }
929 
930                 ty::Dynamic(_, _, ty::DynStar) => {
931                     if i == 0 {
932                         TyMaybeWithLayout::Ty(Ty::new_mut_ptr(tcx, tcx.types.unit))
933                     } else if i == 1 {
934                         // FIXME(dyn-star) same FIXME as above applies here too
935                         TyMaybeWithLayout::Ty(Ty::new_imm_ref(
936                             tcx,
937                             tcx.lifetimes.re_static,
938                             Ty::new_array(tcx, tcx.types.usize, 3),
939                         ))
940                     } else {
941                         bug!("no field {i} on dyn*")
942                     }
943                 }
944 
945                 ty::Alias(..)
946                 | ty::Bound(..)
947                 | ty::Placeholder(..)
948                 | ty::Param(_)
949                 | ty::Infer(_)
950                 | ty::Error(_) => bug!("TyAndLayout::field: unexpected type `{}`", this.ty),
951             }
952         }
953 
954         match field_ty_or_layout(this, cx, i) {
955             TyMaybeWithLayout::Ty(field_ty) => {
956                 cx.tcx().layout_of(cx.param_env().and(field_ty)).unwrap_or_else(|e| {
957                     bug!(
958                         "failed to get layout for `{field_ty}`: {e:?},\n\
959                          despite it being a field (#{i}) of an existing layout: {this:#?}",
960                     )
961                 })
962             }
963             TyMaybeWithLayout::TyAndLayout(field_layout) => field_layout,
964         }
965     }
966 
ty_and_layout_pointee_info_at( this: TyAndLayout<'tcx>, cx: &C, offset: Size, ) -> Option<PointeeInfo>967     fn ty_and_layout_pointee_info_at(
968         this: TyAndLayout<'tcx>,
969         cx: &C,
970         offset: Size,
971     ) -> Option<PointeeInfo> {
972         let tcx = cx.tcx();
973         let param_env = cx.param_env();
974 
975         let pointee_info = match *this.ty.kind() {
976             ty::RawPtr(mt) if offset.bytes() == 0 => {
977                 tcx.layout_of(param_env.and(mt.ty)).ok().map(|layout| PointeeInfo {
978                     size: layout.size,
979                     align: layout.align.abi,
980                     safe: None,
981                 })
982             }
983             ty::FnPtr(fn_sig) if offset.bytes() == 0 => {
984                 tcx.layout_of(param_env.and(Ty::new_fn_ptr(tcx, fn_sig))).ok().map(|layout| {
985                     PointeeInfo { size: layout.size, align: layout.align.abi, safe: None }
986                 })
987             }
988             ty::Ref(_, ty, mt) if offset.bytes() == 0 => {
989                 // Use conservative pointer kind if not optimizing. This saves us the
990                 // Freeze/Unpin queries, and can save time in the codegen backend (noalias
991                 // attributes in LLVM have compile-time cost even in unoptimized builds).
992                 let optimize = tcx.sess.opts.optimize != OptLevel::No;
993                 let kind = match mt {
994                     hir::Mutability::Not => PointerKind::SharedRef {
995                         frozen: optimize && ty.is_freeze(tcx, cx.param_env()),
996                     },
997                     hir::Mutability::Mut => PointerKind::MutableRef {
998                         unpin: optimize && ty.is_unpin(tcx, cx.param_env()),
999                     },
1000                 };
1001 
1002                 tcx.layout_of(param_env.and(ty)).ok().map(|layout| PointeeInfo {
1003                     size: layout.size,
1004                     align: layout.align.abi,
1005                     safe: Some(kind),
1006                 })
1007             }
1008 
1009             _ => {
1010                 let mut data_variant = match this.variants {
1011                     // Within the discriminant field, only the niche itself is
1012                     // always initialized, so we only check for a pointer at its
1013                     // offset.
1014                     //
1015                     // If the niche is a pointer, it's either valid (according
1016                     // to its type), or null (which the niche field's scalar
1017                     // validity range encodes). This allows using
1018                     // `dereferenceable_or_null` for e.g., `Option<&T>`, and
1019                     // this will continue to work as long as we don't start
1020                     // using more niches than just null (e.g., the first page of
1021                     // the address space, or unaligned pointers).
1022                     Variants::Multiple {
1023                         tag_encoding: TagEncoding::Niche { untagged_variant, .. },
1024                         tag_field,
1025                         ..
1026                     } if this.fields.offset(tag_field) == offset => {
1027                         Some(this.for_variant(cx, untagged_variant))
1028                     }
1029                     _ => Some(this),
1030                 };
1031 
1032                 if let Some(variant) = data_variant {
1033                     // We're not interested in any unions.
1034                     if let FieldsShape::Union(_) = variant.fields {
1035                         data_variant = None;
1036                     }
1037                 }
1038 
1039                 let mut result = None;
1040 
1041                 if let Some(variant) = data_variant {
1042                     // FIXME(erikdesjardins): handle non-default addrspace ptr sizes
1043                     // (requires passing in the expected address space from the caller)
1044                     let ptr_end = offset + Pointer(AddressSpace::DATA).size(cx);
1045                     for i in 0..variant.fields.count() {
1046                         let field_start = variant.fields.offset(i);
1047                         if field_start <= offset {
1048                             let field = variant.field(cx, i);
1049                             result = field.to_result().ok().and_then(|field| {
1050                                 if ptr_end <= field_start + field.size {
1051                                     // We found the right field, look inside it.
1052                                     let field_info =
1053                                         field.pointee_info_at(cx, offset - field_start);
1054                                     field_info
1055                                 } else {
1056                                     None
1057                                 }
1058                             });
1059                             if result.is_some() {
1060                                 break;
1061                             }
1062                         }
1063                     }
1064                 }
1065 
1066                 // FIXME(eddyb) This should be for `ptr::Unique<T>`, not `Box<T>`.
1067                 if let Some(ref mut pointee) = result {
1068                     if let ty::Adt(def, _) = this.ty.kind() {
1069                         if def.is_box() && offset.bytes() == 0 {
1070                             let optimize = tcx.sess.opts.optimize != OptLevel::No;
1071                             pointee.safe = Some(PointerKind::Box {
1072                                 unpin: optimize && this.ty.boxed_ty().is_unpin(tcx, cx.param_env()),
1073                             });
1074                         }
1075                     }
1076                 }
1077 
1078                 result
1079             }
1080         };
1081 
1082         debug!(
1083             "pointee_info_at (offset={:?}, type kind: {:?}) => {:?}",
1084             offset,
1085             this.ty.kind(),
1086             pointee_info
1087         );
1088 
1089         pointee_info
1090     }
1091 
is_adt(this: TyAndLayout<'tcx>) -> bool1092     fn is_adt(this: TyAndLayout<'tcx>) -> bool {
1093         matches!(this.ty.kind(), ty::Adt(..))
1094     }
1095 
is_never(this: TyAndLayout<'tcx>) -> bool1096     fn is_never(this: TyAndLayout<'tcx>) -> bool {
1097         this.ty.kind() == &ty::Never
1098     }
1099 
is_tuple(this: TyAndLayout<'tcx>) -> bool1100     fn is_tuple(this: TyAndLayout<'tcx>) -> bool {
1101         matches!(this.ty.kind(), ty::Tuple(..))
1102     }
1103 
is_unit(this: TyAndLayout<'tcx>) -> bool1104     fn is_unit(this: TyAndLayout<'tcx>) -> bool {
1105         matches!(this.ty.kind(), ty::Tuple(list) if list.len() == 0)
1106     }
1107 }
1108 
1109 /// Calculates whether a function's ABI can unwind or not.
1110 ///
1111 /// This takes two primary parameters:
1112 ///
1113 /// * `fn_def_id` - the `DefId` of the function. If this is provided then we can
1114 ///   determine more precisely if the function can unwind. If this is not provided
1115 ///   then we will only infer whether the function can unwind or not based on the
1116 ///   ABI of the function. For example, a function marked with `#[rustc_nounwind]`
1117 ///   is known to not unwind even if it's using Rust ABI.
1118 ///
1119 /// * `abi` - this is the ABI that the function is defined with. This is the
1120 ///   primary factor for determining whether a function can unwind or not.
1121 ///
1122 /// Note that in this case unwinding is not necessarily panicking in Rust. Rust
1123 /// panics are implemented with unwinds on most platform (when
1124 /// `-Cpanic=unwind`), but this also accounts for `-Cpanic=abort` build modes.
1125 /// Notably unwinding is disallowed for more non-Rust ABIs unless it's
1126 /// specifically in the name (e.g. `"C-unwind"`). Unwinding within each ABI is
1127 /// defined for each ABI individually, but it always corresponds to some form of
1128 /// stack-based unwinding (the exact mechanism of which varies
1129 /// platform-by-platform).
1130 ///
1131 /// Rust functions are classified whether or not they can unwind based on the
1132 /// active "panic strategy". In other words Rust functions are considered to
1133 /// unwind in `-Cpanic=unwind` mode and cannot unwind in `-Cpanic=abort` mode.
1134 /// Note that Rust supports intermingling panic=abort and panic=unwind code, but
1135 /// only if the final panic mode is panic=abort. In this scenario any code
1136 /// previously compiled assuming that a function can unwind is still correct, it
1137 /// just never happens to actually unwind at runtime.
1138 ///
1139 /// This function's answer to whether or not a function can unwind is quite
1140 /// impactful throughout the compiler. This affects things like:
1141 ///
1142 /// * Calling a function which can't unwind means codegen simply ignores any
1143 ///   associated unwinding cleanup.
1144 /// * Calling a function which can unwind from a function which can't unwind
1145 ///   causes the `abort_unwinding_calls` MIR pass to insert a landing pad that
1146 ///   aborts the process.
1147 /// * This affects whether functions have the LLVM `nounwind` attribute, which
1148 ///   affects various optimizations and codegen.
1149 #[inline]
1150 #[tracing::instrument(level = "debug", skip(tcx))]
fn_can_unwind(tcx: TyCtxt<'_>, fn_def_id: Option<DefId>, abi: SpecAbi) -> bool1151 pub fn fn_can_unwind(tcx: TyCtxt<'_>, fn_def_id: Option<DefId>, abi: SpecAbi) -> bool {
1152     if let Some(did) = fn_def_id {
1153         // Special attribute for functions which can't unwind.
1154         if tcx.codegen_fn_attrs(did).flags.contains(CodegenFnAttrFlags::NEVER_UNWIND) {
1155             return false;
1156         }
1157 
1158         // With `-C panic=abort`, all non-FFI functions are required to not unwind.
1159         //
1160         // Note that this is true regardless ABI specified on the function -- a `extern "C-unwind"`
1161         // function defined in Rust is also required to abort.
1162         if tcx.sess.panic_strategy() == PanicStrategy::Abort && !tcx.is_foreign_item(did) {
1163             return false;
1164         }
1165 
1166         // With -Z panic-in-drop=abort, drop_in_place never unwinds.
1167         //
1168         // This is not part of `codegen_fn_attrs` as it can differ between crates
1169         // and therefore cannot be computed in core.
1170         if tcx.sess.opts.unstable_opts.panic_in_drop == PanicStrategy::Abort {
1171             if Some(did) == tcx.lang_items().drop_in_place_fn() {
1172                 return false;
1173             }
1174         }
1175     }
1176 
1177     // Otherwise if this isn't special then unwinding is generally determined by
1178     // the ABI of the itself. ABIs like `C` have variants which also
1179     // specifically allow unwinding (`C-unwind`), but not all platform-specific
1180     // ABIs have such an option. Otherwise the only other thing here is Rust
1181     // itself, and those ABIs are determined by the panic strategy configured
1182     // for this compilation.
1183     //
1184     // Unfortunately at this time there's also another caveat. Rust [RFC
1185     // 2945][rfc] has been accepted and is in the process of being implemented
1186     // and stabilized. In this interim state we need to deal with historical
1187     // rustc behavior as well as plan for future rustc behavior.
1188     //
1189     // Historically functions declared with `extern "C"` were marked at the
1190     // codegen layer as `nounwind`. This happened regardless of `panic=unwind`
1191     // or not. This is UB for functions in `panic=unwind` mode that then
1192     // actually panic and unwind. Note that this behavior is true for both
1193     // externally declared functions as well as Rust-defined function.
1194     //
1195     // To fix this UB rustc would like to change in the future to catch unwinds
1196     // from function calls that may unwind within a Rust-defined `extern "C"`
1197     // function and forcibly abort the process, thereby respecting the
1198     // `nounwind` attribute emitted for `extern "C"`. This behavior change isn't
1199     // ready to roll out, so determining whether or not the `C` family of ABIs
1200     // unwinds is conditional not only on their definition but also whether the
1201     // `#![feature(c_unwind)]` feature gate is active.
1202     //
1203     // Note that this means that unlike historical compilers rustc now, by
1204     // default, unconditionally thinks that the `C` ABI may unwind. This will
1205     // prevent some optimization opportunities, however, so we try to scope this
1206     // change and only assume that `C` unwinds with `panic=unwind` (as opposed
1207     // to `panic=abort`).
1208     //
1209     // Eventually the check against `c_unwind` here will ideally get removed and
1210     // this'll be a little cleaner as it'll be a straightforward check of the
1211     // ABI.
1212     //
1213     // [rfc]: https://github.com/rust-lang/rfcs/blob/master/text/2945-c-unwind-abi.md
1214     use SpecAbi::*;
1215     match abi {
1216         C { unwind }
1217         | System { unwind }
1218         | Cdecl { unwind }
1219         | Stdcall { unwind }
1220         | Fastcall { unwind }
1221         | Vectorcall { unwind }
1222         | Thiscall { unwind }
1223         | Aapcs { unwind }
1224         | Win64 { unwind }
1225         | SysV64 { unwind } => {
1226             unwind
1227                 || (!tcx.features().c_unwind && tcx.sess.panic_strategy() == PanicStrategy::Unwind)
1228         }
1229         PtxKernel
1230         | Msp430Interrupt
1231         | X86Interrupt
1232         | AmdGpuKernel
1233         | EfiApi
1234         | AvrInterrupt
1235         | AvrNonBlockingInterrupt
1236         | CCmseNonSecureCall
1237         | Wasm
1238         | PlatformIntrinsic
1239         | Unadjusted => false,
1240         Rust | RustCall | RustCold | RustIntrinsic => {
1241             tcx.sess.panic_strategy() == PanicStrategy::Unwind
1242         }
1243     }
1244 }
1245 
1246 /// Error produced by attempting to compute or adjust a `FnAbi`.
1247 #[derive(Copy, Clone, Debug, HashStable)]
1248 pub enum FnAbiError<'tcx> {
1249     /// Error produced by a `layout_of` call, while computing `FnAbi` initially.
1250     Layout(LayoutError<'tcx>),
1251 
1252     /// Error produced by attempting to adjust a `FnAbi`, for a "foreign" ABI.
1253     AdjustForForeignAbi(call::AdjustForForeignAbiError),
1254 }
1255 
1256 impl<'a, 'b> IntoDiagnostic<'a, !> for FnAbiError<'b> {
into_diagnostic(self, handler: &'a Handler) -> DiagnosticBuilder<'a, !>1257     fn into_diagnostic(self, handler: &'a Handler) -> DiagnosticBuilder<'a, !> {
1258         match self {
1259             Self::Layout(e) => e.into_diagnostic().into_diagnostic(handler),
1260             Self::AdjustForForeignAbi(call::AdjustForForeignAbiError::Unsupported {
1261                 arch,
1262                 abi,
1263             }) => UnsupportedFnAbi { arch, abi: abi.name() }.into_diagnostic(handler),
1264         }
1265     }
1266 }
1267 
1268 // FIXME(eddyb) maybe use something like this for an unified `fn_abi_of`, not
1269 // just for error handling.
1270 #[derive(Debug)]
1271 pub enum FnAbiRequest<'tcx> {
1272     OfFnPtr { sig: ty::PolyFnSig<'tcx>, extra_args: &'tcx ty::List<Ty<'tcx>> },
1273     OfInstance { instance: ty::Instance<'tcx>, extra_args: &'tcx ty::List<Ty<'tcx>> },
1274 }
1275 
1276 /// Trait for contexts that want to be able to compute `FnAbi`s.
1277 /// This automatically gives access to `FnAbiOf`, through a blanket `impl`.
1278 pub trait FnAbiOfHelpers<'tcx>: LayoutOfHelpers<'tcx> {
1279     /// The `&FnAbi`-wrapping type (or `&FnAbi` itself), which will be
1280     /// returned from `fn_abi_of_*` (see also `handle_fn_abi_err`).
1281     type FnAbiOfResult: MaybeResult<&'tcx FnAbi<'tcx, Ty<'tcx>>>;
1282 
1283     /// Helper used for `fn_abi_of_*`, to adapt `tcx.fn_abi_of_*(...)` into a
1284     /// `Self::FnAbiOfResult` (which does not need to be a `Result<...>`).
1285     ///
1286     /// Most `impl`s, which propagate `FnAbiError`s, should simply return `err`,
1287     /// but this hook allows e.g. codegen to return only `&FnAbi` from its
1288     /// `cx.fn_abi_of_*(...)`, without any `Result<...>` around it to deal with
1289     /// (and any `FnAbiError`s are turned into fatal errors or ICEs).
handle_fn_abi_err( &self, err: FnAbiError<'tcx>, span: Span, fn_abi_request: FnAbiRequest<'tcx>, ) -> <Self::FnAbiOfResult as MaybeResult<&'tcx FnAbi<'tcx, Ty<'tcx>>>>::Error1290     fn handle_fn_abi_err(
1291         &self,
1292         err: FnAbiError<'tcx>,
1293         span: Span,
1294         fn_abi_request: FnAbiRequest<'tcx>,
1295     ) -> <Self::FnAbiOfResult as MaybeResult<&'tcx FnAbi<'tcx, Ty<'tcx>>>>::Error;
1296 }
1297 
1298 /// Blanket extension trait for contexts that can compute `FnAbi`s.
1299 pub trait FnAbiOf<'tcx>: FnAbiOfHelpers<'tcx> {
1300     /// Compute a `FnAbi` suitable for indirect calls, i.e. to `fn` pointers.
1301     ///
1302     /// NB: this doesn't handle virtual calls - those should use `fn_abi_of_instance`
1303     /// instead, where the instance is an `InstanceDef::Virtual`.
1304     #[inline]
fn_abi_of_fn_ptr( &self, sig: ty::PolyFnSig<'tcx>, extra_args: &'tcx ty::List<Ty<'tcx>>, ) -> Self::FnAbiOfResult1305     fn fn_abi_of_fn_ptr(
1306         &self,
1307         sig: ty::PolyFnSig<'tcx>,
1308         extra_args: &'tcx ty::List<Ty<'tcx>>,
1309     ) -> Self::FnAbiOfResult {
1310         // FIXME(eddyb) get a better `span` here.
1311         let span = self.layout_tcx_at_span();
1312         let tcx = self.tcx().at(span);
1313 
1314         MaybeResult::from(tcx.fn_abi_of_fn_ptr(self.param_env().and((sig, extra_args))).map_err(
1315             |err| self.handle_fn_abi_err(*err, span, FnAbiRequest::OfFnPtr { sig, extra_args }),
1316         ))
1317     }
1318 
1319     /// Compute a `FnAbi` suitable for declaring/defining an `fn` instance, and for
1320     /// direct calls to an `fn`.
1321     ///
1322     /// NB: that includes virtual calls, which are represented by "direct calls"
1323     /// to an `InstanceDef::Virtual` instance (of `<dyn Trait as Trait>::fn`).
1324     #[inline]
1325     #[tracing::instrument(level = "debug", skip(self))]
fn_abi_of_instance( &self, instance: ty::Instance<'tcx>, extra_args: &'tcx ty::List<Ty<'tcx>>, ) -> Self::FnAbiOfResult1326     fn fn_abi_of_instance(
1327         &self,
1328         instance: ty::Instance<'tcx>,
1329         extra_args: &'tcx ty::List<Ty<'tcx>>,
1330     ) -> Self::FnAbiOfResult {
1331         // FIXME(eddyb) get a better `span` here.
1332         let span = self.layout_tcx_at_span();
1333         let tcx = self.tcx().at(span);
1334 
1335         MaybeResult::from(
1336             tcx.fn_abi_of_instance(self.param_env().and((instance, extra_args))).map_err(|err| {
1337                 // HACK(eddyb) at least for definitions of/calls to `Instance`s,
1338                 // we can get some kind of span even if one wasn't provided.
1339                 // However, we don't do this early in order to avoid calling
1340                 // `def_span` unconditionally (which may have a perf penalty).
1341                 let span = if !span.is_dummy() { span } else { tcx.def_span(instance.def_id()) };
1342                 self.handle_fn_abi_err(
1343                     *err,
1344                     span,
1345                     FnAbiRequest::OfInstance { instance, extra_args },
1346                 )
1347             }),
1348         )
1349     }
1350 }
1351 
1352 impl<'tcx, C: FnAbiOfHelpers<'tcx>> FnAbiOf<'tcx> for C {}
1353