1
Fork 0

refactor(rustc_middle): Substs -> GenericArg

This commit is contained in:
Mahdi Dibaiee 2023-07-11 22:35:29 +01:00
parent df5c2cf9bc
commit e55583c4b8
466 changed files with 4574 additions and 4604 deletions

View file

@ -51,12 +51,12 @@ fn fn_sig_for_fn_abi<'tcx>(
//
// We normalize the `fn_sig` again after substituting at a later point.
let mut sig = match *ty.kind() {
ty::FnDef(def_id, substs) => tcx
ty::FnDef(def_id, args) => tcx
.fn_sig(def_id)
.map_bound(|fn_sig| {
tcx.normalize_erasing_regions(tcx.param_env(def_id), fn_sig)
})
.subst(tcx, substs),
.instantiate(tcx, args),
_ => unreachable!(),
};
@ -71,8 +71,8 @@ fn fn_sig_for_fn_abi<'tcx>(
}
sig
}
ty::Closure(def_id, substs) => {
let sig = substs.as_closure().sig();
ty::Closure(def_id, args) => {
let sig = args.as_closure().sig();
let bound_vars = tcx.mk_bound_variable_kinds_from_iter(
sig.bound_vars().iter().chain(iter::once(ty::BoundVariableKind::Region(ty::BrEnv))),
@ -82,7 +82,7 @@ fn fn_sig_for_fn_abi<'tcx>(
kind: ty::BoundRegionKind::BrEnv,
};
let env_region = ty::Region::new_late_bound(tcx, ty::INNERMOST, br);
let env_ty = tcx.closure_env_ty(def_id, substs, env_region).unwrap();
let env_ty = tcx.closure_env_ty(def_id, args, env_region).unwrap();
let sig = sig.skip_binder();
ty::Binder::bind_with_vars(
@ -96,8 +96,8 @@ fn fn_sig_for_fn_abi<'tcx>(
bound_vars,
)
}
ty::Generator(did, substs, _) => {
let sig = substs.as_generator().poly_sig();
ty::Generator(did, args, _) => {
let sig = args.as_generator().poly_sig();
let bound_vars = tcx.mk_bound_variable_kinds_from_iter(
sig.bound_vars().iter().chain(iter::once(ty::BoundVariableKind::Region(ty::BrEnv))),
@ -111,8 +111,8 @@ fn fn_sig_for_fn_abi<'tcx>(
let pin_did = tcx.require_lang_item(LangItem::Pin, None);
let pin_adt_ref = tcx.adt_def(pin_did);
let pin_substs = tcx.mk_substs(&[env_ty.into()]);
let env_ty = Ty::new_adt(tcx, pin_adt_ref, pin_substs);
let pin_args = tcx.mk_args(&[env_ty.into()]);
let env_ty = Ty::new_adt(tcx, pin_adt_ref, pin_args);
let sig = sig.skip_binder();
// The `FnSig` and the `ret_ty` here is for a generators main
@ -123,8 +123,8 @@ fn fn_sig_for_fn_abi<'tcx>(
// The signature should be `Future::poll(_, &mut Context<'_>) -> Poll<Output>`
let poll_did = tcx.require_lang_item(LangItem::Poll, None);
let poll_adt_ref = tcx.adt_def(poll_did);
let poll_substs = tcx.mk_substs(&[sig.return_ty.into()]);
let ret_ty = Ty::new_adt(tcx, poll_adt_ref, poll_substs);
let poll_args = tcx.mk_args(&[sig.return_ty.into()]);
let ret_ty = Ty::new_adt(tcx, poll_adt_ref, poll_args);
// We have to replace the `ResumeTy` that is used for type and borrow checking
// with `&mut Context<'_>` which is used in codegen.
@ -145,8 +145,8 @@ fn fn_sig_for_fn_abi<'tcx>(
// The signature should be `Generator::resume(_, Resume) -> GeneratorState<Yield, Return>`
let state_did = tcx.require_lang_item(LangItem::GeneratorState, None);
let state_adt_ref = tcx.adt_def(state_did);
let state_substs = tcx.mk_substs(&[sig.yield_ty.into(), sig.return_ty.into()]);
let ret_ty = Ty::new_adt(tcx, state_adt_ref, state_substs);
let state_args = tcx.mk_args(&[sig.yield_ty.into(), sig.return_ty.into()]);
let ret_ty = Ty::new_adt(tcx, state_adt_ref, state_args);
(sig.resume_ty, ret_ty)
};

View file

@ -5,7 +5,7 @@ use rustc_hir::def_id::{DefId, DefIdMap, LocalDefId};
use rustc_hir::definitions::DefPathData;
use rustc_hir::intravisit::{self, Visitor};
use rustc_middle::query::Providers;
use rustc_middle::ty::{self, ImplTraitInTraitData, InternalSubsts, Ty, TyCtxt};
use rustc_middle::ty::{self, GenericArgs, ImplTraitInTraitData, Ty, TyCtxt};
use rustc_span::symbol::kw;
pub fn provide(providers: &mut Providers) {
@ -290,7 +290,7 @@ fn associated_type_for_impl_trait_in_trait(
trait_assoc_ty.type_of(ty::EarlyBinder::bind(Ty::new_opaque(
tcx,
opaque_ty_def_id.to_def_id(),
InternalSubsts::identity_for_item(tcx, opaque_ty_def_id),
GenericArgs::identity_for_item(tcx, opaque_ty_def_id),
)));
trait_assoc_ty.is_type_alias_impl_trait(false);

View file

@ -42,7 +42,7 @@ pub(crate) fn destructure_const<'tcx>(
(field_consts, None)
}
ty::Adt(def, _) if def.variants().is_empty() => bug!("unreachable"),
ty::Adt(def, substs) => {
ty::Adt(def, args) => {
let (variant_idx, branches) = if def.is_enum() {
let (head, rest) = branches.split_first().unwrap();
(VariantIdx::from_u32(head.unwrap_leaf().try_to_u32().unwrap()), rest)
@ -53,7 +53,7 @@ pub(crate) fn destructure_const<'tcx>(
let mut field_consts = Vec::with_capacity(fields.len());
for (field, field_valtree) in iter::zip(fields, branches) {
let field_ty = field.ty(tcx, substs);
let field_ty = field.ty(tcx, args);
let field_const = ty::Const::new_value(tcx, *field_valtree, field_ty);
field_consts.push(field_const);
}
@ -133,8 +133,8 @@ fn recurse_build<'tcx>(
let val = ty::ValTree::zst();
ty::Const::new_value(tcx, val, node.ty)
}
&ExprKind::NamedConst { def_id, substs, user_ty: _ } => {
let uneval = ty::UnevaluatedConst::new(def_id, substs);
&ExprKind::NamedConst { def_id, args, user_ty: _ } => {
let uneval = ty::UnevaluatedConst::new(def_id, args);
ty::Const::new_unevaluated(tcx, uneval, node.ty)
}
ExprKind::ConstParam { param, .. } => ty::Const::new_param(tcx, *param, node.ty),
@ -306,8 +306,9 @@ impl<'a, 'tcx> IsThirPolymorphic<'a, 'tcx> {
}
match expr.kind {
thir::ExprKind::NamedConst { substs, .. }
| thir::ExprKind::ConstBlock { substs, .. } => substs.has_non_region_param(),
thir::ExprKind::NamedConst { args, .. } | thir::ExprKind::ConstBlock { args, .. } => {
args.has_non_region_param()
}
thir::ExprKind::ConstParam { .. } => true,
thir::ExprKind::Repeat { value, count } => {
self.visit_expr(&self.thir()[value]);

View file

@ -13,7 +13,7 @@ pub fn provide(providers: &mut Providers) {
fn assumed_wf_types<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> &'tcx [(Ty<'tcx>, Span)] {
match tcx.def_kind(def_id) {
DefKind::Fn => {
let sig = tcx.fn_sig(def_id).subst_identity();
let sig = tcx.fn_sig(def_id).instantiate_identity();
let liberated_sig = tcx.liberate_late_bound_regions(def_id.to_def_id(), sig);
tcx.arena.alloc_from_iter(itertools::zip_eq(
liberated_sig.inputs_and_output,
@ -21,7 +21,7 @@ fn assumed_wf_types<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> &'tcx [(Ty<'
))
}
DefKind::AssocFn => {
let sig = tcx.fn_sig(def_id).subst_identity();
let sig = tcx.fn_sig(def_id).instantiate_identity();
let liberated_sig = tcx.liberate_late_bound_regions(def_id.to_def_id(), sig);
let mut assumed_wf_types: Vec<_> =
tcx.assumed_wf_types(tcx.local_parent(def_id)).into();
@ -35,8 +35,8 @@ fn assumed_wf_types<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> &'tcx [(Ty<'
// Trait arguments and the self type for trait impls or only the self type for
// inherent impls.
let tys = match tcx.impl_trait_ref(def_id) {
Some(trait_ref) => trait_ref.skip_binder().substs.types().collect(),
None => vec![tcx.type_of(def_id).subst_identity()],
Some(trait_ref) => trait_ref.skip_binder().args.types().collect(),
None => vec![tcx.type_of(def_id).instantiate_identity()],
};
let mut impl_spans = impl_spans(tcx, def_id);

View file

@ -3,19 +3,19 @@ use rustc_hir::def_id::DefId;
use rustc_infer::infer::TyCtxtInferExt;
use rustc_middle::query::Providers;
use rustc_middle::traits::CodegenObligationError;
use rustc_middle::ty::subst::SubstsRef;
use rustc_middle::ty::GenericArgsRef;
use rustc_middle::ty::{self, Instance, TyCtxt, TypeVisitableExt};
use rustc_span::sym;
use rustc_trait_selection::traits;
use traits::{translate_substs, Reveal};
use traits::{translate_args, Reveal};
use crate::errors::UnexpectedFnPtrAssociatedItem;
fn resolve_instance<'tcx>(
tcx: TyCtxt<'tcx>,
key: ty::ParamEnvAnd<'tcx, (DefId, SubstsRef<'tcx>)>,
key: ty::ParamEnvAnd<'tcx, (DefId, GenericArgsRef<'tcx>)>,
) -> Result<Option<Instance<'tcx>>, ErrorGuaranteed> {
let (param_env, (def, substs)) = key.into_parts();
let (param_env, (def, args)) = key.into_parts();
let result = if let Some(trait_def_id) = tcx.trait_of_item(def) {
debug!(" => associated item, attempting to find impl in param_env {:#?}", param_env);
@ -24,19 +24,19 @@ fn resolve_instance<'tcx>(
def,
param_env,
trait_def_id,
tcx.normalize_erasing_regions(param_env, substs),
tcx.normalize_erasing_regions(param_env, args),
)
} else {
let ty = tcx.type_of(def);
let item_type = tcx.subst_and_normalize_erasing_regions(substs, param_env, ty);
let item_type = tcx.subst_and_normalize_erasing_regions(args, param_env, ty);
let def = match *item_type.kind() {
ty::FnDef(def_id, ..) if tcx.is_intrinsic(def_id) => {
debug!(" => intrinsic");
ty::InstanceDef::Intrinsic(def)
}
ty::FnDef(def_id, substs) if Some(def_id) == tcx.lang_items().drop_in_place_fn() => {
let ty = substs.type_at(0);
ty::FnDef(def_id, args) if Some(def_id) == tcx.lang_items().drop_in_place_fn() => {
let ty = args.type_at(0);
if ty.needs_drop(tcx, param_env) {
debug!(" => nontrivial drop glue");
@ -63,7 +63,7 @@ fn resolve_instance<'tcx>(
ty::InstanceDef::Item(def)
}
};
Ok(Some(Instance { def, substs }))
Ok(Some(Instance { def, args }))
};
debug!("inner_resolve_instance: result={:?}", result);
result
@ -74,11 +74,11 @@ fn resolve_associated_item<'tcx>(
trait_item_id: DefId,
param_env: ty::ParamEnv<'tcx>,
trait_id: DefId,
rcvr_substs: SubstsRef<'tcx>,
rcvr_args: GenericArgsRef<'tcx>,
) -> Result<Option<Instance<'tcx>>, ErrorGuaranteed> {
debug!(?trait_item_id, ?param_env, ?trait_id, ?rcvr_substs, "resolve_associated_item");
debug!(?trait_item_id, ?param_env, ?trait_id, ?rcvr_args, "resolve_associated_item");
let trait_ref = ty::TraitRef::from_method(tcx, trait_id, rcvr_substs);
let trait_ref = ty::TraitRef::from_method(tcx, trait_id, rcvr_args);
let vtbl = match tcx.codegen_select_candidate((param_env, trait_ref)) {
Ok(vtbl) => vtbl,
@ -102,9 +102,9 @@ fn resolve_associated_item<'tcx>(
traits::ImplSource::UserDefined(impl_data) => {
debug!(
"resolving ImplSource::UserDefined: {:?}, {:?}, {:?}, {:?}",
param_env, trait_item_id, rcvr_substs, impl_data
param_env, trait_item_id, rcvr_args, impl_data
);
assert!(!rcvr_substs.has_infer());
assert!(!rcvr_args.has_infer());
assert!(!trait_ref.has_infer());
let trait_def_id = tcx.trait_id_of_impl(impl_data.impl_def_id).unwrap();
@ -117,15 +117,15 @@ fn resolve_associated_item<'tcx>(
});
let infcx = tcx.infer_ctxt().build();
let param_env = param_env.with_reveal_all_normalized(tcx);
let substs = rcvr_substs.rebase_onto(tcx, trait_def_id, impl_data.substs);
let substs = translate_substs(
let args = rcvr_args.rebase_onto(tcx, trait_def_id, impl_data.args);
let args = translate_args(
&infcx,
param_env,
impl_data.impl_def_id,
substs,
args,
leaf_def.defining_node,
);
let substs = infcx.tcx.erase_regions(substs);
let args = infcx.tcx.erase_regions(args);
// Since this is a trait item, we need to see if the item is either a trait default item
// or a specialization because we can't resolve those unless we can `Reveal::All`.
@ -159,7 +159,7 @@ fn resolve_associated_item<'tcx>(
return Err(guard);
}
let substs = tcx.erase_regions(substs);
let args = tcx.erase_regions(args);
// Check if we just resolved an associated `const` declaration from
// a `trait` to an associated `const` definition in an `impl`, where
@ -175,14 +175,11 @@ fn resolve_associated_item<'tcx>(
))?;
}
Some(ty::Instance::new(leaf_def.item.def_id, substs))
Some(ty::Instance::new(leaf_def.item.def_id, args))
}
traits::ImplSource::Object(ref data) => {
traits::get_vtable_index_of_object_method(tcx, data, trait_item_id).map(|index| {
Instance {
def: ty::InstanceDef::Virtual(trait_item_id, index),
substs: rcvr_substs,
}
Instance { def: ty::InstanceDef::Virtual(trait_item_id, index), args: rcvr_args }
})
}
traits::ImplSource::Builtin(..) => {
@ -205,14 +202,14 @@ fn resolve_associated_item<'tcx>(
Some(Instance {
def: ty::InstanceDef::CloneShim(trait_item_id, self_ty),
substs: rcvr_substs,
args: rcvr_args,
})
} else {
assert_eq!(name, sym::clone_from);
// Use the default `fn clone_from` from `trait Clone`.
let substs = tcx.erase_regions(rcvr_substs);
Some(ty::Instance::new(trait_item_id, substs))
let args = tcx.erase_regions(rcvr_args);
Some(ty::Instance::new(trait_item_id, args))
}
} else if Some(trait_ref.def_id) == lang_items.fn_ptr_trait() {
if lang_items.fn_ptr_addr() == Some(trait_item_id) {
@ -222,7 +219,7 @@ fn resolve_associated_item<'tcx>(
}
Some(Instance {
def: ty::InstanceDef::FnPtrAddrShim(trait_item_id, self_ty),
substs: rcvr_substs,
args: rcvr_args,
})
} else {
tcx.sess.emit_fatal(UnexpectedFnPtrAssociatedItem {
@ -230,22 +227,20 @@ fn resolve_associated_item<'tcx>(
})
}
} else if Some(trait_ref.def_id) == lang_items.future_trait() {
let ty::Generator(generator_def_id, substs, _) = *rcvr_substs.type_at(0).kind()
else {
let ty::Generator(generator_def_id, args, _) = *rcvr_args.type_at(0).kind() else {
bug!()
};
if Some(trait_item_id) == tcx.lang_items().future_poll_fn() {
// `Future::poll` is generated by the compiler.
Some(Instance { def: ty::InstanceDef::Item(generator_def_id), substs: substs })
Some(Instance { def: ty::InstanceDef::Item(generator_def_id), args: args })
} else {
// All other methods are default methods of the `Future` trait.
// (this assumes that `ImplSource::Builtin` is only used for methods on `Future`)
debug_assert!(tcx.defaultness(trait_item_id).has_value());
Some(Instance::new(trait_item_id, rcvr_substs))
Some(Instance::new(trait_item_id, rcvr_args))
}
} else if Some(trait_ref.def_id) == lang_items.gen_trait() {
let ty::Generator(generator_def_id, substs, _) = *rcvr_substs.type_at(0).kind()
else {
let ty::Generator(generator_def_id, args, _) = *rcvr_args.type_at(0).kind() else {
bug!()
};
if cfg!(debug_assertions) && tcx.item_name(trait_item_id) != sym::resume {
@ -259,7 +254,7 @@ fn resolve_associated_item<'tcx>(
tcx.item_name(trait_item_id)
)
}
Some(Instance { def: ty::InstanceDef::Item(generator_def_id), substs })
Some(Instance { def: ty::InstanceDef::Item(generator_def_id), args })
} else if tcx.fn_trait_kind_from_def_id(trait_ref.def_id).is_some() {
// FIXME: This doesn't check for malformed libcore that defines, e.g.,
// `trait Fn { fn call_once(&self) { .. } }`. This is mostly for extension
@ -277,14 +272,14 @@ fn resolve_associated_item<'tcx>(
tcx.item_name(trait_item_id)
)
}
match *rcvr_substs.type_at(0).kind() {
ty::Closure(closure_def_id, substs) => {
match *rcvr_args.type_at(0).kind() {
ty::Closure(closure_def_id, args) => {
let trait_closure_kind = tcx.fn_trait_kind_from_def_id(trait_id).unwrap();
Instance::resolve_closure(tcx, closure_def_id, substs, trait_closure_kind)
Instance::resolve_closure(tcx, closure_def_id, args, trait_closure_kind)
}
ty::FnDef(..) | ty::FnPtr(..) => Some(Instance {
def: ty::InstanceDef::FnPtrShim(trait_item_id, rcvr_substs.type_at(0)),
substs: rcvr_substs,
def: ty::InstanceDef::FnPtrShim(trait_item_id, rcvr_args.type_at(0)),
args: rcvr_args,
}),
_ => bug!(
"no built-in definition for `{trait_ref}::{}` for non-fn type",

View file

@ -8,7 +8,7 @@ use rustc_middle::ty::layout::{
IntegerExt, LayoutCx, LayoutError, LayoutOf, TyAndLayout, MAX_SIMD_LANES,
};
use rustc_middle::ty::{
self, subst::SubstsRef, AdtDef, EarlyBinder, ReprOptions, Ty, TyCtxt, TypeVisitableExt,
self, AdtDef, EarlyBinder, GenericArgsRef, ReprOptions, Ty, TyCtxt, TypeVisitableExt,
};
use rustc_session::{DataTypeKind, FieldInfo, FieldKind, SizeKind, VariantInfo};
use rustc_span::symbol::Symbol;
@ -299,10 +299,10 @@ fn layout_of_uncached<'tcx>(
tcx.mk_layout(unit)
}
ty::Generator(def_id, substs, _) => generator_layout(cx, ty, def_id, substs)?,
ty::Generator(def_id, args, _) => generator_layout(cx, ty, def_id, args)?,
ty::Closure(_, ref substs) => {
let tys = substs.as_closure().upvar_tys();
ty::Closure(_, ref args) => {
let tys = args.as_closure().upvar_tys();
univariant(
&tys.map(|ty| Ok(cx.layout_of(ty)?.layout)).try_collect::<IndexVec<_, _>>()?,
&ReprOptions::default(),
@ -322,7 +322,7 @@ fn layout_of_uncached<'tcx>(
}
// SIMD vector types.
ty::Adt(def, substs) if def.repr().simd() => {
ty::Adt(def, args) if def.repr().simd() => {
if !def.is_struct() {
// Should have yielded E0517 by now.
tcx.sess.delay_span_bug(
@ -349,12 +349,12 @@ fn layout_of_uncached<'tcx>(
}
// Type of the first ADT field:
let f0_ty = fields[FieldIdx::from_u32(0)].ty(tcx, substs);
let f0_ty = fields[FieldIdx::from_u32(0)].ty(tcx, args);
// Heterogeneous SIMD vectors are not supported:
// (should be caught by typeck)
for fi in fields {
if fi.ty(tcx, substs) != f0_ty {
if fi.ty(tcx, args) != f0_ty {
tcx.sess.delay_span_bug(
DUMMY_SP,
"#[repr(simd)] was applied to an ADT with heterogeneous field type",
@ -435,7 +435,7 @@ fn layout_of_uncached<'tcx>(
}
// ADTs.
ty::Adt(def, substs) => {
ty::Adt(def, args) => {
// Cache the field layouts.
let variants = def
.variants()
@ -443,7 +443,7 @@ fn layout_of_uncached<'tcx>(
.map(|v| {
v.fields
.iter()
.map(|field| Ok(cx.layout_of(field.ty(tcx, substs))?.layout))
.map(|field| Ok(cx.layout_of(field.ty(tcx, args))?.layout))
.try_collect::<IndexVec<_, _>>()
})
.try_collect::<IndexVec<VariantIdx, _>>()?;
@ -482,7 +482,7 @@ fn layout_of_uncached<'tcx>(
let maybe_unsized = def.is_struct()
&& def.non_enum_variant().tail_opt().is_some_and(|last_field| {
let param_env = tcx.param_env(def.did());
!tcx.type_of(last_field.did).subst_identity().is_sized(tcx, param_env)
!tcx.type_of(last_field.did).instantiate_identity().is_sized(tcx, param_env)
});
let Some(layout) = cx.layout_of_struct_or_enum(
@ -502,7 +502,7 @@ fn layout_of_uncached<'tcx>(
// If the struct tail is sized and can be unsized, check that unsizing doesn't move the fields around.
if cfg!(debug_assertions)
&& maybe_unsized
&& def.non_enum_variant().tail().ty(tcx, substs).is_sized(tcx, cx.param_env)
&& def.non_enum_variant().tail().ty(tcx, args).is_sized(tcx, cx.param_env)
{
let mut variants = variants;
let tail_replacement = cx.layout_of(Ty::new_slice(tcx, tcx.types.u8)).unwrap();
@ -696,11 +696,11 @@ fn generator_layout<'tcx>(
cx: &LayoutCx<'tcx, TyCtxt<'tcx>>,
ty: Ty<'tcx>,
def_id: hir::def_id::DefId,
substs: SubstsRef<'tcx>,
args: GenericArgsRef<'tcx>,
) -> Result<Layout<'tcx>, &'tcx LayoutError<'tcx>> {
use SavedLocalEligibility::*;
let tcx = cx.tcx;
let subst_field = |ty: Ty<'tcx>| EarlyBinder::bind(ty).subst(tcx, substs);
let subst_field = |ty: Ty<'tcx>| EarlyBinder::bind(ty).instantiate(tcx, args);
let Some(info) = tcx.generator_layout(def_id) else {
return Err(error(cx, LayoutError::Unknown(ty)));
@ -710,7 +710,7 @@ fn generator_layout<'tcx>(
// Build a prefix layout, including "promoting" all ineligible
// locals as part of the prefix. We compute the layout of all of
// these fields at once to get optimal packing.
let tag_index = substs.as_generator().prefix_tys().count();
let tag_index = args.as_generator().prefix_tys().count();
// `info.variant_fields` already accounts for the reserved variants, so no need to add them.
let max_discr = (info.variant_fields.len() - 1) as u128;
@ -726,7 +726,7 @@ fn generator_layout<'tcx>(
.map(|local| subst_field(info.field_tys[local].ty))
.map(|ty| Ty::new_maybe_uninit(tcx, ty))
.map(|ty| Ok(cx.layout_of(ty)?.layout));
let prefix_layouts = substs
let prefix_layouts = args
.as_generator()
.prefix_tys()
.map(|ty| Ok(cx.layout_of(ty)?.layout))
@ -934,11 +934,11 @@ fn record_layout_for_printing_outlined<'tcx>(
record(adt_kind.into(), adt_packed, opt_discr_size, variant_infos);
}
ty::Generator(def_id, substs, _) => {
ty::Generator(def_id, args, _) => {
debug!("print-type-size t: `{:?}` record generator", layout.ty);
// Generators always have a begin/poisoned/end state with additional suspend points
let (variant_infos, opt_discr_size) =
variant_info_for_generator(cx, layout, def_id, substs);
variant_info_for_generator(cx, layout, def_id, args);
record(DataTypeKind::Generator, false, opt_discr_size, variant_infos);
}
@ -1028,7 +1028,7 @@ fn variant_info_for_generator<'tcx>(
cx: &LayoutCx<'tcx, TyCtxt<'tcx>>,
layout: TyAndLayout<'tcx>,
def_id: DefId,
substs: ty::SubstsRef<'tcx>,
args: ty::GenericArgsRef<'tcx>,
) -> (Vec<VariantInfo>, Option<Size>) {
let Variants::Multiple { tag, ref tag_encoding, tag_field, .. } = layout.variants else {
return (vec![], None);
@ -1038,7 +1038,7 @@ fn variant_info_for_generator<'tcx>(
let upvar_names = cx.tcx.closure_saved_names_of_captured_variables(def_id);
let mut upvars_size = Size::ZERO;
let upvar_fields: Vec<_> = substs
let upvar_fields: Vec<_> = args
.as_generator()
.upvar_tys()
.zip(upvar_names)
@ -1113,7 +1113,7 @@ fn variant_info_for_generator<'tcx>(
}
VariantInfo {
name: Some(Symbol::intern(&ty::GeneratorSubsts::variant_name(variant_idx))),
name: Some(Symbol::intern(&ty::GeneratorArgs::variant_name(variant_idx))),
kind: SizeKind::Exact,
size: variant_size.bytes(),
align: variant_layout.align.abi.bytes(),

View file

@ -3,8 +3,8 @@
use rustc_data_structures::fx::FxHashSet;
use rustc_hir::def_id::DefId;
use rustc_middle::query::Providers;
use rustc_middle::ty::subst::SubstsRef;
use rustc_middle::ty::util::{needs_drop_components, AlwaysRequiresDrop};
use rustc_middle::ty::GenericArgsRef;
use rustc_middle::ty::{self, EarlyBinder, Ty, TyCtxt};
use rustc_session::Limit;
use rustc_span::{sym, DUMMY_SP};
@ -80,7 +80,7 @@ impl<'tcx, F> NeedsDropTypes<'tcx, F> {
impl<'tcx, F, I> Iterator for NeedsDropTypes<'tcx, F>
where
F: Fn(ty::AdtDef<'tcx>, SubstsRef<'tcx>) -> NeedsDropResult<I>,
F: Fn(ty::AdtDef<'tcx>, GenericArgsRef<'tcx>) -> NeedsDropResult<I>,
I: Iterator<Item = Ty<'tcx>>,
{
type Item = NeedsDropResult<Ty<'tcx>>;
@ -119,15 +119,15 @@ where
_ if component.is_copy_modulo_regions(tcx, self.param_env) => (),
ty::Closure(_, substs) => {
queue_type(self, substs.as_closure().tupled_upvars_ty());
ty::Closure(_, args) => {
queue_type(self, args.as_closure().tupled_upvars_ty());
}
ty::Generator(def_id, substs, _) => {
let substs = substs.as_generator();
queue_type(self, substs.tupled_upvars_ty());
ty::Generator(def_id, args, _) => {
let args = args.as_generator();
queue_type(self, args.tupled_upvars_ty());
let witness = substs.witness();
let witness = args.witness();
let interior_tys = match witness.kind() {
&ty::GeneratorWitness(tys) => tcx.erase_late_bound_regions(tys),
_ => {
@ -147,8 +147,8 @@ where
// Check for a `Drop` impl and whether this is a union or
// `ManuallyDrop`. If it's a struct or enum without a `Drop`
// impl then check whether the field types need `Drop`.
ty::Adt(adt_def, substs) => {
let tys = match (self.adt_components)(adt_def, substs) {
ty::Adt(adt_def, args) => {
let tys = match (self.adt_components)(adt_def, args) {
Err(e) => return Some(Err(e)),
Ok(tys) => tys,
};
@ -210,7 +210,7 @@ fn drop_tys_helper<'tcx>(
match subty.kind() {
ty::Adt(adt_id, subst) => {
for subty in tcx.adt_drop_tys(adt_id.did())? {
vec.push(EarlyBinder::bind(subty).subst(tcx, subst));
vec.push(EarlyBinder::bind(subty).instantiate(tcx, subst));
}
}
_ => vec.push(subty),
@ -219,7 +219,7 @@ fn drop_tys_helper<'tcx>(
})
}
let adt_components = move |adt_def: ty::AdtDef<'tcx>, substs: SubstsRef<'tcx>| {
let adt_components = move |adt_def: ty::AdtDef<'tcx>, args: GenericArgsRef<'tcx>| {
if adt_def.is_manually_drop() {
debug!("drop_tys_helper: `{:?}` is manually drop", adt_def);
Ok(Vec::new())
@ -235,7 +235,7 @@ fn drop_tys_helper<'tcx>(
// Since the destructor is insignificant, we just want to make sure all of
// the passed in type parameters are also insignificant.
// Eg: Vec<T> dtor is insignificant when T=i32 but significant when T=Mutex.
Ok(substs.types().collect())
Ok(args.types().collect())
}
}
} else if adt_def.is_union() {
@ -243,8 +243,8 @@ fn drop_tys_helper<'tcx>(
Ok(Vec::new())
} else {
let field_tys = adt_def.all_fields().map(|field| {
let r = tcx.type_of(field.did).subst(tcx, substs);
debug!("drop_tys_helper: Subst into {:?} with {:?} getting {:?}", field, substs, r);
let r = tcx.type_of(field.did).instantiate(tcx, args);
debug!("drop_tys_helper: Subst into {:?} with {:?} getting {:?}", field, args, r);
r
});
if only_significant {
@ -295,10 +295,10 @@ fn adt_drop_tys<'tcx>(
// significant.
let adt_has_dtor =
|adt_def: ty::AdtDef<'tcx>| adt_def.destructor(tcx).map(|_| DtorType::Significant);
// `tcx.type_of(def_id)` identical to `tcx.make_adt(def, identity_substs)`
// `tcx.type_of(def_id)` identical to `tcx.make_adt(def, identity_args)`
drop_tys_helper(
tcx,
tcx.type_of(def_id).subst_identity(),
tcx.type_of(def_id).instantiate_identity(),
tcx.param_env(def_id),
adt_has_dtor,
false,
@ -307,7 +307,7 @@ fn adt_drop_tys<'tcx>(
.map(|components| tcx.mk_type_list(&components))
}
// If `def_id` refers to a generic ADT, the queries above and below act as if they had been handed
// a `tcx.make_ty(def, identity_substs)` and as such it is legal to substitute the generic parameters
// a `tcx.make_ty(def, identity_args)` and as such it is legal to substitute the generic parameters
// of the ADT into the outputted `ty`s.
fn adt_significant_drop_tys(
tcx: TyCtxt<'_>,
@ -315,7 +315,7 @@ fn adt_significant_drop_tys(
) -> Result<&ty::List<Ty<'_>>, AlwaysRequiresDrop> {
drop_tys_helper(
tcx,
tcx.type_of(def_id).subst_identity(), // identical to `tcx.make_adt(def, identity_substs)`
tcx.type_of(def_id).instantiate_identity(), // identical to `tcx.make_adt(def, identity_args)`
tcx.param_env(def_id),
adt_consider_insignificant_dtor(tcx),
true,

View file

@ -7,7 +7,7 @@ use rustc_middle::ty::util::{CheckRegions, NotUniqueParam};
use rustc_middle::ty::{self, Ty, TyCtxt};
use rustc_middle::ty::{TypeSuperVisitable, TypeVisitable, TypeVisitor};
use rustc_span::Span;
use rustc_trait_selection::traits::check_substs_compatible;
use rustc_trait_selection::traits::check_args_compatible;
use std::ops::ControlFlow;
use crate::errors::{DuplicateArg, NotParam};
@ -45,7 +45,7 @@ impl<'tcx> OpaqueTypeCollector<'tcx> {
fn parent_trait_ref(&self) -> Option<ty::TraitRef<'tcx>> {
let parent = self.parent()?;
if matches!(self.tcx.def_kind(parent), DefKind::Impl { .. }) {
Some(self.tcx.impl_trait_ref(parent)?.subst_identity())
Some(self.tcx.impl_trait_ref(parent)?.instantiate_identity())
} else {
None
}
@ -159,19 +159,19 @@ impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for OpaqueTypeCollector<'tcx> {
self.opaques.push(alias_ty.def_id.expect_local());
match self.tcx.uses_unique_generic_params(alias_ty.substs, CheckRegions::Bound) {
match self.tcx.uses_unique_generic_params(alias_ty.args, CheckRegions::Bound) {
Ok(()) => {
// FIXME: implement higher kinded lifetime bounds on nested opaque types. They are not
// supported at all, so this is sound to do, but once we want to support them, you'll
// start seeing the error below.
// Collect opaque types nested within the associated type bounds of this opaque type.
// We use identity substs here, because we already know that the opaque type uses
// We use identity args here, because we already know that the opaque type uses
// only generic parameters, and thus substituting would not give us more information.
for (pred, span) in self
.tcx
.explicit_item_bounds(alias_ty.def_id)
.subst_identity_iter_copied()
.instantiate_identity_iter_copied()
{
trace!(?pred);
self.visit_spanned(span, pred);
@ -196,7 +196,7 @@ impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for OpaqueTypeCollector<'tcx> {
ty::Alias(ty::Weak, alias_ty) if alias_ty.def_id.is_local() => {
self.tcx
.type_of(alias_ty.def_id)
.subst(self.tcx, alias_ty.substs)
.instantiate(self.tcx, alias_ty.args)
.visit_with(self)?;
}
ty::Alias(ty::Projection, alias_ty) => {
@ -222,22 +222,22 @@ impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for OpaqueTypeCollector<'tcx> {
continue;
}
let impl_substs = alias_ty.substs.rebase_onto(
let impl_args = alias_ty.args.rebase_onto(
self.tcx,
parent_trait_ref.def_id,
ty::InternalSubsts::identity_for_item(self.tcx, parent),
ty::GenericArgs::identity_for_item(self.tcx, parent),
);
if check_substs_compatible(self.tcx, assoc, impl_substs) {
if check_args_compatible(self.tcx, assoc, impl_args) {
return self
.tcx
.type_of(assoc.def_id)
.subst(self.tcx, impl_substs)
.instantiate(self.tcx, impl_args)
.visit_with(self);
} else {
self.tcx.sess.delay_span_bug(
self.tcx.def_span(assoc.def_id),
"item had incorrect substs",
"item had incorrect args",
);
}
}
@ -250,15 +250,15 @@ impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for OpaqueTypeCollector<'tcx> {
}
for variant in def.variants().iter() {
for field in variant.fields.iter() {
// Don't use the `ty::Adt` substs, we either
// * found the opaque in the substs
// Don't use the `ty::Adt` args, we either
// * found the opaque in the args
// * will find the opaque in the unsubstituted fields
// The only other situation that can occur is that after substituting,
// some projection resolves to an opaque that we would have otherwise
// not found. While we could substitute and walk those, that would mean we
// would have to walk all substitutions of an Adt, which can quickly
// degenerate into looking at an exponential number of types.
let ty = self.tcx.type_of(field.did).subst_identity();
let ty = self.tcx.type_of(field.did).instantiate_identity();
self.visit_spanned(self.tcx.def_span(field.did), ty);
}
}
@ -276,7 +276,7 @@ fn opaque_types_defined_by<'tcx>(tcx: TyCtxt<'tcx>, item: LocalDefId) -> &'tcx [
match kind {
// Walk over the signature of the function-like to find the opaques.
DefKind::AssocFn | DefKind::Fn => {
let ty_sig = tcx.fn_sig(item).subst_identity();
let ty_sig = tcx.fn_sig(item).instantiate_identity();
let hir_sig = tcx.hir().get_by_def_id(item).fn_sig().unwrap();
// Walk over the inputs and outputs manually in order to get good spans for them.
collector.visit_spanned(hir_sig.decl.output.span(), ty_sig.output());
@ -291,15 +291,15 @@ fn opaque_types_defined_by<'tcx>(tcx: TyCtxt<'tcx>, item: LocalDefId) -> &'tcx [
Some(ty) => ty.span,
_ => tcx.def_span(item),
};
collector.visit_spanned(span, tcx.type_of(item).subst_identity());
collector.visit_spanned(span, tcx.type_of(item).instantiate_identity());
collector.collect_body_and_predicate_taits();
}
// We're also doing this for `AssocTy` for the wf checks in `check_opaque_meets_bounds`
DefKind::TyAlias | DefKind::AssocTy => {
tcx.type_of(item).subst_identity().visit_with(&mut collector);
tcx.type_of(item).instantiate_identity().visit_with(&mut collector);
}
DefKind::OpaqueTy => {
for (pred, span) in tcx.explicit_item_bounds(item).subst_identity_iter_copied() {
for (pred, span) in tcx.explicit_item_bounds(item).instantiate_identity_iter_copied() {
collector.visit_spanned(span, pred);
}
}

View file

@ -31,7 +31,7 @@ fn representability(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Representability {
}
Representability::Representable
}
DefKind::Field => representability_ty(tcx, tcx.type_of(def_id).subst_identity()),
DefKind::Field => representability_ty(tcx, tcx.type_of(def_id).instantiate_identity()),
def_kind => bug!("unexpected {def_kind:?}"),
}
}
@ -68,14 +68,14 @@ representability_adt_ty(Bar<..>) is in the cycle and representability(Bar) is
*not* in the cycle.
*/
fn representability_adt_ty<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Representability {
let ty::Adt(adt, substs) = ty.kind() else { bug!("expected adt") };
let ty::Adt(adt, args) = ty.kind() else { bug!("expected adt") };
if let Some(def_id) = adt.did().as_local() {
rtry!(tcx.representability(def_id));
}
// At this point, we know that the item of the ADT type is representable;
// but the type parameters may cause a cycle with an upstream type
let params_in_repr = tcx.params_in_repr(adt.did());
for (i, subst) in substs.iter().enumerate() {
for (i, subst) in args.iter().enumerate() {
if let ty::GenericArgKind::Type(ty) = subst.unpack() {
if params_in_repr.contains(i as u32) {
rtry!(representability_ty(tcx, ty));
@ -91,7 +91,11 @@ fn params_in_repr(tcx: TyCtxt<'_>, def_id: LocalDefId) -> BitSet<u32> {
let mut params_in_repr = BitSet::new_empty(generics.params.len());
for variant in adt_def.variants() {
for field in variant.fields.iter() {
params_in_repr_ty(tcx, tcx.type_of(field.did).subst_identity(), &mut params_in_repr);
params_in_repr_ty(
tcx,
tcx.type_of(field.did).instantiate_identity(),
&mut params_in_repr,
);
}
}
params_in_repr
@ -99,9 +103,9 @@ fn params_in_repr(tcx: TyCtxt<'_>, def_id: LocalDefId) -> BitSet<u32> {
fn params_in_repr_ty<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, params_in_repr: &mut BitSet<u32>) {
match *ty.kind() {
ty::Adt(adt, substs) => {
ty::Adt(adt, args) => {
let inner_params_in_repr = tcx.params_in_repr(adt.did());
for (i, subst) in substs.iter().enumerate() {
for (i, subst) in args.iter().enumerate() {
if let ty::GenericArgKind::Type(ty) = subst.unpack() {
if inner_params_in_repr.contains(i as u32) {
params_in_repr_ty(tcx, ty, params_in_repr);

View file

@ -37,12 +37,12 @@ fn sized_constraint_for_ty<'tcx>(
Some(&ty) => sized_constraint_for_ty(tcx, adtdef, ty),
},
Adt(adt, substs) => {
Adt(adt, args) => {
// recursive case
let adt_tys = adt.sized_constraint(tcx);
debug!("sized_constraint_for_ty({:?}) intermediate = {:?}", ty, adt_tys);
adt_tys
.subst_iter_copied(tcx, substs)
.arg_iter_copied(tcx, args)
.flat_map(|ty| sized_constraint_for_ty(tcx, adtdef, ty))
.collect()
}
@ -100,12 +100,10 @@ fn adt_sized_constraint(tcx: TyCtxt<'_>, def_id: DefId) -> &[Ty<'_>] {
}
let def = tcx.adt_def(def_id);
let result = tcx.mk_type_list_from_iter(
def.variants()
.iter()
.filter_map(|v| v.tail_opt())
.flat_map(|f| sized_constraint_for_ty(tcx, def, tcx.type_of(f.did).subst_identity())),
);
let result =
tcx.mk_type_list_from_iter(def.variants().iter().filter_map(|v| v.tail_opt()).flat_map(
|f| sized_constraint_for_ty(tcx, def, tcx.type_of(f.did).instantiate_identity()),
));
debug!("adt_sized_constraint: {:?} => {:?}", def, result);
@ -133,7 +131,7 @@ fn param_env(tcx: TyCtxt<'_>, def_id: DefId) -> ty::ParamEnv<'_> {
if tcx.def_kind(def_id) == DefKind::AssocFn
&& tcx.associated_item(def_id).container == ty::AssocItemContainer::TraitContainer
{
let sig = tcx.fn_sig(def_id).subst_identity();
let sig = tcx.fn_sig(def_id).instantiate_identity();
// We accounted for the binder of the fn sig, so skip the binder.
sig.skip_binder().visit_with(&mut ImplTraitInTraitFinder {
tcx,
@ -282,7 +280,7 @@ impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for ImplTraitInTraitFinder<'_, 'tcx> {
// If we're lowering to associated item, install the opaque type which is just
// the `type_of` of the trait's associated item. If we're using the old lowering
// strategy, then just reinterpret the associated type like an opaque :^)
let default_ty = self.tcx.type_of(shifted_alias_ty.def_id).subst(self.tcx, shifted_alias_ty.substs);
let default_ty = self.tcx.type_of(shifted_alias_ty.def_id).instantiate(self.tcx, shifted_alias_ty.args);
self.predicates.push(
ty::Binder::bind_with_vars(
@ -299,7 +297,7 @@ impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for ImplTraitInTraitFinder<'_, 'tcx> {
for bound in self
.tcx
.item_bounds(unshifted_alias_ty.def_id)
.subst_iter(self.tcx, unshifted_alias_ty.substs)
.arg_iter(self.tcx, unshifted_alias_ty.args)
{
bound.visit_with(self);
}
@ -352,8 +350,8 @@ fn issue33140_self_ty(tcx: TyCtxt<'_>, def_id: DefId) -> Option<EarlyBinder<Ty<'
}
// impl must be `impl Trait for dyn Marker1 + Marker2 + ...`
if trait_ref.substs.len() != 1 {
debug!("issue33140_self_ty - impl has substs!");
if trait_ref.args.len() != 1 {
debug!("issue33140_self_ty - impl has args!");
return None;
}
@ -409,7 +407,7 @@ fn unsizing_params_for_adt<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> BitSet<u32
};
let mut unsizing_params = BitSet::new_empty(num_params);
for arg in tcx.type_of(tail_field.did).subst_identity().walk() {
for arg in tcx.type_of(tail_field.did).instantiate_identity().walk() {
if let Some(i) = maybe_unsizing_param_idx(arg) {
unsizing_params.insert(i);
}
@ -418,7 +416,7 @@ fn unsizing_params_for_adt<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> BitSet<u32
// Ensure none of the other fields mention the parameters used
// in unsizing.
for field in prefix_fields {
for arg in tcx.type_of(field.did).subst_identity().walk() {
for arg in tcx.type_of(field.did).instantiate_identity().walk() {
if let Some(i) = maybe_unsizing_param_idx(arg) {
unsizing_params.remove(i);
}