Rollup merge of #137633 - compiler-errors:no-implied-bounds-hack-unless-bevy, r=lcnr

Only use implied bounds hack if bevy, and use deeply normalize in implied bounds hack

Consolidates the implied bounds computation mode into a single function, which deeply normalizes, and if it's in **compat** mode (for bevy), it extracts outlives bounds from the infcx.

Previously, we were using the implied bounds compat mode in two cases:
1. During WF, if it detects `ParamSet`
2. EVERYWHERE ELSE (lol) -- e.g. borrowck, predicate entailment, etc.

While I think this is fine, and the net effect was just that we emitted fewer diagnostics, it makes me uncomfortable that all crates were using the supposed "compat" code.

Fixes #137767
This commit is contained in:
许杰友 Jieyou Xu (Joe) 2025-03-05 21:46:42 +08:00 committed by GitHub
commit 6c60abf51a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
19 changed files with 264 additions and 269 deletions

View file

@ -126,13 +126,14 @@ where
let infcx_compat = infcx.fork(); let infcx_compat = infcx.fork();
// We specifically want to call the non-compat version of `implied_bounds_tys`; we do this always. // We specifically want to *disable* the implied bounds hack, first,
// so we can detect when failures are due to bevy's implied bounds.
let outlives_env = OutlivesEnvironment::new_with_implied_bounds_compat( let outlives_env = OutlivesEnvironment::new_with_implied_bounds_compat(
&infcx, &infcx,
body_def_id, body_def_id,
param_env, param_env,
assumed_wf_types.iter().copied(), assumed_wf_types.iter().copied(),
false, true,
); );
lint_redundant_lifetimes(tcx, body_def_id, &outlives_env); lint_redundant_lifetimes(tcx, body_def_id, &outlives_env);
@ -142,53 +143,22 @@ where
return Ok(()); return Ok(());
} }
let is_bevy = assumed_wf_types.visit_with(&mut ContainsBevyParamSet { tcx }).is_break(); let outlives_env = OutlivesEnvironment::new_with_implied_bounds_compat(
&infcx_compat,
// If we have set `no_implied_bounds_compat`, then do not attempt compatibility. body_def_id,
// We could also just always enter if `is_bevy`, and call `implied_bounds_tys`, param_env,
// but that does result in slightly more work when this option is set and assumed_wf_types,
// just obscures what we mean here anyways. Let's just be explicit. // Don't *disable* the implied bounds hack; though this will only apply
if is_bevy && !infcx.tcx.sess.opts.unstable_opts.no_implied_bounds_compat { // the implied bounds hack if this contains `bevy_ecs`'s `ParamSet` type.
let outlives_env = OutlivesEnvironment::new_with_implied_bounds_compat( false,
&infcx, );
body_def_id, let errors_compat = infcx_compat.resolve_regions_with_outlives_env(&outlives_env);
param_env, if errors_compat.is_empty() {
assumed_wf_types, // FIXME: Once we fix bevy, this would be the place to insert a warning
true, // to upgrade bevy.
); Ok(())
let errors_compat = infcx_compat.resolve_regions_with_outlives_env(&outlives_env);
if errors_compat.is_empty() {
Ok(())
} else {
Err(infcx_compat.err_ctxt().report_region_errors(body_def_id, &errors_compat))
}
} else { } else {
Err(infcx.err_ctxt().report_region_errors(body_def_id, &errors)) Err(infcx_compat.err_ctxt().report_region_errors(body_def_id, &errors_compat))
}
}
struct ContainsBevyParamSet<'tcx> {
tcx: TyCtxt<'tcx>,
}
impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for ContainsBevyParamSet<'tcx> {
type Result = ControlFlow<()>;
fn visit_ty(&mut self, t: Ty<'tcx>) -> Self::Result {
// We only care to match `ParamSet<T>` or `&ParamSet<T>`.
match t.kind() {
ty::Adt(def, _) => {
if self.tcx.item_name(def.did()) == sym::ParamSet
&& self.tcx.crate_name(def.did().krate) == sym::bevy_ecs
{
return ControlFlow::Break(());
}
}
ty::Ref(_, ty, _) => ty.visit_with(self)?,
_ => {}
}
ControlFlow::Continue(())
} }
} }

View file

@ -508,6 +508,14 @@ impl<'tcx, T: Clone> Key for CanonicalQueryInput<'tcx, T> {
} }
} }
impl<'tcx, T: Clone> Key for (CanonicalQueryInput<'tcx, T>, bool) {
type Cache<V> = DefaultCache<Self, V>;
fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
DUMMY_SP
}
}
impl Key for (Symbol, u32, u32) { impl Key for (Symbol, u32, u32) {
type Cache<V> = DefaultCache<Self, V>; type Cache<V> = DefaultCache<Self, V>;

View file

@ -2262,22 +2262,13 @@ rustc_queries! {
desc { "normalizing `{}`", goal.value } desc { "normalizing `{}`", goal.value }
} }
query implied_outlives_bounds_compat(
goal: CanonicalImpliedOutlivesBoundsGoal<'tcx>
) -> Result<
&'tcx Canonical<'tcx, canonical::QueryResponse<'tcx, Vec<OutlivesBound<'tcx>>>>,
NoSolution,
> {
desc { "computing implied outlives bounds for `{}`", goal.canonical.value.value.ty }
}
query implied_outlives_bounds( query implied_outlives_bounds(
goal: CanonicalImpliedOutlivesBoundsGoal<'tcx> key: (CanonicalImpliedOutlivesBoundsGoal<'tcx>, bool)
) -> Result< ) -> Result<
&'tcx Canonical<'tcx, canonical::QueryResponse<'tcx, Vec<OutlivesBound<'tcx>>>>, &'tcx Canonical<'tcx, canonical::QueryResponse<'tcx, Vec<OutlivesBound<'tcx>>>>,
NoSolution, NoSolution,
> { > {
desc { "computing implied outlives bounds v2 for `{}`", goal.canonical.value.value.ty } desc { "computing implied outlives bounds for `{}` (hack disabled = {:?})", key.0.canonical.value.value.ty, key.1 }
} }
/// Do not call this query directly: /// Do not call this query directly:

View file

@ -17,13 +17,7 @@ impl<'tcx> OutlivesEnvironment<'tcx> {
param_env: ty::ParamEnv<'tcx>, param_env: ty::ParamEnv<'tcx>,
assumed_wf_tys: impl IntoIterator<Item = Ty<'tcx>>, assumed_wf_tys: impl IntoIterator<Item = Ty<'tcx>>,
) -> Self { ) -> Self {
Self::new_with_implied_bounds_compat( Self::new_with_implied_bounds_compat(infcx, body_id, param_env, assumed_wf_tys, false)
infcx,
body_id,
param_env,
assumed_wf_tys,
!infcx.tcx.sess.opts.unstable_opts.no_implied_bounds_compat,
)
} }
fn new_with_implied_bounds_compat( fn new_with_implied_bounds_compat(
@ -31,7 +25,7 @@ impl<'tcx> OutlivesEnvironment<'tcx> {
body_id: LocalDefId, body_id: LocalDefId,
param_env: ty::ParamEnv<'tcx>, param_env: ty::ParamEnv<'tcx>,
assumed_wf_tys: impl IntoIterator<Item = Ty<'tcx>>, assumed_wf_tys: impl IntoIterator<Item = Ty<'tcx>>,
implied_bounds_compat: bool, disable_implied_bounds_hack: bool,
) -> Self { ) -> Self {
let mut bounds = vec![]; let mut bounds = vec![];
@ -59,11 +53,11 @@ impl<'tcx> OutlivesEnvironment<'tcx> {
OutlivesEnvironment::from_normalized_bounds( OutlivesEnvironment::from_normalized_bounds(
param_env, param_env,
bounds, bounds,
infcx.implied_bounds_tys_with_compat( infcx.implied_bounds_tys(
body_id, body_id,
param_env, param_env,
assumed_wf_tys, assumed_wf_tys,
implied_bounds_compat, disable_implied_bounds_hack,
), ),
) )
} }

View file

@ -36,7 +36,7 @@ fn implied_outlives_bounds<'a, 'tcx>(
param_env: ty::ParamEnv<'tcx>, param_env: ty::ParamEnv<'tcx>,
body_id: LocalDefId, body_id: LocalDefId,
ty: Ty<'tcx>, ty: Ty<'tcx>,
compat: bool, disable_implied_bounds_hack: bool,
) -> Vec<OutlivesBound<'tcx>> { ) -> Vec<OutlivesBound<'tcx>> {
let ty = infcx.resolve_vars_if_possible(ty); let ty = infcx.resolve_vars_if_possible(ty);
let ty = OpportunisticRegionResolver::new(infcx).fold_ty(ty); let ty = OpportunisticRegionResolver::new(infcx).fold_ty(ty);
@ -52,11 +52,8 @@ fn implied_outlives_bounds<'a, 'tcx>(
let mut canonical_var_values = OriginalQueryValues::default(); let mut canonical_var_values = OriginalQueryValues::default();
let input = ImpliedOutlivesBounds { ty }; let input = ImpliedOutlivesBounds { ty };
let canonical = infcx.canonicalize_query(param_env.and(input), &mut canonical_var_values); let canonical = infcx.canonicalize_query(param_env.and(input), &mut canonical_var_values);
let implied_bounds_result = if compat { let implied_bounds_result =
infcx.tcx.implied_outlives_bounds_compat(canonical) infcx.tcx.implied_outlives_bounds((canonical, disable_implied_bounds_hack));
} else {
infcx.tcx.implied_outlives_bounds(canonical)
};
let Ok(canonical_result) = implied_bounds_result else { let Ok(canonical_result) = implied_bounds_result else {
return vec![]; return vec![];
}; };
@ -110,14 +107,15 @@ fn implied_outlives_bounds<'a, 'tcx>(
impl<'tcx> InferCtxt<'tcx> { impl<'tcx> InferCtxt<'tcx> {
/// Do *NOT* call this directly. You probably want to construct a `OutlivesEnvironment` /// Do *NOT* call this directly. You probably want to construct a `OutlivesEnvironment`
/// instead if you're interested in the implied bounds for a given signature. /// instead if you're interested in the implied bounds for a given signature.
fn implied_bounds_tys_with_compat<Tys: IntoIterator<Item = Ty<'tcx>>>( fn implied_bounds_tys<Tys: IntoIterator<Item = Ty<'tcx>>>(
&self, &self,
body_id: LocalDefId, body_id: LocalDefId,
param_env: ParamEnv<'tcx>, param_env: ParamEnv<'tcx>,
tys: Tys, tys: Tys,
compat: bool, disable_implied_bounds_hack: bool,
) -> impl Iterator<Item = OutlivesBound<'tcx>> { ) -> impl Iterator<Item = OutlivesBound<'tcx>> {
tys.into_iter() tys.into_iter().flat_map(move |ty| {
.flat_map(move |ty| implied_outlives_bounds(self, param_env, body_id, ty, compat)) implied_outlives_bounds(self, param_env, body_id, ty, disable_implied_bounds_hack)
})
} }
} }

View file

@ -1,15 +1,16 @@
use std::ops::ControlFlow;
use rustc_infer::infer::RegionObligation;
use rustc_infer::infer::canonical::CanonicalQueryInput; use rustc_infer::infer::canonical::CanonicalQueryInput;
use rustc_infer::infer::resolve::OpportunisticRegionResolver;
use rustc_infer::traits::query::OutlivesBound; use rustc_infer::traits::query::OutlivesBound;
use rustc_infer::traits::query::type_op::ImpliedOutlivesBounds; use rustc_infer::traits::query::type_op::ImpliedOutlivesBounds;
use rustc_middle::infer::canonical::CanonicalQueryResponse; use rustc_middle::infer::canonical::CanonicalQueryResponse;
use rustc_middle::traits::ObligationCause; use rustc_middle::traits::ObligationCause;
use rustc_middle::ty::{self, ParamEnvAnd, Ty, TyCtxt, TypeFolder, TypeVisitableExt}; use rustc_middle::ty::{self, ParamEnvAnd, Ty, TyCtxt, TypeVisitable, TypeVisitor};
use rustc_span::def_id::CRATE_DEF_ID; use rustc_span::def_id::CRATE_DEF_ID;
use rustc_span::{DUMMY_SP, Span}; use rustc_span::{DUMMY_SP, Span, sym};
use rustc_type_ir::outlives::{Component, push_outlives_components}; use rustc_type_ir::outlives::{Component, push_outlives_components};
use smallvec::{SmallVec, smallvec}; use smallvec::{SmallVec, smallvec};
use tracing::debug;
use crate::traits::query::NoSolution; use crate::traits::query::NoSolution;
use crate::traits::{ObligationCtxt, wf}; use crate::traits::{ObligationCtxt, wf};
@ -35,11 +36,7 @@ impl<'tcx> super::QueryTypeOp<'tcx> for ImpliedOutlivesBounds<'tcx> {
tcx: TyCtxt<'tcx>, tcx: TyCtxt<'tcx>,
canonicalized: CanonicalQueryInput<'tcx, ParamEnvAnd<'tcx, Self>>, canonicalized: CanonicalQueryInput<'tcx, ParamEnvAnd<'tcx, Self>>,
) -> Result<CanonicalQueryResponse<'tcx, Self::QueryResponse>, NoSolution> { ) -> Result<CanonicalQueryResponse<'tcx, Self::QueryResponse>, NoSolution> {
if tcx.sess.opts.unstable_opts.no_implied_bounds_compat { tcx.implied_outlives_bounds((canonicalized, false))
tcx.implied_outlives_bounds(canonicalized)
} else {
tcx.implied_outlives_bounds_compat(canonicalized)
}
} }
fn perform_locally_with_next_solver( fn perform_locally_with_next_solver(
@ -47,11 +44,7 @@ impl<'tcx> super::QueryTypeOp<'tcx> for ImpliedOutlivesBounds<'tcx> {
key: ParamEnvAnd<'tcx, Self>, key: ParamEnvAnd<'tcx, Self>,
span: Span, span: Span,
) -> Result<Self::QueryResponse, NoSolution> { ) -> Result<Self::QueryResponse, NoSolution> {
if ocx.infcx.tcx.sess.opts.unstable_opts.no_implied_bounds_compat { compute_implied_outlives_bounds_inner(ocx, key.param_env, key.value.ty, span, false)
compute_implied_outlives_bounds_inner(ocx, key.param_env, key.value.ty, span)
} else {
compute_implied_outlives_bounds_compat_inner(ocx, key.param_env, key.value.ty, span)
}
} }
} }
@ -60,18 +53,15 @@ pub fn compute_implied_outlives_bounds_inner<'tcx>(
param_env: ty::ParamEnv<'tcx>, param_env: ty::ParamEnv<'tcx>,
ty: Ty<'tcx>, ty: Ty<'tcx>,
span: Span, span: Span,
disable_implied_bounds_hack: bool,
) -> Result<Vec<OutlivesBound<'tcx>>, NoSolution> { ) -> Result<Vec<OutlivesBound<'tcx>>, NoSolution> {
let normalize_op = |ty| -> Result<_, NoSolution> { let normalize_ty = |ty| -> Result<_, NoSolution> {
// We must normalize the type so we can compute the right outlives components. // We must normalize the type so we can compute the right outlives components.
// for example, if we have some constrained param type like `T: Trait<Out = U>`, // for example, if we have some constrained param type like `T: Trait<Out = U>`,
// and we know that `&'a T::Out` is WF, then we want to imply `U: 'a`. // and we know that `&'a T::Out` is WF, then we want to imply `U: 'a`.
let ty = ocx let ty = ocx
.deeply_normalize(&ObligationCause::dummy_with_span(span), param_env, ty) .deeply_normalize(&ObligationCause::dummy_with_span(span), param_env, ty)
.map_err(|_| NoSolution)?; .map_err(|_| NoSolution)?;
if !ocx.select_all_or_error().is_empty() {
return Err(NoSolution);
}
let ty = OpportunisticRegionResolver::new(&ocx.infcx).fold_ty(ty);
Ok(ty) Ok(ty)
}; };
@ -81,7 +71,7 @@ pub fn compute_implied_outlives_bounds_inner<'tcx>(
// guaranteed to be a subset of the original type, so we need to store the // guaranteed to be a subset of the original type, so we need to store the
// WF args we've computed in a set. // WF args we've computed in a set.
let mut checked_wf_args = rustc_data_structures::fx::FxHashSet::default(); let mut checked_wf_args = rustc_data_structures::fx::FxHashSet::default();
let mut wf_args = vec![ty.into(), normalize_op(ty)?.into()]; let mut wf_args = vec![ty.into(), normalize_ty(ty)?.into()];
let mut outlives_bounds: Vec<OutlivesBound<'tcx>> = vec![]; let mut outlives_bounds: Vec<OutlivesBound<'tcx>> = vec![];
@ -96,8 +86,14 @@ pub fn compute_implied_outlives_bounds_inner<'tcx>(
.into_iter() .into_iter()
.flatten() .flatten()
{ {
assert!(!obligation.has_escaping_bound_vars()); let pred = ocx
let Some(pred) = obligation.predicate.kind().no_bound_vars() else { .deeply_normalize(
&ObligationCause::dummy_with_span(span),
param_env,
obligation.predicate,
)
.map_err(|_| NoSolution)?;
let Some(pred) = pred.kind().no_bound_vars() else {
continue; continue;
}; };
match pred { match pred {
@ -130,7 +126,6 @@ pub fn compute_implied_outlives_bounds_inner<'tcx>(
ty_a, ty_a,
r_b, r_b,
))) => { ))) => {
let ty_a = normalize_op(ty_a)?;
let mut components = smallvec![]; let mut components = smallvec![];
push_outlives_components(ocx.infcx.tcx, ty_a, &mut components); push_outlives_components(ocx.infcx.tcx, ty_a, &mut components);
outlives_bounds.extend(implied_bounds_from_components(r_b, components)) outlives_bounds.extend(implied_bounds_from_components(r_b, components))
@ -139,141 +134,48 @@ pub fn compute_implied_outlives_bounds_inner<'tcx>(
} }
} }
// If we detect `bevy_ecs::*::ParamSet` in the WF args list (and `disable_implied_bounds_hack`
// or `-Zno-implied-bounds-compat` are not set), then use the registered outlives obligations
// as implied bounds.
if !disable_implied_bounds_hack
&& !ocx.infcx.tcx.sess.opts.unstable_opts.no_implied_bounds_compat
&& ty.visit_with(&mut ContainsBevyParamSet { tcx: ocx.infcx.tcx }).is_break()
{
for RegionObligation { sup_type, sub_region, .. } in
ocx.infcx.take_registered_region_obligations()
{
let mut components = smallvec![];
push_outlives_components(ocx.infcx.tcx, sup_type, &mut components);
outlives_bounds.extend(implied_bounds_from_components(sub_region, components));
}
}
Ok(outlives_bounds) Ok(outlives_bounds)
} }
pub fn compute_implied_outlives_bounds_compat_inner<'tcx>( struct ContainsBevyParamSet<'tcx> {
ocx: &ObligationCtxt<'_, 'tcx>, tcx: TyCtxt<'tcx>,
param_env: ty::ParamEnv<'tcx>, }
ty: Ty<'tcx>,
span: Span,
) -> Result<Vec<OutlivesBound<'tcx>>, NoSolution> {
let tcx = ocx.infcx.tcx;
// Sometimes when we ask what it takes for T: WF, we get back that impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for ContainsBevyParamSet<'tcx> {
// U: WF is required; in that case, we push U onto this stack and type Result = ControlFlow<()>;
// process it next. Because the resulting predicates aren't always
// guaranteed to be a subset of the original type, so we need to store the
// WF args we've computed in a set.
let mut checked_wf_args = rustc_data_structures::fx::FxHashSet::default();
let mut wf_args = vec![ty.into()];
let mut outlives_bounds: Vec<ty::OutlivesPredicate<'tcx, ty::GenericArg<'tcx>>> = vec![]; fn visit_ty(&mut self, t: Ty<'tcx>) -> Self::Result {
// We only care to match `ParamSet<T>` or `&ParamSet<T>`.
while let Some(arg) = wf_args.pop() { match t.kind() {
if !checked_wf_args.insert(arg) { ty::Adt(def, _) => {
continue; if self.tcx.item_name(def.did()) == sym::ParamSet
} && self.tcx.crate_name(def.did().krate) == sym::bevy_ecs
{
// Compute the obligations for `arg` to be well-formed. If `arg` is return ControlFlow::Break(());
// an unresolved inference variable, just instantiated an empty set
// -- because the return type here is going to be things we *add*
// to the environment, it's always ok for this set to be smaller
// than the ultimate set. (Note: normally there won't be
// unresolved inference variables here anyway, but there might be
// during typeck under some circumstances.)
//
// FIXME(@lcnr): It's not really "always fine", having fewer implied
// bounds can be backward incompatible, e.g. #101951 was caused by
// us not dealing with inference vars in `TypeOutlives` predicates.
let obligations =
wf::obligations(ocx.infcx, param_env, CRATE_DEF_ID, 0, arg, span).unwrap_or_default();
for obligation in obligations {
debug!(?obligation);
assert!(!obligation.has_escaping_bound_vars());
// While these predicates should all be implied by other parts of
// the program, they are still relevant as they may constrain
// inference variables, which is necessary to add the correct
// implied bounds in some cases, mostly when dealing with projections.
//
// Another important point here: we only register `Projection`
// predicates, since otherwise we might register outlives
// predicates containing inference variables, and we don't
// learn anything new from those.
if obligation.predicate.has_non_region_infer() {
match obligation.predicate.kind().skip_binder() {
ty::PredicateKind::Clause(ty::ClauseKind::Projection(..))
| ty::PredicateKind::AliasRelate(..) => {
ocx.register_obligation(obligation.clone());
}
_ => {}
} }
} }
ty::Ref(_, ty, _) => ty.visit_with(self)?,
let pred = match obligation.predicate.kind().no_bound_vars() { _ => {}
None => continue,
Some(pred) => pred,
};
match pred {
// FIXME(const_generics): Make sure that `<'a, 'b, const N: &'a &'b u32>` is sound
// if we ever support that
ty::PredicateKind::Clause(ty::ClauseKind::Trait(..))
| ty::PredicateKind::Clause(ty::ClauseKind::HostEffect(..))
| ty::PredicateKind::Clause(ty::ClauseKind::ConstArgHasType(..))
| ty::PredicateKind::Subtype(..)
| ty::PredicateKind::Coerce(..)
| ty::PredicateKind::Clause(ty::ClauseKind::Projection(..))
| ty::PredicateKind::DynCompatible(..)
| ty::PredicateKind::Clause(ty::ClauseKind::ConstEvaluatable(..))
| ty::PredicateKind::ConstEquate(..)
| ty::PredicateKind::Ambiguous
| ty::PredicateKind::NormalizesTo(..)
| ty::PredicateKind::AliasRelate(..) => {}
// We need to search through *all* WellFormed predicates
ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(arg)) => {
wf_args.push(arg);
}
// We need to register region relationships
ty::PredicateKind::Clause(ty::ClauseKind::RegionOutlives(
ty::OutlivesPredicate(r_a, r_b),
)) => outlives_bounds.push(ty::OutlivesPredicate(r_a.into(), r_b)),
ty::PredicateKind::Clause(ty::ClauseKind::TypeOutlives(ty::OutlivesPredicate(
ty_a,
r_b,
))) => outlives_bounds.push(ty::OutlivesPredicate(ty_a.into(), r_b)),
}
} }
}
// This call to `select_all_or_error` is necessary to constrain inference variables, which we ControlFlow::Continue(())
// use further down when computing the implied bounds.
match ocx.select_all_or_error().as_slice() {
[] => (),
_ => return Err(NoSolution),
} }
// We lazily compute the outlives components as
// `select_all_or_error` constrains inference variables.
let mut implied_bounds = Vec::new();
for ty::OutlivesPredicate(a, r_b) in outlives_bounds {
match a.unpack() {
ty::GenericArgKind::Lifetime(r_a) => {
implied_bounds.push(OutlivesBound::RegionSubRegion(r_b, r_a))
}
ty::GenericArgKind::Type(ty_a) => {
let mut ty_a = ocx.infcx.resolve_vars_if_possible(ty_a);
// Need to manually normalize in the new solver as `wf::obligations` does not.
if ocx.infcx.next_trait_solver() {
ty_a = ocx
.deeply_normalize(&ObligationCause::dummy_with_span(span), param_env, ty_a)
.map_err(|_| NoSolution)?;
}
let mut components = smallvec![];
push_outlives_components(tcx, ty_a, &mut components);
implied_bounds.extend(implied_bounds_from_components(r_b, components))
}
ty::GenericArgKind::Const(_) => {
unreachable!("consts do not participate in outlives bounds")
}
}
}
Ok(implied_bounds)
} }
/// When we have an implied bound that `T: 'a`, we can further break /// When we have an implied bound that `T: 'a`, we can further break

View file

@ -10,38 +10,28 @@ use rustc_middle::query::Providers;
use rustc_middle::ty::TyCtxt; use rustc_middle::ty::TyCtxt;
use rustc_span::DUMMY_SP; use rustc_span::DUMMY_SP;
use rustc_trait_selection::infer::InferCtxtBuilderExt; use rustc_trait_selection::infer::InferCtxtBuilderExt;
use rustc_trait_selection::traits::query::type_op::implied_outlives_bounds::{ use rustc_trait_selection::traits::query::type_op::implied_outlives_bounds::compute_implied_outlives_bounds_inner;
compute_implied_outlives_bounds_compat_inner, compute_implied_outlives_bounds_inner,
};
use rustc_trait_selection::traits::query::{CanonicalImpliedOutlivesBoundsGoal, NoSolution}; use rustc_trait_selection::traits::query::{CanonicalImpliedOutlivesBoundsGoal, NoSolution};
pub(crate) fn provide(p: &mut Providers) { pub(crate) fn provide(p: &mut Providers) {
*p = Providers { implied_outlives_bounds_compat, ..*p };
*p = Providers { implied_outlives_bounds, ..*p }; *p = Providers { implied_outlives_bounds, ..*p };
} }
fn implied_outlives_bounds_compat<'tcx>(
tcx: TyCtxt<'tcx>,
goal: CanonicalImpliedOutlivesBoundsGoal<'tcx>,
) -> Result<
&'tcx Canonical<'tcx, canonical::QueryResponse<'tcx, Vec<OutlivesBound<'tcx>>>>,
NoSolution,
> {
tcx.infer_ctxt().enter_canonical_trait_query(&goal, |ocx, key| {
let (param_env, ImpliedOutlivesBounds { ty }) = key.into_parts();
compute_implied_outlives_bounds_compat_inner(ocx, param_env, ty, DUMMY_SP)
})
}
fn implied_outlives_bounds<'tcx>( fn implied_outlives_bounds<'tcx>(
tcx: TyCtxt<'tcx>, tcx: TyCtxt<'tcx>,
goal: CanonicalImpliedOutlivesBoundsGoal<'tcx>, (goal, disable_implied_bounds_hack): (CanonicalImpliedOutlivesBoundsGoal<'tcx>, bool),
) -> Result< ) -> Result<
&'tcx Canonical<'tcx, canonical::QueryResponse<'tcx, Vec<OutlivesBound<'tcx>>>>, &'tcx Canonical<'tcx, canonical::QueryResponse<'tcx, Vec<OutlivesBound<'tcx>>>>,
NoSolution, NoSolution,
> { > {
tcx.infer_ctxt().enter_canonical_trait_query(&goal, |ocx, key| { tcx.infer_ctxt().enter_canonical_trait_query(&goal, |ocx, key| {
let (param_env, ImpliedOutlivesBounds { ty }) = key.into_parts(); let (param_env, ImpliedOutlivesBounds { ty }) = key.into_parts();
compute_implied_outlives_bounds_inner(ocx, param_env, ty, DUMMY_SP) compute_implied_outlives_bounds_inner(
ocx,
param_env,
ty,
DUMMY_SP,
disable_implied_bounds_hack,
)
}) })
} }

