2019-02-05 17:20:45 +00:00
|
|
|
use crate::infer::InferCtxt;
|
2020-02-22 10:44:18 +00:00
|
|
|
use crate::opaque_types::required_region_bounds;
|
2020-04-09 20:07:22 +00:00
|
|
|
use crate::traits;
|
2020-03-29 15:19:48 +00:00
|
|
|
use rustc_hir as hir;
|
|
|
|
use rustc_hir::def_id::DefId;
|
2020-08-18 10:47:27 +00:00
|
|
|
use rustc_hir::lang_items::LangItem;
|
2020-05-23 13:29:51 +00:00
|
|
|
use rustc_middle::ty::subst::{GenericArg, GenericArgKind, SubstsRef};
|
2020-03-29 14:41:09 +00:00
|
|
|
use rustc_middle::ty::{self, ToPredicate, Ty, TyCtxt, TypeFoldable, WithConstness};
|
2019-12-31 17:15:40 +00:00
|
|
|
use rustc_span::Span;
|
2015-08-06 18:27:21 +00:00
|
|
|
|
2020-09-22 09:36:54 +00:00
|
|
|
use std::iter;
|
|
|
|
use std::rc::Rc;
|
2020-05-26 21:12:01 +00:00
|
|
|
/// Returns the set of obligations needed to make `arg` well-formed.
|
|
|
|
/// If `arg` contains unresolved inference variables, this may include
|
|
|
|
/// further WF obligations. However, if `arg` IS an unresolved
|
2015-08-06 18:27:21 +00:00
|
|
|
/// inference variable, returns `None`, because we are not able to
|
|
|
|
/// make any progress at all. This is to prevent "livelock" where we
|
|
|
|
/// say "$0 is WF if $0 is WF".
|
2019-06-13 22:32:15 +00:00
|
|
|
pub fn obligations<'a, 'tcx>(
|
|
|
|
infcx: &InferCtxt<'a, 'tcx>,
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
body_id: hir::HirId,
|
2020-07-22 21:43:18 +00:00
|
|
|
recursion_depth: usize,
|
2020-05-26 21:12:01 +00:00
|
|
|
arg: GenericArg<'tcx>,
|
2019-06-13 22:32:15 +00:00
|
|
|
span: Span,
|
|
|
|
) -> Option<Vec<traits::PredicateObligation<'tcx>>> {
|
2020-03-19 23:49:01 +00:00
|
|
|
// Handle the "livelock" case (see comment above) by bailing out if necessary.
|
2020-05-26 21:12:01 +00:00
|
|
|
let arg = match arg.unpack() {
|
|
|
|
GenericArgKind::Type(ty) => {
|
2020-08-02 22:49:11 +00:00
|
|
|
match ty.kind() {
|
2020-05-26 21:12:01 +00:00
|
|
|
ty::Infer(ty::TyVar(_)) => {
|
|
|
|
let resolved_ty = infcx.shallow_resolve(ty);
|
|
|
|
if resolved_ty == ty {
|
|
|
|
// No progress, bail out to prevent "livelock".
|
|
|
|
return None;
|
|
|
|
}
|
2020-03-19 23:49:01 +00:00
|
|
|
|
2020-05-26 21:12:01 +00:00
|
|
|
resolved_ty
|
|
|
|
}
|
|
|
|
_ => ty,
|
|
|
|
}
|
|
|
|
.into()
|
2020-03-19 23:49:01 +00:00
|
|
|
}
|
2020-05-26 21:12:01 +00:00
|
|
|
GenericArgKind::Const(ct) => {
|
|
|
|
match ct.val {
|
|
|
|
ty::ConstKind::Infer(infer) => {
|
|
|
|
let resolved = infcx.shallow_resolve(infer);
|
|
|
|
if resolved == infer {
|
|
|
|
// No progress.
|
|
|
|
return None;
|
|
|
|
}
|
2015-08-06 18:27:21 +00:00
|
|
|
|
2020-05-26 21:12:01 +00:00
|
|
|
infcx.tcx.mk_const(ty::Const { val: ty::ConstKind::Infer(resolved), ty: ct.ty })
|
|
|
|
}
|
|
|
|
_ => ct,
|
2020-05-23 13:29:51 +00:00
|
|
|
}
|
2020-05-26 21:12:01 +00:00
|
|
|
.into()
|
2020-05-23 13:29:51 +00:00
|
|
|
}
|
2020-05-26 21:12:01 +00:00
|
|
|
// There is nothing we have to do for lifetimes.
|
|
|
|
GenericArgKind::Lifetime(..) => return Some(Vec::new()),
|
2020-05-23 13:29:51 +00:00
|
|
|
};
|
|
|
|
|
2020-07-22 21:43:18 +00:00
|
|
|
let mut wf =
|
|
|
|
WfPredicates { infcx, param_env, body_id, span, out: vec![], recursion_depth, item: None };
|
2020-05-26 21:12:01 +00:00
|
|
|
wf.compute(arg);
|
|
|
|
debug!("wf::obligations({:?}, body_id={:?}) = {:?}", arg, body_id, wf.out);
|
2020-05-23 13:29:51 +00:00
|
|
|
|
|
|
|
let result = wf.normalize();
|
2020-05-26 21:12:01 +00:00
|
|
|
debug!("wf::obligations({:?}, body_id={:?}) ~~> {:?}", arg, body_id, result);
|
2020-05-23 13:29:51 +00:00
|
|
|
Some(result)
|
|
|
|
}
|
|
|
|
|
2015-08-06 18:27:21 +00:00
|
|
|
/// Returns the obligations that make this trait reference
|
|
|
|
/// well-formed. For example, if there is a trait `Set` defined like
|
|
|
|
/// `trait Set<K:Eq>`, then the trait reference `Foo: Set<Bar>` is WF
|
|
|
|
/// if `Bar: Eq`.
|
2019-06-13 22:32:15 +00:00
|
|
|
pub fn trait_obligations<'a, 'tcx>(
|
|
|
|
infcx: &InferCtxt<'a, 'tcx>,
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
body_id: hir::HirId,
|
|
|
|
trait_ref: &ty::TraitRef<'tcx>,
|
|
|
|
span: Span,
|
2019-11-28 18:28:50 +00:00
|
|
|
item: Option<&'tcx hir::Item<'tcx>>,
|
2019-06-13 22:32:15 +00:00
|
|
|
) -> Vec<traits::PredicateObligation<'tcx>> {
|
2020-07-22 21:43:18 +00:00
|
|
|
let mut wf =
|
|
|
|
WfPredicates { infcx, param_env, body_id, span, out: vec![], recursion_depth: 0, item };
|
2017-08-10 13:52:08 +00:00
|
|
|
wf.compute_trait_ref(trait_ref, Elaborate::All);
|
2015-08-06 18:27:21 +00:00
|
|
|
wf.normalize()
|
|
|
|
}
|
|
|
|
|
2019-06-13 22:32:15 +00:00
|
|
|
pub fn predicate_obligations<'a, 'tcx>(
|
|
|
|
infcx: &InferCtxt<'a, 'tcx>,
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
body_id: hir::HirId,
|
2020-06-24 16:06:04 +00:00
|
|
|
predicate: ty::Predicate<'tcx>,
|
2019-06-13 22:32:15 +00:00
|
|
|
span: Span,
|
|
|
|
) -> Vec<traits::PredicateObligation<'tcx>> {
|
2020-07-22 21:43:18 +00:00
|
|
|
let mut wf = WfPredicates {
|
|
|
|
infcx,
|
|
|
|
param_env,
|
|
|
|
body_id,
|
|
|
|
span,
|
|
|
|
out: vec![],
|
|
|
|
recursion_depth: 0,
|
|
|
|
item: None,
|
|
|
|
};
|
2015-08-06 18:27:21 +00:00
|
|
|
|
2020-07-18 09:46:38 +00:00
|
|
|
// It's ok to skip the binder here because wf code is prepared for it
|
|
|
|
match predicate.skip_binders() {
|
|
|
|
ty::PredicateAtom::Trait(t, _) => {
|
|
|
|
wf.compute_trait_ref(&t.trait_ref, Elaborate::None);
|
2015-08-06 18:27:21 +00:00
|
|
|
}
|
2020-07-18 09:46:38 +00:00
|
|
|
ty::PredicateAtom::RegionOutlives(..) => {}
|
|
|
|
ty::PredicateAtom::TypeOutlives(ty::OutlivesPredicate(ty, _reg)) => {
|
|
|
|
wf.compute(ty.into());
|
|
|
|
}
|
|
|
|
ty::PredicateAtom::Projection(t) => {
|
|
|
|
wf.compute_projection(t.projection_ty);
|
|
|
|
wf.compute(t.ty.into());
|
|
|
|
}
|
|
|
|
ty::PredicateAtom::WellFormed(arg) => {
|
|
|
|
wf.compute(arg);
|
|
|
|
}
|
|
|
|
ty::PredicateAtom::ObjectSafe(_) => {}
|
|
|
|
ty::PredicateAtom::ClosureKind(..) => {}
|
|
|
|
ty::PredicateAtom::Subtype(ty::SubtypePredicate { a, b, a_is_expected: _ }) => {
|
|
|
|
wf.compute(a.into());
|
|
|
|
wf.compute(b.into());
|
|
|
|
}
|
|
|
|
ty::PredicateAtom::ConstEvaluatable(def, substs) => {
|
|
|
|
let obligations = wf.nominal_obligations(def.did, substs);
|
|
|
|
wf.out.extend(obligations);
|
2020-07-08 22:35:55 +00:00
|
|
|
|
2020-07-18 09:46:38 +00:00
|
|
|
for arg in substs.iter() {
|
|
|
|
wf.compute(arg);
|
2020-07-08 22:35:55 +00:00
|
|
|
}
|
2020-07-18 09:46:38 +00:00
|
|
|
}
|
|
|
|
ty::PredicateAtom::ConstEquate(c1, c2) => {
|
|
|
|
wf.compute(c1.into());
|
|
|
|
wf.compute(c2.into());
|
|
|
|
}
|
2020-09-01 15:58:34 +00:00
|
|
|
ty::PredicateAtom::TypeWellFormedFromEnv(..) => {
|
|
|
|
bug!("TypeWellFormedFromEnv is only used for Chalk")
|
|
|
|
}
|
2015-08-06 18:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
wf.normalize()
|
|
|
|
}
|
|
|
|
|
2019-06-14 16:39:39 +00:00
|
|
|
struct WfPredicates<'a, 'tcx> {
|
2019-06-13 21:48:52 +00:00
|
|
|
infcx: &'a InferCtxt<'a, 'tcx>,
|
2017-05-23 08:19:47 +00:00
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
2019-02-04 19:01:14 +00:00
|
|
|
body_id: hir::HirId,
|
2015-08-06 18:27:21 +00:00
|
|
|
span: Span,
|
|
|
|
out: Vec<traits::PredicateObligation<'tcx>>,
|
2020-07-22 21:43:18 +00:00
|
|
|
recursion_depth: usize,
|
2019-11-28 18:28:50 +00:00
|
|
|
item: Option<&'tcx hir::Item<'tcx>>,
|
2015-08-06 18:27:21 +00:00
|
|
|
}
|
|
|
|
|
2017-08-23 22:56:19 +00:00
|
|
|
/// Controls whether we "elaborate" supertraits and so forth on the WF
|
|
|
|
/// predicates. This is a kind of hack to address #43784. The
|
|
|
|
/// underlying problem in that issue was a trait structure like:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// trait Foo: Copy { }
|
|
|
|
/// trait Bar: Foo { }
|
|
|
|
/// impl<T: Bar> Foo for T { }
|
|
|
|
/// impl<T> Bar for T { }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Here, in the `Foo` impl, we will check that `T: Copy` holds -- but
|
|
|
|
/// we decide that this is true because `T: Bar` is in the
|
|
|
|
/// where-clauses (and we can elaborate that to include `T:
|
|
|
|
/// Copy`). This wouldn't be a problem, except that when we check the
|
|
|
|
/// `Bar` impl, we decide that `T: Foo` must hold because of the `Foo`
|
|
|
|
/// impl. And so nowhere did we check that `T: Copy` holds!
|
|
|
|
///
|
|
|
|
/// To resolve this, we elaborate the WF requirements that must be
|
|
|
|
/// proven when checking impls. This means that (e.g.) the `impl Bar
|
|
|
|
/// for T` will be forced to prove not only that `T: Foo` but also `T:
|
|
|
|
/// Copy` (which it won't be able to do, because there is no `Copy`
|
|
|
|
/// impl for `T`).
|
2017-08-10 13:52:08 +00:00
|
|
|
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
|
|
|
|
enum Elaborate {
|
|
|
|
All,
|
|
|
|
None,
|
|
|
|
}
|
|
|
|
|
2020-04-07 15:49:02 +00:00
|
|
|
fn extend_cause_with_original_assoc_item_obligation<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
trait_ref: &ty::TraitRef<'tcx>,
|
|
|
|
item: Option<&hir::Item<'tcx>>,
|
|
|
|
cause: &mut traits::ObligationCause<'tcx>,
|
2020-06-18 18:41:43 +00:00
|
|
|
pred: &ty::Predicate<'tcx>,
|
2020-04-18 18:07:41 +00:00
|
|
|
mut trait_assoc_items: impl Iterator<Item = &'tcx ty::AssocItem>,
|
2020-04-07 15:49:02 +00:00
|
|
|
) {
|
2020-04-09 20:07:22 +00:00
|
|
|
debug!(
|
|
|
|
"extended_cause_with_original_assoc_item_obligation {:?} {:?} {:?} {:?}",
|
|
|
|
trait_ref, item, cause, pred
|
|
|
|
);
|
|
|
|
let items = match item {
|
2020-11-22 22:46:21 +00:00
|
|
|
Some(hir::Item { kind: hir::ItemKind::Impl(impl_), .. }) => impl_.items,
|
2020-04-09 20:07:22 +00:00
|
|
|
_ => return,
|
2020-04-07 15:49:02 +00:00
|
|
|
};
|
2020-04-09 20:07:22 +00:00
|
|
|
let fix_span =
|
|
|
|
|impl_item_ref: &hir::ImplItemRef<'_>| match tcx.hir().impl_item(impl_item_ref.id).kind {
|
|
|
|
hir::ImplItemKind::Const(ty, _) | hir::ImplItemKind::TyAlias(ty) => ty.span,
|
|
|
|
_ => impl_item_ref.span,
|
|
|
|
};
|
2020-06-18 18:41:43 +00:00
|
|
|
|
|
|
|
// It is fine to skip the binder as we don't care about regions here.
|
2020-07-08 22:35:55 +00:00
|
|
|
match pred.skip_binders() {
|
|
|
|
ty::PredicateAtom::Projection(proj) => {
|
2020-05-30 03:26:46 +00:00
|
|
|
// The obligation comes not from the current `impl` nor the `trait` being implemented,
|
|
|
|
// but rather from a "second order" obligation, where an associated type has a
|
|
|
|
// projection coming from another associated type. See
|
|
|
|
// `src/test/ui/associated-types/point-at-type-on-obligation-failure.rs` and
|
|
|
|
// `traits-assoc-type-in-supertrait-bad.rs`.
|
2020-08-02 22:49:11 +00:00
|
|
|
if let ty::Projection(projection_ty) = proj.ty.kind() {
|
2020-05-30 03:26:46 +00:00
|
|
|
let trait_assoc_item = tcx.associated_item(projection_ty.item_def_id);
|
|
|
|
if let Some(impl_item_span) =
|
|
|
|
items.iter().find(|item| item.ident == trait_assoc_item.ident).map(fix_span)
|
|
|
|
{
|
2020-06-03 21:59:30 +00:00
|
|
|
cause.make_mut().span = impl_item_span;
|
2020-04-07 15:49:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-08 22:35:55 +00:00
|
|
|
ty::PredicateAtom::Trait(pred, _) => {
|
2020-04-09 20:07:22 +00:00
|
|
|
// An associated item obligation born out of the `trait` failed to be met. An example
|
|
|
|
// can be seen in `ui/associated-types/point-at-type-on-obligation-failure-2.rs`.
|
|
|
|
debug!("extended_cause_with_original_assoc_item_obligation trait proj {:?}", pred);
|
2020-08-02 22:49:11 +00:00
|
|
|
if let ty::Projection(ty::ProjectionTy { item_def_id, .. }) = *pred.self_ty().kind() {
|
2020-04-09 20:07:22 +00:00
|
|
|
if let Some(impl_item_span) = trait_assoc_items
|
2020-06-18 18:41:43 +00:00
|
|
|
.find(|i| i.def_id == item_def_id)
|
2020-04-07 15:49:02 +00:00
|
|
|
.and_then(|trait_assoc_item| {
|
2020-04-09 20:07:22 +00:00
|
|
|
items.iter().find(|i| i.ident == trait_assoc_item.ident).map(fix_span)
|
2020-04-07 15:49:02 +00:00
|
|
|
})
|
|
|
|
{
|
2020-06-03 21:59:30 +00:00
|
|
|
cause.make_mut().span = impl_item_span;
|
2020-04-07 15:49:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-13 21:48:52 +00:00
|
|
|
impl<'a, 'tcx> WfPredicates<'a, 'tcx> {
|
2020-05-11 20:06:41 +00:00
|
|
|
fn tcx(&self) -> TyCtxt<'tcx> {
|
|
|
|
self.infcx.tcx
|
|
|
|
}
|
|
|
|
|
2020-05-23 13:29:51 +00:00
|
|
|
fn cause(&self, code: traits::ObligationCauseCode<'tcx>) -> traits::ObligationCause<'tcx> {
|
2015-12-15 09:31:58 +00:00
|
|
|
traits::ObligationCause::new(self.span, self.body_id, code)
|
2015-08-06 18:27:21 +00:00
|
|
|
}
|
|
|
|
|
2020-06-29 21:15:11 +00:00
|
|
|
fn normalize(mut self) -> Vec<traits::PredicateObligation<'tcx>> {
|
2015-08-06 18:27:21 +00:00
|
|
|
let cause = self.cause(traits::MiscObligation);
|
|
|
|
let infcx = &mut self.infcx;
|
2017-05-23 08:19:47 +00:00
|
|
|
let param_env = self.param_env;
|
2020-02-10 13:28:56 +00:00
|
|
|
let mut obligations = Vec::with_capacity(self.out.len());
|
2020-06-29 21:15:11 +00:00
|
|
|
for mut obligation in self.out {
|
|
|
|
assert!(!obligation.has_escaping_bound_vars());
|
2020-01-08 21:06:25 +00:00
|
|
|
let mut selcx = traits::SelectionContext::new(infcx);
|
2020-06-29 21:15:11 +00:00
|
|
|
// Don't normalize the whole obligation, the param env is either
|
|
|
|
// already normalized, or we're currently normalizing the
|
|
|
|
// param_env. Either way we should only normalize the predicate.
|
2020-07-22 21:43:18 +00:00
|
|
|
let normalized_predicate = traits::project::normalize_with_depth_to(
|
2020-06-29 21:15:11 +00:00
|
|
|
&mut selcx,
|
|
|
|
param_env,
|
|
|
|
cause.clone(),
|
2020-07-22 21:43:18 +00:00
|
|
|
self.recursion_depth,
|
2020-10-24 00:21:18 +00:00
|
|
|
obligation.predicate,
|
2020-06-29 21:15:11 +00:00
|
|
|
&mut obligations,
|
|
|
|
);
|
|
|
|
obligation.predicate = normalized_predicate;
|
2020-07-22 21:43:18 +00:00
|
|
|
obligations.push(obligation);
|
2020-02-10 13:28:56 +00:00
|
|
|
}
|
2020-01-08 21:06:25 +00:00
|
|
|
obligations
|
2015-08-06 18:27:21 +00:00
|
|
|
}
|
|
|
|
|
2019-10-10 23:20:42 +00:00
|
|
|
/// Pushes the obligations required for `trait_ref` to be WF into `self.out`.
|
2017-08-10 13:52:08 +00:00
|
|
|
fn compute_trait_ref(&mut self, trait_ref: &ty::TraitRef<'tcx>, elaborate: Elaborate) {
|
2019-10-11 02:25:34 +00:00
|
|
|
let tcx = self.infcx.tcx;
|
2015-08-06 18:27:21 +00:00
|
|
|
let obligations = self.nominal_obligations(trait_ref.def_id, trait_ref.substs);
|
|
|
|
|
2020-04-09 20:07:22 +00:00
|
|
|
debug!("compute_trait_ref obligations {:?}", obligations);
|
2015-08-06 18:27:21 +00:00
|
|
|
let cause = self.cause(traits::MiscObligation);
|
2017-05-23 08:19:47 +00:00
|
|
|
let param_env = self.param_env;
|
2020-07-22 21:43:18 +00:00
|
|
|
let depth = self.recursion_depth;
|
2017-08-10 12:41:24 +00:00
|
|
|
|
2020-04-07 15:49:02 +00:00
|
|
|
let item = self.item;
|
2019-10-22 19:40:46 +00:00
|
|
|
|
2020-04-18 18:07:41 +00:00
|
|
|
let extend = |obligation: traits::PredicateObligation<'tcx>| {
|
|
|
|
let mut cause = cause.clone();
|
2020-06-21 10:26:17 +00:00
|
|
|
if let Some(parent_trait_ref) = obligation.predicate.to_opt_poly_trait_ref() {
|
2020-04-18 18:07:41 +00:00
|
|
|
let derived_cause = traits::DerivedObligationCause {
|
2020-11-22 01:13:53 +00:00
|
|
|
parent_trait_ref: parent_trait_ref.value,
|
2020-04-18 18:07:41 +00:00
|
|
|
parent_code: Rc::new(obligation.cause.code.clone()),
|
|
|
|
};
|
2020-06-03 21:59:30 +00:00
|
|
|
cause.make_mut().code =
|
|
|
|
traits::ObligationCauseCode::DerivedObligation(derived_cause);
|
2020-04-18 18:07:41 +00:00
|
|
|
}
|
|
|
|
extend_cause_with_original_assoc_item_obligation(
|
|
|
|
tcx,
|
|
|
|
trait_ref,
|
|
|
|
item,
|
|
|
|
&mut cause,
|
|
|
|
&obligation.predicate,
|
|
|
|
tcx.associated_items(trait_ref.def_id).in_definition_order(),
|
|
|
|
);
|
2020-07-22 21:43:18 +00:00
|
|
|
traits::Obligation::with_depth(cause, depth, param_env, obligation.predicate)
|
2020-04-18 18:07:41 +00:00
|
|
|
};
|
|
|
|
|
2017-08-10 13:52:08 +00:00
|
|
|
if let Elaborate::All = elaborate {
|
2020-04-18 18:07:41 +00:00
|
|
|
let implied_obligations = traits::util::elaborate_obligations(tcx, obligations);
|
|
|
|
let implied_obligations = implied_obligations.map(extend);
|
2017-08-10 13:52:08 +00:00
|
|
|
self.out.extend(implied_obligations);
|
2020-04-18 18:07:41 +00:00
|
|
|
} else {
|
|
|
|
self.out.extend(obligations);
|
2017-08-10 13:52:08 +00:00
|
|
|
}
|
2017-08-10 13:02:41 +00:00
|
|
|
|
2020-05-11 20:06:41 +00:00
|
|
|
let tcx = self.tcx();
|
2020-05-26 21:12:01 +00:00
|
|
|
self.out.extend(
|
|
|
|
trait_ref
|
|
|
|
.substs
|
|
|
|
.iter()
|
2020-07-25 06:05:19 +00:00
|
|
|
.enumerate()
|
|
|
|
.filter(|(_, arg)| {
|
2020-05-26 21:12:01 +00:00
|
|
|
matches!(arg.unpack(), GenericArgKind::Type(..) | GenericArgKind::Const(..))
|
|
|
|
})
|
2020-07-25 06:05:19 +00:00
|
|
|
.filter(|(_, arg)| !arg.has_escaping_bound_vars())
|
|
|
|
.map(|(i, arg)| {
|
|
|
|
let mut new_cause = cause.clone();
|
|
|
|
// The first subst is the self ty - use the correct span for it.
|
|
|
|
if i == 0 {
|
2020-11-22 22:46:21 +00:00
|
|
|
if let Some(hir::ItemKind::Impl(hir::Impl { self_ty, .. })) =
|
|
|
|
item.map(|i| &i.kind)
|
|
|
|
{
|
2020-07-25 06:05:19 +00:00
|
|
|
new_cause.make_mut().span = self_ty.span;
|
|
|
|
}
|
|
|
|
}
|
2020-07-22 21:43:18 +00:00
|
|
|
traits::Obligation::with_depth(
|
2020-07-25 06:05:19 +00:00
|
|
|
new_cause,
|
2020-07-22 21:43:18 +00:00
|
|
|
depth,
|
2020-05-26 21:12:01 +00:00
|
|
|
param_env,
|
2020-07-08 22:35:55 +00:00
|
|
|
ty::PredicateAtom::WellFormed(arg).to_predicate(tcx),
|
2020-05-26 21:12:01 +00:00
|
|
|
)
|
|
|
|
}),
|
|
|
|
);
|
2015-08-06 18:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Pushes the obligations required for `trait_ref::Item` to be WF
|
|
|
|
/// into `self.out`.
|
|
|
|
fn compute_projection(&mut self, data: ty::ProjectionTy<'tcx>) {
|
2020-06-28 09:51:32 +00:00
|
|
|
// A projection is well-formed if
|
2020-07-02 20:45:28 +00:00
|
|
|
//
|
|
|
|
// (a) its predicates hold (*)
|
2020-06-28 09:51:32 +00:00
|
|
|
// (b) its substs are wf
|
2020-07-02 20:45:28 +00:00
|
|
|
//
|
|
|
|
// (*) The predicates of an associated type include the predicates of
|
|
|
|
// the trait that it's contained in. For example, given
|
|
|
|
//
|
|
|
|
// trait A<T>: Clone {
|
|
|
|
// type X where T: Copy;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// The predicates of `<() as A<i32>>::X` are:
|
|
|
|
// [
|
|
|
|
// `(): Sized`
|
|
|
|
// `(): Clone`
|
|
|
|
// `(): A<i32>`
|
|
|
|
// `i32: Sized`
|
|
|
|
// `i32: Clone`
|
|
|
|
// `i32: Copy`
|
|
|
|
// ]
|
2020-06-28 09:51:32 +00:00
|
|
|
let obligations = self.nominal_obligations(data.item_def_id, data.substs);
|
|
|
|
self.out.extend(obligations);
|
2015-08-06 18:27:21 +00:00
|
|
|
|
2020-06-28 09:51:32 +00:00
|
|
|
let tcx = self.tcx();
|
|
|
|
let cause = self.cause(traits::MiscObligation);
|
|
|
|
let param_env = self.param_env;
|
2020-07-22 21:43:18 +00:00
|
|
|
let depth = self.recursion_depth;
|
2020-06-28 09:51:32 +00:00
|
|
|
|
|
|
|
self.out.extend(
|
|
|
|
data.substs
|
|
|
|
.iter()
|
|
|
|
.filter(|arg| {
|
|
|
|
matches!(arg.unpack(), GenericArgKind::Type(..) | GenericArgKind::Const(..))
|
|
|
|
})
|
|
|
|
.filter(|arg| !arg.has_escaping_bound_vars())
|
|
|
|
.map(|arg| {
|
2020-07-22 21:43:18 +00:00
|
|
|
traits::Obligation::with_depth(
|
2020-06-28 09:51:32 +00:00
|
|
|
cause.clone(),
|
2020-07-22 21:43:18 +00:00
|
|
|
depth,
|
2020-06-28 09:51:32 +00:00
|
|
|
param_env,
|
2020-08-14 20:41:20 +00:00
|
|
|
ty::PredicateAtom::WellFormed(arg).to_predicate(tcx),
|
2020-06-28 09:51:32 +00:00
|
|
|
)
|
|
|
|
}),
|
|
|
|
);
|
2015-08-06 18:27:21 +00:00
|
|
|
}
|
|
|
|
|
2016-09-01 10:34:56 +00:00
|
|
|
fn require_sized(&mut self, subty: Ty<'tcx>, cause: traits::ObligationCauseCode<'tcx>) {
|
2018-10-22 20:38:51 +00:00
|
|
|
if !subty.has_escaping_bound_vars() {
|
2016-04-21 22:46:23 +00:00
|
|
|
let cause = self.cause(cause);
|
2016-11-14 02:42:15 +00:00
|
|
|
let trait_ref = ty::TraitRef {
|
2020-08-18 10:47:27 +00:00
|
|
|
def_id: self.infcx.tcx.require_lang_item(LangItem::Sized, None),
|
2016-11-14 02:42:15 +00:00
|
|
|
substs: self.infcx.tcx.mk_substs_trait(subty, &[]),
|
|
|
|
};
|
2020-07-22 21:43:18 +00:00
|
|
|
self.out.push(traits::Obligation::with_depth(
|
2020-01-14 04:30:32 +00:00
|
|
|
cause,
|
2020-07-22 21:43:18 +00:00
|
|
|
self.recursion_depth,
|
2020-01-14 04:30:32 +00:00
|
|
|
self.param_env,
|
2020-05-07 10:12:19 +00:00
|
|
|
trait_ref.without_const().to_predicate(self.infcx.tcx),
|
2020-01-14 04:30:32 +00:00
|
|
|
));
|
2016-04-21 22:46:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-19 23:49:01 +00:00
|
|
|
/// Pushes all the predicates needed to validate that `ty` is WF into `out`.
|
2020-05-23 13:29:51 +00:00
|
|
|
fn compute(&mut self, arg: GenericArg<'tcx>) {
|
|
|
|
let mut walker = arg.walk();
|
2017-05-23 08:19:47 +00:00
|
|
|
let param_env = self.param_env;
|
2020-07-22 21:43:18 +00:00
|
|
|
let depth = self.recursion_depth;
|
2020-03-23 01:57:04 +00:00
|
|
|
while let Some(arg) = walker.next() {
|
|
|
|
let ty = match arg.unpack() {
|
|
|
|
GenericArgKind::Type(ty) => ty,
|
|
|
|
|
|
|
|
// No WF constraints for lifetimes being present, any outlives
|
|
|
|
// obligations are handled by the parent (e.g. `ty::Ref`).
|
|
|
|
GenericArgKind::Lifetime(_) => continue,
|
|
|
|
|
2020-05-23 13:29:51 +00:00
|
|
|
GenericArgKind::Const(constant) => {
|
|
|
|
match constant.val {
|
2020-07-02 21:13:32 +00:00
|
|
|
ty::ConstKind::Unevaluated(def, substs, promoted) => {
|
2020-05-23 13:29:51 +00:00
|
|
|
assert!(promoted.is_none());
|
|
|
|
|
2020-07-02 21:13:32 +00:00
|
|
|
let obligations = self.nominal_obligations(def.did, substs);
|
2020-05-23 13:29:51 +00:00
|
|
|
self.out.extend(obligations);
|
|
|
|
|
2020-07-08 22:35:55 +00:00
|
|
|
let predicate = ty::PredicateAtom::ConstEvaluatable(def, substs)
|
2020-05-23 16:28:50 +00:00
|
|
|
.to_predicate(self.tcx());
|
2020-05-23 13:29:51 +00:00
|
|
|
let cause = self.cause(traits::MiscObligation);
|
2020-07-22 21:43:18 +00:00
|
|
|
self.out.push(traits::Obligation::with_depth(
|
2020-05-23 13:29:51 +00:00
|
|
|
cause,
|
2020-07-22 21:43:18 +00:00
|
|
|
self.recursion_depth,
|
2020-05-23 13:29:51 +00:00
|
|
|
self.param_env,
|
|
|
|
predicate,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
ty::ConstKind::Infer(infer) => {
|
|
|
|
let resolved = self.infcx.shallow_resolve(infer);
|
|
|
|
// the `InferConst` changed, meaning that we made progress.
|
|
|
|
if resolved != infer {
|
|
|
|
let cause = self.cause(traits::MiscObligation);
|
|
|
|
|
|
|
|
let resolved_constant = self.infcx.tcx.mk_const(ty::Const {
|
|
|
|
val: ty::ConstKind::Infer(resolved),
|
|
|
|
..*constant
|
|
|
|
});
|
2020-07-22 21:43:18 +00:00
|
|
|
self.out.push(traits::Obligation::with_depth(
|
2020-05-23 13:29:51 +00:00
|
|
|
cause,
|
2020-07-22 21:43:18 +00:00
|
|
|
self.recursion_depth,
|
2020-05-23 13:29:51 +00:00
|
|
|
self.param_env,
|
2020-07-08 22:35:55 +00:00
|
|
|
ty::PredicateAtom::WellFormed(resolved_constant.into())
|
2020-05-23 16:28:50 +00:00
|
|
|
.to_predicate(self.tcx()),
|
2020-05-23 13:29:51 +00:00
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
2020-05-06 04:02:09 +00:00
|
|
|
ty::ConstKind::Error(_)
|
2020-05-23 16:28:50 +00:00
|
|
|
| ty::ConstKind::Param(_)
|
|
|
|
| ty::ConstKind::Bound(..)
|
|
|
|
| ty::ConstKind::Placeholder(..) => {
|
|
|
|
// These variants are trivially WF, so nothing to do here.
|
|
|
|
}
|
|
|
|
ty::ConstKind::Value(..) => {
|
2020-05-26 21:13:18 +00:00
|
|
|
// FIXME: Enforce that values are structurally-matchable.
|
2020-05-23 16:28:50 +00:00
|
|
|
}
|
2020-05-23 13:29:51 +00:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2020-03-23 01:57:04 +00:00
|
|
|
};
|
|
|
|
|
2020-08-02 22:49:11 +00:00
|
|
|
match *ty.kind() {
|
2019-12-24 22:38:22 +00:00
|
|
|
ty::Bool
|
|
|
|
| ty::Char
|
|
|
|
| ty::Int(..)
|
|
|
|
| ty::Uint(..)
|
|
|
|
| ty::Float(..)
|
2020-05-06 04:02:09 +00:00
|
|
|
| ty::Error(_)
|
2019-12-24 22:38:22 +00:00
|
|
|
| ty::Str
|
|
|
|
| ty::GeneratorWitness(..)
|
|
|
|
| ty::Never
|
|
|
|
| ty::Param(_)
|
|
|
|
| ty::Bound(..)
|
|
|
|
| ty::Placeholder(..)
|
|
|
|
| ty::Foreign(..) => {
|
2015-08-06 18:27:21 +00:00
|
|
|
// WfScalar, WfParameter, etc
|
|
|
|
}
|
|
|
|
|
2020-04-02 22:01:30 +00:00
|
|
|
// Can only infer to `ty::Int(_) | ty::Uint(_)`.
|
|
|
|
ty::Infer(ty::IntVar(_)) => {}
|
|
|
|
|
|
|
|
// Can only infer to `ty::Float(_)`.
|
|
|
|
ty::Infer(ty::FloatVar(_)) => {}
|
|
|
|
|
2018-08-22 00:35:02 +00:00
|
|
|
ty::Slice(subty) => {
|
2017-08-05 13:11:24 +00:00
|
|
|
self.require_sized(subty, traits::SliceOrArrayElem);
|
|
|
|
}
|
|
|
|
|
2020-05-23 13:29:51 +00:00
|
|
|
ty::Array(subty, _) => {
|
2016-09-01 10:34:56 +00:00
|
|
|
self.require_sized(subty, traits::SliceOrArrayElem);
|
2020-05-26 21:13:18 +00:00
|
|
|
// Note that we handle the len is implicitly checked while walking `arg`.
|
2016-04-21 22:46:23 +00:00
|
|
|
}
|
|
|
|
|
2018-08-22 00:35:02 +00:00
|
|
|
ty::Tuple(ref tys) => {
|
2016-04-21 22:46:23 +00:00
|
|
|
if let Some((_last, rest)) = tys.split_last() {
|
|
|
|
for elem in rest {
|
2019-04-25 23:27:33 +00:00
|
|
|
self.require_sized(elem.expect_ty(), traits::TupleElem);
|
2015-08-06 18:27:21 +00:00
|
|
|
}
|
2015-12-15 09:31:58 +00:00
|
|
|
}
|
2015-08-06 18:27:21 +00:00
|
|
|
}
|
|
|
|
|
2018-08-22 00:35:02 +00:00
|
|
|
ty::RawPtr(_) => {
|
2020-05-26 21:13:18 +00:00
|
|
|
// Simple cases that are WF if their type args are WF.
|
2015-08-06 18:27:21 +00:00
|
|
|
}
|
|
|
|
|
2018-08-22 00:35:02 +00:00
|
|
|
ty::Projection(data) => {
|
2020-05-26 21:13:18 +00:00
|
|
|
walker.skip_current_subtree(); // Subtree handled by compute_projection.
|
2015-08-06 18:27:21 +00:00
|
|
|
self.compute_projection(data);
|
|
|
|
}
|
|
|
|
|
2018-08-22 00:35:02 +00:00
|
|
|
ty::Adt(def, substs) => {
|
2015-08-06 18:27:21 +00:00
|
|
|
// WfNominalType
|
|
|
|
let obligations = self.nominal_obligations(def.did, substs);
|
|
|
|
self.out.extend(obligations);
|
|
|
|
}
|
|
|
|
|
2018-12-18 20:27:22 +00:00
|
|
|
ty::FnDef(did, substs) => {
|
|
|
|
let obligations = self.nominal_obligations(did, substs);
|
|
|
|
self.out.extend(obligations);
|
|
|
|
}
|
|
|
|
|
2018-08-22 00:35:02 +00:00
|
|
|
ty::Ref(r, rty, _) => {
|
2015-08-06 18:27:21 +00:00
|
|
|
// WfReference
|
2018-10-22 20:38:51 +00:00
|
|
|
if !r.has_escaping_bound_vars() && !rty.has_escaping_bound_vars() {
|
2015-08-06 18:27:21 +00:00
|
|
|
let cause = self.cause(traits::ReferenceOutlivesReferent(ty));
|
2020-07-22 21:43:18 +00:00
|
|
|
self.out.push(traits::Obligation::with_depth(
|
2019-12-24 22:38:22 +00:00
|
|
|
cause,
|
2020-07-22 21:43:18 +00:00
|
|
|
depth,
|
2019-12-24 22:38:22 +00:00
|
|
|
param_env,
|
2020-07-08 22:35:55 +00:00
|
|
|
ty::PredicateAtom::TypeOutlives(ty::OutlivesPredicate(rty, r))
|
2020-06-18 15:43:26 +00:00
|
|
|
.to_predicate(self.tcx()),
|
2019-12-24 22:38:22 +00:00
|
|
|
));
|
2015-08-06 18:27:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-22 00:35:02 +00:00
|
|
|
ty::Generator(..) => {
|
2017-11-17 16:13:49 +00:00
|
|
|
// Walk ALL the types in the generator: this will
|
|
|
|
// include the upvar types as well as the yield
|
|
|
|
// type. Note that this is mildly distinct from
|
|
|
|
// the closure case, where we have to be careful
|
|
|
|
// about the signature of the closure. We don't
|
|
|
|
// have the problem of implied bounds here since
|
|
|
|
// generators don't take arguments.
|
2015-08-06 18:27:21 +00:00
|
|
|
}
|
|
|
|
|
2020-03-13 01:23:38 +00:00
|
|
|
ty::Closure(_, substs) => {
|
2017-11-17 16:13:49 +00:00
|
|
|
// Only check the upvar types for WF, not the rest
|
|
|
|
// of the types within. This is needed because we
|
|
|
|
// capture the signature and it may not be WF
|
|
|
|
// without the implied bounds. Consider a closure
|
|
|
|
// like `|x: &'a T|` -- it may be that `T: 'a` is
|
|
|
|
// not known to hold in the creator's context (and
|
|
|
|
// indeed the closure may not be invoked by its
|
|
|
|
// creator, but rather turned to someone who *can*
|
|
|
|
// verify that).
|
|
|
|
//
|
|
|
|
// The special treatment of closures here really
|
|
|
|
// ought not to be necessary either; the problem
|
|
|
|
// is related to #25860 -- there is no way for us
|
|
|
|
// to express a fn type complete with the implied
|
|
|
|
// bounds that it is assuming. I think in reality
|
|
|
|
// the WF rules around fn are a bit messed up, and
|
|
|
|
// that is the rot problem: `fn(&'a T)` should
|
|
|
|
// probably always be WF, because it should be
|
|
|
|
// shorthand for something like `where(T: 'a) {
|
|
|
|
// fn(&'a T) }`, as discussed in #25860.
|
2017-11-20 18:49:18 +00:00
|
|
|
//
|
|
|
|
// Note that we are also skipping the generic
|
|
|
|
// types. This is consistent with the `outlives`
|
|
|
|
// code, but anyway doesn't matter: within the fn
|
|
|
|
// body where they are created, the generics will
|
|
|
|
// always be WF, and outside of that fn body we
|
|
|
|
// are not directly inspecting closure types
|
|
|
|
// anyway, except via auto trait matching (which
|
|
|
|
// only inspects the upvar types).
|
2020-03-19 23:49:01 +00:00
|
|
|
walker.skip_current_subtree(); // subtree handled below
|
2020-07-19 21:26:51 +00:00
|
|
|
// FIXME(eddyb) add the type to `walker` instead of recursing.
|
|
|
|
self.compute(substs.as_closure().tupled_upvars_ty().into());
|
2017-11-08 21:31:47 +00:00
|
|
|
}
|
|
|
|
|
2018-12-18 20:27:22 +00:00
|
|
|
ty::FnPtr(_) => {
|
2015-06-13 20:15:03 +00:00
|
|
|
// let the loop iterate into the argument/return
|
2015-12-15 09:31:58 +00:00
|
|
|
// types appearing in the fn signature
|
2015-08-06 18:27:21 +00:00
|
|
|
}
|
|
|
|
|
2018-08-23 19:51:32 +00:00
|
|
|
ty::Opaque(did, substs) => {
|
2016-07-22 15:56:22 +00:00
|
|
|
// all of the requirements on type parameters
|
|
|
|
// should've been checked by the instantiation
|
|
|
|
// of whatever returned this exact `impl Trait`.
|
2018-07-17 11:44:42 +00:00
|
|
|
|
2019-07-31 23:41:54 +00:00
|
|
|
// for named opaque `impl Trait` types we still need to check them
|
2020-01-05 19:52:34 +00:00
|
|
|
if ty::is_impl_trait_defn(self.infcx.tcx, did).is_none() {
|
2018-07-17 11:44:42 +00:00
|
|
|
let obligations = self.nominal_obligations(did, substs);
|
|
|
|
self.out.extend(obligations);
|
|
|
|
}
|
2016-07-22 15:56:22 +00:00
|
|
|
}
|
|
|
|
|
2018-08-22 00:35:02 +00:00
|
|
|
ty::Dynamic(data, r) => {
|
2015-08-06 18:27:21 +00:00
|
|
|
// WfObject
|
|
|
|
//
|
|
|
|
// Here, we defer WF checking due to higher-ranked
|
|
|
|
// regions. This is perhaps not ideal.
|
2016-11-16 16:21:49 +00:00
|
|
|
self.from_object_ty(ty, data, r);
|
2015-08-06 18:27:21 +00:00
|
|
|
|
|
|
|
// FIXME(#27579) RFC also considers adding trait
|
|
|
|
// obligations that don't refer to Self and
|
|
|
|
// checking those
|
|
|
|
|
2020-05-11 20:06:41 +00:00
|
|
|
let defer_to_coercion = self.tcx().features().object_safe_for_dispatch;
|
2019-01-08 21:14:04 +00:00
|
|
|
|
|
|
|
if !defer_to_coercion {
|
|
|
|
let cause = self.cause(traits::MiscObligation);
|
2019-12-24 22:38:22 +00:00
|
|
|
let component_traits = data.auto_traits().chain(data.principal_def_id());
|
2020-05-11 20:06:41 +00:00
|
|
|
let tcx = self.tcx();
|
2019-12-24 22:38:22 +00:00
|
|
|
self.out.extend(component_traits.map(|did| {
|
2020-07-22 21:43:18 +00:00
|
|
|
traits::Obligation::with_depth(
|
2019-01-08 21:14:04 +00:00
|
|
|
cause.clone(),
|
2020-07-22 21:43:18 +00:00
|
|
|
depth,
|
2019-01-08 21:14:04 +00:00
|
|
|
param_env,
|
2020-07-08 22:35:55 +00:00
|
|
|
ty::PredicateAtom::ObjectSafe(did).to_predicate(tcx),
|
2019-12-24 22:38:22 +00:00
|
|
|
)
|
|
|
|
}));
|
2019-01-08 21:14:04 +00:00
|
|
|
}
|
2015-08-06 18:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Inference variables are the complicated case, since we don't
|
|
|
|
// know what type they are. We do two things:
|
|
|
|
//
|
|
|
|
// 1. Check if they have been resolved, and if so proceed with
|
|
|
|
// THAT type.
|
2020-03-19 23:49:01 +00:00
|
|
|
// 2. If not, we've at least simplified things (e.g., we went
|
|
|
|
// from `Vec<$0>: WF` to `$0: WF`), so we can
|
2015-08-06 18:27:21 +00:00
|
|
|
// register a pending obligation and keep
|
|
|
|
// moving. (Goal is that an "inductive hypothesis"
|
|
|
|
// is satisfied to ensure termination.)
|
2020-03-19 23:49:01 +00:00
|
|
|
// See also the comment on `fn obligations`, describing "livelock"
|
|
|
|
// prevention, which happens before this can be reached.
|
2018-08-22 00:35:02 +00:00
|
|
|
ty::Infer(_) => {
|
2019-04-30 21:27:33 +00:00
|
|
|
let ty = self.infcx.shallow_resolve(ty);
|
2020-08-02 22:49:11 +00:00
|
|
|
if let ty::Infer(ty::TyVar(_)) = ty.kind() {
|
2020-03-19 23:49:01 +00:00
|
|
|
// Not yet resolved, but we've made progress.
|
2015-08-06 18:27:21 +00:00
|
|
|
let cause = self.cause(traits::MiscObligation);
|
2020-07-22 21:43:18 +00:00
|
|
|
self.out.push(traits::Obligation::with_depth(
|
2020-03-19 23:49:01 +00:00
|
|
|
cause,
|
2020-07-22 21:43:18 +00:00
|
|
|
self.recursion_depth,
|
2020-03-19 23:49:01 +00:00
|
|
|
param_env,
|
2020-07-08 22:35:55 +00:00
|
|
|
ty::PredicateAtom::WellFormed(ty.into()).to_predicate(self.tcx()),
|
2020-03-19 23:49:01 +00:00
|
|
|
));
|
2015-08-06 18:27:21 +00:00
|
|
|
} else {
|
2020-03-19 23:49:01 +00:00
|
|
|
// Yes, resolved, proceed with the result.
|
|
|
|
// FIXME(eddyb) add the type to `walker` instead of recursing.
|
2020-05-23 13:29:51 +00:00
|
|
|
self.compute(ty.into());
|
2015-08-06 18:27:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-24 22:38:22 +00:00
|
|
|
fn nominal_obligations(
|
|
|
|
&mut self,
|
|
|
|
def_id: DefId,
|
|
|
|
substs: SubstsRef<'tcx>,
|
|
|
|
) -> Vec<traits::PredicateObligation<'tcx>> {
|
2020-09-22 09:36:54 +00:00
|
|
|
let predicates = self.infcx.tcx.predicates_of(def_id);
|
|
|
|
let mut origins = vec![def_id; predicates.predicates.len()];
|
|
|
|
let mut head = predicates;
|
|
|
|
while let Some(parent) = head.parent {
|
|
|
|
head = self.infcx.tcx.predicates_of(parent);
|
|
|
|
origins.extend(iter::repeat(parent).take(head.predicates.len()));
|
|
|
|
}
|
|
|
|
|
|
|
|
let predicates = predicates.instantiate(self.infcx.tcx, substs);
|
|
|
|
debug_assert_eq!(predicates.predicates.len(), origins.len());
|
|
|
|
|
2019-12-24 22:38:22 +00:00
|
|
|
predicates
|
|
|
|
.predicates
|
|
|
|
.into_iter()
|
2020-03-03 23:07:04 +00:00
|
|
|
.zip(predicates.spans.into_iter())
|
2020-09-22 09:36:54 +00:00
|
|
|
.zip(origins.into_iter().rev())
|
|
|
|
.map(|((pred, span), origin_def_id)| {
|
|
|
|
let cause = self.cause(traits::BindingObligation(origin_def_id, span));
|
2020-07-22 21:43:18 +00:00
|
|
|
traits::Obligation::with_depth(cause, self.recursion_depth, self.param_env, pred)
|
2020-03-03 23:07:04 +00:00
|
|
|
})
|
2019-12-24 22:38:22 +00:00
|
|
|
.filter(|pred| !pred.has_escaping_bound_vars())
|
|
|
|
.collect()
|
2015-08-06 18:27:21 +00:00
|
|
|
}
|
|
|
|
|
2019-12-24 22:38:22 +00:00
|
|
|
fn from_object_ty(
|
|
|
|
&mut self,
|
|
|
|
ty: Ty<'tcx>,
|
2020-12-11 20:02:46 +00:00
|
|
|
data: &'tcx ty::List<ty::Binder<ty::ExistentialPredicate<'tcx>>>,
|
2019-12-24 22:38:22 +00:00
|
|
|
region: ty::Region<'tcx>,
|
|
|
|
) {
|
2015-08-06 18:27:21 +00:00
|
|
|
// Imagine a type like this:
|
|
|
|
//
|
|
|
|
// trait Foo { }
|
|
|
|
// trait Bar<'c> : 'c { }
|
|
|
|
//
|
|
|
|
// &'b (Foo+'c+Bar<'d>)
|
|
|
|
// ^
|
|
|
|
//
|
|
|
|
// In this case, the following relationships must hold:
|
|
|
|
//
|
|
|
|
// 'b <= 'c
|
|
|
|
// 'd <= 'c
|
|
|
|
//
|
|
|
|
// The first conditions is due to the normal region pointer
|
|
|
|
// rules, which say that a reference cannot outlive its
|
|
|
|
// referent.
|
|
|
|
//
|
|
|
|
// The final condition may be a bit surprising. In particular,
|
|
|
|
// you may expect that it would have been `'c <= 'd`, since
|
|
|
|
// usually lifetimes of outer things are conservative
|
|
|
|
// approximations for inner things. However, it works somewhat
|
|
|
|
// differently with trait objects: here the idea is that if the
|
|
|
|
// user specifies a region bound (`'c`, in this case) it is the
|
|
|
|
// "master bound" that *implies* that bounds from other traits are
|
|
|
|
// all met. (Remember that *all bounds* in a type like
|
|
|
|
// `Foo+Bar+Zed` must be met, not just one, hence if we write
|
|
|
|
// `Foo<'x>+Bar<'y>`, we know that the type outlives *both* 'x and
|
|
|
|
// 'y.)
|
|
|
|
//
|
|
|
|
// Note: in fact we only permit builtin traits, not `Bar<'d>`, I
|
|
|
|
// am looking forward to the future here.
|
2019-03-12 14:57:06 +00:00
|
|
|
if !data.has_escaping_bound_vars() && !region.has_escaping_bound_vars() {
|
2019-12-24 22:38:22 +00:00
|
|
|
let implicit_bounds = object_region_bounds(self.infcx.tcx, data);
|
2015-08-06 18:27:21 +00:00
|
|
|
|
2016-11-16 16:21:49 +00:00
|
|
|
let explicit_bound = region;
|
2015-08-06 18:27:21 +00:00
|
|
|
|
2018-10-02 09:00:41 +00:00
|
|
|
self.out.reserve(implicit_bounds.len());
|
2015-08-06 18:27:21 +00:00
|
|
|
for implicit_bound in implicit_bounds {
|
2016-10-14 14:09:59 +00:00
|
|
|
let cause = self.cause(traits::ObjectTypeBound(ty, explicit_bound));
|
2019-12-24 22:38:22 +00:00
|
|
|
let outlives =
|
|
|
|
ty::Binder::dummy(ty::OutlivesPredicate(explicit_bound, implicit_bound));
|
2020-07-22 21:43:18 +00:00
|
|
|
self.out.push(traits::Obligation::with_depth(
|
2019-12-24 22:38:22 +00:00
|
|
|
cause,
|
2020-07-22 21:43:18 +00:00
|
|
|
self.recursion_depth,
|
2019-12-24 22:38:22 +00:00
|
|
|
self.param_env,
|
2020-05-07 10:12:19 +00:00
|
|
|
outlives.to_predicate(self.infcx.tcx),
|
2019-12-24 22:38:22 +00:00
|
|
|
));
|
2015-08-06 18:27:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-08 13:53:55 +00:00
|
|
|
/// Given an object type like `SomeTrait + Send`, computes the lifetime
|
2015-08-06 18:27:21 +00:00
|
|
|
/// bounds that must hold on the elided self type. These are derived
|
|
|
|
/// from the declarations of `SomeTrait`, `Send`, and friends -- if
|
|
|
|
/// they declare `trait SomeTrait : 'static`, for example, then
|
|
|
|
/// `'static` would appear in the list. The hard work is done by
|
2020-01-05 22:28:45 +00:00
|
|
|
/// `infer::required_region_bounds`, see that for more information.
|
2019-06-13 21:48:52 +00:00
|
|
|
pub fn object_region_bounds<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
2020-12-11 20:02:46 +00:00
|
|
|
existential_predicates: &'tcx ty::List<ty::Binder<ty::ExistentialPredicate<'tcx>>>,
|
2019-06-11 21:11:55 +00:00
|
|
|
) -> Vec<ty::Region<'tcx>> {
|
2015-08-06 18:27:21 +00:00
|
|
|
// Since we don't actually *know* the self type for an object,
|
|
|
|
// this "open(err)" serves as a kind of dummy standin -- basically
|
2018-09-07 13:46:53 +00:00
|
|
|
// a placeholder type.
|
2019-03-01 04:02:32 +00:00
|
|
|
let open_ty = tcx.mk_ty_infer(ty::FreshTy(0));
|
2015-08-06 18:27:21 +00:00
|
|
|
|
2020-04-18 01:31:25 +00:00
|
|
|
let predicates = existential_predicates.iter().filter_map(|predicate| {
|
2020-06-24 21:40:33 +00:00
|
|
|
if let ty::ExistentialPredicate::Projection(_) = predicate.skip_binder() {
|
2020-04-18 01:31:25 +00:00
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(predicate.with_self_ty(tcx, open_ty))
|
|
|
|
}
|
|
|
|
});
|
2015-08-06 18:27:21 +00:00
|
|
|
|
2020-01-05 22:28:45 +00:00
|
|
|
required_region_bounds(tcx, open_ty, predicates)
|
2015-08-06 18:27:21 +00:00
|
|
|
}
|