2020-04-10 05:13:29 +03:00
|
|
|
use rustc_errors::ErrorReported;
|
2020-07-08 01:03:19 +02:00
|
|
|
use rustc_hir::def_id::{DefId, LocalDefId};
|
2020-03-30 21:40:53 +02:00
|
|
|
use rustc_infer::infer::TyCtxtInferExt;
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::ty::subst::SubstsRef;
|
|
|
|
use rustc_middle::ty::{self, Instance, TyCtxt, TypeFoldable};
|
2020-02-09 02:39:14 +02:00
|
|
|
use rustc_span::sym;
|
2020-02-12 17:24:32 +01:00
|
|
|
use rustc_target::spec::abi::Abi;
|
2020-02-11 21:19:40 +01:00
|
|
|
use rustc_trait_selection::traits;
|
2020-03-30 21:40:53 +02:00
|
|
|
use traits::{translate_substs, Reveal};
|
2020-02-12 17:24:32 +01:00
|
|
|
|
|
|
|
use log::debug;
|
|
|
|
|
2020-04-10 05:13:20 +03:00
|
|
|
fn resolve_instance<'tcx>(
|
2020-02-12 17:24:32 +01:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2020-04-10 05:13:20 +03:00
|
|
|
key: ty::ParamEnvAnd<'tcx, (DefId, SubstsRef<'tcx>)>,
|
2020-04-10 05:13:29 +03:00
|
|
|
) -> Result<Option<Instance<'tcx>>, ErrorReported> {
|
2020-07-02 23:56:17 +02:00
|
|
|
let (param_env, (did, substs)) = key.into_parts();
|
2020-07-08 01:03:19 +02:00
|
|
|
if let Some(did) = did.as_local() {
|
|
|
|
if let Some(param_did) = tcx.opt_const_param_of(did) {
|
|
|
|
return tcx.resolve_instance_of_const_arg(param_env.and((did, param_did, substs)));
|
|
|
|
}
|
2020-07-02 23:56:17 +02:00
|
|
|
}
|
2020-07-08 01:03:19 +02:00
|
|
|
|
2020-07-15 10:55:41 +02:00
|
|
|
inner_resolve_instance(tcx, param_env.and((ty::WithOptConstParam::unknown(did), substs)))
|
2020-07-02 23:56:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn resolve_instance_of_const_arg<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
2020-07-08 01:03:19 +02:00
|
|
|
key: ty::ParamEnvAnd<'tcx, (LocalDefId, DefId, SubstsRef<'tcx>)>,
|
2020-07-02 23:56:17 +02:00
|
|
|
) -> Result<Option<Instance<'tcx>>, ErrorReported> {
|
2020-07-15 10:50:54 +02:00
|
|
|
let (param_env, (did, const_param_did, substs)) = key.into_parts();
|
2020-07-08 01:03:19 +02:00
|
|
|
inner_resolve_instance(
|
|
|
|
tcx,
|
2020-07-15 10:50:54 +02:00
|
|
|
param_env.and((
|
|
|
|
ty::WithOptConstParam { did: did.to_def_id(), const_param_did: Some(const_param_did) },
|
|
|
|
substs,
|
|
|
|
)),
|
2020-07-08 01:03:19 +02:00
|
|
|
)
|
2020-07-02 23:56:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn inner_resolve_instance<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
2020-07-15 10:50:54 +02:00
|
|
|
key: ty::ParamEnvAnd<'tcx, (ty::WithOptConstParam<DefId>, SubstsRef<'tcx>)>,
|
2020-07-02 23:56:17 +02:00
|
|
|
) -> Result<Option<Instance<'tcx>>, ErrorReported> {
|
|
|
|
let (param_env, (def, substs)) = key.into_parts();
|
2020-04-10 05:13:20 +03:00
|
|
|
|
2020-07-02 23:56:17 +02:00
|
|
|
debug!("resolve(def={:?}, substs={:?})", def.did, substs);
|
|
|
|
let result = if let Some(trait_def_id) = tcx.trait_of_item(def.did) {
|
2020-02-12 17:24:32 +01:00
|
|
|
debug!(" => associated item, attempting to find impl in param_env {:#?}", param_env);
|
2020-07-02 23:56:17 +02:00
|
|
|
let item = tcx.associated_item(def.did);
|
2020-02-12 17:24:32 +01:00
|
|
|
resolve_associated_item(tcx, &item, param_env, trait_def_id, substs)
|
|
|
|
} else {
|
2020-07-15 10:53:11 +02:00
|
|
|
let ty = tcx.type_of(def.def_id_for_type_of());
|
2020-02-12 17:24:32 +01:00
|
|
|
let item_type = tcx.subst_and_normalize_erasing_regions(substs, param_env, &ty);
|
|
|
|
|
|
|
|
let def = match item_type.kind {
|
|
|
|
ty::FnDef(..)
|
|
|
|
if {
|
|
|
|
let f = item_type.fn_sig(tcx);
|
|
|
|
f.abi() == Abi::RustIntrinsic || f.abi() == Abi::PlatformIntrinsic
|
|
|
|
} =>
|
|
|
|
{
|
|
|
|
debug!(" => intrinsic");
|
2020-07-02 23:56:17 +02:00
|
|
|
ty::InstanceDef::Intrinsic(def.did)
|
2020-02-12 17:24:32 +01:00
|
|
|
}
|
2020-02-09 02:39:14 +02:00
|
|
|
ty::FnDef(def_id, substs) if Some(def_id) == tcx.lang_items().drop_in_place_fn() => {
|
|
|
|
let ty = substs.type_at(0);
|
|
|
|
|
|
|
|
if ty.needs_drop(tcx, param_env) {
|
|
|
|
// `DropGlue` requires a monomorphic aka concrete type.
|
|
|
|
if ty.needs_subst() {
|
2020-04-10 05:13:29 +03:00
|
|
|
return Ok(None);
|
2020-02-12 17:24:32 +01:00
|
|
|
}
|
2020-02-09 02:39:14 +02:00
|
|
|
|
|
|
|
debug!(" => nontrivial drop glue");
|
|
|
|
ty::InstanceDef::DropGlue(def_id, Some(ty))
|
2020-02-12 17:24:32 +01:00
|
|
|
} else {
|
2020-02-09 02:39:14 +02:00
|
|
|
debug!(" => trivial drop glue");
|
|
|
|
ty::InstanceDef::DropGlue(def_id, None)
|
2020-02-12 17:24:32 +01:00
|
|
|
}
|
|
|
|
}
|
2020-02-09 02:39:14 +02:00
|
|
|
_ => {
|
|
|
|
debug!(" => free item");
|
2020-07-03 19:13:39 +02:00
|
|
|
ty::InstanceDef::Item(def)
|
2020-02-09 02:39:14 +02:00
|
|
|
}
|
2020-02-12 17:24:32 +01:00
|
|
|
};
|
2020-04-10 05:13:29 +03:00
|
|
|
Ok(Some(Instance { def, substs }))
|
2020-02-12 17:24:32 +01:00
|
|
|
};
|
2020-07-02 23:56:17 +02:00
|
|
|
debug!("resolve(def.did={:?}, substs={:?}) = {:?}", def.did, substs, result);
|
2020-02-12 17:24:32 +01:00
|
|
|
result
|
|
|
|
}
|
|
|
|
|
|
|
|
fn resolve_associated_item<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
trait_item: &ty::AssocItem,
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
trait_id: DefId,
|
|
|
|
rcvr_substs: SubstsRef<'tcx>,
|
2020-04-10 05:13:29 +03:00
|
|
|
) -> Result<Option<Instance<'tcx>>, ErrorReported> {
|
2020-02-12 17:24:32 +01:00
|
|
|
let def_id = trait_item.def_id;
|
|
|
|
debug!(
|
|
|
|
"resolve_associated_item(trait_item={:?}, \
|
|
|
|
param_env={:?}, \
|
|
|
|
trait_id={:?}, \
|
|
|
|
rcvr_substs={:?})",
|
|
|
|
def_id, param_env, trait_id, rcvr_substs
|
|
|
|
);
|
|
|
|
|
|
|
|
let trait_ref = ty::TraitRef::from_method(tcx, trait_id, rcvr_substs);
|
2020-03-01 10:46:07 -08:00
|
|
|
let vtbl = tcx.codegen_fulfill_obligation((param_env, ty::Binder::bind(trait_ref)))?;
|
2020-02-12 17:24:32 +01:00
|
|
|
|
|
|
|
// Now that we know which impl is being used, we can dispatch to
|
|
|
|
// the actual function:
|
2020-04-10 05:13:29 +03:00
|
|
|
Ok(match vtbl {
|
2020-06-02 15:54:24 +00:00
|
|
|
traits::ImplSourceUserDefined(impl_data) => {
|
2020-03-29 16:40:30 +02:00
|
|
|
debug!(
|
2020-06-02 15:54:24 +00:00
|
|
|
"resolving ImplSourceUserDefined: {:?}, {:?}, {:?}, {:?}",
|
2020-03-29 16:40:30 +02:00
|
|
|
param_env, trait_item, rcvr_substs, impl_data
|
|
|
|
);
|
|
|
|
assert!(!rcvr_substs.needs_infer());
|
2020-03-29 20:01:41 +02:00
|
|
|
assert!(!trait_ref.needs_infer());
|
2020-02-12 17:24:32 +01:00
|
|
|
|
2020-03-29 16:40:30 +02:00
|
|
|
let trait_def_id = tcx.trait_id_of_impl(impl_data.impl_def_id).unwrap();
|
|
|
|
let trait_def = tcx.trait_def(trait_def_id);
|
|
|
|
let leaf_def = trait_def
|
2020-04-10 05:13:29 +03:00
|
|
|
.ancestors(tcx, impl_data.impl_def_id)?
|
2020-03-29 16:40:30 +02:00
|
|
|
.leaf_def(tcx, trait_item.ident, trait_item.kind)
|
|
|
|
.unwrap_or_else(|| {
|
|
|
|
bug!("{:?} not found in {:?}", trait_item, impl_data.impl_def_id);
|
|
|
|
});
|
|
|
|
|
|
|
|
let substs = tcx.infer_ctxt().enter(|infcx| {
|
|
|
|
let param_env = param_env.with_reveal_all();
|
|
|
|
let substs = rcvr_substs.rebase_onto(tcx, trait_def_id, impl_data.substs);
|
|
|
|
let substs = translate_substs(
|
|
|
|
&infcx,
|
|
|
|
param_env,
|
|
|
|
impl_data.impl_def_id,
|
|
|
|
substs,
|
|
|
|
leaf_def.defining_node,
|
|
|
|
);
|
|
|
|
infcx.tcx.erase_regions(&substs)
|
|
|
|
});
|
2020-02-12 17:24:32 +01:00
|
|
|
|
|
|
|
// 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`.
|
|
|
|
// NOTE: This should be kept in sync with the similar code in
|
2020-04-03 19:03:13 +09:00
|
|
|
// `rustc_trait_selection::traits::project::assemble_candidates_from_impls()`.
|
2020-03-29 16:40:30 +02:00
|
|
|
let eligible = if leaf_def.is_final() {
|
|
|
|
// Non-specializable items are always projectable.
|
2020-02-12 17:24:32 +01:00
|
|
|
true
|
|
|
|
} else {
|
2020-03-29 16:40:30 +02:00
|
|
|
// Only reveal a specializable default if we're past type-checking
|
|
|
|
// and the obligation is monomorphic, otherwise passes such as
|
|
|
|
// transmute checking and polymorphic MIR optimizations could
|
|
|
|
// get a result which isn't correct for all monomorphizations.
|
2020-07-02 20:52:40 -04:00
|
|
|
if param_env.reveal() == Reveal::All {
|
2020-04-01 16:20:27 +01:00
|
|
|
!trait_ref.still_further_specializable()
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
2020-02-12 17:24:32 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
if !eligible {
|
2020-04-10 05:13:29 +03:00
|
|
|
return Ok(None);
|
2020-02-12 17:24:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
let substs = tcx.erase_regions(&substs);
|
2020-04-10 05:13:29 +03:00
|
|
|
|
|
|
|
// Check if we just resolved an associated `const` declaration from
|
|
|
|
// a `trait` to an associated `const` definition in an `impl`, where
|
|
|
|
// the definition in the `impl` has the wrong type (for which an
|
|
|
|
// error has already been/will be emitted elsewhere).
|
|
|
|
//
|
|
|
|
// NB: this may be expensive, we try to skip it in all the cases where
|
|
|
|
// we know the error would've been caught (e.g. in an upstream crate).
|
|
|
|
//
|
|
|
|
// A better approach might be to just introduce a query (returning
|
|
|
|
// `Result<(), ErrorReported>`) for the check that `rustc_typeck`
|
|
|
|
// performs (i.e. that the definition's type in the `impl` matches
|
|
|
|
// the declaration in the `trait`), so that we can cheaply check
|
|
|
|
// here if it failed, instead of approximating it.
|
|
|
|
if trait_item.kind == ty::AssocKind::Const
|
|
|
|
&& trait_item.def_id != leaf_def.item.def_id
|
|
|
|
&& leaf_def.item.def_id.is_local()
|
|
|
|
{
|
|
|
|
let normalized_type_of = |def_id, substs| {
|
|
|
|
tcx.subst_and_normalize_erasing_regions(substs, param_env, &tcx.type_of(def_id))
|
|
|
|
};
|
|
|
|
|
|
|
|
let original_ty = normalized_type_of(trait_item.def_id, rcvr_substs);
|
|
|
|
let resolved_ty = normalized_type_of(leaf_def.item.def_id, substs);
|
|
|
|
|
|
|
|
if original_ty != resolved_ty {
|
|
|
|
let msg = format!(
|
|
|
|
"Instance::resolve: inconsistent associated `const` type: \
|
|
|
|
was `{}: {}` but resolved to `{}: {}`",
|
|
|
|
tcx.def_path_str_with_substs(trait_item.def_id, rcvr_substs),
|
|
|
|
original_ty,
|
|
|
|
tcx.def_path_str_with_substs(leaf_def.item.def_id, substs),
|
|
|
|
resolved_ty,
|
|
|
|
);
|
|
|
|
let span = tcx.def_span(leaf_def.item.def_id);
|
|
|
|
tcx.sess.delay_span_bug(span, &msg);
|
|
|
|
|
|
|
|
return Err(ErrorReported);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(ty::Instance::new(leaf_def.item.def_id, substs))
|
2020-02-12 17:24:32 +01:00
|
|
|
}
|
2020-05-11 15:25:33 +00:00
|
|
|
traits::ImplSourceGenerator(generator_data) => Some(Instance {
|
2020-07-15 10:55:41 +02:00
|
|
|
def: ty::InstanceDef::Item(ty::WithOptConstParam::unknown(
|
2020-07-15 10:50:54 +02:00
|
|
|
generator_data.generator_def_id,
|
|
|
|
)),
|
2020-02-12 17:24:32 +01:00
|
|
|
substs: generator_data.substs,
|
|
|
|
}),
|
2020-05-11 15:25:33 +00:00
|
|
|
traits::ImplSourceClosure(closure_data) => {
|
2020-02-12 17:24:32 +01:00
|
|
|
let trait_closure_kind = tcx.fn_trait_kind_from_lang_item(trait_id).unwrap();
|
|
|
|
Some(Instance::resolve_closure(
|
|
|
|
tcx,
|
|
|
|
closure_data.closure_def_id,
|
|
|
|
closure_data.substs,
|
|
|
|
trait_closure_kind,
|
|
|
|
))
|
|
|
|
}
|
2020-05-11 15:25:33 +00:00
|
|
|
traits::ImplSourceFnPointer(ref data) => {
|
2020-02-09 02:39:14 +02:00
|
|
|
// `FnPtrShim` requires a monomorphic aka concrete type.
|
|
|
|
if data.fn_ty.needs_subst() {
|
2020-04-10 05:13:29 +03:00
|
|
|
return Ok(None);
|
2020-02-09 02:39:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Some(Instance {
|
|
|
|
def: ty::InstanceDef::FnPtrShim(trait_item.def_id, data.fn_ty),
|
|
|
|
substs: rcvr_substs,
|
|
|
|
})
|
|
|
|
}
|
2020-05-11 15:25:33 +00:00
|
|
|
traits::ImplSourceObject(ref data) => {
|
2020-02-12 17:24:32 +01:00
|
|
|
let index = traits::get_vtable_index_of_object_method(tcx, data, def_id);
|
|
|
|
Some(Instance { def: ty::InstanceDef::Virtual(def_id, index), substs: rcvr_substs })
|
|
|
|
}
|
2020-05-11 15:25:33 +00:00
|
|
|
traits::ImplSourceBuiltin(..) => {
|
2020-02-09 02:39:14 +02:00
|
|
|
if Some(trait_ref.def_id) == tcx.lang_items().clone_trait() {
|
|
|
|
// FIXME(eddyb) use lang items for methods instead of names.
|
|
|
|
let name = tcx.item_name(def_id);
|
|
|
|
if name == sym::clone {
|
|
|
|
let self_ty = trait_ref.self_ty();
|
|
|
|
|
|
|
|
// `CloneShim` requires a monomorphic aka concrete type.
|
|
|
|
if self_ty.needs_subst() {
|
2020-04-10 05:13:29 +03:00
|
|
|
return Ok(None);
|
2020-02-09 02:39:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Some(Instance {
|
|
|
|
def: ty::InstanceDef::CloneShim(def_id, self_ty),
|
|
|
|
substs: rcvr_substs,
|
|
|
|
})
|
|
|
|
} 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(def_id, substs))
|
|
|
|
}
|
2020-02-12 17:24:32 +01:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2020-05-11 15:25:33 +00:00
|
|
|
traits::ImplSourceAutoImpl(..)
|
|
|
|
| traits::ImplSourceParam(..)
|
|
|
|
| traits::ImplSourceTraitAlias(..)
|
|
|
|
| traits::ImplSourceDiscriminantKind(..) => None,
|
2020-04-10 05:13:29 +03:00
|
|
|
})
|
2020-02-12 17:24:32 +01:00
|
|
|
}
|
2020-04-05 01:21:36 -04:00
|
|
|
|
2020-07-05 23:00:14 +03:00
|
|
|
pub fn provide(providers: &mut ty::query::Providers) {
|
2020-07-02 23:56:17 +02:00
|
|
|
*providers =
|
|
|
|
ty::query::Providers { resolve_instance, resolve_instance_of_const_arg, ..*providers };
|
2020-04-05 01:21:36 -04:00
|
|
|
}
|