2020-09-01 10:58:34 -05:00
|
|
|
use rustc_data_structures::fx::FxIndexSet;
|
2020-01-01 12:10:11 -06:00
|
|
|
use rustc_hir as hir;
|
2021-11-18 15:44:45 -06:00
|
|
|
use rustc_hir::def_id::DefId;
|
2020-06-23 12:18:06 -05:00
|
|
|
use rustc_middle::ty::subst::Subst;
|
2021-11-18 15:44:45 -06:00
|
|
|
use rustc_middle::ty::{self, Binder, Predicate, PredicateKind, ToPredicate, Ty, TyCtxt};
|
2021-12-17 06:39:55 -06:00
|
|
|
use rustc_span::{sym, 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>,
|
2022-03-04 14:28:41 -06:00
|
|
|
adtdef: ty::AdtDef<'tcx>,
|
2020-01-30 14:25:39 -06:00
|
|
|
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![],
|
2022-02-07 09:06:31 -06:00
|
|
|
Some(&ty) => sized_constraint_for_ty(tcx, adtdef, ty),
|
2020-01-01 12:10:11 -06:00
|
|
|
},
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
2022-02-18 17:48:49 -06:00
|
|
|
let Some(sized_trait) = tcx.lang_items().sized_trait() else { return vec![ty] };
|
2020-01-01 12:10:11 -06:00
|
|
|
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);
|
2022-03-04 14:28:41 -06:00
|
|
|
let predicates = tcx.predicates_of(adtdef.did()).predicates;
|
2020-01-01 12:10:11 -06: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 13:01:14 -05:00
|
|
|
fn impl_defaultness(tcx: TyCtxt<'_>, def_id: DefId) -> hir::Defaultness {
|
2021-10-20 15:38:10 -05:00
|
|
|
let item = tcx.hir().expect_item(def_id.expect_local());
|
2020-11-22 16:46:21 -06:00
|
|
|
if let hir::ItemKind::Impl(impl_) = &item.kind {
|
|
|
|
impl_.defaultness
|
2020-03-29 13:01:14 -05:00
|
|
|
} else {
|
|
|
|
bug!("`impl_defaultness` called on {:?}", item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-09 22:15:11 -05:00
|
|
|
fn impl_constness(tcx: TyCtxt<'_>, def_id: DefId) -> hir::Constness {
|
2021-10-20 15:38:10 -05:00
|
|
|
let item = tcx.hir().expect_item(def_id.expect_local());
|
2021-07-09 22:15:11 -05:00
|
|
|
if let hir::ItemKind::Impl(impl_) = &item.kind {
|
|
|
|
impl_.constness
|
|
|
|
} else {
|
|
|
|
bug!("`impl_constness` 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.
|
2021-08-22 07:46:15 -05:00
|
|
|
/// - an Error, if a type contained itself. The representability
|
2020-01-01 12:10:11 -06:00
|
|
|
/// 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(
|
2022-03-04 14:28:41 -06:00
|
|
|
def.variants()
|
2020-01-01 12:10:11 -06:00
|
|
|
.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)
|
|
|
|
}
|
|
|
|
|
2021-01-02 12:45:11 -06:00
|
|
|
fn def_ident_span(tcx: TyCtxt<'_>, def_id: DefId) -> Option<Span> {
|
2021-08-18 18:13:52 -05:00
|
|
|
tcx.hir()
|
|
|
|
.get_if_local(def_id)
|
|
|
|
.and_then(|node| match node {
|
|
|
|
// A `Ctor` doesn't have an identifier itself, but its parent
|
|
|
|
// struct/variant does. Compare with `hir::Map::opt_span`.
|
|
|
|
hir::Node::Ctor(ctor) => ctor
|
|
|
|
.ctor_hir_id()
|
|
|
|
.and_then(|ctor_id| tcx.hir().find(tcx.hir().get_parent_node(ctor_id)))
|
|
|
|
.and_then(|parent| parent.ident()),
|
|
|
|
_ => node.ident(),
|
|
|
|
})
|
|
|
|
.map(|ident| ident.span)
|
2021-01-02 12:45:11 -06:00
|
|
|
}
|
|
|
|
|
2020-01-01 12:10:11 -06:00
|
|
|
/// See `ParamEnv` struct definition for details.
|
2021-10-25 08:34:59 -05:00
|
|
|
#[instrument(level = "debug", skip(tcx))]
|
2020-01-01 12:10:11 -06:00
|
|
|
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) {
|
2021-11-30 12:11:35 -06:00
|
|
|
return param_env(tcx, parent.to_def_id());
|
2020-01-01 12:10:11 -06:00
|
|
|
}
|
|
|
|
// 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
|
2021-08-22 07:46:15 -05:00
|
|
|
// kind of an "idempotent" action, but I'm not sure where would be
|
2020-01-01 12:10:11 -06: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 12:10:11 -06:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2021-12-11 22:34:46 -06:00
|
|
|
let local_did = def_id.as_local();
|
|
|
|
let hir_id = local_did.map(|def_id| tcx.hir().local_def_id_to_hir_id(def_id));
|
2020-01-01 12:10:11 -06:00
|
|
|
|
2021-12-11 22:34:46 -06:00
|
|
|
let constness = match hir_id {
|
|
|
|
Some(hir_id) => match tcx.hir().get(hir_id) {
|
2021-12-17 06:39:55 -06:00
|
|
|
hir::Node::TraitItem(hir::TraitItem { kind: hir::TraitItemKind::Fn(..), .. })
|
|
|
|
if tcx.has_attr(def_id, sym::default_method_body_is_const) =>
|
|
|
|
{
|
|
|
|
hir::Constness::Const
|
|
|
|
}
|
|
|
|
|
2021-12-11 22:34:46 -06:00
|
|
|
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::TyAlias(..) | hir::ImplItemKind::Fn(..),
|
|
|
|
..
|
|
|
|
}) => {
|
|
|
|
let parent_hir_id = tcx.hir().get_parent_node(hir_id);
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
None => hir::Constness::NotConst,
|
|
|
|
};
|
|
|
|
|
|
|
|
let unnormalized_env = ty::ParamEnv::new(
|
|
|
|
tcx.intern_predicates(&predicates),
|
|
|
|
traits::Reveal::UserFacing,
|
|
|
|
constness,
|
|
|
|
);
|
|
|
|
|
|
|
|
let body_id = hir_id.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) {
|
2021-11-30 12:11:35 -06:00
|
|
|
return well_formed_types_in_env(tcx, parent.to_def_id());
|
2020-09-01 10:58:34 -05: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 13:59:15 -05:00
|
|
|
let node = tcx.hir().get_by_def_id(def_id.expect_local());
|
2020-09-01 10:58:34 -05:00
|
|
|
|
|
|
|
enum NodeKind {
|
|
|
|
TraitImpl,
|
|
|
|
InherentImpl,
|
|
|
|
Fn,
|
|
|
|
Other,
|
2020-11-25 16:00:28 -06:00
|
|
|
}
|
2020-09-01 10:58:34 -05: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 16:46:21 -06:00
|
|
|
ItemKind::Impl(hir::Impl { of_trait: Some(_), .. }) => NodeKind::TraitImpl,
|
|
|
|
ItemKind::Impl(hir::Impl { of_trait: None, .. }) => NodeKind::InherentImpl,
|
2020-09-01 10:58:34 -05: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 => {
|
|
|
|
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);
|
2022-01-11 21:19:52 -06:00
|
|
|
inputs.extend(self_ty.walk());
|
2020-09-01 10:58:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
2020-10-23 19:21:18 -05:00
|
|
|
let fn_sig = tcx.liberate_late_bound_regions(def_id, fn_sig);
|
2020-09-01 10:58:34 -05:00
|
|
|
|
2022-01-11 21:19:52 -06:00
|
|
|
inputs.extend(fn_sig.inputs().iter().flat_map(|ty| ty.walk()));
|
2020-09-01 10:58:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
NodeKind::Other => (),
|
|
|
|
}
|
|
|
|
let input_clauses = inputs.into_iter().filter_map(|arg| {
|
|
|
|
match arg.unpack() {
|
|
|
|
GenericArgKind::Type(ty) => {
|
2021-01-07 10:20:28 -06:00
|
|
|
let binder = Binder::dummy(PredicateKind::TypeWellFormedFromEnv(ty));
|
2020-12-23 15:36:23 -06:00
|
|
|
Some(tcx.mk_predicate(binder))
|
2020-09-01 10:58:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 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);
|
Include terminators in instance size estimate
For example, drop glue generated for struct below, doesn't have any
statements, only terminators. Previously it received an estimate of 0,
the new estimate is 13 (6+5 drop terminators, +1 resume, +1 return).
struct S {
a: String,
b: String,
c: String,
d: String,
e: String,
f: String,
}
Originally reported in https://github.com/rust-lang/rust/issues/69382#issue-569392141
2021-06-28 19:00:00 -05:00
|
|
|
mir.basic_blocks().iter().map(|bb| bb.statements.len() + 1).sum()
|
2020-01-01 12:10:11 -06: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 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() {
|
2022-01-27 18:25:15 -06:00
|
|
|
ty::Dynamic(ref data, re) if re.is_static() => data.principal().is_none(),
|
2020-01-01 12:10:11 -06:00
|
|
|
_ => 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 {
|
2021-10-20 13:59:15 -05:00
|
|
|
let node = tcx.hir().get_by_def_id(def_id.expect_local());
|
2020-01-01 12:10:11 -06:00
|
|
|
|
2021-10-19 16:31:51 -05:00
|
|
|
let fn_kind = node.fn_kind().unwrap_or_else(|| {
|
2020-01-01 12:10:11 -06:00
|
|
|
bug!("asyncness: expected fn-like node but got `{:?}`", def_id);
|
|
|
|
});
|
|
|
|
|
2021-10-19 16:31:51 -05:00
|
|
|
fn_kind.asyncness()
|
2020-01-01 12:10:11 -06:00
|
|
|
}
|
|
|
|
|
2021-02-23 17:35:48 -06:00
|
|
|
/// Don't call this directly: use ``tcx.conservative_is_privately_uninhabited`` instead.
|
|
|
|
#[instrument(level = "debug", skip(tcx))]
|
|
|
|
pub fn conservative_is_privately_uninhabited_raw<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
param_env_and: ty::ParamEnvAnd<'tcx, Ty<'tcx>>,
|
|
|
|
) -> bool {
|
|
|
|
let (param_env, ty) = param_env_and.into_parts();
|
|
|
|
match ty.kind() {
|
|
|
|
ty::Never => {
|
|
|
|
debug!("ty::Never =>");
|
|
|
|
true
|
|
|
|
}
|
|
|
|
ty::Adt(def, _) if def.is_union() => {
|
|
|
|
debug!("ty::Adt(def, _) if def.is_union() =>");
|
|
|
|
// For now, `union`s are never considered uninhabited.
|
|
|
|
false
|
|
|
|
}
|
|
|
|
ty::Adt(def, substs) => {
|
|
|
|
debug!("ty::Adt(def, _) if def.is_not_union() =>");
|
|
|
|
// Any ADT is uninhabited if either:
|
|
|
|
// (a) It has no variants (i.e. an empty `enum`);
|
|
|
|
// (b) Each of its variants (a single one in the case of a `struct`) has at least
|
|
|
|
// one uninhabited field.
|
2022-03-04 14:28:41 -06:00
|
|
|
def.variants().iter().all(|var| {
|
2021-02-23 17:35:48 -06:00
|
|
|
var.fields.iter().any(|field| {
|
|
|
|
let ty = tcx.type_of(field.did).subst(tcx, substs);
|
|
|
|
tcx.conservative_is_privately_uninhabited(param_env.and(ty))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2022-02-07 09:06:31 -06:00
|
|
|
ty::Tuple(fields) => {
|
2021-02-23 17:35:48 -06:00
|
|
|
debug!("ty::Tuple(..) =>");
|
2022-02-07 09:06:31 -06:00
|
|
|
fields.iter().any(|ty| tcx.conservative_is_privately_uninhabited(param_env.and(ty)))
|
2021-02-23 17:35:48 -06:00
|
|
|
}
|
|
|
|
ty::Array(ty, len) => {
|
|
|
|
debug!("ty::Array(ty, len) =>");
|
|
|
|
match len.try_eval_usize(tcx, param_env) {
|
|
|
|
Some(0) | None => false,
|
|
|
|
// If the array is definitely non-empty, it's uninhabited if
|
|
|
|
// the type of its elements is uninhabited.
|
Overhaul `TyS` and `Ty`.
Specifically, change `Ty` from this:
```
pub type Ty<'tcx> = &'tcx TyS<'tcx>;
```
to this
```
pub struct Ty<'tcx>(Interned<'tcx, TyS<'tcx>>);
```
There are two benefits to this.
- It's now a first class type, so we can define methods on it. This
means we can move a lot of methods away from `TyS`, leaving `TyS` as a
barely-used type, which is appropriate given that it's not meant to
be used directly.
- The uniqueness requirement is now explicit, via the `Interned` type.
E.g. the pointer-based `Eq` and `Hash` comes from `Interned`, rather
than via `TyS`, which wasn't obvious at all.
Much of this commit is boring churn. The interesting changes are in
these files:
- compiler/rustc_middle/src/arena.rs
- compiler/rustc_middle/src/mir/visit.rs
- compiler/rustc_middle/src/ty/context.rs
- compiler/rustc_middle/src/ty/mod.rs
Specifically:
- Most mentions of `TyS` are removed. It's very much a dumb struct now;
`Ty` has all the smarts.
- `TyS` now has `crate` visibility instead of `pub`.
- `TyS::make_for_test` is removed in favour of the static `BOOL_TY`,
which just works better with the new structure.
- The `Eq`/`Ord`/`Hash` impls are removed from `TyS`. `Interned`s impls
of `Eq`/`Hash` now suffice. `Ord` is now partly on `Interned`
(pointer-based, for the `Equal` case) and partly on `TyS`
(contents-based, for the other cases).
- There are many tedious sigil adjustments, i.e. adding or removing `*`
or `&`. They seem to be unavoidable.
2022-01-24 21:13:38 -06:00
|
|
|
Some(1..) => tcx.conservative_is_privately_uninhabited(param_env.and(*ty)),
|
2021-02-23 17:35:48 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ty::Ref(..) => {
|
|
|
|
debug!("ty::Ref(..) =>");
|
|
|
|
// References to uninitialised memory is valid for any type, including
|
|
|
|
// uninhabited types, in unsafe code, so we treat all references as
|
|
|
|
// inhabited.
|
|
|
|
false
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
debug!("_ =>");
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
adt_sized_constraint,
|
2021-01-02 12:45:11 -06:00
|
|
|
def_ident_span,
|
2020-01-01 12:10:11 -06:00
|
|
|
param_env,
|
2020-04-10 23:50:02 -05:00
|
|
|
param_env_reveal_all_normalized,
|
2020-01-01 12:10:11 -06:00
|
|
|
instance_def_size_estimate,
|
|
|
|
issue33140_self_ty,
|
2020-03-29 13:01:14 -05:00
|
|
|
impl_defaultness,
|
2021-07-09 22:15:11 -05:00
|
|
|
impl_constness,
|
2021-02-23 17:35:48 -06:00
|
|
|
conservative_is_privately_uninhabited: conservative_is_privately_uninhabited_raw,
|
2020-01-01 12:10:11 -06:00
|
|
|
..*providers
|
|
|
|
};
|
|
|
|
}
|