2022-01-23 12:34:26 -06:00
|
|
|
use rustc_errors::ErrorGuaranteed;
|
2023-08-09 20:00:07 +00:00
|
|
|
use rustc_hir::def::DefKind;
|
2022-05-08 15:53:19 +02:00
|
|
|
use rustc_hir::def_id::DefId;
|
2020-03-30 21:40:53 +02:00
|
|
|
use rustc_infer::infer::TyCtxtInferExt;
|
2023-05-15 06:24:45 +02:00
|
|
|
use rustc_middle::query::Providers;
|
2023-07-24 22:02:52 +00:00
|
|
|
use rustc_middle::traits::{BuiltinImplSource, CodegenObligationError};
|
2023-07-11 22:35:29 +01:00
|
|
|
use rustc_middle::ty::GenericArgsRef;
|
2023-02-22 02:18:40 +00:00
|
|
|
use rustc_middle::ty::{self, Instance, TyCtxt, TypeVisitableExt};
|
2022-10-27 14:45:02 +04:00
|
|
|
use rustc_span::sym;
|
2020-02-11 21:19:40 +01:00
|
|
|
use rustc_trait_selection::traits;
|
2023-07-11 22:35:29 +01:00
|
|
|
use traits::{translate_args, Reveal};
|
2020-02-12 17:24:32 +01:00
|
|
|
|
2023-04-07 19:45:17 +01:00
|
|
|
use crate::errors::UnexpectedFnPtrAssociatedItem;
|
|
|
|
|
2020-04-10 05:13:20 +03:00
|
|
|
fn resolve_instance<'tcx>(
|
2020-02-12 17:24:32 +01:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2023-07-11 22:35:29 +01:00
|
|
|
key: ty::ParamEnvAnd<'tcx, (DefId, GenericArgsRef<'tcx>)>,
|
2022-01-23 12:34:26 -06:00
|
|
|
) -> Result<Option<Instance<'tcx>>, ErrorGuaranteed> {
|
2023-08-09 20:00:07 +00:00
|
|
|
let (param_env, (def_id, args)) = key.into_parts();
|
2020-04-10 05:13:20 +03:00
|
|
|
|
2023-08-09 20:00:07 +00:00
|
|
|
let result = if let Some(trait_def_id) = tcx.trait_of_item(def_id) {
|
2020-02-12 17:24:32 +01:00
|
|
|
debug!(" => associated item, attempting to find impl in param_env {:#?}", param_env);
|
2022-12-12 22:11:33 +09:00
|
|
|
resolve_associated_item(
|
|
|
|
tcx,
|
2023-08-09 20:00:07 +00:00
|
|
|
def_id,
|
2022-12-12 22:11:33 +09:00
|
|
|
param_env,
|
|
|
|
trait_def_id,
|
2023-07-11 22:35:29 +01:00
|
|
|
tcx.normalize_erasing_regions(param_env, args),
|
2022-12-12 22:11:33 +09:00
|
|
|
)
|
2020-02-12 17:24:32 +01:00
|
|
|
} else {
|
2023-08-09 20:00:07 +00:00
|
|
|
let def = if matches!(tcx.def_kind(def_id), DefKind::Fn) && tcx.is_intrinsic(def_id) {
|
|
|
|
debug!(" => intrinsic");
|
|
|
|
ty::InstanceDef::Intrinsic(def_id)
|
|
|
|
} else if Some(def_id) == tcx.lang_items().drop_in_place_fn() {
|
|
|
|
let ty = args.type_at(0);
|
2020-02-12 17:24:32 +01:00
|
|
|
|
2023-08-09 20:00:07 +00:00
|
|
|
if ty.needs_drop(tcx, param_env) {
|
|
|
|
debug!(" => nontrivial drop glue");
|
|
|
|
match *ty.kind() {
|
|
|
|
ty::Closure(..)
|
2023-10-19 16:06:43 +00:00
|
|
|
| ty::Coroutine(..)
|
2023-08-09 20:00:07 +00:00
|
|
|
| ty::Tuple(..)
|
|
|
|
| ty::Adt(..)
|
|
|
|
| ty::Dynamic(..)
|
|
|
|
| ty::Array(..)
|
|
|
|
| ty::Slice(..) => {}
|
|
|
|
// Drop shims can only be built from ADTs.
|
|
|
|
_ => return Ok(None),
|
2020-02-12 17:24:32 +01:00
|
|
|
}
|
2023-08-09 20:00:07 +00:00
|
|
|
|
|
|
|
ty::InstanceDef::DropGlue(def_id, Some(ty))
|
|
|
|
} else {
|
|
|
|
debug!(" => trivial drop glue");
|
|
|
|
ty::InstanceDef::DropGlue(def_id, None)
|
2020-02-12 17:24:32 +01:00
|
|
|
}
|
2023-08-09 20:00:07 +00:00
|
|
|
} else {
|
|
|
|
debug!(" => free item");
|
2023-09-13 13:55:23 +00:00
|
|
|
// FIXME(effects): we may want to erase the effect param if that is present on this item.
|
2023-08-09 20:00:07 +00:00
|
|
|
ty::InstanceDef::Item(def_id)
|
2020-02-12 17:24:32 +01:00
|
|
|
};
|
2023-08-09 20:00:07 +00:00
|
|
|
|
2023-07-11 22:35:29 +01:00
|
|
|
Ok(Some(Instance { def, args }))
|
2020-02-12 17:24:32 +01:00
|
|
|
};
|
2023-11-20 22:44:39 -03:00
|
|
|
debug!("resolve_instance: result={:?}", result);
|
2020-02-12 17:24:32 +01:00
|
|
|
result
|
|
|
|
}
|
|
|
|
|
|
|
|
fn resolve_associated_item<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
2021-11-18 22:29:07 +00:00
|
|
|
trait_item_id: DefId,
|
2020-02-12 17:24:32 +01:00
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
trait_id: DefId,
|
2023-07-11 22:35:29 +01:00
|
|
|
rcvr_args: GenericArgsRef<'tcx>,
|
2022-01-23 12:34:26 -06:00
|
|
|
) -> Result<Option<Instance<'tcx>>, ErrorGuaranteed> {
|
2023-07-11 22:35:29 +01:00
|
|
|
debug!(?trait_item_id, ?param_env, ?trait_id, ?rcvr_args, "resolve_associated_item");
|
2020-02-12 17:24:32 +01:00
|
|
|
|
2023-07-11 22:35:29 +01:00
|
|
|
let trait_ref = ty::TraitRef::from_method(tcx, trait_id, rcvr_args);
|
2021-07-02 10:14:13 +09:00
|
|
|
|
2023-07-04 01:18:31 +00:00
|
|
|
let vtbl = match tcx.codegen_select_candidate((param_env, trait_ref)) {
|
2022-05-01 11:03:14 +02:00
|
|
|
Ok(vtbl) => vtbl,
|
|
|
|
Err(CodegenObligationError::Ambiguity) => {
|
2023-12-18 22:21:37 +11:00
|
|
|
let reported = tcx.dcx().span_delayed_bug(
|
2022-05-01 11:03:14 +02:00
|
|
|
tcx.def_span(trait_item_id),
|
Restrict `From<S>` for `{D,Subd}iagnosticMessage`.
Currently a `{D,Subd}iagnosticMessage` can be created from any type that
impls `Into<String>`. That includes `&str`, `String`, and `Cow<'static,
str>`, which are reasonable. It also includes `&String`, which is pretty
weird, and results in many places making unnecessary allocations for
patterns like this:
```
self.fatal(&format!(...))
```
This creates a string with `format!`, takes a reference, passes the
reference to `fatal`, which does an `into()`, which clones the
reference, doing a second allocation. Two allocations for a single
string, bleh.
This commit changes the `From` impls so that you can only create a
`{D,Subd}iagnosticMessage` from `&str`, `String`, or `Cow<'static,
str>`. This requires changing all the places that currently create one
from a `&String`. Most of these are of the `&format!(...)` form
described above; each one removes an unnecessary static `&`, plus an
allocation when executed. There are also a few places where the existing
use of `&String` was more reasonable; these now just use `clone()` at
the call site.
As well as making the code nicer and more efficient, this is a step
towards possibly using `Cow<'static, str>` in
`{D,Subd}iagnosticMessage::{Str,Eager}`. That would require changing
the `From<&'a str>` impls to `From<&'static str>`, which is doable, but
I'm not yet sure if it's worthwhile.
2023-04-20 13:26:58 +10:00
|
|
|
format!(
|
2022-09-08 17:02:09 +02:00
|
|
|
"encountered ambiguity selecting `{trait_ref:?}` during codegen, presuming due to \
|
2022-05-01 11:03:14 +02:00
|
|
|
overflow or prior type error",
|
|
|
|
),
|
|
|
|
);
|
|
|
|
return Err(reported);
|
|
|
|
}
|
|
|
|
Err(CodegenObligationError::Unimplemented) => return Ok(None),
|
|
|
|
Err(CodegenObligationError::FulfillmentError) => return Ok(None),
|
|
|
|
};
|
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-09-24 19:22:36 +02:00
|
|
|
traits::ImplSource::UserDefined(impl_data) => {
|
2020-03-29 16:40:30 +02:00
|
|
|
debug!(
|
2020-09-24 19:22:36 +02:00
|
|
|
"resolving ImplSource::UserDefined: {:?}, {:?}, {:?}, {:?}",
|
2023-07-11 22:35:29 +01:00
|
|
|
param_env, trait_item_id, rcvr_args, impl_data
|
2020-03-29 16:40:30 +02:00
|
|
|
);
|
2023-07-11 22:35:29 +01:00
|
|
|
assert!(!rcvr_args.has_infer());
|
2023-04-27 08:34:11 +01:00
|
|
|
assert!(!trait_ref.has_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)?
|
2021-11-18 22:29:07 +00:00
|
|
|
.leaf_def(tcx, trait_item_id)
|
2020-03-29 16:40:30 +02:00
|
|
|
.unwrap_or_else(|| {
|
2021-11-18 22:29:07 +00:00
|
|
|
bug!("{:?} not found in {:?}", trait_item_id, impl_data.impl_def_id);
|
2020-03-29 16:40:30 +02:00
|
|
|
});
|
|
|
|
let infcx = tcx.infer_ctxt().build();
|
2020-04-11 00:50:02 -04:00
|
|
|
let param_env = param_env.with_reveal_all_normalized(tcx);
|
2023-07-11 22:35:29 +01:00
|
|
|
let args = rcvr_args.rebase_onto(tcx, trait_def_id, impl_data.args);
|
|
|
|
let args = translate_args(
|
2020-03-29 16:40:30 +02:00
|
|
|
&infcx,
|
|
|
|
param_env,
|
|
|
|
impl_data.impl_def_id,
|
2023-07-11 22:35:29 +01:00
|
|
|
args,
|
2020-03-29 16:40:30 +02:00
|
|
|
leaf_def.defining_node,
|
|
|
|
);
|
2023-07-11 22:35:29 +01:00
|
|
|
let args = infcx.tcx.erase_regions(args);
|
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
|
|
|
}
|
|
|
|
|
2023-08-21 22:39:34 +00:00
|
|
|
// HACK: We may have overlapping `dyn Trait` built-in impls and
|
|
|
|
// user-provided blanket impls. Detect that case here, and return
|
|
|
|
// ambiguity.
|
|
|
|
//
|
|
|
|
// This should not affect totally monomorphized contexts, only
|
|
|
|
// resolve calls that happen polymorphically, such as the mir-inliner
|
|
|
|
// and const-prop (and also some lints).
|
|
|
|
let self_ty = rcvr_args.type_at(0);
|
|
|
|
if !self_ty.is_known_rigid() {
|
|
|
|
let predicates = tcx
|
|
|
|
.predicates_of(impl_data.impl_def_id)
|
|
|
|
.instantiate(tcx, impl_data.args)
|
|
|
|
.predicates;
|
|
|
|
let sized_def_id = tcx.lang_items().sized_trait();
|
|
|
|
// If we find a `Self: Sized` bound on the item, then we know
|
|
|
|
// that `dyn Trait` can certainly never apply here.
|
|
|
|
if !predicates.into_iter().filter_map(ty::Clause::as_trait_clause).any(|clause| {
|
|
|
|
Some(clause.def_id()) == sized_def_id
|
|
|
|
&& clause.skip_binder().self_ty() == self_ty
|
|
|
|
}) {
|
|
|
|
return Ok(None);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-04 14:18:11 +02:00
|
|
|
// Any final impl is required to define all associated items.
|
2022-03-12 19:36:11 +01:00
|
|
|
if !leaf_def.item.defaultness(tcx).has_value() {
|
2023-12-18 22:21:37 +11:00
|
|
|
let guard = tcx.dcx().span_delayed_bug(
|
2022-10-04 14:18:11 +02:00
|
|
|
tcx.def_span(leaf_def.item.def_id),
|
|
|
|
"missing value for assoc item in impl",
|
|
|
|
);
|
|
|
|
return Err(guard);
|
2022-07-15 05:37:32 +00:00
|
|
|
}
|
|
|
|
|
2023-07-11 22:35:29 +01:00
|
|
|
let args = tcx.erase_regions(args);
|
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).
|
2021-11-18 22:29:07 +00:00
|
|
|
if leaf_def.item.kind == ty::AssocKind::Const
|
|
|
|
&& trait_item_id != leaf_def.item.def_id
|
2022-09-30 18:53:32 +01:00
|
|
|
&& let Some(leaf_def_item) = leaf_def.item.def_id.as_local()
|
2020-04-10 05:13:29 +03:00
|
|
|
{
|
2022-12-24 21:12:38 +00:00
|
|
|
tcx.compare_impl_const((leaf_def_item, trait_item_id))?;
|
2020-04-10 05:13:29 +03:00
|
|
|
}
|
|
|
|
|
2023-07-11 22:35:29 +01:00
|
|
|
Some(ty::Instance::new(leaf_def.item.def_id, args))
|
2020-02-12 17:24:32 +01:00
|
|
|
}
|
2023-07-24 22:02:52 +00:00
|
|
|
traits::ImplSource::Builtin(BuiltinImplSource::Object { vtable_base }, _) => {
|
|
|
|
traits::get_vtable_index_of_object_method(tcx, *vtable_base, trait_item_id).map(
|
|
|
|
|index| Instance {
|
|
|
|
def: ty::InstanceDef::Virtual(trait_item_id, index),
|
|
|
|
args: rcvr_args,
|
|
|
|
},
|
|
|
|
)
|
2020-02-12 17:24:32 +01:00
|
|
|
}
|
2023-07-24 22:02:52 +00:00
|
|
|
traits::ImplSource::Builtin(BuiltinImplSource::Misc, _) => {
|
2022-07-20 14:32:58 +02:00
|
|
|
let lang_items = tcx.lang_items();
|
|
|
|
if Some(trait_ref.def_id) == lang_items.clone_trait() {
|
2020-02-09 02:39:14 +02:00
|
|
|
// FIXME(eddyb) use lang items for methods instead of names.
|
2021-11-18 22:29:07 +00:00
|
|
|
let name = tcx.item_name(trait_item_id);
|
2020-02-09 02:39:14 +02:00
|
|
|
if name == sym::clone {
|
|
|
|
let self_ty = trait_ref.self_ty();
|
|
|
|
|
2022-10-27 14:45:02 +04:00
|
|
|
let is_copy = self_ty.is_copy_modulo_regions(tcx, param_env);
|
2020-08-03 00:49:11 +02:00
|
|
|
match self_ty.kind() {
|
2020-06-22 13:45:05 +01:00
|
|
|
_ if is_copy => (),
|
2023-10-19 16:06:43 +00:00
|
|
|
ty::Coroutine(..)
|
|
|
|
| ty::CoroutineWitness(..)
|
2022-03-21 12:57:06 +08:00
|
|
|
| ty::Closure(..)
|
2024-01-24 18:01:56 +00:00
|
|
|
| ty::CoroutineClosure(..)
|
2022-03-21 12:57:06 +08:00
|
|
|
| ty::Tuple(..) => {}
|
2020-06-22 13:45:05 +01:00
|
|
|
_ => return Ok(None),
|
|
|
|
};
|
2020-02-09 02:39:14 +02:00
|
|
|
|
|
|
|
Some(Instance {
|
2021-11-18 22:29:07 +00:00
|
|
|
def: ty::InstanceDef::CloneShim(trait_item_id, self_ty),
|
2023-07-11 22:35:29 +01:00
|
|
|
args: rcvr_args,
|
2020-02-09 02:39:14 +02:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
assert_eq!(name, sym::clone_from);
|
|
|
|
|
|
|
|
// Use the default `fn clone_from` from `trait Clone`.
|
2023-07-11 22:35:29 +01:00
|
|
|
let args = tcx.erase_regions(rcvr_args);
|
|
|
|
Some(ty::Instance::new(trait_item_id, args))
|
2020-02-09 02:39:14 +02:00
|
|
|
}
|
2022-07-20 14:32:58 +02:00
|
|
|
} else if Some(trait_ref.def_id) == lang_items.fn_ptr_trait() {
|
|
|
|
if lang_items.fn_ptr_addr() == Some(trait_item_id) {
|
|
|
|
let self_ty = trait_ref.self_ty();
|
|
|
|
if !matches!(self_ty.kind(), ty::FnPtr(..)) {
|
|
|
|
return Ok(None);
|
|
|
|
}
|
|
|
|
Some(Instance {
|
|
|
|
def: ty::InstanceDef::FnPtrAddrShim(trait_item_id, self_ty),
|
2023-07-11 22:35:29 +01:00
|
|
|
args: rcvr_args,
|
2022-07-20 14:32:58 +02:00
|
|
|
})
|
|
|
|
} else {
|
2023-12-18 22:21:37 +11:00
|
|
|
tcx.dcx().emit_fatal(UnexpectedFnPtrAssociatedItem {
|
2023-04-07 19:45:17 +01:00
|
|
|
span: tcx.def_span(trait_item_id),
|
|
|
|
})
|
2022-07-20 14:32:58 +02:00
|
|
|
}
|
2023-06-16 02:10:10 +00:00
|
|
|
} 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
|
|
|
|
// methods.
|
|
|
|
if cfg!(debug_assertions)
|
|
|
|
&& ![sym::call, sym::call_mut, sym::call_once]
|
|
|
|
.contains(&tcx.item_name(trait_item_id))
|
|
|
|
{
|
|
|
|
// For compiler developers who'd like to add new items to `Fn`/`FnMut`/`FnOnce`,
|
|
|
|
// you either need to generate a shim body, or perhaps return
|
|
|
|
// `InstanceDef::Item` pointing to a trait default method body if
|
|
|
|
// it is given a default implementation by the trait.
|
|
|
|
bug!(
|
|
|
|
"no definition for `{trait_ref}::{}` for built-in callable type",
|
|
|
|
tcx.item_name(trait_item_id)
|
|
|
|
)
|
|
|
|
}
|
2023-07-11 22:35:29 +01:00
|
|
|
match *rcvr_args.type_at(0).kind() {
|
|
|
|
ty::Closure(closure_def_id, args) => {
|
2023-06-16 02:10:10 +00:00
|
|
|
let trait_closure_kind = tcx.fn_trait_kind_from_def_id(trait_id).unwrap();
|
2024-01-19 20:04:14 +00:00
|
|
|
Some(Instance::resolve_closure(
|
|
|
|
tcx,
|
|
|
|
closure_def_id,
|
|
|
|
args,
|
|
|
|
trait_closure_kind,
|
|
|
|
))
|
2023-06-16 02:10:10 +00:00
|
|
|
}
|
|
|
|
ty::FnDef(..) | ty::FnPtr(..) => Some(Instance {
|
2023-07-11 22:35:29 +01:00
|
|
|
def: ty::InstanceDef::FnPtrShim(trait_item_id, rcvr_args.type_at(0)),
|
|
|
|
args: rcvr_args,
|
2023-06-16 02:10:10 +00:00
|
|
|
}),
|
|
|
|
_ => bug!(
|
|
|
|
"no built-in definition for `{trait_ref}::{}` for non-fn type",
|
|
|
|
tcx.item_name(trait_item_id)
|
|
|
|
),
|
|
|
|
}
|
2020-02-12 17:24:32 +01:00
|
|
|
} else {
|
2024-01-19 21:28:37 +00:00
|
|
|
Instance::try_resolve_item_for_coroutine(tcx, trait_item_id, trait_id, rcvr_args)
|
2020-02-12 17:24:32 +01:00
|
|
|
}
|
|
|
|
}
|
2023-07-16 22:42:46 +00:00
|
|
|
traits::ImplSource::Param(..)
|
2023-07-24 22:02:52 +00:00
|
|
|
| traits::ImplSource::Builtin(BuiltinImplSource::TraitUpcasting { .. }, _)
|
|
|
|
| traits::ImplSource::Builtin(BuiltinImplSource::TupleUnsizing, _) => 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
|
|
|
|
2023-11-16 17:08:27 +11:00
|
|
|
pub(crate) fn provide(providers: &mut Providers) {
|
2023-05-15 06:24:45 +02:00
|
|
|
*providers = Providers { resolve_instance, ..*providers };
|
2020-04-05 01:21:36 -04:00
|
|
|
}
|