Move rustc_ty
-> rustc_ty_utils
This commit is contained in:
parent
fe982319aa
commit
f59d03038c
11 changed files with 7 additions and 7 deletions
17
compiler/rustc_ty_utils/Cargo.toml
Normal file
17
compiler/rustc_ty_utils/Cargo.toml
Normal file
|
@ -0,0 +1,17 @@
|
|||
[package]
|
||||
authors = ["The Rust Project Developers"]
|
||||
name = "rustc_ty_utils"
|
||||
version = "0.0.0"
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
tracing = "0.1"
|
||||
rustc_middle = { path = "../rustc_middle" }
|
||||
rustc_data_structures = { path = "../rustc_data_structures" }
|
||||
rustc_errors = { path = "../rustc_errors" }
|
||||
rustc_hir = { path = "../rustc_hir" }
|
||||
rustc_infer = { path = "../rustc_infer" }
|
||||
rustc_span = { path = "../rustc_span" }
|
||||
rustc_session = { path = "../rustc_session" }
|
||||
rustc_target = { path = "../rustc_target" }
|
||||
rustc_trait_selection = { path = "../rustc_trait_selection" }
|
41
compiler/rustc_ty_utils/src/common_traits.rs
Normal file
41
compiler/rustc_ty_utils/src/common_traits.rs
Normal file
|
@ -0,0 +1,41 @@
|
|||
//! Queries for checking whether a type implements one of a few common traits.
|
||||
|
||||
use rustc_hir::lang_items::LangItem;
|
||||
use rustc_infer::infer::TyCtxtInferExt;
|
||||
use rustc_middle::ty::{self, Ty, TyCtxt};
|
||||
use rustc_span::DUMMY_SP;
|
||||
use rustc_trait_selection::traits;
|
||||
|
||||
fn is_copy_raw<'tcx>(tcx: TyCtxt<'tcx>, query: ty::ParamEnvAnd<'tcx, Ty<'tcx>>) -> bool {
|
||||
is_item_raw(tcx, query, LangItem::Copy)
|
||||
}
|
||||
|
||||
fn is_sized_raw<'tcx>(tcx: TyCtxt<'tcx>, query: ty::ParamEnvAnd<'tcx, Ty<'tcx>>) -> bool {
|
||||
is_item_raw(tcx, query, LangItem::Sized)
|
||||
}
|
||||
|
||||
fn is_freeze_raw<'tcx>(tcx: TyCtxt<'tcx>, query: ty::ParamEnvAnd<'tcx, Ty<'tcx>>) -> bool {
|
||||
is_item_raw(tcx, query, LangItem::Freeze)
|
||||
}
|
||||
|
||||
fn is_item_raw<'tcx>(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
query: ty::ParamEnvAnd<'tcx, Ty<'tcx>>,
|
||||
item: LangItem,
|
||||
) -> bool {
|
||||
let (param_env, ty) = query.into_parts();
|
||||
let trait_def_id = tcx.require_lang_item(item, None);
|
||||
tcx.infer_ctxt().enter(|infcx| {
|
||||
traits::type_known_to_meet_bound_modulo_regions(
|
||||
&infcx,
|
||||
param_env,
|
||||
ty,
|
||||
trait_def_id,
|
||||
DUMMY_SP,
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn provide(providers: &mut ty::query::Providers) {
|
||||
*providers = ty::query::Providers { is_copy_raw, is_sized_raw, is_freeze_raw, ..*providers };
|
||||
}
|
284
compiler/rustc_ty_utils/src/instance.rs
Normal file
284
compiler/rustc_ty_utils/src/instance.rs
Normal file
|
@ -0,0 +1,284 @@
|
|||
use rustc_errors::ErrorReported;
|
||||
use rustc_hir::def_id::{DefId, LocalDefId};
|
||||
use rustc_infer::infer::TyCtxtInferExt;
|
||||
use rustc_middle::ty::subst::SubstsRef;
|
||||
use rustc_middle::ty::{self, Instance, TyCtxt, TypeFoldable};
|
||||
use rustc_span::{sym, DUMMY_SP};
|
||||
use rustc_target::spec::abi::Abi;
|
||||
use rustc_trait_selection::traits;
|
||||
use traits::{translate_substs, Reveal};
|
||||
|
||||
use tracing::debug;
|
||||
|
||||
fn resolve_instance<'tcx>(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
key: ty::ParamEnvAnd<'tcx, (DefId, SubstsRef<'tcx>)>,
|
||||
) -> Result<Option<Instance<'tcx>>, ErrorReported> {
|
||||
let (param_env, (did, substs)) = key.into_parts();
|
||||
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)));
|
||||
}
|
||||
}
|
||||
|
||||
inner_resolve_instance(tcx, param_env.and((ty::WithOptConstParam::unknown(did), substs)))
|
||||
}
|
||||
|
||||
fn resolve_instance_of_const_arg<'tcx>(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
key: ty::ParamEnvAnd<'tcx, (LocalDefId, DefId, SubstsRef<'tcx>)>,
|
||||
) -> Result<Option<Instance<'tcx>>, ErrorReported> {
|
||||
let (param_env, (did, const_param_did, substs)) = key.into_parts();
|
||||
inner_resolve_instance(
|
||||
tcx,
|
||||
param_env.and((
|
||||
ty::WithOptConstParam { did: did.to_def_id(), const_param_did: Some(const_param_did) },
|
||||
substs,
|
||||
)),
|
||||
)
|
||||
}
|
||||
|
||||
fn inner_resolve_instance<'tcx>(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
key: ty::ParamEnvAnd<'tcx, (ty::WithOptConstParam<DefId>, SubstsRef<'tcx>)>,
|
||||
) -> Result<Option<Instance<'tcx>>, ErrorReported> {
|
||||
let (param_env, (def, substs)) = key.into_parts();
|
||||
|
||||
debug!("resolve(def={:?}, substs={:?})", def.did, substs);
|
||||
let result = if let Some(trait_def_id) = tcx.trait_of_item(def.did) {
|
||||
debug!(" => associated item, attempting to find impl in param_env {:#?}", param_env);
|
||||
let item = tcx.associated_item(def.did);
|
||||
resolve_associated_item(tcx, &item, param_env, trait_def_id, substs)
|
||||
} else {
|
||||
let ty = tcx.type_of(def.def_id_for_type_of());
|
||||
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");
|
||||
ty::InstanceDef::Intrinsic(def.did)
|
||||
}
|
||||
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) {
|
||||
debug!(" => nontrivial drop glue");
|
||||
match *ty.kind() {
|
||||
ty::Closure(..)
|
||||
| ty::Generator(..)
|
||||
| ty::Tuple(..)
|
||||
| ty::Adt(..)
|
||||
| ty::Dynamic(..)
|
||||
| ty::Array(..)
|
||||
| ty::Slice(..) => {}
|
||||
// Drop shims can only be built from ADTs.
|
||||
_ => return Ok(None),
|
||||
}
|
||||
|
||||
ty::InstanceDef::DropGlue(def_id, Some(ty))
|
||||
} else {
|
||||
debug!(" => trivial drop glue");
|
||||
ty::InstanceDef::DropGlue(def_id, None)
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
debug!(" => free item");
|
||||
ty::InstanceDef::Item(def)
|
||||
}
|
||||
};
|
||||
Ok(Some(Instance { def, substs }))
|
||||
};
|
||||
debug!("resolve(def.did={:?}, substs={:?}) = {:?}", def.did, substs, result);
|
||||
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>,
|
||||
) -> Result<Option<Instance<'tcx>>, ErrorReported> {
|
||||
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);
|
||||
let vtbl = tcx.codegen_fulfill_obligation((param_env, ty::Binder::bind(trait_ref)))?;
|
||||
|
||||
// Now that we know which impl is being used, we can dispatch to
|
||||
// the actual function:
|
||||
Ok(match vtbl {
|
||||
traits::ImplSource::UserDefined(impl_data) => {
|
||||
debug!(
|
||||
"resolving ImplSource::UserDefined: {:?}, {:?}, {:?}, {:?}",
|
||||
param_env, trait_item, rcvr_substs, impl_data
|
||||
);
|
||||
assert!(!rcvr_substs.needs_infer());
|
||||
assert!(!trait_ref.needs_infer());
|
||||
|
||||
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
|
||||
.ancestors(tcx, impl_data.impl_def_id)?
|
||||
.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_normalized(tcx);
|
||||
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)
|
||||
});
|
||||
|
||||
// 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
|
||||
// `rustc_trait_selection::traits::project::assemble_candidates_from_impls()`.
|
||||
let eligible = if leaf_def.is_final() {
|
||||
// Non-specializable items are always projectable.
|
||||
true
|
||||
} else {
|
||||
// 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.
|
||||
if param_env.reveal() == Reveal::All {
|
||||
!trait_ref.still_further_specializable()
|
||||
} else {
|
||||
false
|
||||
}
|
||||
};
|
||||
|
||||
if !eligible {
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
let substs = tcx.erase_regions(substs);
|
||||
|
||||
// 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))
|
||||
}
|
||||
traits::ImplSource::Generator(generator_data) => Some(Instance {
|
||||
def: ty::InstanceDef::Item(ty::WithOptConstParam::unknown(
|
||||
generator_data.generator_def_id,
|
||||
)),
|
||||
substs: generator_data.substs,
|
||||
}),
|
||||
traits::ImplSource::Closure(closure_data) => {
|
||||
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,
|
||||
))
|
||||
}
|
||||
traits::ImplSource::FnPointer(ref data) => match data.fn_ty.kind() {
|
||||
ty::FnDef(..) | ty::FnPtr(..) => Some(Instance {
|
||||
def: ty::InstanceDef::FnPtrShim(trait_item.def_id, data.fn_ty),
|
||||
substs: rcvr_substs,
|
||||
}),
|
||||
_ => None,
|
||||
},
|
||||
traits::ImplSource::Object(ref data) => {
|
||||
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 })
|
||||
}
|
||||
traits::ImplSource::Builtin(..) => {
|
||||
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();
|
||||
|
||||
let is_copy = self_ty.is_copy_modulo_regions(tcx.at(DUMMY_SP), param_env);
|
||||
match self_ty.kind() {
|
||||
_ if is_copy => (),
|
||||
ty::Array(..) | ty::Closure(..) | ty::Tuple(..) => {}
|
||||
_ => return Ok(None),
|
||||
};
|
||||
|
||||
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))
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
traits::ImplSource::AutoImpl(..)
|
||||
| traits::ImplSource::Param(..)
|
||||
| traits::ImplSource::TraitAlias(..)
|
||||
| traits::ImplSource::DiscriminantKind(..) => None,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn provide(providers: &mut ty::query::Providers) {
|
||||
*providers =
|
||||
ty::query::Providers { resolve_instance, resolve_instance_of_const_arg, ..*providers };
|
||||
}
|
28
compiler/rustc_ty_utils/src/lib.rs
Normal file
28
compiler/rustc_ty_utils/src/lib.rs
Normal file
|
@ -0,0 +1,28 @@
|
|||
//! Various checks
|
||||
//!
|
||||
//! # Note
|
||||
//!
|
||||
//! This API is completely unstable and subject to change.
|
||||
|
||||
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
|
||||
#![feature(nll)]
|
||||
#![recursion_limit = "256"]
|
||||
|
||||
#[macro_use]
|
||||
extern crate rustc_middle;
|
||||
#[macro_use]
|
||||
extern crate tracing;
|
||||
|
||||
use rustc_middle::ty::query::Providers;
|
||||
|
||||
mod common_traits;
|
||||
pub mod instance;
|
||||
mod needs_drop;
|
||||
mod ty;
|
||||
|
||||
pub fn provide(providers: &mut Providers) {
|
||||
common_traits::provide(providers);
|
||||
needs_drop::provide(providers);
|
||||
ty::provide(providers);
|
||||
instance::provide(providers);
|
||||
}
|
188
compiler/rustc_ty_utils/src/needs_drop.rs
Normal file
188
compiler/rustc_ty_utils/src/needs_drop.rs
Normal file
|
@ -0,0 +1,188 @@
|
|||
//! Check whether a type has (potentially) non-trivial drop glue.
|
||||
|
||||
use rustc_data_structures::fx::FxHashSet;
|
||||
use rustc_hir::def_id::DefId;
|
||||
use rustc_middle::ty::subst::Subst;
|
||||
use rustc_middle::ty::util::{needs_drop_components, AlwaysRequiresDrop};
|
||||
use rustc_middle::ty::{self, Ty, TyCtxt};
|
||||
use rustc_session::Limit;
|
||||
use rustc_span::DUMMY_SP;
|
||||
|
||||
type NeedsDropResult<T> = Result<T, AlwaysRequiresDrop>;
|
||||
|
||||
fn needs_drop_raw<'tcx>(tcx: TyCtxt<'tcx>, query: ty::ParamEnvAnd<'tcx, Ty<'tcx>>) -> bool {
|
||||
let adt_fields =
|
||||
move |adt_def: &ty::AdtDef| tcx.adt_drop_tys(adt_def.did).map(|tys| tys.iter());
|
||||
// If we don't know a type doesn't need drop, for example if it's a type
|
||||
// parameter without a `Copy` bound, then we conservatively return that it
|
||||
// needs drop.
|
||||
let res = NeedsDropTypes::new(tcx, query.param_env, query.value, adt_fields).next().is_some();
|
||||
debug!("needs_drop_raw({:?}) = {:?}", query, res);
|
||||
res
|
||||
}
|
||||
|
||||
struct NeedsDropTypes<'tcx, F> {
|
||||
tcx: TyCtxt<'tcx>,
|
||||
param_env: ty::ParamEnv<'tcx>,
|
||||
query_ty: Ty<'tcx>,
|
||||
seen_tys: FxHashSet<Ty<'tcx>>,
|
||||
/// A stack of types left to process, and the recursion depth when we
|
||||
/// pushed that type. Each round, we pop something from the stack and check
|
||||
/// if it needs drop. If the result depends on whether some other types
|
||||
/// need drop we push them onto the stack.
|
||||
unchecked_tys: Vec<(Ty<'tcx>, usize)>,
|
||||
recursion_limit: Limit,
|
||||
adt_components: F,
|
||||
}
|
||||
|
||||
impl<'tcx, F> NeedsDropTypes<'tcx, F> {
|
||||
fn new(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
param_env: ty::ParamEnv<'tcx>,
|
||||
ty: Ty<'tcx>,
|
||||
adt_components: F,
|
||||
) -> Self {
|
||||
let mut seen_tys = FxHashSet::default();
|
||||
seen_tys.insert(ty);
|
||||
Self {
|
||||
tcx,
|
||||
param_env,
|
||||
seen_tys,
|
||||
query_ty: ty,
|
||||
unchecked_tys: vec![(ty, 0)],
|
||||
recursion_limit: tcx.sess.recursion_limit(),
|
||||
adt_components,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx, F, I> Iterator for NeedsDropTypes<'tcx, F>
|
||||
where
|
||||
F: Fn(&ty::AdtDef) -> NeedsDropResult<I>,
|
||||
I: Iterator<Item = Ty<'tcx>>,
|
||||
{
|
||||
type Item = NeedsDropResult<Ty<'tcx>>;
|
||||
|
||||
fn next(&mut self) -> Option<NeedsDropResult<Ty<'tcx>>> {
|
||||
let tcx = self.tcx;
|
||||
|
||||
while let Some((ty, level)) = self.unchecked_tys.pop() {
|
||||
if !self.recursion_limit.value_within_limit(level) {
|
||||
// Not having a `Span` isn't great. But there's hopefully some other
|
||||
// recursion limit error as well.
|
||||
tcx.sess.span_err(
|
||||
DUMMY_SP,
|
||||
&format!("overflow while checking whether `{}` requires drop", self.query_ty),
|
||||
);
|
||||
return Some(Err(AlwaysRequiresDrop));
|
||||
}
|
||||
|
||||
let components = match needs_drop_components(ty, &tcx.data_layout) {
|
||||
Err(e) => return Some(Err(e)),
|
||||
Ok(components) => components,
|
||||
};
|
||||
debug!("needs_drop_components({:?}) = {:?}", ty, components);
|
||||
|
||||
let queue_type = move |this: &mut Self, component: Ty<'tcx>| {
|
||||
if this.seen_tys.insert(component) {
|
||||
this.unchecked_tys.push((component, level + 1));
|
||||
}
|
||||
};
|
||||
|
||||
for component in components {
|
||||
match *component.kind() {
|
||||
_ if component.is_copy_modulo_regions(tcx.at(DUMMY_SP), self.param_env) => (),
|
||||
|
||||
ty::Closure(_, substs) => {
|
||||
for upvar_ty in substs.as_closure().upvar_tys() {
|
||||
queue_type(self, upvar_ty);
|
||||
}
|
||||
}
|
||||
|
||||
ty::Generator(def_id, substs, _) => {
|
||||
let substs = substs.as_generator();
|
||||
for upvar_ty in substs.upvar_tys() {
|
||||
queue_type(self, upvar_ty);
|
||||
}
|
||||
|
||||
let witness = substs.witness();
|
||||
let interior_tys = match witness.kind() {
|
||||
&ty::GeneratorWitness(tys) => tcx.erase_late_bound_regions(tys),
|
||||
_ => {
|
||||
tcx.sess.delay_span_bug(
|
||||
tcx.hir().span_if_local(def_id).unwrap_or(DUMMY_SP),
|
||||
&format!("unexpected generator witness type {:?}", witness),
|
||||
);
|
||||
return Some(Err(AlwaysRequiresDrop));
|
||||
}
|
||||
};
|
||||
|
||||
for interior_ty in interior_tys {
|
||||
queue_type(self, interior_ty);
|
||||
}
|
||||
}
|
||||
|
||||
// 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) {
|
||||
Err(e) => return Some(Err(e)),
|
||||
Ok(tys) => tys,
|
||||
};
|
||||
for required_ty in tys {
|
||||
let subst_ty = tcx.normalize_erasing_regions(
|
||||
self.param_env,
|
||||
required_ty.subst(tcx, substs),
|
||||
);
|
||||
queue_type(self, subst_ty);
|
||||
}
|
||||
}
|
||||
ty::Array(..) | ty::Opaque(..) | ty::Projection(..) | ty::Param(_) => {
|
||||
if ty == component {
|
||||
// Return the type to the caller: they may be able
|
||||
// to normalize further than we can.
|
||||
return Some(Ok(component));
|
||||
} else {
|
||||
// Store the type for later. We can't return here
|
||||
// because we would then lose any other components
|
||||
// of the type.
|
||||
queue_type(self, component);
|
||||
}
|
||||
}
|
||||
_ => return Some(Err(AlwaysRequiresDrop)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn adt_drop_tys(tcx: TyCtxt<'_>, def_id: DefId) -> Result<&ty::List<Ty<'_>>, AlwaysRequiresDrop> {
|
||||
let adt_components = move |adt_def: &ty::AdtDef| {
|
||||
if adt_def.is_manually_drop() {
|
||||
debug!("adt_drop_tys: `{:?}` is manually drop", adt_def);
|
||||
return Ok(Vec::new().into_iter());
|
||||
} else if adt_def.destructor(tcx).is_some() {
|
||||
debug!("adt_drop_tys: `{:?}` implements `Drop`", adt_def);
|
||||
return Err(AlwaysRequiresDrop);
|
||||
} else if adt_def.is_union() {
|
||||
debug!("adt_drop_tys: `{:?}` is a union", adt_def);
|
||||
return Ok(Vec::new().into_iter());
|
||||
}
|
||||
Ok(adt_def.all_fields().map(|field| tcx.type_of(field.did)).collect::<Vec<_>>().into_iter())
|
||||
};
|
||||
|
||||
let adt_ty = tcx.type_of(def_id);
|
||||
let param_env = tcx.param_env(def_id);
|
||||
let res: Result<Vec<_>, _> =
|
||||
NeedsDropTypes::new(tcx, param_env, adt_ty, adt_components).collect();
|
||||
|
||||
debug!("adt_drop_tys(`{}`) = `{:?}`", tcx.def_path_str(def_id), res);
|
||||
res.map(|components| tcx.intern_type_list(&components))
|
||||
}
|
||||
|
||||
pub(crate) fn provide(providers: &mut ty::query::Providers) {
|
||||
*providers = ty::query::Providers { needs_drop_raw, adt_drop_tys, ..*providers };
|
||||
}
|
505
compiler/rustc_ty_utils/src/ty.rs
Normal file
505
compiler/rustc_ty_utils/src/ty.rs
Normal file
|
@ -0,0 +1,505 @@
|
|||
use rustc_data_structures::fx::FxIndexSet;
|
||||
use rustc_data_structures::svh::Svh;
|
||||
use rustc_hir as hir;
|
||||
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE};
|
||||
use rustc_middle::hir::map as hir_map;
|
||||
use rustc_middle::ty::subst::Subst;
|
||||
use rustc_middle::ty::{
|
||||
self, Binder, Predicate, PredicateAtom, PredicateKind, ToPredicate, Ty, TyCtxt, WithConstness,
|
||||
};
|
||||
use rustc_session::CrateDisambiguator;
|
||||
use rustc_span::symbol::Symbol;
|
||||
use rustc_span::Span;
|
||||
use rustc_trait_selection::traits;
|
||||
|
||||
fn sized_constraint_for_ty<'tcx>(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
adtdef: &ty::AdtDef,
|
||||
ty: Ty<'tcx>,
|
||||
) -> Vec<Ty<'tcx>> {
|
||||
use ty::TyKind::*;
|
||||
|
||||
let result = match ty.kind() {
|
||||
Bool | Char | Int(..) | Uint(..) | Float(..) | RawPtr(..) | Ref(..) | FnDef(..)
|
||||
| FnPtr(_) | Array(..) | Closure(..) | Generator(..) | Never => vec![],
|
||||
|
||||
Str | Dynamic(..) | Slice(_) | Foreign(..) | Error(_) | GeneratorWitness(..) => {
|
||||
// these are never sized - return the target type
|
||||
vec![ty]
|
||||
}
|
||||
|
||||
Tuple(ref tys) => match tys.last() {
|
||||
None => vec![],
|
||||
Some(ty) => sized_constraint_for_ty(tcx, adtdef, ty.expect_ty()),
|
||||
},
|
||||
|
||||
Adt(adt, substs) => {
|
||||
// recursive case
|
||||
let adt_tys = adt.sized_constraint(tcx);
|
||||
debug!("sized_constraint_for_ty({:?}) intermediate = {:?}", ty, adt_tys);
|
||||
adt_tys
|
||||
.iter()
|
||||
.map(|ty| ty.subst(tcx, substs))
|
||||
.flat_map(|ty| sized_constraint_for_ty(tcx, adtdef, ty))
|
||||
.collect()
|
||||
}
|
||||
|
||||
Projection(..) | Opaque(..) => {
|
||||
// must calculate explicitly.
|
||||
// FIXME: consider special-casing always-Sized projections
|
||||
vec![ty]
|
||||
}
|
||||
|
||||
Param(..) => {
|
||||
// perf hack: if there is a `T: Sized` bound, then
|
||||
// we know that `T` is Sized and do not need to check
|
||||
// it on the impl.
|
||||
|
||||
let sized_trait = match tcx.lang_items().sized_trait() {
|
||||
Some(x) => x,
|
||||
_ => return vec![ty],
|
||||
};
|
||||
let sized_predicate = ty::Binder::dummy(ty::TraitRef {
|
||||
def_id: sized_trait,
|
||||
substs: tcx.mk_substs_trait(ty, &[]),
|
||||
})
|
||||
.without_const()
|
||||
.to_predicate(tcx);
|
||||
let predicates = tcx.predicates_of(adtdef.did).predicates;
|
||||
if predicates.iter().any(|(p, _)| *p == sized_predicate) { vec![] } else { vec![ty] }
|
||||
}
|
||||
|
||||
Placeholder(..) | Bound(..) | Infer(..) => {
|
||||
bug!("unexpected type `{:?}` in sized_constraint_for_ty", ty)
|
||||
}
|
||||
};
|
||||
debug!("sized_constraint_for_ty({:?}) = {:?}", ty, result);
|
||||
result
|
||||
}
|
||||
|
||||
fn associated_item_from_trait_item_ref(
|
||||
tcx: TyCtxt<'_>,
|
||||
parent_def_id: LocalDefId,
|
||||
trait_item_ref: &hir::TraitItemRef,
|
||||
) -> ty::AssocItem {
|
||||
let def_id = tcx.hir().local_def_id(trait_item_ref.id.hir_id);
|
||||
let (kind, has_self) = match trait_item_ref.kind {
|
||||
hir::AssocItemKind::Const => (ty::AssocKind::Const, false),
|
||||
hir::AssocItemKind::Fn { has_self } => (ty::AssocKind::Fn, has_self),
|
||||
hir::AssocItemKind::Type => (ty::AssocKind::Type, false),
|
||||
};
|
||||
|
||||
ty::AssocItem {
|
||||
ident: trait_item_ref.ident,
|
||||
kind,
|
||||
vis: tcx.visibility(def_id),
|
||||
defaultness: trait_item_ref.defaultness,
|
||||
def_id: def_id.to_def_id(),
|
||||
container: ty::TraitContainer(parent_def_id.to_def_id()),
|
||||
fn_has_self_parameter: has_self,
|
||||
}
|
||||
}
|
||||
|
||||
fn associated_item_from_impl_item_ref(
|
||||
tcx: TyCtxt<'_>,
|
||||
parent_def_id: LocalDefId,
|
||||
impl_item_ref: &hir::ImplItemRef<'_>,
|
||||
) -> ty::AssocItem {
|
||||
let def_id = tcx.hir().local_def_id(impl_item_ref.id.hir_id);
|
||||
let (kind, has_self) = match impl_item_ref.kind {
|
||||
hir::AssocItemKind::Const => (ty::AssocKind::Const, false),
|
||||
hir::AssocItemKind::Fn { has_self } => (ty::AssocKind::Fn, has_self),
|
||||
hir::AssocItemKind::Type => (ty::AssocKind::Type, false),
|
||||
};
|
||||
|
||||
ty::AssocItem {
|
||||
ident: impl_item_ref.ident,
|
||||
kind,
|
||||
vis: tcx.visibility(def_id),
|
||||
defaultness: impl_item_ref.defaultness,
|
||||
def_id: def_id.to_def_id(),
|
||||
container: ty::ImplContainer(parent_def_id.to_def_id()),
|
||||
fn_has_self_parameter: has_self,
|
||||
}
|
||||
}
|
||||
|
||||
fn associated_item(tcx: TyCtxt<'_>, def_id: DefId) -> ty::AssocItem {
|
||||
let id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
|
||||
let parent_id = tcx.hir().get_parent_item(id);
|
||||
let parent_def_id = tcx.hir().local_def_id(parent_id);
|
||||
let parent_item = tcx.hir().expect_item(parent_id);
|
||||
match parent_item.kind {
|
||||
hir::ItemKind::Impl { ref items, .. } => {
|
||||
if let Some(impl_item_ref) = items.iter().find(|i| i.id.hir_id == id) {
|
||||
let assoc_item =
|
||||
associated_item_from_impl_item_ref(tcx, parent_def_id, impl_item_ref);
|
||||
debug_assert_eq!(assoc_item.def_id, def_id);
|
||||
return assoc_item;
|
||||
}
|
||||
}
|
||||
|
||||
hir::ItemKind::Trait(.., ref trait_item_refs) => {
|
||||
if let Some(trait_item_ref) = trait_item_refs.iter().find(|i| i.id.hir_id == id) {
|
||||
let assoc_item =
|
||||
associated_item_from_trait_item_ref(tcx, parent_def_id, trait_item_ref);
|
||||
debug_assert_eq!(assoc_item.def_id, def_id);
|
||||
return assoc_item;
|
||||
}
|
||||
}
|
||||
|
||||
_ => {}
|
||||
}
|
||||
|
||||
span_bug!(
|
||||
parent_item.span,
|
||||
"unexpected parent of trait or impl item or item not found: {:?}",
|
||||
parent_item.kind
|
||||
)
|
||||
}
|
||||
|
||||
fn impl_defaultness(tcx: TyCtxt<'_>, def_id: DefId) -> hir::Defaultness {
|
||||
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
|
||||
let item = tcx.hir().expect_item(hir_id);
|
||||
if let hir::ItemKind::Impl { defaultness, .. } = item.kind {
|
||||
defaultness
|
||||
} else {
|
||||
bug!("`impl_defaultness` called on {:?}", item);
|
||||
}
|
||||
}
|
||||
|
||||
/// Calculates the `Sized` constraint.
|
||||
///
|
||||
/// In fact, there are only a few options for the types in the constraint:
|
||||
/// - an obviously-unsized type
|
||||
/// - a type parameter or projection whose Sizedness can't be known
|
||||
/// - a tuple of type parameters or projections, if there are multiple
|
||||
/// such.
|
||||
/// - a Error, if a type contained itself. The representability
|
||||
/// check should catch this case.
|
||||
fn adt_sized_constraint(tcx: TyCtxt<'_>, def_id: DefId) -> ty::AdtSizedConstraint<'_> {
|
||||
let def = tcx.adt_def(def_id);
|
||||
|
||||
let result = tcx.mk_type_list(
|
||||
def.variants
|
||||
.iter()
|
||||
.flat_map(|v| v.fields.last())
|
||||
.flat_map(|f| sized_constraint_for_ty(tcx, def, tcx.type_of(f.did))),
|
||||
);
|
||||
|
||||
debug!("adt_sized_constraint: {:?} => {:?}", def, result);
|
||||
|
||||
ty::AdtSizedConstraint(result)
|
||||
}
|
||||
|
||||
fn associated_item_def_ids(tcx: TyCtxt<'_>, def_id: DefId) -> &[DefId] {
|
||||
let id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
|
||||
let item = tcx.hir().expect_item(id);
|
||||
match item.kind {
|
||||
hir::ItemKind::Trait(.., ref trait_item_refs) => tcx.arena.alloc_from_iter(
|
||||
trait_item_refs
|
||||
.iter()
|
||||
.map(|trait_item_ref| trait_item_ref.id)
|
||||
.map(|id| tcx.hir().local_def_id(id.hir_id).to_def_id()),
|
||||
),
|
||||
hir::ItemKind::Impl { ref items, .. } => tcx.arena.alloc_from_iter(
|
||||
items
|
||||
.iter()
|
||||
.map(|impl_item_ref| impl_item_ref.id)
|
||||
.map(|id| tcx.hir().local_def_id(id.hir_id).to_def_id()),
|
||||
),
|
||||
hir::ItemKind::TraitAlias(..) => &[],
|
||||
_ => span_bug!(item.span, "associated_item_def_ids: not impl or trait"),
|
||||
}
|
||||
}
|
||||
|
||||
fn associated_items(tcx: TyCtxt<'_>, def_id: DefId) -> ty::AssociatedItems<'_> {
|
||||
let items = tcx.associated_item_def_ids(def_id).iter().map(|did| tcx.associated_item(*did));
|
||||
ty::AssociatedItems::new(items)
|
||||
}
|
||||
|
||||
fn def_span(tcx: TyCtxt<'_>, def_id: DefId) -> Span {
|
||||
tcx.hir().span_if_local(def_id).unwrap()
|
||||
}
|
||||
|
||||
/// If the given `DefId` describes an item belonging to a trait,
|
||||
/// returns the `DefId` of the trait that the trait item belongs to;
|
||||
/// otherwise, returns `None`.
|
||||
fn trait_of_item(tcx: TyCtxt<'_>, def_id: DefId) -> Option<DefId> {
|
||||
tcx.opt_associated_item(def_id).and_then(|associated_item| match associated_item.container {
|
||||
ty::TraitContainer(def_id) => Some(def_id),
|
||||
ty::ImplContainer(_) => None,
|
||||
})
|
||||
}
|
||||
|
||||
/// See `ParamEnv` struct definition for details.
|
||||
fn param_env(tcx: TyCtxt<'_>, def_id: DefId) -> ty::ParamEnv<'_> {
|
||||
// The param_env of an impl Trait type is its defining function's param_env
|
||||
if let Some(parent) = ty::is_impl_trait_defn(tcx, def_id) {
|
||||
return param_env(tcx, parent);
|
||||
}
|
||||
// Compute the bounds on Self and the type parameters.
|
||||
|
||||
let ty::InstantiatedPredicates { mut predicates, .. } =
|
||||
tcx.predicates_of(def_id).instantiate_identity(tcx);
|
||||
|
||||
// Finally, we have to normalize the bounds in the environment, in
|
||||
// case they contain any associated type projections. This process
|
||||
// can yield errors if the put in illegal associated types, like
|
||||
// `<i32 as Foo>::Bar` where `i32` does not implement `Foo`. We
|
||||
// report these errors right here; this doesn't actually feel
|
||||
// right to me, because constructing the environment feels like a
|
||||
// kind of a "idempotent" action, but I'm not sure where would be
|
||||
// a better place. In practice, we construct environments for
|
||||
// every fn once during type checking, and we'll abort if there
|
||||
// are any errors at that point, so after type checking you can be
|
||||
// sure that this will succeed without errors anyway.
|
||||
|
||||
if tcx.sess.opts.debugging_opts.chalk {
|
||||
let environment = well_formed_types_in_env(tcx, def_id);
|
||||
predicates.extend(environment);
|
||||
}
|
||||
|
||||
let unnormalized_env =
|
||||
ty::ParamEnv::new(tcx.intern_predicates(&predicates), traits::Reveal::UserFacing);
|
||||
|
||||
let body_id = def_id
|
||||
.as_local()
|
||||
.map(|def_id| tcx.hir().local_def_id_to_hir_id(def_id))
|
||||
.map_or(hir::CRATE_HIR_ID, |id| {
|
||||
tcx.hir().maybe_body_owned_by(id).map_or(id, |body| body.hir_id)
|
||||
});
|
||||
let cause = traits::ObligationCause::misc(tcx.def_span(def_id), body_id);
|
||||
traits::normalize_param_env_or_error(tcx, def_id, unnormalized_env, cause)
|
||||
}
|
||||
|
||||
/// Elaborate the environment.
|
||||
///
|
||||
/// Collect a list of `Predicate`'s used for building the `ParamEnv`. Adds `TypeWellFormedFromEnv`'s
|
||||
/// that are assumed to be well-formed (because they come from the environment).
|
||||
///
|
||||
/// Used only in chalk mode.
|
||||
fn well_formed_types_in_env<'tcx>(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
def_id: DefId,
|
||||
) -> &'tcx ty::List<Predicate<'tcx>> {
|
||||
use rustc_hir::{ForeignItemKind, ImplItemKind, ItemKind, Node, TraitItemKind};
|
||||
use rustc_middle::ty::subst::GenericArgKind;
|
||||
|
||||
debug!("environment(def_id = {:?})", def_id);
|
||||
|
||||
// The environment of an impl Trait type is its defining function's environment.
|
||||
if let Some(parent) = ty::is_impl_trait_defn(tcx, def_id) {
|
||||
return well_formed_types_in_env(tcx, parent);
|
||||
}
|
||||
|
||||
// Compute the bounds on `Self` and the type parameters.
|
||||
let ty::InstantiatedPredicates { predicates, .. } =
|
||||
tcx.predicates_of(def_id).instantiate_identity(tcx);
|
||||
|
||||
let clauses = predicates.into_iter();
|
||||
|
||||
if !def_id.is_local() {
|
||||
return ty::List::empty();
|
||||
}
|
||||
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
|
||||
let node = tcx.hir().get(hir_id);
|
||||
|
||||
enum NodeKind {
|
||||
TraitImpl,
|
||||
InherentImpl,
|
||||
Fn,
|
||||
Other,
|
||||
};
|
||||
|
||||
let node_kind = match node {
|
||||
Node::TraitItem(item) => match item.kind {
|
||||
TraitItemKind::Fn(..) => NodeKind::Fn,
|
||||
_ => NodeKind::Other,
|
||||
},
|
||||
|
||||
Node::ImplItem(item) => match item.kind {
|
||||
ImplItemKind::Fn(..) => NodeKind::Fn,
|
||||
_ => NodeKind::Other,
|
||||
},
|
||||
|
||||
Node::Item(item) => match item.kind {
|
||||
ItemKind::Impl { of_trait: Some(_), .. } => NodeKind::TraitImpl,
|
||||
ItemKind::Impl { of_trait: None, .. } => NodeKind::InherentImpl,
|
||||
ItemKind::Fn(..) => NodeKind::Fn,
|
||||
_ => NodeKind::Other,
|
||||
},
|
||||
|
||||
Node::ForeignItem(item) => match item.kind {
|
||||
ForeignItemKind::Fn(..) => NodeKind::Fn,
|
||||
_ => NodeKind::Other,
|
||||
},
|
||||
|
||||
// FIXME: closures?
|
||||
_ => NodeKind::Other,
|
||||
};
|
||||
|
||||
// FIXME(eddyb) isn't the unordered nature of this a hazard?
|
||||
let mut inputs = FxIndexSet::default();
|
||||
|
||||
match node_kind {
|
||||
// In a trait impl, we assume that the header trait ref and all its
|
||||
// constituents are well-formed.
|
||||
NodeKind::TraitImpl => {
|
||||
let trait_ref = tcx.impl_trait_ref(def_id).expect("not an impl");
|
||||
|
||||
// FIXME(chalk): this has problems because of late-bound regions
|
||||
//inputs.extend(trait_ref.substs.iter().flat_map(|arg| arg.walk()));
|
||||
inputs.extend(trait_ref.substs.iter());
|
||||
}
|
||||
|
||||
// In an inherent impl, we assume that the receiver type and all its
|
||||
// constituents are well-formed.
|
||||
NodeKind::InherentImpl => {
|
||||
let self_ty = tcx.type_of(def_id);
|
||||
inputs.extend(self_ty.walk());
|
||||
}
|
||||
|
||||
// In an fn, we assume that the arguments and all their constituents are
|
||||
// well-formed.
|
||||
NodeKind::Fn => {
|
||||
let fn_sig = tcx.fn_sig(def_id);
|
||||
let fn_sig = tcx.liberate_late_bound_regions(def_id, fn_sig);
|
||||
|
||||
inputs.extend(fn_sig.inputs().iter().flat_map(|ty| ty.walk()));
|
||||
}
|
||||
|
||||
NodeKind::Other => (),
|
||||
}
|
||||
let input_clauses = inputs.into_iter().filter_map(|arg| {
|
||||
match arg.unpack() {
|
||||
GenericArgKind::Type(ty) => {
|
||||
let binder = Binder::dummy(PredicateAtom::TypeWellFormedFromEnv(ty));
|
||||
Some(tcx.mk_predicate(PredicateKind::ForAll(binder)))
|
||||
}
|
||||
|
||||
// FIXME(eddyb) no WF conditions from lifetimes?
|
||||
GenericArgKind::Lifetime(_) => None,
|
||||
|
||||
// FIXME(eddyb) support const generics in Chalk
|
||||
GenericArgKind::Const(_) => None,
|
||||
}
|
||||
});
|
||||
|
||||
tcx.mk_predicates(clauses.chain(input_clauses))
|
||||
}
|
||||
|
||||
fn param_env_reveal_all_normalized(tcx: TyCtxt<'_>, def_id: DefId) -> ty::ParamEnv<'_> {
|
||||
tcx.param_env(def_id).with_reveal_all_normalized(tcx)
|
||||
}
|
||||
|
||||
fn crate_disambiguator(tcx: TyCtxt<'_>, crate_num: CrateNum) -> CrateDisambiguator {
|
||||
assert_eq!(crate_num, LOCAL_CRATE);
|
||||
tcx.sess.local_crate_disambiguator()
|
||||
}
|
||||
|
||||
fn original_crate_name(tcx: TyCtxt<'_>, crate_num: CrateNum) -> Symbol {
|
||||
assert_eq!(crate_num, LOCAL_CRATE);
|
||||
tcx.crate_name
|
||||
}
|
||||
|
||||
fn crate_hash(tcx: TyCtxt<'_>, crate_num: CrateNum) -> Svh {
|
||||
tcx.index_hir(crate_num).crate_hash
|
||||
}
|
||||
|
||||
fn instance_def_size_estimate<'tcx>(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
instance_def: ty::InstanceDef<'tcx>,
|
||||
) -> usize {
|
||||
use ty::InstanceDef;
|
||||
|
||||
match instance_def {
|
||||
InstanceDef::Item(..) | InstanceDef::DropGlue(..) => {
|
||||
let mir = tcx.instance_mir(instance_def);
|
||||
mir.basic_blocks().iter().map(|bb| bb.statements.len()).sum()
|
||||
}
|
||||
// Estimate the size of other compiler-generated shims to be 1.
|
||||
_ => 1,
|
||||
}
|
||||
}
|
||||
|
||||
/// If `def_id` is an issue 33140 hack impl, returns its self type; otherwise, returns `None`.
|
||||
///
|
||||
/// See [`ty::ImplOverlapKind::Issue33140`] for more details.
|
||||
fn issue33140_self_ty(tcx: TyCtxt<'_>, def_id: DefId) -> Option<Ty<'_>> {
|
||||
debug!("issue33140_self_ty({:?})", def_id);
|
||||
|
||||
let trait_ref = tcx
|
||||
.impl_trait_ref(def_id)
|
||||
.unwrap_or_else(|| bug!("issue33140_self_ty called on inherent impl {:?}", def_id));
|
||||
|
||||
debug!("issue33140_self_ty({:?}), trait-ref={:?}", def_id, trait_ref);
|
||||
|
||||
let is_marker_like = tcx.impl_polarity(def_id) == ty::ImplPolarity::Positive
|
||||
&& tcx.associated_item_def_ids(trait_ref.def_id).is_empty();
|
||||
|
||||
// Check whether these impls would be ok for a marker trait.
|
||||
if !is_marker_like {
|
||||
debug!("issue33140_self_ty - not marker-like!");
|
||||
return None;
|
||||
}
|
||||
|
||||
// impl must be `impl Trait for dyn Marker1 + Marker2 + ...`
|
||||
if trait_ref.substs.len() != 1 {
|
||||
debug!("issue33140_self_ty - impl has substs!");
|
||||
return None;
|
||||
}
|
||||
|
||||
let predicates = tcx.predicates_of(def_id);
|
||||
if predicates.parent.is_some() || !predicates.predicates.is_empty() {
|
||||
debug!("issue33140_self_ty - impl has predicates {:?}!", predicates);
|
||||
return None;
|
||||
}
|
||||
|
||||
let self_ty = trait_ref.self_ty();
|
||||
let self_ty_matches = match self_ty.kind() {
|
||||
ty::Dynamic(ref data, ty::ReStatic) => data.principal().is_none(),
|
||||
_ => false,
|
||||
};
|
||||
|
||||
if self_ty_matches {
|
||||
debug!("issue33140_self_ty - MATCHES!");
|
||||
Some(self_ty)
|
||||
} else {
|
||||
debug!("issue33140_self_ty - non-matching self type");
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Check if a function is async.
|
||||
fn asyncness(tcx: TyCtxt<'_>, def_id: DefId) -> hir::IsAsync {
|
||||
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
|
||||
|
||||
let node = tcx.hir().get(hir_id);
|
||||
|
||||
let fn_like = hir_map::blocks::FnLikeNode::from_node(node).unwrap_or_else(|| {
|
||||
bug!("asyncness: expected fn-like node but got `{:?}`", def_id);
|
||||
});
|
||||
|
||||
fn_like.asyncness()
|
||||
}
|
||||
|
||||
pub fn provide(providers: &mut ty::query::Providers) {
|
||||
*providers = ty::query::Providers {
|
||||
asyncness,
|
||||
associated_item,
|
||||
associated_item_def_ids,
|
||||
associated_items,
|
||||
adt_sized_constraint,
|
||||
def_span,
|
||||
param_env,
|
||||
param_env_reveal_all_normalized,
|
||||
trait_of_item,
|
||||
crate_disambiguator,
|
||||
original_crate_name,
|
||||
crate_hash,
|
||||
instance_def_size_estimate,
|
||||
issue33140_self_ty,
|
||||
impl_defaultness,
|
||||
..*providers
|
||||
};
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue