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:
commit
6c60abf51a
19 changed files with 264 additions and 269 deletions
|
@ -17,13 +17,7 @@ impl<'tcx> OutlivesEnvironment<'tcx> {
|
|||
param_env: ty::ParamEnv<'tcx>,
|
||||
assumed_wf_tys: impl IntoIterator<Item = Ty<'tcx>>,
|
||||
) -> Self {
|
||||
Self::new_with_implied_bounds_compat(
|
||||
infcx,
|
||||
body_id,
|
||||
param_env,
|
||||
assumed_wf_tys,
|
||||
!infcx.tcx.sess.opts.unstable_opts.no_implied_bounds_compat,
|
||||
)
|
||||
Self::new_with_implied_bounds_compat(infcx, body_id, param_env, assumed_wf_tys, false)
|
||||
}
|
||||
|
||||
fn new_with_implied_bounds_compat(
|
||||
|
@ -31,7 +25,7 @@ impl<'tcx> OutlivesEnvironment<'tcx> {
|
|||
body_id: LocalDefId,
|
||||
param_env: ty::ParamEnv<'tcx>,
|
||||
assumed_wf_tys: impl IntoIterator<Item = Ty<'tcx>>,
|
||||
implied_bounds_compat: bool,
|
||||
disable_implied_bounds_hack: bool,
|
||||
) -> Self {
|
||||
let mut bounds = vec![];
|
||||
|
||||
|
@ -59,11 +53,11 @@ impl<'tcx> OutlivesEnvironment<'tcx> {
|
|||
OutlivesEnvironment::from_normalized_bounds(
|
||||
param_env,
|
||||
bounds,
|
||||
infcx.implied_bounds_tys_with_compat(
|
||||
infcx.implied_bounds_tys(
|
||||
body_id,
|
||||
param_env,
|
||||
assumed_wf_tys,
|
||||
implied_bounds_compat,
|
||||
disable_implied_bounds_hack,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
|
|
@ -36,7 +36,7 @@ fn implied_outlives_bounds<'a, 'tcx>(
|
|||
param_env: ty::ParamEnv<'tcx>,
|
||||
body_id: LocalDefId,
|
||||
ty: Ty<'tcx>,
|
||||
compat: bool,
|
||||
disable_implied_bounds_hack: bool,
|
||||
) -> Vec<OutlivesBound<'tcx>> {
|
||||
let ty = infcx.resolve_vars_if_possible(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 input = ImpliedOutlivesBounds { ty };
|
||||
let canonical = infcx.canonicalize_query(param_env.and(input), &mut canonical_var_values);
|
||||
let implied_bounds_result = if compat {
|
||||
infcx.tcx.implied_outlives_bounds_compat(canonical)
|
||||
} else {
|
||||
infcx.tcx.implied_outlives_bounds(canonical)
|
||||
};
|
||||
let implied_bounds_result =
|
||||
infcx.tcx.implied_outlives_bounds((canonical, disable_implied_bounds_hack));
|
||||
let Ok(canonical_result) = implied_bounds_result else {
|
||||
return vec![];
|
||||
};
|
||||
|
@ -110,14 +107,15 @@ fn implied_outlives_bounds<'a, 'tcx>(
|
|||
impl<'tcx> InferCtxt<'tcx> {
|
||||
/// 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.
|
||||
fn implied_bounds_tys_with_compat<Tys: IntoIterator<Item = Ty<'tcx>>>(
|
||||
fn implied_bounds_tys<Tys: IntoIterator<Item = Ty<'tcx>>>(
|
||||
&self,
|
||||
body_id: LocalDefId,
|
||||
param_env: ParamEnv<'tcx>,
|
||||
tys: Tys,
|
||||
compat: bool,
|
||||
disable_implied_bounds_hack: bool,
|
||||
) -> impl Iterator<Item = OutlivesBound<'tcx>> {
|
||||
tys.into_iter()
|
||||
.flat_map(move |ty| implied_outlives_bounds(self, param_env, body_id, ty, compat))
|
||||
tys.into_iter().flat_map(move |ty| {
|
||||
implied_outlives_bounds(self, param_env, body_id, ty, disable_implied_bounds_hack)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,15 +1,16 @@
|
|||
use std::ops::ControlFlow;
|
||||
|
||||
use rustc_infer::infer::RegionObligation;
|
||||
use rustc_infer::infer::canonical::CanonicalQueryInput;
|
||||
use rustc_infer::infer::resolve::OpportunisticRegionResolver;
|
||||
use rustc_infer::traits::query::OutlivesBound;
|
||||
use rustc_infer::traits::query::type_op::ImpliedOutlivesBounds;
|
||||
use rustc_middle::infer::canonical::CanonicalQueryResponse;
|
||||
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::{DUMMY_SP, Span};
|
||||
use rustc_span::{DUMMY_SP, Span, sym};
|
||||
use rustc_type_ir::outlives::{Component, push_outlives_components};
|
||||
use smallvec::{SmallVec, smallvec};
|
||||
use tracing::debug;
|
||||
|
||||
use crate::traits::query::NoSolution;
|
||||
use crate::traits::{ObligationCtxt, wf};
|
||||
|
@ -35,11 +36,7 @@ impl<'tcx> super::QueryTypeOp<'tcx> for ImpliedOutlivesBounds<'tcx> {
|
|||
tcx: TyCtxt<'tcx>,
|
||||
canonicalized: CanonicalQueryInput<'tcx, ParamEnvAnd<'tcx, Self>>,
|
||||
) -> Result<CanonicalQueryResponse<'tcx, Self::QueryResponse>, NoSolution> {
|
||||
if tcx.sess.opts.unstable_opts.no_implied_bounds_compat {
|
||||
tcx.implied_outlives_bounds(canonicalized)
|
||||
} else {
|
||||
tcx.implied_outlives_bounds_compat(canonicalized)
|
||||
}
|
||||
tcx.implied_outlives_bounds((canonicalized, false))
|
||||
}
|
||||
|
||||
fn perform_locally_with_next_solver(
|
||||
|
@ -47,11 +44,7 @@ impl<'tcx> super::QueryTypeOp<'tcx> for ImpliedOutlivesBounds<'tcx> {
|
|||
key: ParamEnvAnd<'tcx, Self>,
|
||||
span: Span,
|
||||
) -> 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)
|
||||
} else {
|
||||
compute_implied_outlives_bounds_compat_inner(ocx, key.param_env, key.value.ty, span)
|
||||
}
|
||||
compute_implied_outlives_bounds_inner(ocx, key.param_env, key.value.ty, span, false)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -60,18 +53,15 @@ pub fn compute_implied_outlives_bounds_inner<'tcx>(
|
|||
param_env: ty::ParamEnv<'tcx>,
|
||||
ty: Ty<'tcx>,
|
||||
span: Span,
|
||||
disable_implied_bounds_hack: bool,
|
||||
) -> 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.
|
||||
// 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`.
|
||||
let ty = ocx
|
||||
.deeply_normalize(&ObligationCause::dummy_with_span(span), param_env, ty)
|
||||
.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)
|
||||
};
|
||||
|
||||
|
@ -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
|
||||
// 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(), normalize_op(ty)?.into()];
|
||||
let mut wf_args = vec![ty.into(), normalize_ty(ty)?.into()];
|
||||
|
||||
let mut outlives_bounds: Vec<OutlivesBound<'tcx>> = vec![];
|
||||
|
||||
|
@ -96,8 +86,14 @@ pub fn compute_implied_outlives_bounds_inner<'tcx>(
|
|||
.into_iter()
|
||||
.flatten()
|
||||
{
|
||||
assert!(!obligation.has_escaping_bound_vars());
|
||||
let Some(pred) = obligation.predicate.kind().no_bound_vars() else {
|
||||
let pred = ocx
|
||||
.deeply_normalize(
|
||||
&ObligationCause::dummy_with_span(span),
|
||||
param_env,
|
||||
obligation.predicate,
|
||||
)
|
||||
.map_err(|_| NoSolution)?;
|
||||
let Some(pred) = pred.kind().no_bound_vars() else {
|
||||
continue;
|
||||
};
|
||||
match pred {
|
||||
|
@ -130,7 +126,6 @@ pub fn compute_implied_outlives_bounds_inner<'tcx>(
|
|||
ty_a,
|
||||
r_b,
|
||||
))) => {
|
||||
let ty_a = normalize_op(ty_a)?;
|
||||
let mut components = smallvec![];
|
||||
push_outlives_components(ocx.infcx.tcx, ty_a, &mut 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)
|
||||
}
|
||||
|
||||
pub fn compute_implied_outlives_bounds_compat_inner<'tcx>(
|
||||
ocx: &ObligationCtxt<'_, 'tcx>,
|
||||
param_env: ty::ParamEnv<'tcx>,
|
||||
ty: Ty<'tcx>,
|
||||
span: Span,
|
||||
) -> Result<Vec<OutlivesBound<'tcx>>, NoSolution> {
|
||||
let tcx = ocx.infcx.tcx;
|
||||
struct ContainsBevyParamSet<'tcx> {
|
||||
tcx: TyCtxt<'tcx>,
|
||||
}
|
||||
|
||||
// Sometimes when we ask what it takes for T: WF, we get back that
|
||||
// U: WF is required; in that case, we push U onto this stack and
|
||||
// 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()];
|
||||
impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for ContainsBevyParamSet<'tcx> {
|
||||
type Result = ControlFlow<()>;
|
||||
|
||||
let mut outlives_bounds: Vec<ty::OutlivesPredicate<'tcx, ty::GenericArg<'tcx>>> = vec![];
|
||||
|
||||
while let Some(arg) = wf_args.pop() {
|
||||
if !checked_wf_args.insert(arg) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Compute the obligations for `arg` to be well-formed. If `arg` is
|
||||
// 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());
|
||||
}
|
||||
_ => {}
|
||||
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(());
|
||||
}
|
||||
}
|
||||
|
||||
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)),
|
||||
}
|
||||
ty::Ref(_, ty, _) => ty.visit_with(self)?,
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
// This call to `select_all_or_error` is necessary to constrain inference variables, which we
|
||||
// use further down when computing the implied bounds.
|
||||
match ocx.select_all_or_error().as_slice() {
|
||||
[] => (),
|
||||
_ => return Err(NoSolution),
|
||||
ControlFlow::Continue(())
|
||||
}
|
||||
|
||||
// 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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue