1//! Transforms instances and types for LLVM CFI and cross-language LLVM CFI support using Itanium
2//! C++ ABI mangling.
3//!
4//! For more information about LLVM CFI and cross-language LLVM CFI support for the Rust compiler,
5//! see design document in the tracking issue #89653.
67use std::iter;
89use rustc_hir::attrs::lang_items::LangItem;
10use rustc_hir::{selfas hir, find_attr};
11use rustc_middle::bug;
12use rustc_middle::ty::{
13self, AssocContainer, ExistentialPredicateStableCmpExtas _, Instance, IntTy, List, TraitRef,
14Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable, TypeVisitableExt, UintTy,
15Unnormalized,
16};
17use rustc_span::DUMMY_SP;
18use rustc_span::def_id::DefId;
19use rustc_trait_selection::traits;
20use tracing::{debug, instrument};
2122use crate::cfi::typeid::TypeIdOptions;
23use crate::cfi::typeid::itanium_cxx_abi::encode::EncodeTyOptions;
2425/// Options for transform_ty.
26pub(crate) type TransformTyOptions = TypeIdOptions;
2728pub(crate) struct TransformTy<'tcx> {
29 tcx: TyCtxt<'tcx>,
30 options: TransformTyOptions,
31 parents: Vec<Ty<'tcx>>,
32}
3334impl<'tcx> TransformTy<'tcx> {
35pub(crate) fn new(tcx: TyCtxt<'tcx>, options: TransformTyOptions) -> Self {
36TransformTy { tcx, options, parents: Vec::new() }
37 }
38}
3940/// Transforms a ty:Ty for being encoded and used in the substitution dictionary.
41///
42/// * Transforms all c_void types into unit types.
43/// * Generalizes pointers if TransformTyOptions::GENERALIZE_POINTERS option is set.
44/// * Normalizes integers if TransformTyOptions::NORMALIZE_INTEGERS option is set.
45/// * Generalizes any repr(transparent) user-defined type that is either a pointer or reference, and
46/// either references itself or any other type that contains or references itself, to avoid a
47/// reference cycle.
48/// * Transforms repr(transparent) types without non-ZST field into ().
49///
50impl<'tcx> TypeFolder<TyCtxt<'tcx>> for TransformTy<'tcx> {
51// Transforms a ty:Ty for being encoded and used in the substitution dictionary.
52fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
53match t.kind() {
54 ty::Closure(..)
55 | ty::Coroutine(..)
56 | ty::CoroutineClosure(..)
57 | ty::CoroutineWitness(..)
58 | ty::Dynamic(..)
59 | ty::Float(..)
60 | ty::FnDef(..)
61 | ty::Foreign(..)
62 | ty::Never63 | ty::Pat(..)
64 | ty::Slice(..)
65 | ty::Str66 | ty::Tuple(..)
67 | ty::UnsafeBinder(_) => t.super_fold_with(self),
6869// Don't transform the type of the array length and keep it as `usize`.
70 // This is required for `try_to_target_usize` to work correctly.
71&ty::Array(inner, len) => {
72let inner = self.fold_ty(inner);
73Ty::new_array_with_const_len(self.tcx, inner, len)
74 }
7576 ty::Bool => {
77if self.options.contains(EncodeTyOptions::NORMALIZE_INTEGERS) {
78// Note: on all platforms that Rust's currently supports, its size and alignment
79 // are 1, and its ABI class is INTEGER - see Rust Layout and ABIs.
80 //
81 // (See https://rust-lang.github.io/unsafe-code-guidelines/layout/scalars.html#bool.)
82 //
83 // Clang represents bool as an 8-bit unsigned integer.
84self.tcx.types.u8
85 } else {
86t87 }
88 }
8990 ty::Char => {
91if self.options.contains(EncodeTyOptions::NORMALIZE_INTEGERS) {
92// Since #118032, char is guaranteed to have the same size, alignment, and
93 // function call ABI as u32 on all platforms.
94self.tcx.types.u32
95 } else {
96t97 }
98 }
99100 ty::Int(..) | ty::Uint(..) => {
101if self.options.contains(EncodeTyOptions::NORMALIZE_INTEGERS) {
102// Note: C99 7.18.2.4 requires uintptr_t and intptr_t to be at least 16-bit
103 // wide. All platforms we currently support have a C platform, and as a
104 // consequence, isize/usize are at least 16-bit wide for all of them.
105 //
106 // (See https://rust-lang.github.io/unsafe-code-guidelines/layout/scalars.html#isize-and-usize.)
107match t.kind() {
108 ty::Int(IntTy::Isize) => match self.tcx.sess.target.pointer_width {
10916 => self.tcx.types.i16,
11032 => self.tcx.types.i32,
11164 => self.tcx.types.i64,
112128 => self.tcx.types.i128,
113_ => ::rustc_middle::util::bug::bug_fmt(format_args!("fold_ty: unexpected pointer width `{0}`",
self.tcx.sess.target.pointer_width))bug!(
114"fold_ty: unexpected pointer width `{}`",
115self.tcx.sess.target.pointer_width
116 ),
117 },
118 ty::Uint(UintTy::Usize) => match self.tcx.sess.target.pointer_width {
11916 => self.tcx.types.u16,
12032 => self.tcx.types.u32,
12164 => self.tcx.types.u64,
122128 => self.tcx.types.u128,
123_ => ::rustc_middle::util::bug::bug_fmt(format_args!("fold_ty: unexpected pointer width `{0}`",
self.tcx.sess.target.pointer_width))bug!(
124"fold_ty: unexpected pointer width `{}`",
125self.tcx.sess.target.pointer_width
126 ),
127 },
128_ => t,
129 }
130 } else {
131t132 }
133 }
134135 ty::Adt(..) if t.is_c_void(self.tcx) => self.tcx.types.unit,
136137 ty::Adt(adt_def, args) => {
138if adt_def.repr().transparent() && adt_def.is_struct() && !self.parents.contains(&t)
139 {
140// Don't transform repr(transparent) types with an user-defined CFI encoding to
141 // preserve the user-defined CFI encoding.
142if {
{
'done:
{
for i in
::rustc_attr_ir::HasAttrs::get_attrs(adt_def.did(),
&self.tcx) {
#[allow(unused_imports)]
use ::rustc_attr_ir::AttributeKind::*;
let i: &::rustc_attr_ir::Attribute = i;
match i {
::rustc_attr_ir::Attribute::Parsed(CfiEncoding { .. }) => {
break 'done Some(());
}
::rustc_attr_ir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}
}.is_some()find_attr!(self.tcx, adt_def.did(), CfiEncoding { .. }) {
143return t;
144 }
145let variant = adt_def.non_enum_variant();
146let typing_env = ty::TypingEnv::post_analysis(self.tcx, variant.def_id);
147let field = variant.fields.iter().find(|field| {
148let ty = self.tcx.type_of(field.did).instantiate_identity().skip_norm_wip();
149let is_zst = self150 .tcx
151 .layout_of(typing_env.as_query_input(ty))
152 .is_ok_and(|layout| layout.is_zst());
153 !is_zst154 });
155if let Some(field) = field {
156let ty0 = self.tcx.normalize_erasing_regions(
157 ty::TypingEnv::fully_monomorphized(),
158field.ty(self.tcx, args),
159 );
160// Generalize any repr(transparent) user-defined type that is either a
161 // pointer or reference, and either references itself or any other type that
162 // contains or references itself, to avoid a reference cycle.
163164 // If the self reference is not through a pointer, for example, due
165 // to using `PhantomData`, need to skip normalizing it if we hit it again.
166self.parents.push(t);
167let ty = if ty0.is_any_ptr() && ty0.contains(t) {
168let options = self.options;
169self.options |= TransformTyOptions::GENERALIZE_POINTERS;
170let ty = ty0.fold_with(self);
171self.options = options;
172ty173 } else {
174ty0.fold_with(self)
175 };
176self.parents.pop();
177ty178 } else {
179// Transform repr(transparent) types without non-ZST field into ()
180self.tcx.types.unit
181 }
182 } else {
183t.super_fold_with(self)
184 }
185 }
186187 ty::Ref(..) => {
188if self.options.contains(TransformTyOptions::GENERALIZE_POINTERS) {
189if t.is_mutable_ptr() {
190Ty::new_mut_ref(self.tcx, self.tcx.lifetimes.re_static, self.tcx.types.unit)
191 } else {
192Ty::new_imm_ref(self.tcx, self.tcx.lifetimes.re_static, self.tcx.types.unit)
193 }
194 } else {
195t.super_fold_with(self)
196 }
197 }
198199 ty::RawPtr(..) => {
200if self.options.contains(TransformTyOptions::GENERALIZE_POINTERS) {
201if t.is_mutable_ptr() {
202Ty::new_mut_ptr(self.tcx, self.tcx.types.unit)
203 } else {
204Ty::new_imm_ptr(self.tcx, self.tcx.types.unit)
205 }
206 } else {
207t.super_fold_with(self)
208 }
209 }
210211 ty::FnPtr(..) => {
212if self.options.contains(TransformTyOptions::GENERALIZE_POINTERS) {
213Ty::new_imm_ptr(self.tcx, self.tcx.types.unit)
214 } else {
215t.super_fold_with(self)
216 }
217 }
218219 ty::Alias(..) => self.fold_ty(self.tcx.normalize_erasing_regions(
220 ty::TypingEnv::fully_monomorphized(),
221Unnormalized::new_wip(t),
222 )),
223224 ty::Bound(..) | ty::Error(..) | ty::Infer(..) | ty::Param(..) | ty::Placeholder(..) => {
225::rustc_middle::util::bug::bug_fmt(format_args!("fold_ty: unexpected `{0:?}`",
t.kind()));bug!("fold_ty: unexpected `{:?}`", t.kind());
226 }
227 }
228 }
229230fn cx(&self) -> TyCtxt<'tcx> {
231self.tcx
232 }
233}
234235x;#[instrument(skip(tcx), ret)]236fn trait_object_ty<'tcx>(tcx: TyCtxt<'tcx>, poly_trait_ref: ty::PolyTraitRef<'tcx>) -> Ty<'tcx> {
237assert!(!poly_trait_ref.has_non_region_param());
238let principal_pred = poly_trait_ref.map_bound(|trait_ref| {
239 ty::ExistentialPredicate::Trait(ty::ExistentialTraitRef::erase_self_ty(tcx, trait_ref))
240 });
241let mut assoc_preds: Vec<_> = traits::supertraits(tcx, poly_trait_ref)
242 .flat_map(|super_poly_trait_ref| {
243 tcx.associated_items(super_poly_trait_ref.def_id())
244 .in_definition_order()
245 .filter(|item| item.can_have_equality_constraint(tcx))
246 .filter(|item| !tcx.generics_require_sized_self(item.def_id))
247 .map(move |assoc_item| {
248 super_poly_trait_ref.map_bound(|super_trait_ref| {
249let projection_term = ty::AliasTerm::new_from_def_id(
250 tcx,
251 assoc_item.def_id,
252 super_trait_ref.args,
253 );
254let term = tcx.normalize_erasing_regions(
255 ty::TypingEnv::fully_monomorphized(),
256 Unnormalized::new_wip(projection_term.to_term(tcx, ty::IsRigid::No)),
257 );
258debug!(
259"Projection {:?} -> {term}",
260 projection_term.to_term(tcx, ty::IsRigid::No)
261 );
262 ty::ExistentialPredicate::Projection(
263 ty::ExistentialProjection::erase_self_ty(
264 tcx,
265 ty::ProjectionPredicate { projection_term, term },
266 ),
267 )
268 })
269 })
270 })
271 .collect();
272 assoc_preds.sort_by(|a, b| a.skip_binder().stable_cmp(tcx, &b.skip_binder()));
273let preds = tcx.mk_poly_existential_predicates_from_iter(
274 iter::once(principal_pred).chain(assoc_preds.into_iter()),
275 );
276 Ty::new_dynamic(tcx, preds, tcx.lifetimes.re_erased)
277}
278279/// Transforms an instance for LLVM CFI and cross-language LLVM CFI support using Itanium C++ ABI
280/// mangling.
281///
282/// typeid_for_instance is called at two locations, initially when declaring/defining functions and
283/// methods, and later during code generation at call sites, after type erasure might have occurred.
284///
285/// In the first call (i.e., when declaring/defining functions and methods), it encodes type ids for
286/// an FnAbi or Instance, and these type ids are attached to functions and methods. (These type ids
287/// are used later by the LowerTypeTests LLVM pass to aggregate functions in groups derived from
288/// these type ids.)
289///
290/// In the second call (i.e., during code generation at call sites), it encodes a type id for an
291/// FnAbi or Instance, after type erasure might have occurred, and this type id is used for testing
292/// if a function is member of the group derived from this type id. Therefore, in the first call to
293/// typeid_for_fnabi (when type ids are attached to functions and methods), it can only include at
294/// most as much information that would be available in the second call (i.e., during code
295/// generation at call sites); otherwise, the type ids would not match.
296///
297/// For this, it:
298///
299/// * Adjust the type ids of DropGlues (see below).
300/// * Adjusts the type ids of VTableShims to the type id expected in the call sites for the
301/// entry in the vtable (i.e., by using the signature of the closure passed as an argument to the
302/// shim, or by just removing self).
303/// * Performs type erasure for calls on trait objects by transforming self into a trait object of
304/// the trait that defines the method.
305/// * Performs type erasure for closures call methods by transforming self into a trait object of
306/// the Fn trait that defines the method (for being attached as a secondary type id).
307///
308#[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::TRACE <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("transform_instance",
"rustc_sanitizers::cfi::typeid::itanium_cxx_abi::transform",
::tracing::Level::TRACE,
::tracing_core::__macro_support::Option::Some("compiler/rustc_sanitizers/src/cfi/typeid/itanium_cxx_abi/transform.rs"),
::tracing_core::__macro_support::Option::Some(308u32),
::tracing_core::__macro_support::Option::Some("rustc_sanitizers::cfi::typeid::itanium_cxx_abi::transform"),
::tracing_core::field::FieldSet::new(&[{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("instance")
}> =
::tracing::__macro_support::FieldName::new("instance");
NAME.as_str()
},
{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("options")
}> =
::tracing::__macro_support::FieldName::new("options");
NAME.as_str()
}], ::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::TRACE <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::TRACE <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
meta.fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&instance)
as &dyn ::tracing::field::Value)),
(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&options)
as &dyn ::tracing::field::Value))])
})
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return: Instance<'tcx> = loop {};
return __tracing_attr_fake_return;
}
{
if (#[allow(non_exhaustive_omitted_patterns)] match instance.def {
ty::InstanceKind::Virtual(..) => true,
_ => false,
} &&
tcx.is_lang_item(instance.def_id(), LangItem::DropGlue)) ||
#[allow(non_exhaustive_omitted_patterns)] match instance.def
{
ty::InstanceKind::Shim(ty::ShimKind::DropGlue(..)) => true,
_ => false,
} {
let def_id =
tcx.lang_items().drop_trait().unwrap_or_else(||
::rustc_middle::util::bug::bug_fmt(format_args!("typeid_for_instance: couldn\'t get drop_trait lang item")));
let predicate =
ty::ExistentialPredicate::Trait(ty::ExistentialTraitRef::new_from_args(tcx,
def_id, ty::List::empty()));
let predicates =
tcx.mk_poly_existential_predicates(&[ty::Binder::dummy(predicate)]);
let self_ty =
Ty::new_dynamic(tcx, predicates, tcx.lifetimes.re_erased);
instance.args = tcx.mk_args_trait(self_ty, List::empty());
} else if let ty::InstanceKind::Virtual(def_id, _) = instance.def
{
let upcast_ty =
match tcx.trait_of_assoc(def_id) {
Some(trait_id) =>
trait_object_ty(tcx,
ty::Binder::dummy(ty::TraitRef::from_assoc(tcx, trait_id,
instance.args))),
None => instance.args.type_at(0),
};
let ty::Dynamic(preds, lifetime) =
upcast_ty.kind() else {
::rustc_middle::util::bug::bug_fmt(format_args!("Tried to remove autotraits from non-dynamic type {0}",
upcast_ty));
};
let self_ty =
if preds.principal().is_some() {
let filtered_preds =
tcx.mk_poly_existential_predicates_from_iter(preds.into_iter().filter(|pred|
{
!#[allow(non_exhaustive_omitted_patterns)] match pred.skip_binder()
{
ty::ExistentialPredicate::AutoTrait(..) => true,
_ => false,
}
}));
Ty::new_dynamic(tcx, filtered_preds, *lifetime)
} else { tcx.types.unit };
instance.args =
tcx.mk_args_trait(self_ty,
instance.args.into_iter().skip(1));
} else if let ty::InstanceKind::Shim(ty::ShimKind::VTable(def_id))
= instance.def &&
let Some(trait_id) = tcx.trait_of_assoc(def_id) {
let trait_ref =
ty::TraitRef::new_from_args(tcx, trait_id, instance.args);
let invoke_ty =
trait_object_ty(tcx, ty::Binder::dummy(trait_ref));
instance.args =
tcx.mk_args_trait(invoke_ty,
trait_ref.args.into_iter().skip(1));
}
if !options.contains(TransformTyOptions::USE_CONCRETE_SELF) {
if let Some((trait_ref, method_id, ancestor)) =
implemented_method(tcx, instance) {
let trait_ref =
tcx.instantiate_and_normalize_erasing_regions(instance.args,
ty::TypingEnv::fully_monomorphized(), trait_ref);
let invoke_ty =
trait_object_ty(tcx, ty::Binder::dummy(trait_ref));
instance.def = ty::InstanceKind::Virtual(method_id, 0);
let abstract_trait_args =
tcx.mk_args_trait(invoke_ty,
trait_ref.args.into_iter().skip(1));
instance.args =
instance.args.rebase_onto(tcx, ancestor,
abstract_trait_args);
} else if tcx.is_closure_like(instance.def_id()) {
let closure_ty =
instance.ty(tcx, ty::TypingEnv::fully_monomorphized());
let (trait_id, inputs) =
match closure_ty.kind() {
ty::Closure(..) => {
let closure_args = instance.args.as_closure();
let trait_id =
tcx.fn_trait_kind_to_def_id(closure_args.kind()).unwrap();
let tuple_args =
tcx.instantiate_bound_regions_with_erased(closure_args.sig()).inputs()[0];
(trait_id, Some(tuple_args))
}
ty::Coroutine(..) =>
match tcx.coroutine_kind(instance.def_id()).unwrap() {
hir::CoroutineKind::Coroutine(..) =>
(tcx.require_lang_item(LangItem::Coroutine, DUMMY_SP),
Some(instance.args.as_coroutine().resume_ty())),
hir::CoroutineKind::Desugared(desugaring, _) => {
let lang_item =
match desugaring {
hir::CoroutineDesugaring::Async => LangItem::Future,
hir::CoroutineDesugaring::AsyncGen =>
LangItem::AsyncIterator,
hir::CoroutineDesugaring::Gen => LangItem::Iterator,
};
(tcx.require_lang_item(lang_item, DUMMY_SP), None)
}
},
ty::CoroutineClosure(..) =>
(tcx.require_lang_item(LangItem::FnOnce, DUMMY_SP),
Some(tcx.instantiate_bound_regions_with_erased(instance.args.as_coroutine_closure().coroutine_closure_sig()).tupled_inputs_ty)),
x =>
::rustc_middle::util::bug::bug_fmt(format_args!("Unexpected type kind for closure-like: {0:?}",
x)),
};
let concrete_args =
tcx.mk_args_trait(closure_ty, inputs.map(Into::into));
let trait_ref =
ty::TraitRef::new_from_args(tcx, trait_id, concrete_args);
let invoke_ty =
trait_object_ty(tcx, ty::Binder::dummy(trait_ref));
let abstract_args =
tcx.mk_args_trait(invoke_ty,
trait_ref.args.into_iter().skip(1));
let call =
tcx.associated_items(trait_id).in_definition_order().find(|it|
it.is_fn()).expect("No call-family function on closure-like Fn trait?").def_id;
instance.def = ty::InstanceKind::Virtual(call, 0);
instance.args = abstract_args;
}
}
instance
}
}
}#[instrument(level = "trace", skip(tcx))]309pub(crate) fn transform_instance<'tcx>(
310 tcx: TyCtxt<'tcx>,
311mut instance: Instance<'tcx>,
312 options: TransformTyOptions,
313) -> Instance<'tcx> {
314// FIXME: account for async-drop-glue
315if (matches!(instance.def, ty::InstanceKind::Virtual(..))
316 && tcx.is_lang_item(instance.def_id(), LangItem::DropGlue))
317 || matches!(instance.def, ty::InstanceKind::Shim(ty::ShimKind::DropGlue(..)))
318 {
319// Adjust the type ids of DropGlues
320 //
321 // DropGlues may have indirect calls to one or more given types drop function. Rust allows
322 // for types to be erased to any trait object and retains the drop function for the original
323 // type, which means at the indirect call sites in DropGlues, when typeid_for_fnabi is
324 // called a second time, it only has information after type erasure and it could be a call
325 // on any arbitrary trait object. Normalize them to a synthesized Drop trait object, both on
326 // declaration/definition, and during code generation at call sites so they have the same
327 // type id and match.
328 //
329 // FIXME(rcvalle): This allows a drop call on any trait object to call the drop function of
330 // any other type.
331 //
332let def_id = tcx
333 .lang_items()
334 .drop_trait()
335 .unwrap_or_else(|| bug!("typeid_for_instance: couldn't get drop_trait lang item"));
336let predicate = ty::ExistentialPredicate::Trait(ty::ExistentialTraitRef::new_from_args(
337 tcx,
338 def_id,
339 ty::List::empty(),
340 ));
341let predicates = tcx.mk_poly_existential_predicates(&[ty::Binder::dummy(predicate)]);
342let self_ty = Ty::new_dynamic(tcx, predicates, tcx.lifetimes.re_erased);
343 instance.args = tcx.mk_args_trait(self_ty, List::empty());
344 } else if let ty::InstanceKind::Virtual(def_id, _) = instance.def {
345// Transform self into a trait object of the trait that defines the method for virtual
346 // functions to match the type erasure done below.
347let upcast_ty = match tcx.trait_of_assoc(def_id) {
348Some(trait_id) => trait_object_ty(
349 tcx,
350 ty::Binder::dummy(ty::TraitRef::from_assoc(tcx, trait_id, instance.args)),
351 ),
352// drop_in_place won't have a defining trait, skip the upcast
353None => instance.args.type_at(0),
354 };
355let ty::Dynamic(preds, lifetime) = upcast_ty.kind() else {
356bug!("Tried to remove autotraits from non-dynamic type {upcast_ty}");
357 };
358let self_ty = if preds.principal().is_some() {
359let filtered_preds =
360 tcx.mk_poly_existential_predicates_from_iter(preds.into_iter().filter(|pred| {
361 !matches!(pred.skip_binder(), ty::ExistentialPredicate::AutoTrait(..))
362 }));
363 Ty::new_dynamic(tcx, filtered_preds, *lifetime)
364 } else {
365// If there's no principal type, re-encode it as a unit, since we don't know anything
366 // about it. This technically discards the knowledge that it was a type that was made
367 // into a trait object at some point, but that's not a lot.
368tcx.types.unit
369 };
370 instance.args = tcx.mk_args_trait(self_ty, instance.args.into_iter().skip(1));
371 } else if let ty::InstanceKind::Shim(ty::ShimKind::VTable(def_id)) = instance.def
372 && let Some(trait_id) = tcx.trait_of_assoc(def_id)
373 {
374// Adjust the type ids of VTableShims to the type id expected in the call sites for the
375 // entry in the vtable (i.e., by using the signature of the closure passed as an argument
376 // to the shim, or by just removing self).
377let trait_ref = ty::TraitRef::new_from_args(tcx, trait_id, instance.args);
378let invoke_ty = trait_object_ty(tcx, ty::Binder::dummy(trait_ref));
379 instance.args = tcx.mk_args_trait(invoke_ty, trait_ref.args.into_iter().skip(1));
380 }
381382if !options.contains(TransformTyOptions::USE_CONCRETE_SELF) {
383// Perform type erasure for calls on trait objects by transforming self into a trait object
384 // of the trait that defines the method.
385if let Some((trait_ref, method_id, ancestor)) = implemented_method(tcx, instance) {
386// Trait methods will have a Self polymorphic parameter, where the concreteized
387 // implementation will not. We need to walk back to the more general trait method
388let trait_ref = tcx.instantiate_and_normalize_erasing_regions(
389 instance.args,
390 ty::TypingEnv::fully_monomorphized(),
391 trait_ref,
392 );
393let invoke_ty = trait_object_ty(tcx, ty::Binder::dummy(trait_ref));
394395// At the call site, any call to this concrete function through a vtable will be
396 // `Virtual(method_id, idx)` with appropriate arguments for the method. Since we have the
397 // original method id, and we've recovered the trait arguments, we can make the callee
398 // instance we're computing the alias set for match the caller instance.
399 //
400 // Right now, our code ignores the vtable index everywhere, so we use 0 as a placeholder.
401 // If we ever *do* start encoding the vtable index, we will need to generate an alias set
402 // based on which vtables we are putting this method into, as there will be more than one
403 // index value when supertraits are involved.
404instance.def = ty::InstanceKind::Virtual(method_id, 0);
405let abstract_trait_args =
406 tcx.mk_args_trait(invoke_ty, trait_ref.args.into_iter().skip(1));
407 instance.args = instance.args.rebase_onto(tcx, ancestor, abstract_trait_args);
408 } else if tcx.is_closure_like(instance.def_id()) {
409// We're either a closure or a coroutine. Our goal is to find the trait we're defined on,
410 // instantiate it, and take the type of its only method as our own.
411let closure_ty = instance.ty(tcx, ty::TypingEnv::fully_monomorphized());
412let (trait_id, inputs) = match closure_ty.kind() {
413 ty::Closure(..) => {
414let closure_args = instance.args.as_closure();
415let trait_id = tcx.fn_trait_kind_to_def_id(closure_args.kind()).unwrap();
416let tuple_args =
417 tcx.instantiate_bound_regions_with_erased(closure_args.sig()).inputs()[0];
418 (trait_id, Some(tuple_args))
419 }
420 ty::Coroutine(..) => match tcx.coroutine_kind(instance.def_id()).unwrap() {
421 hir::CoroutineKind::Coroutine(..) => (
422 tcx.require_lang_item(LangItem::Coroutine, DUMMY_SP),
423Some(instance.args.as_coroutine().resume_ty()),
424 ),
425 hir::CoroutineKind::Desugared(desugaring, _) => {
426let lang_item = match desugaring {
427 hir::CoroutineDesugaring::Async => LangItem::Future,
428 hir::CoroutineDesugaring::AsyncGen => LangItem::AsyncIterator,
429 hir::CoroutineDesugaring::Gen => LangItem::Iterator,
430 };
431 (tcx.require_lang_item(lang_item, DUMMY_SP), None)
432 }
433 },
434 ty::CoroutineClosure(..) => (
435 tcx.require_lang_item(LangItem::FnOnce, DUMMY_SP),
436Some(
437 tcx.instantiate_bound_regions_with_erased(
438 instance.args.as_coroutine_closure().coroutine_closure_sig(),
439 )
440 .tupled_inputs_ty,
441 ),
442 ),
443 x => bug!("Unexpected type kind for closure-like: {x:?}"),
444 };
445let concrete_args = tcx.mk_args_trait(closure_ty, inputs.map(Into::into));
446let trait_ref = ty::TraitRef::new_from_args(tcx, trait_id, concrete_args);
447let invoke_ty = trait_object_ty(tcx, ty::Binder::dummy(trait_ref));
448let abstract_args = tcx.mk_args_trait(invoke_ty, trait_ref.args.into_iter().skip(1));
449// There should be exactly one method on this trait, and it should be the one we're
450 // defining.
451let call = tcx
452 .associated_items(trait_id)
453 .in_definition_order()
454 .find(|it| it.is_fn())
455 .expect("No call-family function on closure-like Fn trait?")
456 .def_id;
457458 instance.def = ty::InstanceKind::Virtual(call, 0);
459 instance.args = abstract_args;
460 }
461 }
462463 instance
464}
465466fn default_or_shim<'tcx>(tcx: TyCtxt<'tcx>, instance: Instance<'tcx>) -> Option<DefId> {
467match instance.def {
468 ty::InstanceKind::Item(def_id) | ty::InstanceKind::Shim(ty::ShimKind::FnPtr(def_id, _)) => {
469tcx.opt_associated_item(def_id).map(|item| item.def_id)
470 }
471_ => None,
472 }
473}
474475/// Determines if an instance represents a trait method implementation and returns the necessary
476/// information for type erasure.
477///
478/// This function handles two main cases:
479///
480/// * **Implementation in an `impl` block**: When the instance represents a concrete implementation
481/// of a trait method in an `impl` block, it extracts the trait reference, method ID, and trait
482/// ID from the implementation. The method ID is obtained from the `trait_item_def_id` field of
483/// the associated item, which points to the original trait method definition.
484///
485/// * **Provided method in a `trait` block or synthetic `shim`**: When the instance represents a
486/// default implementation provided in the trait definition itself or a synthetic shim, it uses
487/// the instance's own `def_id` as the method ID and determines the trait ID from the associated
488/// item.
489///
490fn implemented_method<'tcx>(
491 tcx: TyCtxt<'tcx>,
492 instance: Instance<'tcx>,
493) -> Option<(ty::EarlyBinder<'tcx, TraitRef<'tcx>>, DefId, DefId)> {
494let trait_ref;
495let method_id;
496let trait_id;
497let trait_method;
498let assoc = tcx.opt_associated_item(instance.def_id())?;
499let ancestor = if let AssocContainer::TraitImpl(Ok(trait_method_id)) = assoc.container {
500let impl_id = tcx.parent(instance.def_id());
501trait_ref = tcx.impl_trait_ref(impl_id);
502method_id = trait_method_id;
503trait_method = tcx.associated_item(method_id);
504trait_id = trait_ref.skip_binder().def_id;
505impl_id506 } else if let AssocContainer::Trait = assoc.container
507 && let Some(trait_method_def_id) = default_or_shim(tcx, instance)
508 {
509// Provided method in a `trait` block or a synthetic `shim`
510trait_method = assoc;
511method_id = trait_method_def_id;
512trait_id = tcx.parent(method_id);
513trait_ref = ty::EarlyBinder::bind(tcx, TraitRef::from_assoc(tcx, trait_id, instance.args));
514trait_id515 } else {
516return None;
517 };
518let vtable_possible = traits::is_vtable_safe_method(tcx, trait_id, trait_method)
519 && tcx.is_dyn_compatible(trait_id);
520vtable_possible.then_some((trait_ref, method_id, ancestor))
521}