refactor(rustc_middle): Substs -> GenericArg
This commit is contained in:
parent
df5c2cf9bc
commit
e55583c4b8
466 changed files with 4574 additions and 4604 deletions
|
@ -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",
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue