2020-09-01 10:58:34 -05:00
|
|
|
use rustc_data_structures::fx::FxIndexSet;
|
2020-01-01 12:10:11 -06:00
|
|
|
use rustc_data_structures::svh::Svh;
|
|
|
|
use rustc_hir as hir;
|
2020-04-09 03:43:00 -05:00
|
|
|
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE};
|
2020-03-29 10:19:48 -05:00
|
|
|
use rustc_middle::hir::map as hir_map;
|
2020-06-23 12:18:06 -05:00
|
|
|
use rustc_middle::ty::subst::Subst;
|
2020-09-01 10:58:34 -05:00
|
|
|
use rustc_middle::ty::{
|
|
|
|
self, Binder, Predicate, PredicateAtom, PredicateKind, ToPredicate, Ty, TyCtxt, WithConstness,
|
|
|
|
};
|
2020-03-11 06:49:08 -05:00
|
|
|
use rustc_session::CrateDisambiguator;
|
2020-01-01 12:10:11 -06:00
|
|
|
use rustc_span::symbol::Symbol;
|
|
|
|
use rustc_span::Span;
|
2020-02-11 14:19:40 -06:00
|
|
|
use rustc_trait_selection::traits;
|
2020-01-01 12:10:11 -06:00
|
|
|
|
2020-01-30 14:25:39 -06:00
|
|
|
fn sized_constraint_for_ty<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
adtdef: &ty::AdtDef,
|
|
|
|
ty: Ty<'tcx>,
|
|
|
|
) -> Vec<Ty<'tcx>> {
|
2020-01-01 12:10:11 -06:00
|
|
|
use ty::TyKind::*;
|
|
|
|
|
2020-08-02 17:49:11 -05:00
|
|
|
let result = match ty.kind() {
|
2020-01-01 12:10:11 -06:00
|
|
|
Bool | Char | Int(..) | Uint(..) | Float(..) | RawPtr(..) | Ref(..) | FnDef(..)
|
|
|
|
| FnPtr(_) | Array(..) | Closure(..) | Generator(..) | Never => vec![],
|
|
|
|
|
2020-05-05 23:02:09 -05:00
|
|
|
Str | Dynamic(..) | Slice(_) | Foreign(..) | Error(_) | GeneratorWitness(..) => {
|
2020-01-01 12:10:11 -06:00
|
|
|
// 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, &[]),
|
|
|
|
})
|
2020-01-13 22:30:32 -06:00
|
|
|
.without_const()
|
2020-05-07 05:12:19 -05:00
|
|
|
.to_predicate(tcx);
|
2020-01-01 12:10:11 -06:00
|
|
|
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<'_>,
|
2020-04-09 03:43:00 -05:00
|
|
|
parent_def_id: LocalDefId,
|
2020-01-01 12:10:11 -06:00
|
|
|
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),
|
2020-03-31 21:09:50 -05:00
|
|
|
hir::AssocItemKind::Fn { has_self } => (ty::AssocKind::Fn, has_self),
|
2020-01-01 12:10:11 -06:00
|
|
|
hir::AssocItemKind::Type => (ty::AssocKind::Type, false),
|
|
|
|
};
|
|
|
|
|
|
|
|
ty::AssocItem {
|
|
|
|
ident: trait_item_ref.ident,
|
|
|
|
kind,
|
2020-10-16 18:28:11 -05:00
|
|
|
vis: tcx.visibility(def_id),
|
2020-01-01 12:10:11 -06:00
|
|
|
defaultness: trait_item_ref.defaultness,
|
2020-04-09 03:43:00 -05:00
|
|
|
def_id: def_id.to_def_id(),
|
|
|
|
container: ty::TraitContainer(parent_def_id.to_def_id()),
|
2020-03-31 21:09:50 -05:00
|
|
|
fn_has_self_parameter: has_self,
|
2020-01-01 12:10:11 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn associated_item_from_impl_item_ref(
|
|
|
|
tcx: TyCtxt<'_>,
|
2020-04-09 03:43:00 -05:00
|
|
|
parent_def_id: LocalDefId,
|
2020-01-01 12:10:11 -06:00
|
|
|
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),
|
2020-03-31 21:09:50 -05:00
|
|
|
hir::AssocItemKind::Fn { has_self } => (ty::AssocKind::Fn, has_self),
|
2020-01-01 12:10:11 -06:00
|
|
|
hir::AssocItemKind::Type => (ty::AssocKind::Type, false),
|
|
|
|
};
|
|
|
|
|
|
|
|
ty::AssocItem {
|
|
|
|
ident: impl_item_ref.ident,
|
|
|
|
kind,
|
2020-10-16 18:28:11 -05:00
|
|
|
vis: tcx.visibility(def_id),
|
2020-01-01 12:10:11 -06:00
|
|
|
defaultness: impl_item_ref.defaultness,
|
2020-04-09 03:43:00 -05:00
|
|
|
def_id: def_id.to_def_id(),
|
|
|
|
container: ty::ImplContainer(parent_def_id.to_def_id()),
|
2020-03-31 21:09:50 -05:00
|
|
|
fn_has_self_parameter: has_self,
|
2020-01-01 12:10:11 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn associated_item(tcx: TyCtxt<'_>, def_id: DefId) -> ty::AssocItem {
|
2020-08-12 05:22:56 -05:00
|
|
|
let id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
|
2020-01-01 12:10:11 -06:00
|
|
|
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 {
|
2020-01-17 18:14:29 -06:00
|
|
|
hir::ItemKind::Impl { ref items, .. } => {
|
|
|
|
if let Some(impl_item_ref) = items.iter().find(|i| i.id.hir_id == id) {
|
2020-01-01 12:10:11 -06:00
|
|
|
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) {
|
2020-10-16 18:28:11 -05:00
|
|
|
let assoc_item =
|
|
|
|
associated_item_from_trait_item_ref(tcx, parent_def_id, trait_item_ref);
|
2020-01-01 12:10:11 -06:00
|
|
|
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
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-03-29 13:01:14 -05:00
|
|
|
fn impl_defaultness(tcx: TyCtxt<'_>, def_id: DefId) -> hir::Defaultness {
|
2020-08-12 05:22:56 -05:00
|
|
|
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
|
2020-03-29 13:01:14 -05:00
|
|
|
let item = tcx.hir().expect_item(hir_id);
|
|
|
|
if let hir::ItemKind::Impl { defaultness, .. } = item.kind {
|
|
|
|
defaultness
|
|
|
|
} else {
|
|
|
|
bug!("`impl_defaultness` called on {:?}", item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-01 12:10:11 -06:00
|
|
|
/// 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] {
|
2020-08-12 05:22:56 -05:00
|
|
|
let id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
|
2020-01-01 12:10:11 -06:00
|
|
|
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)
|
2020-04-09 03:43:00 -05:00
|
|
|
.map(|id| tcx.hir().local_def_id(id.hir_id).to_def_id()),
|
2020-01-01 12:10:11 -06:00
|
|
|
),
|
2020-01-17 18:14:29 -06:00
|
|
|
hir::ItemKind::Impl { ref items, .. } => tcx.arena.alloc_from_iter(
|
|
|
|
items
|
2020-01-01 12:10:11 -06:00
|
|
|
.iter()
|
|
|
|
.map(|impl_item_ref| impl_item_ref.id)
|
2020-04-09 03:43:00 -05:00
|
|
|
.map(|id| tcx.hir().local_def_id(id.hir_id).to_def_id()),
|
2020-01-01 12:10:11 -06:00
|
|
|
),
|
|
|
|
hir::ItemKind::TraitAlias(..) => &[],
|
|
|
|
_ => span_bug!(item.span, "associated_item_def_ids: not impl or trait"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-14 17:07:31 -05:00
|
|
|
fn associated_items(tcx: TyCtxt<'_>, def_id: DefId) -> ty::AssociatedItems<'_> {
|
2020-02-19 14:55:59 -06:00
|
|
|
let items = tcx.associated_item_def_ids(def_id).iter().map(|did| tcx.associated_item(*did));
|
2020-03-27 14:26:20 -05:00
|
|
|
ty::AssociatedItems::new(items)
|
2020-02-04 18:43:03 -06:00
|
|
|
}
|
|
|
|
|
2020-01-01 12:10:11 -06:00
|
|
|
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.
|
|
|
|
|
2020-09-01 10:58:34 -05:00
|
|
|
let ty::InstantiatedPredicates { mut predicates, .. } =
|
2020-01-01 12:10:11 -06:00
|
|
|
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.
|
|
|
|
|
2020-09-01 10:58:34 -05:00
|
|
|
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);
|
2020-01-01 12:10:11 -06:00
|
|
|
|
2020-04-12 07:45:41 -05:00
|
|
|
let body_id = def_id
|
|
|
|
.as_local()
|
2020-08-12 05:22:56 -05:00
|
|
|
.map(|def_id| tcx.hir().local_def_id_to_hir_id(def_id))
|
2020-04-12 07:45:41 -05:00
|
|
|
.map_or(hir::CRATE_HIR_ID, |id| {
|
|
|
|
tcx.hir().maybe_body_owned_by(id).map_or(id, |body| body.hir_id)
|
|
|
|
});
|
2020-01-01 12:10:11 -06:00
|
|
|
let cause = traits::ObligationCause::misc(tcx.def_span(def_id), body_id);
|
|
|
|
traits::normalize_param_env_or_error(tcx, def_id, unnormalized_env, cause)
|
|
|
|
}
|
|
|
|
|
2020-09-01 10:58:34 -05:00
|
|
|
/// 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))
|
|
|
|
}
|
|
|
|
|
2020-04-10 23:50:02 -05:00
|
|
|
fn param_env_reveal_all_normalized(tcx: TyCtxt<'_>, def_id: DefId) -> ty::ParamEnv<'_> {
|
|
|
|
tcx.param_env(def_id).with_reveal_all_normalized(tcx)
|
|
|
|
}
|
|
|
|
|
2020-01-01 12:10:11 -06:00
|
|
|
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);
|
2020-01-22 09:30:15 -06:00
|
|
|
tcx.crate_name
|
2020-01-01 12:10:11 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn crate_hash(tcx: TyCtxt<'_>, crate_num: CrateNum) -> Svh {
|
2020-02-09 08:32:00 -06:00
|
|
|
tcx.index_hir(crate_num).crate_hash
|
2020-01-01 12:10:11 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
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`.
|
|
|
|
///
|
2020-05-01 15:28:15 -05:00
|
|
|
/// See [`ty::ImplOverlapKind::Issue33140`] for more details.
|
2020-01-01 12:10:11 -06:00
|
|
|
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();
|
2020-08-02 17:49:11 -05:00
|
|
|
let self_ty_matches = match self_ty.kind() {
|
2020-01-01 12:10:11 -06:00
|
|
|
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 {
|
2020-08-12 05:22:56 -05:00
|
|
|
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
|
2020-01-01 12:10:11 -06:00
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2020-07-05 15:00:14 -05:00
|
|
|
pub fn provide(providers: &mut ty::query::Providers) {
|
2020-01-01 12:10:11 -06:00
|
|
|
*providers = ty::query::Providers {
|
|
|
|
asyncness,
|
|
|
|
associated_item,
|
|
|
|
associated_item_def_ids,
|
2020-02-04 18:43:03 -06:00
|
|
|
associated_items,
|
2020-01-01 12:10:11 -06:00
|
|
|
adt_sized_constraint,
|
|
|
|
def_span,
|
|
|
|
param_env,
|
2020-04-10 23:50:02 -05:00
|
|
|
param_env_reveal_all_normalized,
|
2020-01-01 12:10:11 -06:00
|
|
|
trait_of_item,
|
|
|
|
crate_disambiguator,
|
|
|
|
original_crate_name,
|
|
|
|
crate_hash,
|
|
|
|
instance_def_size_estimate,
|
|
|
|
issue33140_self_ty,
|
2020-03-29 13:01:14 -05:00
|
|
|
impl_defaultness,
|
2020-01-01 12:10:11 -06:00
|
|
|
..*providers
|
|
|
|
};
|
|
|
|
}
|