2023-02-17 17:16:43 +00:00
|
|
|
use rustc_data_structures::fx::{FxHashSet, FxIndexSet};
|
2020-01-01 19:10:11 +01:00
|
|
|
use rustc_hir as hir;
|
2023-02-17 17:16:43 +00:00
|
|
|
use rustc_hir::def::DefKind;
|
2023-01-21 05:43:37 +00:00
|
|
|
use rustc_index::bit_set::BitSet;
|
2023-02-14 15:55:31 -07:00
|
|
|
use rustc_middle::ty::{
|
2023-03-03 12:38:36 -03:00
|
|
|
self, Binder, EarlyBinder, ImplTraitInTraitData, Predicate, PredicateKind, ToPredicate, Ty,
|
|
|
|
TyCtxt, TypeSuperVisitable, TypeVisitable, TypeVisitor,
|
2023-02-14 15:55:31 -07:00
|
|
|
};
|
2023-01-02 23:12:47 +00:00
|
|
|
use rustc_session::config::TraitSolver;
|
2023-01-15 12:58:46 +01:00
|
|
|
use rustc_span::def_id::{DefId, CRATE_DEF_ID};
|
2020-02-11 21:19:40 +01:00
|
|
|
use rustc_trait_selection::traits;
|
2020-01-01 19:10:11 +01:00
|
|
|
|
2020-01-30 20:25:39 +00:00
|
|
|
fn sized_constraint_for_ty<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
2022-03-05 07:28:41 +11:00
|
|
|
adtdef: ty::AdtDef<'tcx>,
|
2020-01-30 20:25:39 +00:00
|
|
|
ty: Ty<'tcx>,
|
|
|
|
) -> Vec<Ty<'tcx>> {
|
2021-01-31 10:32:34 +01:00
|
|
|
use rustc_type_ir::sty::TyKind::*;
|
2020-01-01 19:10:11 +01:00
|
|
|
|
2020-08-03 00:49:11 +02:00
|
|
|
let result = match ty.kind() {
|
2020-01-01 19:10:11 +01:00
|
|
|
Bool | Char | Int(..) | Uint(..) | Float(..) | RawPtr(..) | Ref(..) | FnDef(..)
|
|
|
|
| FnPtr(_) | Array(..) | Closure(..) | Generator(..) | Never => vec![],
|
|
|
|
|
2022-10-01 14:56:24 +02:00
|
|
|
Str
|
|
|
|
| Dynamic(..)
|
|
|
|
| Slice(_)
|
|
|
|
| Foreign(..)
|
|
|
|
| Error(_)
|
|
|
|
| GeneratorWitness(..)
|
|
|
|
| GeneratorWitnessMIR(..) => {
|
2020-01-01 19:10:11 +01:00
|
|
|
// these are never sized - return the target type
|
|
|
|
vec![ty]
|
|
|
|
}
|
|
|
|
|
|
|
|
Tuple(ref tys) => match tys.last() {
|
|
|
|
None => vec![],
|
2022-02-07 16:06:31 +01:00
|
|
|
Some(&ty) => sized_constraint_for_ty(tcx, adtdef, ty),
|
2020-01-01 19:10:11 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
Adt(adt, substs) => {
|
|
|
|
// recursive case
|
|
|
|
let adt_tys = adt.sized_constraint(tcx);
|
|
|
|
debug!("sized_constraint_for_ty({:?}) intermediate = {:?}", ty, adt_tys);
|
|
|
|
adt_tys
|
2022-08-03 00:14:24 -04:00
|
|
|
.0
|
2020-01-01 19:10:11 +01:00
|
|
|
.iter()
|
2022-08-03 00:14:24 -04:00
|
|
|
.map(|ty| adt_tys.rebind(*ty).subst(tcx, substs))
|
2020-01-01 19:10:11 +01:00
|
|
|
.flat_map(|ty| sized_constraint_for_ty(tcx, adtdef, ty))
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
2022-11-26 21:51:55 +00:00
|
|
|
Alias(..) => {
|
2020-01-01 19:10:11 +01:00
|
|
|
// 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.
|
|
|
|
|
2022-02-19 00:48:49 +01:00
|
|
|
let Some(sized_trait) = tcx.lang_items().sized_trait() else { return vec![ty] };
|
2022-11-21 12:24:53 +00:00
|
|
|
let sized_predicate = ty::Binder::dummy(tcx.mk_trait_ref(sized_trait, [ty]))
|
2022-11-17 11:21:39 +00:00
|
|
|
.without_const()
|
|
|
|
.to_predicate(tcx);
|
2022-03-05 07:28:41 +11:00
|
|
|
let predicates = tcx.predicates_of(adtdef.did()).predicates;
|
2020-01-01 19:10:11 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-03-29 20:01:14 +02:00
|
|
|
fn impl_defaultness(tcx: TyCtxt<'_>, def_id: DefId) -> hir::Defaultness {
|
2022-03-12 19:36:11 +01:00
|
|
|
match tcx.hir().get_by_def_id(def_id.expect_local()) {
|
|
|
|
hir::Node::Item(hir::Item { kind: hir::ItemKind::Impl(impl_), .. }) => impl_.defaultness,
|
|
|
|
hir::Node::ImplItem(hir::ImplItem { defaultness, .. })
|
|
|
|
| hir::Node::TraitItem(hir::TraitItem { defaultness, .. }) => *defaultness,
|
|
|
|
node => {
|
|
|
|
bug!("`impl_defaultness` called on {:?}", node);
|
|
|
|
}
|
2020-03-29 20:01:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-01 19:10:11 +01: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.
|
2022-10-10 11:22:41 -05:00
|
|
|
/// - an Error, if a type is infinitely sized
|
|
|
|
fn adt_sized_constraint(tcx: TyCtxt<'_>, def_id: DefId) -> &[Ty<'_>] {
|
|
|
|
if let Some(def_id) = def_id.as_local() {
|
|
|
|
if matches!(tcx.representability(def_id), ty::Representability::Infinite) {
|
2023-02-17 14:33:08 +11:00
|
|
|
return tcx.mk_type_list(&[tcx.ty_error_misc()]);
|
2022-10-10 11:22:41 -05:00
|
|
|
}
|
|
|
|
}
|
2020-01-01 19:10:11 +01:00
|
|
|
let def = tcx.adt_def(def_id);
|
|
|
|
|
2023-02-17 14:33:08 +11:00
|
|
|
let result = tcx.mk_type_list_from_iter(
|
2022-03-05 07:28:41 +11:00
|
|
|
def.variants()
|
2020-01-01 19:10:11 +01:00
|
|
|
.iter()
|
|
|
|
.flat_map(|v| v.fields.last())
|
2023-02-07 01:29:48 -07:00
|
|
|
.flat_map(|f| sized_constraint_for_ty(tcx, def, tcx.type_of(f.did).subst_identity())),
|
2020-01-01 19:10:11 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
debug!("adt_sized_constraint: {:?} => {:?}", def, result);
|
|
|
|
|
2022-10-10 11:22:41 -05:00
|
|
|
result
|
2020-01-01 19:10:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// See `ParamEnv` struct definition for details.
|
|
|
|
fn param_env(tcx: TyCtxt<'_>, def_id: DefId) -> ty::ParamEnv<'_> {
|
|
|
|
// Compute the bounds on Self and the type parameters.
|
2020-09-01 17:58:34 +02:00
|
|
|
let ty::InstantiatedPredicates { mut predicates, .. } =
|
2020-01-01 19:10:11 +01:00
|
|
|
tcx.predicates_of(def_id).instantiate_identity(tcx);
|
|
|
|
|
2023-03-16 01:11:04 +00:00
|
|
|
// When computing the param_env of an RPITIT, use predicates of the containing function,
|
|
|
|
// *except* for the additional assumption that the RPITIT normalizes to the trait method's
|
|
|
|
// default opaque type. This is needed to properly check the item bounds of the assoc
|
|
|
|
// type hold (`check_type_bounds`), since that method already installs a similar projection
|
|
|
|
// bound, so they will conflict.
|
|
|
|
// FIXME(-Zlower-impl-trait-in-trait-to-assoc-ty): I don't like this, we should
|
|
|
|
// at least be making sure that the generics in RPITITs and their parent fn don't
|
|
|
|
// get out of alignment, or else we do actually need to substitute these predicates.
|
|
|
|
if let Some(ImplTraitInTraitData::Trait { fn_def_id, .. }) = tcx.opt_rpitit_info(def_id) {
|
|
|
|
predicates = tcx.predicates_of(fn_def_id).instantiate_identity(tcx).predicates;
|
|
|
|
}
|
|
|
|
|
2020-01-01 19:10:11 +01:00
|
|
|
// 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
|
2021-08-22 14:46:15 +02:00
|
|
|
// kind of an "idempotent" action, but I'm not sure where would be
|
2020-01-01 19:10:11 +01:00
|
|
|
// a better place. In practice, we construct environments for
|
|
|
|
// every fn once during type checking, and we'll abort if there
|
2021-10-25 15:43:07 -05:00
|
|
|
// are any errors at that point, so outside of type inference you can be
|
2020-01-01 19:10:11 +01:00
|
|
|
// sure that this will succeed without errors anyway.
|
|
|
|
|
2023-01-02 23:12:47 +00:00
|
|
|
if tcx.sess.opts.unstable_opts.trait_solver == TraitSolver::Chalk {
|
2020-09-01 17:58:34 +02:00
|
|
|
let environment = well_formed_types_in_env(tcx, def_id);
|
|
|
|
predicates.extend(environment);
|
|
|
|
}
|
|
|
|
|
2023-02-17 17:16:43 +00:00
|
|
|
if tcx.def_kind(def_id) == DefKind::AssocFn
|
|
|
|
&& tcx.associated_item(def_id).container == ty::AssocItemContainer::TraitContainer
|
|
|
|
{
|
|
|
|
let sig = tcx.fn_sig(def_id).subst_identity();
|
2023-02-28 21:34:04 +00:00
|
|
|
// We accounted for the binder of the fn sig, so skip the binder.
|
|
|
|
sig.skip_binder().visit_with(&mut ImplTraitInTraitFinder {
|
2023-02-17 17:16:43 +00:00
|
|
|
tcx,
|
|
|
|
fn_def_id: def_id,
|
|
|
|
bound_vars: sig.bound_vars(),
|
|
|
|
predicates: &mut predicates,
|
|
|
|
seen: FxHashSet::default(),
|
2023-02-28 21:34:04 +00:00
|
|
|
depth: ty::INNERMOST,
|
2023-02-17 17:16:43 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-12-12 12:34:46 +08:00
|
|
|
let local_did = def_id.as_local();
|
2023-03-16 01:11:04 +00:00
|
|
|
// FIXME(-Zlower-impl-trait-in-trait-to-assoc-ty): This isn't correct for
|
|
|
|
// RPITITs in const trait fn.
|
|
|
|
let hir_id = local_did.and_then(|def_id| tcx.opt_local_def_id_to_hir_id(def_id));
|
2020-01-01 19:10:11 +01:00
|
|
|
|
2022-10-19 19:05:15 +00:00
|
|
|
// FIXME(consts): This is not exactly in line with the constness query.
|
2022-10-19 18:34:01 +00:00
|
|
|
let constness = match hir_id {
|
|
|
|
Some(hir_id) => match tcx.hir().get(hir_id) {
|
|
|
|
hir::Node::TraitItem(hir::TraitItem { kind: hir::TraitItemKind::Fn(..), .. })
|
|
|
|
if tcx.is_const_default_method(def_id) =>
|
|
|
|
{
|
|
|
|
hir::Constness::Const
|
|
|
|
}
|
|
|
|
|
|
|
|
hir::Node::Item(hir::Item { kind: hir::ItemKind::Const(..), .. })
|
|
|
|
| hir::Node::Item(hir::Item { kind: hir::ItemKind::Static(..), .. })
|
|
|
|
| hir::Node::TraitItem(hir::TraitItem {
|
|
|
|
kind: hir::TraitItemKind::Const(..), ..
|
|
|
|
})
|
|
|
|
| hir::Node::AnonConst(_)
|
|
|
|
| hir::Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Const(..), .. })
|
|
|
|
| hir::Node::ImplItem(hir::ImplItem {
|
|
|
|
kind:
|
|
|
|
hir::ImplItemKind::Fn(
|
|
|
|
hir::FnSig {
|
|
|
|
header: hir::FnHeader { constness: hir::Constness::Const, .. },
|
|
|
|
..
|
|
|
|
},
|
|
|
|
..,
|
|
|
|
),
|
|
|
|
..
|
|
|
|
}) => hir::Constness::Const,
|
|
|
|
|
|
|
|
hir::Node::ImplItem(hir::ImplItem {
|
|
|
|
kind: hir::ImplItemKind::Type(..) | hir::ImplItemKind::Fn(..),
|
|
|
|
..
|
|
|
|
}) => {
|
2023-01-03 07:31:04 +00:00
|
|
|
let parent_hir_id = tcx.hir().parent_id(hir_id);
|
2022-10-19 18:34:01 +00:00
|
|
|
match tcx.hir().get(parent_hir_id) {
|
|
|
|
hir::Node::Item(hir::Item {
|
|
|
|
kind: hir::ItemKind::Impl(hir::Impl { constness, .. }),
|
|
|
|
..
|
|
|
|
}) => *constness,
|
|
|
|
_ => span_bug!(
|
|
|
|
tcx.def_span(parent_hir_id.owner),
|
|
|
|
"impl item's parent node is not an impl",
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
hir::Node::Item(hir::Item {
|
|
|
|
kind:
|
|
|
|
hir::ItemKind::Fn(hir::FnSig { header: hir::FnHeader { constness, .. }, .. }, ..),
|
|
|
|
..
|
|
|
|
})
|
|
|
|
| hir::Node::TraitItem(hir::TraitItem {
|
|
|
|
kind:
|
|
|
|
hir::TraitItemKind::Fn(
|
|
|
|
hir::FnSig { header: hir::FnHeader { constness, .. }, .. },
|
|
|
|
..,
|
|
|
|
),
|
|
|
|
..
|
|
|
|
})
|
|
|
|
| hir::Node::Item(hir::Item {
|
|
|
|
kind: hir::ItemKind::Impl(hir::Impl { constness, .. }),
|
|
|
|
..
|
|
|
|
}) => *constness,
|
|
|
|
|
|
|
|
_ => hir::Constness::NotConst,
|
|
|
|
},
|
2022-10-19 19:05:15 +00:00
|
|
|
// FIXME(consts): It's suspicious that a param-env for a foreign item
|
|
|
|
// will always have NotConst param-env, though we don't typically use
|
|
|
|
// that param-env for anything meaningful right now, so it's likely
|
|
|
|
// not an issue.
|
2022-10-19 18:34:01 +00:00
|
|
|
None => hir::Constness::NotConst,
|
|
|
|
};
|
|
|
|
|
2023-02-17 14:33:08 +11:00
|
|
|
let unnormalized_env =
|
|
|
|
ty::ParamEnv::new(tcx.mk_predicates(&predicates), traits::Reveal::UserFacing, constness);
|
2021-12-12 12:34:46 +08:00
|
|
|
|
2023-01-15 12:58:46 +01:00
|
|
|
let body_id = local_did.unwrap_or(CRATE_DEF_ID);
|
2020-01-01 19:10:11 +01:00
|
|
|
let cause = traits::ObligationCause::misc(tcx.def_span(def_id), body_id);
|
2022-06-27 17:18:49 +02:00
|
|
|
traits::normalize_param_env_or_error(tcx, unnormalized_env, cause)
|
2020-01-01 19:10:11 +01:00
|
|
|
}
|
|
|
|
|
2023-02-17 17:16:43 +00:00
|
|
|
/// Walk through a function type, gathering all RPITITs and installing a
|
|
|
|
/// `NormalizesTo(Projection(RPITIT) -> Opaque(RPITIT))` predicate into the
|
|
|
|
/// predicates list. This allows us to observe that an RPITIT projects to
|
|
|
|
/// its corresponding opaque within the body of a default-body trait method.
|
|
|
|
struct ImplTraitInTraitFinder<'a, 'tcx> {
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
predicates: &'a mut Vec<Predicate<'tcx>>,
|
|
|
|
fn_def_id: DefId,
|
|
|
|
bound_vars: &'tcx ty::List<ty::BoundVariableKind>,
|
|
|
|
seen: FxHashSet<DefId>,
|
2023-02-28 21:34:04 +00:00
|
|
|
depth: ty::DebruijnIndex,
|
2023-02-17 17:16:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for ImplTraitInTraitFinder<'_, 'tcx> {
|
2023-02-28 21:34:04 +00:00
|
|
|
fn visit_binder<T: TypeVisitable<TyCtxt<'tcx>>>(
|
|
|
|
&mut self,
|
|
|
|
binder: &ty::Binder<'tcx, T>,
|
|
|
|
) -> std::ops::ControlFlow<Self::BreakTy> {
|
|
|
|
self.depth.shift_in(1);
|
|
|
|
let binder = binder.super_visit_with(self);
|
|
|
|
self.depth.shift_out(1);
|
|
|
|
binder
|
|
|
|
}
|
|
|
|
|
2023-02-17 17:16:43 +00:00
|
|
|
fn visit_ty(&mut self, ty: Ty<'tcx>) -> std::ops::ControlFlow<Self::BreakTy> {
|
|
|
|
if let ty::Alias(ty::Projection, alias_ty) = *ty.kind()
|
2023-03-14 18:28:48 -03:00
|
|
|
&& self.tcx.is_impl_trait_in_trait(alias_ty.def_id)
|
2023-03-10 17:48:11 -03:00
|
|
|
&& self.tcx.impl_trait_in_trait_parent_fn(alias_ty.def_id) == self.fn_def_id
|
2023-02-17 17:16:43 +00:00
|
|
|
&& self.seen.insert(alias_ty.def_id)
|
|
|
|
{
|
2023-02-28 21:34:04 +00:00
|
|
|
// We have entered some binders as we've walked into the
|
|
|
|
// bounds of the RPITIT. Shift these binders back out when
|
|
|
|
// constructing the top-level projection predicate.
|
|
|
|
let alias_ty = self.tcx.fold_regions(alias_ty, |re, _| {
|
|
|
|
if let ty::ReLateBound(index, bv) = re.kind() {
|
|
|
|
self.tcx.mk_re_late_bound(index.shifted_out_to_binder(self.depth), bv)
|
|
|
|
} else {
|
|
|
|
re
|
|
|
|
}
|
|
|
|
});
|
2023-03-15 22:55:00 +00:00
|
|
|
|
|
|
|
// If we're lowering to associated item, install the opaque type which is just
|
|
|
|
// the `type_of` of the trait's associated item. If we're using the old lowering
|
|
|
|
// strategy, then just reinterpret the associated type like an opaque :^)
|
|
|
|
let default_ty = if self.tcx.lower_impl_trait_in_trait_to_assoc_ty() {
|
|
|
|
self
|
|
|
|
.tcx
|
|
|
|
.type_of(alias_ty.def_id)
|
|
|
|
.subst(self.tcx, alias_ty.substs)
|
|
|
|
} else {
|
|
|
|
self.tcx.mk_alias(ty::Opaque, alias_ty)
|
|
|
|
};
|
|
|
|
|
2023-02-17 17:16:43 +00:00
|
|
|
self.predicates.push(
|
|
|
|
ty::Binder::bind_with_vars(
|
|
|
|
ty::ProjectionPredicate {
|
|
|
|
projection_ty: alias_ty,
|
2023-03-15 22:55:00 +00:00
|
|
|
term: default_ty.into(),
|
2023-02-17 17:16:43 +00:00
|
|
|
},
|
|
|
|
self.bound_vars,
|
|
|
|
)
|
|
|
|
.to_predicate(self.tcx),
|
|
|
|
);
|
|
|
|
|
|
|
|
for bound in self.tcx.item_bounds(alias_ty.def_id).subst_iter(self.tcx, alias_ty.substs)
|
|
|
|
{
|
|
|
|
bound.visit_with(self);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ty.super_visit_with(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-01 17:58:34 +02: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.
|
2022-12-20 22:10:40 +01:00
|
|
|
fn well_formed_types_in_env(tcx: TyCtxt<'_>, def_id: DefId) -> &ty::List<Predicate<'_>> {
|
2020-09-01 17:58:34 +02:00
|
|
|
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) {
|
2021-11-30 19:11:35 +01:00
|
|
|
return well_formed_types_in_env(tcx, parent.to_def_id());
|
2020-09-01 17:58:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
}
|
2021-10-20 20:59:15 +02:00
|
|
|
let node = tcx.hir().get_by_def_id(def_id.expect_local());
|
2020-09-01 17:58:34 +02:00
|
|
|
|
|
|
|
enum NodeKind {
|
|
|
|
TraitImpl,
|
|
|
|
InherentImpl,
|
|
|
|
Fn,
|
|
|
|
Other,
|
2020-11-25 17:00:28 -05:00
|
|
|
}
|
2020-09-01 17:58:34 +02:00
|
|
|
|
|
|
|
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 {
|
2020-11-22 17:46:21 -05:00
|
|
|
ItemKind::Impl(hir::Impl { of_trait: Some(_), .. }) => NodeKind::TraitImpl,
|
|
|
|
ItemKind::Impl(hir::Impl { of_trait: None, .. }) => NodeKind::InherentImpl,
|
2020-09-01 17:58:34 +02:00
|
|
|
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 => {
|
2023-01-10 14:57:22 -07:00
|
|
|
let trait_ref = tcx.impl_trait_ref(def_id).expect("not an impl").subst_identity();
|
2020-09-01 17:58:34 +02:00
|
|
|
|
|
|
|
// 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 => {
|
2023-02-07 01:29:48 -07:00
|
|
|
let self_ty = tcx.type_of(def_id).subst_identity();
|
2022-01-12 03:19:52 +00:00
|
|
|
inputs.extend(self_ty.walk());
|
2020-09-01 17:58:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// In an fn, we assume that the arguments and all their constituents are
|
|
|
|
// well-formed.
|
|
|
|
NodeKind::Fn => {
|
2023-01-18 16:52:47 -07:00
|
|
|
let fn_sig = tcx.fn_sig(def_id).subst_identity();
|
2020-10-24 02:21:18 +02:00
|
|
|
let fn_sig = tcx.liberate_late_bound_regions(def_id, fn_sig);
|
2020-09-01 17:58:34 +02:00
|
|
|
|
2022-01-12 03:19:52 +00:00
|
|
|
inputs.extend(fn_sig.inputs().iter().flat_map(|ty| ty.walk()));
|
2020-09-01 17:58:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
NodeKind::Other => (),
|
|
|
|
}
|
|
|
|
let input_clauses = inputs.into_iter().filter_map(|arg| {
|
|
|
|
match arg.unpack() {
|
|
|
|
GenericArgKind::Type(ty) => {
|
2021-01-07 11:20:28 -05:00
|
|
|
let binder = Binder::dummy(PredicateKind::TypeWellFormedFromEnv(ty));
|
2020-12-23 16:36:23 -05:00
|
|
|
Some(tcx.mk_predicate(binder))
|
2020-09-01 17:58:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME(eddyb) no WF conditions from lifetimes?
|
|
|
|
GenericArgKind::Lifetime(_) => None,
|
|
|
|
|
|
|
|
// FIXME(eddyb) support const generics in Chalk
|
|
|
|
GenericArgKind::Const(_) => None,
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-02-17 14:33:08 +11:00
|
|
|
tcx.mk_predicates_from_iter(clauses.chain(input_clauses))
|
2020-09-01 17:58:34 +02:00
|
|
|
}
|
|
|
|
|
2020-04-11 00:50:02 -04: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 19:10:11 +01: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);
|
2022-07-05 00:00:00 +00:00
|
|
|
mir.basic_blocks.iter().map(|bb| bb.statements.len() + 1).sum()
|
2020-01-01 19:10:11 +01:00
|
|
|
}
|
|
|
|
// 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 22:28:15 +02:00
|
|
|
/// See [`ty::ImplOverlapKind::Issue33140`] for more details.
|
2023-02-14 15:55:31 -07:00
|
|
|
fn issue33140_self_ty(tcx: TyCtxt<'_>, def_id: DefId) -> Option<EarlyBinder<Ty<'_>>> {
|
2020-01-01 19:10:11 +01:00
|
|
|
debug!("issue33140_self_ty({:?})", def_id);
|
|
|
|
|
|
|
|
let trait_ref = tcx
|
2023-01-10 14:57:22 -07:00
|
|
|
.impl_trait_ref(def_id)
|
2023-01-10 14:22:52 -07:00
|
|
|
.unwrap_or_else(|| bug!("issue33140_self_ty called on inherent impl {:?}", def_id))
|
|
|
|
.skip_binder();
|
2020-01-01 19:10:11 +01:00
|
|
|
|
|
|
|
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-03 00:49:11 +02:00
|
|
|
let self_ty_matches = match self_ty.kind() {
|
2022-04-13 16:11:28 -07:00
|
|
|
ty::Dynamic(ref data, re, _) if re.is_static() => data.principal().is_none(),
|
2020-01-01 19:10:11 +01:00
|
|
|
_ => false,
|
|
|
|
};
|
|
|
|
|
|
|
|
if self_ty_matches {
|
|
|
|
debug!("issue33140_self_ty - MATCHES!");
|
2023-02-14 15:55:31 -07:00
|
|
|
Some(EarlyBinder(self_ty))
|
2020-01-01 19:10:11 +01:00
|
|
|
} 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 {
|
2021-10-20 20:59:15 +02:00
|
|
|
let node = tcx.hir().get_by_def_id(def_id.expect_local());
|
2022-11-02 17:33:25 +00:00
|
|
|
node.fn_sig().map_or(hir::IsAsync::NotAsync, |sig| sig.header.asyncness)
|
2020-01-01 19:10:11 +01:00
|
|
|
}
|
|
|
|
|
2023-01-21 05:43:37 +00:00
|
|
|
fn unsizing_params_for_adt<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> BitSet<u32> {
|
|
|
|
let def = tcx.adt_def(def_id);
|
|
|
|
let num_params = tcx.generics_of(def_id).count();
|
|
|
|
|
|
|
|
let maybe_unsizing_param_idx = |arg: ty::GenericArg<'tcx>| match arg.unpack() {
|
|
|
|
ty::GenericArgKind::Type(ty) => match ty.kind() {
|
|
|
|
ty::Param(p) => Some(p.index),
|
|
|
|
_ => None,
|
|
|
|
},
|
|
|
|
|
|
|
|
// We can't unsize a lifetime
|
|
|
|
ty::GenericArgKind::Lifetime(_) => None,
|
|
|
|
|
|
|
|
ty::GenericArgKind::Const(ct) => match ct.kind() {
|
|
|
|
ty::ConstKind::Param(p) => Some(p.index),
|
|
|
|
_ => None,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
// The last field of the structure has to exist and contain type/const parameters.
|
|
|
|
let Some((tail_field, prefix_fields)) =
|
|
|
|
def.non_enum_variant().fields.split_last() else
|
|
|
|
{
|
|
|
|
return BitSet::new_empty(num_params);
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut unsizing_params = BitSet::new_empty(num_params);
|
2023-02-07 01:29:48 -07:00
|
|
|
for arg in tcx.type_of(tail_field.did).subst_identity().walk() {
|
2023-01-21 05:43:37 +00:00
|
|
|
if let Some(i) = maybe_unsizing_param_idx(arg) {
|
|
|
|
unsizing_params.insert(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure none of the other fields mention the parameters used
|
|
|
|
// in unsizing.
|
|
|
|
for field in prefix_fields {
|
2023-02-07 01:29:48 -07:00
|
|
|
for arg in tcx.type_of(field.did).subst_identity().walk() {
|
2023-01-21 05:43:37 +00:00
|
|
|
if let Some(i) = maybe_unsizing_param_idx(arg) {
|
|
|
|
unsizing_params.remove(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsizing_params
|
|
|
|
}
|
|
|
|
|
2020-07-05 23:00:14 +03:00
|
|
|
pub fn provide(providers: &mut ty::query::Providers) {
|
2020-01-01 19:10:11 +01:00
|
|
|
*providers = ty::query::Providers {
|
|
|
|
asyncness,
|
|
|
|
adt_sized_constraint,
|
|
|
|
param_env,
|
2020-04-11 00:50:02 -04:00
|
|
|
param_env_reveal_all_normalized,
|
2020-01-01 19:10:11 +01:00
|
|
|
instance_def_size_estimate,
|
|
|
|
issue33140_self_ty,
|
2020-03-29 20:01:14 +02:00
|
|
|
impl_defaultness,
|
2023-01-21 05:43:37 +00:00
|
|
|
unsizing_params_for_adt,
|
2020-01-01 19:10:11 +01:00
|
|
|
..*providers
|
|
|
|
};
|
|
|
|
}
|