View file

@ -20,5 +20,6 @@ fn bar(_: Foo<for<'a> fn(&'a ())>::Assoc) {}
//~| ERROR mismatched types //~| ERROR mismatched types
//~| ERROR higher-ranked subtype error //~| ERROR higher-ranked subtype error
//~| ERROR higher-ranked subtype error //~| ERROR higher-ranked subtype error
//~| ERROR higher-ranked subtype error
fn main() {} fn main() {}

View file

@ -31,6 +31,14 @@ LL | fn bar(_: Foo<for<'a> fn(&'a ())>::Assoc) {}
| |
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no` = note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
error: aborting due to 4 previous errors error: higher-ranked subtype error
--> $DIR/issue-109789.rs:18:1
|
LL | fn bar(_: Foo<for<'a> fn(&'a ())>::Assoc) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
error: aborting due to 5 previous errors
For more information about this error, try `rustc --explain E0308`. For more information about this error, try `rustc --explain E0308`.

View file

@ -12,5 +12,6 @@ fn bar(_: fn(Foo<for<'b> fn(Foo<fn(&'b ())>::Assoc)>::Assoc)) {}
//~| ERROR mismatched types [E0308] //~| ERROR mismatched types [E0308]
//~| ERROR higher-ranked subtype error //~| ERROR higher-ranked subtype error
//~| ERROR higher-ranked subtype error //~| ERROR higher-ranked subtype error
//~| ERROR higher-ranked subtype error
fn main() {} fn main() {}

View file

@ -31,6 +31,14 @@ LL | fn bar(_: fn(Foo<for<'b> fn(Foo<fn(&'b ())>::Assoc)>::Assoc)) {}
| |
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no` = note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
error: aborting due to 4 previous errors error: higher-ranked subtype error
--> $DIR/issue-111404-1.rs:10:1
|
LL | fn bar(_: fn(Foo<for<'b> fn(Foo<fn(&'b ())>::Assoc)>::Assoc)) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
error: aborting due to 5 previous errors
For more information about this error, try `rustc --explain E0308`. For more information about this error, try `rustc --explain E0308`.

View file

@ -12,6 +12,7 @@ where
fn func1(foo: Foo<(&str,)>) { fn func1(foo: Foo<(&str,)>) {
//~^ ERROR `&str` does not fulfill the required lifetime //~^ ERROR `&str` does not fulfill the required lifetime
//~| ERROR lifetime may not live long enough
let _: &'static str = foo.0.0; let _: &'static str = foo.0.0;
} }
@ -19,5 +20,6 @@ trait TestTrait {}
impl<X> TestTrait for [Foo<(X,)>; 1] {} impl<X> TestTrait for [Foo<(X,)>; 1] {}
//~^ ERROR `X` may not live long enough //~^ ERROR `X` may not live long enough
//~| ERROR `X` may not live long enough
fn main() {} fn main() {}

View file

@ -7,7 +7,21 @@ LL | fn func1(foo: Foo<(&str,)>) {
= note: type must satisfy the static lifetime = note: type must satisfy the static lifetime
error[E0310]: the parameter type `X` may not live long enough error[E0310]: the parameter type `X` may not live long enough
--> $DIR/from-trait-impl.rs:20:23 --> $DIR/from-trait-impl.rs:21:1
|
LL | impl<X> TestTrait for [Foo<(X,)>; 1] {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| the parameter type `X` must be valid for the static lifetime...
| ...so that the type `X` will meet its required lifetime bounds
|
help: consider adding an explicit lifetime bound
|
LL | impl<X: 'static> TestTrait for [Foo<(X,)>; 1] {}
| +++++++++
error[E0310]: the parameter type `X` may not live long enough
--> $DIR/from-trait-impl.rs:21:23
| |
LL | impl<X> TestTrait for [Foo<(X,)>; 1] {} LL | impl<X> TestTrait for [Foo<(X,)>; 1] {}
| ^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^
@ -20,7 +34,16 @@ help: consider adding an explicit lifetime bound
LL | impl<X: 'static> TestTrait for [Foo<(X,)>; 1] {} LL | impl<X: 'static> TestTrait for [Foo<(X,)>; 1] {}
| +++++++++ | +++++++++
error: aborting due to 2 previous errors error: lifetime may not live long enough
--> $DIR/from-trait-impl.rs:13:1
|
LL | fn func1(foo: Foo<(&str,)>) {
| ^^^^^^^^^^^^^^^^^^^-^^^^^^^
| | |
| | let's call the lifetime of this reference `'1`
| requires that `'1` must outlive `'static`
error: aborting due to 4 previous errors
Some errors have detailed explanations: E0310, E0477. Some errors have detailed explanations: E0310, E0477.
For more information about an error, try `rustc --explain E0310`. For more information about an error, try `rustc --explain E0310`.

View file

@ -1,10 +0,0 @@
error: lifetime may not live long enough
--> $DIR/normalization-nested.rs:40:5
|
LL | pub fn test_borrowck<'x>(_: Map<Vec<&'x ()>>, s: &'x str) -> &'static str {
| -- lifetime `'x` defined here
LL | s
| ^ returning this value requires that `'x` must outlive `'static`
error: aborting due to 1 previous error

View file

@ -3,9 +3,7 @@
// //
//@ revisions: param_ty lifetime param_ty_no_compat lifetime_no_compat //@ revisions: param_ty lifetime param_ty_no_compat lifetime_no_compat
//@[param_ty] check-pass //@ check-pass
//@[param_ty_no_compat] check-pass
//@[lifetime_no_compat] check-pass
//@[param_ty_no_compat] compile-flags: -Zno-implied-bounds-compat //@[param_ty_no_compat] compile-flags: -Zno-implied-bounds-compat
//@[lifetime_no_compat] compile-flags: -Zno-implied-bounds-compat //@[lifetime_no_compat] compile-flags: -Zno-implied-bounds-compat
@ -38,7 +36,6 @@ pub fn test_wfcheck<'x>(_: Map<Vec<&'x ()>>) {}
pub fn test_borrowck<'x>(_: Map<Vec<&'x ()>>, s: &'x str) -> &'static str { pub fn test_borrowck<'x>(_: Map<Vec<&'x ()>>, s: &'x str) -> &'static str {
s s
//[lifetime]~^ ERROR lifetime may not live long enough
} }
fn main() {} fn main() {}

View file

@ -1,3 +1,9 @@
error[E0477]: the type `&'lt u8` does not fulfill the required lifetime
--> $DIR/normalization-placeholder-leak.rs:31:5
|
LL | fn test_lifetime<'lt, T: Trait>(_: Foo<&'lt u8>) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error[E0477]: the type `&'lt u8` does not fulfill the required lifetime error[E0477]: the type `&'lt u8` does not fulfill the required lifetime
--> $DIR/normalization-placeholder-leak.rs:31:40 --> $DIR/normalization-placeholder-leak.rs:31:40
| |
@ -5,11 +11,35 @@ LL | fn test_lifetime<'lt, T: Trait>(_: Foo<&'lt u8>) {}
| ^^^^^^^^^^^^ | ^^^^^^^^^^^^
error[E0477]: the type `<T as AnotherTrait>::Ty2<'lt>` does not fulfill the required lifetime error[E0477]: the type `<T as AnotherTrait>::Ty2<'lt>` does not fulfill the required lifetime
--> $DIR/normalization-placeholder-leak.rs:36:44 --> $DIR/normalization-placeholder-leak.rs:38:5
|
LL | fn test_alias<'lt, T: AnotherTrait>(_: Foo<T::Ty2::<'lt>>) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error[E0477]: the type `<T as AnotherTrait>::Ty2<'lt>` does not fulfill the required lifetime
--> $DIR/normalization-placeholder-leak.rs:38:44
| |
LL | fn test_alias<'lt, T: AnotherTrait>(_: Foo<T::Ty2::<'lt>>) {} LL | fn test_alias<'lt, T: AnotherTrait>(_: Foo<T::Ty2::<'lt>>) {}
| ^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^
error: aborting due to 2 previous errors error: lifetime may not live long enough
--> $DIR/normalization-placeholder-leak.rs:31:5
|
LL | fn test_lifetime<'lt, T: Trait>(_: Foo<&'lt u8>) {}
| ^^^^^^^^^^^^^^^^^---^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| | |
| | lifetime `'lt` defined here
| requires that `'lt` must outlive `'static`
error: lifetime may not live long enough
--> $DIR/normalization-placeholder-leak.rs:38:5
|
LL | fn test_alias<'lt, T: AnotherTrait>(_: Foo<T::Ty2::<'lt>>) {}
| ^^^^^^^^^^^^^^---^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| | |
| | lifetime `'lt` defined here
| requires that `'lt` must outlive `'static`
error: aborting due to 6 previous errors
For more information about this error, try `rustc --explain E0477`. For more information about this error, try `rustc --explain E0477`.

View file

@ -30,11 +30,15 @@ mod fail {
// don't use the bound to prove `'lt: 'static`. // don't use the bound to prove `'lt: 'static`.
fn test_lifetime<'lt, T: Trait>(_: Foo<&'lt u8>) {} fn test_lifetime<'lt, T: Trait>(_: Foo<&'lt u8>) {}
//[fail]~^ ERROR `&'lt u8` does not fulfill the required lifetime //[fail]~^ ERROR `&'lt u8` does not fulfill the required lifetime
//[fail]~| ERROR `&'lt u8` does not fulfill the required lifetime
//[fail]~| ERROR may not live long enough
// implied bound: `T::Ty2<'lt>: placeholder('x)`. // implied bound: `T::Ty2<'lt>: placeholder('x)`.
// don't use the bound to prove `T::Ty2<'lt>: 'static`. // don't use the bound to prove `T::Ty2<'lt>: 'static`.
fn test_alias<'lt, T: AnotherTrait>(_: Foo<T::Ty2::<'lt>>) {} fn test_alias<'lt, T: AnotherTrait>(_: Foo<T::Ty2::<'lt>>) {}
//[fail]~^ ERROR `<T as AnotherTrait>::Ty2<'lt>` does not fulfill the required lifetime //[fail]~^ ERROR `<T as AnotherTrait>::Ty2<'lt>` does not fulfill the required lifetime
//[fail]~| ERROR `<T as AnotherTrait>::Ty2<'lt>` does not fulfill the required lifetime
//[fail]~| ERROR may not live long enough
} }

View file

@ -27,11 +27,19 @@ pub struct ServiceChainBuilder<P: Service, S: Service<Input = P::Output>> {
} }
impl<P: Service, S: Service<Input = P::Output>> ServiceChainBuilder<P, S> { impl<P: Service, S: Service<Input = P::Output>> ServiceChainBuilder<P, S> {
pub fn next<NS: Service<Input = S::Output>>( pub fn next<NS: Service<Input = S::Output>>(
//~^ the associated type
//~| the associated type
//~| the associated type
//~| the associated type
//~| the associated type
//~| the associated type
//~| may not live long enough
self, self,
) -> ServiceChainBuilder<ServiceChain<P, S>, NS> { ) -> ServiceChainBuilder<ServiceChain<P, S>, NS> {
//~^ the associated type //~^ the associated type
//~| the associated type //~| the associated type
//~| the associated type //~| the associated type
//~| the associated type
panic!(); panic!();
} }
} }

View file

@ -1,5 +1,39 @@
error[E0310]: the associated type `<P as Service>::Error` may not live long enough error[E0310]: the associated type `<P as Service>::Error` may not live long enough
--> $DIR/sod_service_chain.rs:31:10 --> $DIR/sod_service_chain.rs:29:5
|
LL | / pub fn next<NS: Service<Input = S::Output>>(
... |
LL | | self,
LL | | ) -> ServiceChainBuilder<ServiceChain<P, S>, NS> {
| | ^
| | |
| |____________________________________________________the associated type `<P as Service>::Error` must be valid for the static lifetime...
| ...so that the type `<P as Service>::Error` will meet its required lifetime bounds
|
help: consider adding an explicit lifetime bound
|
LL | ) -> ServiceChainBuilder<ServiceChain<P, S>, NS> where <P as Service>::Error: 'static {
| ++++++++++++++++++++++++++++++++++++
error[E0310]: the associated type `<S as Service>::Error` may not live long enough
--> $DIR/sod_service_chain.rs:29:5
|
LL | / pub fn next<NS: Service<Input = S::Output>>(
... |
LL | | self,
LL | | ) -> ServiceChainBuilder<ServiceChain<P, S>, NS> {
| | ^
| | |
| |____________________________________________________the associated type `<S as Service>::Error` must be valid for the static lifetime...
| ...so that the type `<S as Service>::Error` will meet its required lifetime bounds
|
help: consider adding an explicit lifetime bound
|
LL | ) -> ServiceChainBuilder<ServiceChain<P, S>, NS> where <S as Service>::Error: 'static {
| ++++++++++++++++++++++++++++++++++++
error[E0310]: the associated type `<P as Service>::Error` may not live long enough
--> $DIR/sod_service_chain.rs:38:10
| |
LL | ) -> ServiceChainBuilder<ServiceChain<P, S>, NS> { LL | ) -> ServiceChainBuilder<ServiceChain<P, S>, NS> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -13,7 +47,7 @@ LL | ) -> ServiceChainBuilder<ServiceChain<P, S>, NS> where <P as Service>::
| ++++++++++++++++++++++++++++++++++++ | ++++++++++++++++++++++++++++++++++++
error[E0310]: the associated type `<S as Service>::Error` may not live long enough error[E0310]: the associated type `<S as Service>::Error` may not live long enough
--> $DIR/sod_service_chain.rs:31:10 --> $DIR/sod_service_chain.rs:38:10
| |
LL | ) -> ServiceChainBuilder<ServiceChain<P, S>, NS> { LL | ) -> ServiceChainBuilder<ServiceChain<P, S>, NS> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -26,6 +60,42 @@ help: consider adding an explicit lifetime bound
LL | ) -> ServiceChainBuilder<ServiceChain<P, S>, NS> where <S as Service>::Error: 'static { LL | ) -> ServiceChainBuilder<ServiceChain<P, S>, NS> where <S as Service>::Error: 'static {
| ++++++++++++++++++++++++++++++++++++ | ++++++++++++++++++++++++++++++++++++
error: aborting due to 2 previous errors error[E0310]: the associated type `<P as Service>::Error` may not live long enough
--> $DIR/sod_service_chain.rs:29:5
|
LL | / pub fn next<NS: Service<Input = S::Output>>(
... |
LL | | self,
LL | | ) -> ServiceChainBuilder<ServiceChain<P, S>, NS> {
| | ^
| | |
| |____________________________________________________the associated type `<P as Service>::Error` must be valid for the static lifetime...
| ...so that the type `<P as Service>::Error` will meet its required lifetime bounds
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
help: consider adding an explicit lifetime bound
|
LL | ) -> ServiceChainBuilder<ServiceChain<P, S>, NS> where <P as Service>::Error: 'static {
| ++++++++++++++++++++++++++++++++++++
error[E0310]: the associated type `<S as Service>::Error` may not live long enough
--> $DIR/sod_service_chain.rs:29:5
|
LL | / pub fn next<NS: Service<Input = S::Output>>(
... |
LL | | self,
LL | | ) -> ServiceChainBuilder<ServiceChain<P, S>, NS> {
| | ^
| | |
| |____________________________________________________the associated type `<S as Service>::Error` must be valid for the static lifetime...
| ...so that the type `<S as Service>::Error` will meet its required lifetime bounds
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
help: consider adding an explicit lifetime bound
|
LL | ) -> ServiceChainBuilder<ServiceChain<P, S>, NS> where <S as Service>::Error: 'static {
| ++++++++++++++++++++++++++++++++++++
error: aborting due to 6 previous errors
For more information about this error, try `rustc --explain E0310`. For more information about this error, try `rustc --explain E0310`.