Skip to content

Commit d759958

Browse files
Only use implied bounds hack if bevy, and use deeply normalize in implied bounds hack
1 parent fd17dea commit d759958

19 files changed

+264
-269
lines changed

compiler/rustc_hir_analysis/src/check/wfcheck.rs

+18-48
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,14 @@ where
126126

127127
let infcx_compat = infcx.fork();
128128

129-
// We specifically want to call the non-compat version of `implied_bounds_tys`; we do this always.
129+
// We specifically want to *disable* the implied bounds hack, first,
130+
// so we can detect when failures are due to bevy's implied bounds.
130131
let outlives_env = OutlivesEnvironment::new_with_implied_bounds_compat(
131132
&infcx,
132133
body_def_id,
133134
param_env,
134135
assumed_wf_types.iter().copied(),
135-
false,
136+
true,
136137
);
137138

138139
lint_redundant_lifetimes(tcx, body_def_id, &outlives_env);
@@ -142,53 +143,22 @@ where
142143
return Ok(());
143144
}
144145

145-
let is_bevy = assumed_wf_types.visit_with(&mut ContainsBevyParamSet { tcx }).is_break();
146-
147-
// If we have set `no_implied_bounds_compat`, then do not attempt compatibility.
148-
// We could also just always enter if `is_bevy`, and call `implied_bounds_tys`,
149-
// but that does result in slightly more work when this option is set and
150-
// just obscures what we mean here anyways. Let's just be explicit.
151-
if is_bevy && !infcx.tcx.sess.opts.unstable_opts.no_implied_bounds_compat {
152-
let outlives_env = OutlivesEnvironment::new_with_implied_bounds_compat(
153-
&infcx,
154-
body_def_id,
155-
param_env,
156-
assumed_wf_types,
157-
true,
158-
);
159-
let errors_compat = infcx_compat.resolve_regions_with_outlives_env(&outlives_env);
160-
if errors_compat.is_empty() {
161-
Ok(())
162-
} else {
163-
Err(infcx_compat.err_ctxt().report_region_errors(body_def_id, &errors_compat))
164-
}
146+
let outlives_env = OutlivesEnvironment::new_with_implied_bounds_compat(
147+
&infcx_compat,
148+
body_def_id,
149+
param_env,
150+
assumed_wf_types,
151+
// Don't *disable* the implied bounds hack; though this will only apply
152+
// the implied bounds hack if this contains `bevy_ecs`'s `ParamSet` type.
153+
false,
154+
);
155+
let errors_compat = infcx_compat.resolve_regions_with_outlives_env(&outlives_env);
156+
if errors_compat.is_empty() {
157+
// FIXME: Once we fix bevy, this would be the place to insert a warning
158+
// to upgrade bevy.
159+
Ok(())
165160
} else {
166-
Err(infcx.err_ctxt().report_region_errors(body_def_id, &errors))
167-
}
168-
}
169-
170-
struct ContainsBevyParamSet<'tcx> {
171-
tcx: TyCtxt<'tcx>,
172-
}
173-
174-
impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for ContainsBevyParamSet<'tcx> {
175-
type Result = ControlFlow<()>;
176-
177-
fn visit_ty(&mut self, t: Ty<'tcx>) -> Self::Result {
178-
// We only care to match `ParamSet<T>` or `&ParamSet<T>`.
179-
match t.kind() {
180-
ty::Adt(def, _) => {
181-
if self.tcx.item_name(def.did()) == sym::ParamSet
182-
&& self.tcx.crate_name(def.did().krate) == sym::bevy_ecs
183-
{
184-
return ControlFlow::Break(());
185-
}
186-
}
187-
ty::Ref(_, ty, _) => ty.visit_with(self)?,
188-
_ => {}
189-
}
190-
191-
ControlFlow::Continue(())
161+
Err(infcx_compat.err_ctxt().report_region_errors(body_def_id, &errors_compat))
192162
}
193163
}
194164

compiler/rustc_middle/src/query/keys.rs

+8
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,14 @@ impl<'tcx, T: Clone> Key for CanonicalQueryInput<'tcx, T> {
508508
}
509509
}
510510

511+
impl<'tcx, T: Clone> Key for (CanonicalQueryInput<'tcx, T>, bool) {
512+
type Cache<V> = DefaultCache<Self, V>;
513+
514+
fn default_span(&self, _tcx: TyCtxt<'_>) -> Span {
515+
DUMMY_SP
516+
}
517+
}
518+
511519
impl Key for (Symbol, u32, u32) {
512520
type Cache<V> = DefaultCache<Self, V>;
513521

compiler/rustc_middle/src/query/mod.rs

+2-11
Original file line numberDiff line numberDiff line change
@@ -2262,22 +2262,13 @@ rustc_queries! {
22622262
desc { "normalizing `{}`", goal.value }
22632263
}
22642264

2265-
query implied_outlives_bounds_compat(
2266-
goal: CanonicalImpliedOutlivesBoundsGoal<'tcx>
2267-
) -> Result<
2268-
&'tcx Canonical<'tcx, canonical::QueryResponse<'tcx, Vec<OutlivesBound<'tcx>>>>,
2269-
NoSolution,
2270-
> {
2271-
desc { "computing implied outlives bounds for `{}`", goal.canonical.value.value.ty }
2272-
}
2273-
22742265
query implied_outlives_bounds(
2275-
goal: CanonicalImpliedOutlivesBoundsGoal<'tcx>
2266+
key: (CanonicalImpliedOutlivesBoundsGoal<'tcx>, bool)
22762267
) -> Result<
22772268
&'tcx Canonical<'tcx, canonical::QueryResponse<'tcx, Vec<OutlivesBound<'tcx>>>>,
22782269
NoSolution,
22792270
> {
2280-
desc { "computing implied outlives bounds v2 for `{}`", goal.canonical.value.value.ty }
2271+
desc { "computing implied outlives bounds for `{}` (hack disabled = {:?})", key.0.canonical.value.value.ty, key.1 }
22812272
}
22822273

22832274
/// Do not call this query directly:

compiler/rustc_trait_selection/src/regions.rs

+4-10
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,15 @@ impl<'tcx> OutlivesEnvironment<'tcx> {
1717
param_env: ty::ParamEnv<'tcx>,
1818
assumed_wf_tys: impl IntoIterator<Item = Ty<'tcx>>,
1919
) -> Self {
20-
Self::new_with_implied_bounds_compat(
21-
infcx,
22-
body_id,
23-
param_env,
24-
assumed_wf_tys,
25-
!infcx.tcx.sess.opts.unstable_opts.no_implied_bounds_compat,
26-
)
20+
Self::new_with_implied_bounds_compat(infcx, body_id, param_env, assumed_wf_tys, false)
2721
}
2822

2923
fn new_with_implied_bounds_compat(
3024
infcx: &InferCtxt<'tcx>,
3125
body_id: LocalDefId,
3226
param_env: ty::ParamEnv<'tcx>,
3327
assumed_wf_tys: impl IntoIterator<Item = Ty<'tcx>>,
34-
implied_bounds_compat: bool,
28+
disable_implied_bounds_hack: bool,
3529
) -> Self {
3630
let mut bounds = vec![];
3731

@@ -59,11 +53,11 @@ impl<'tcx> OutlivesEnvironment<'tcx> {
5953
OutlivesEnvironment::from_normalized_bounds(
6054
param_env,
6155
bounds,
62-
infcx.implied_bounds_tys_with_compat(
56+
infcx.implied_bounds_tys(
6357
body_id,
6458
param_env,
6559
assumed_wf_tys,
66-
implied_bounds_compat,
60+
disable_implied_bounds_hack,
6761
),
6862
)
6963
}

compiler/rustc_trait_selection/src/traits/outlives_bounds.rs

+8-10
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ fn implied_outlives_bounds<'a, 'tcx>(
3636
param_env: ty::ParamEnv<'tcx>,
3737
body_id: LocalDefId,
3838
ty: Ty<'tcx>,
39-
compat: bool,
39+
disable_implied_bounds_hack: bool,
4040
) -> Vec<OutlivesBound<'tcx>> {
4141
let ty = infcx.resolve_vars_if_possible(ty);
4242
let ty = OpportunisticRegionResolver::new(infcx).fold_ty(ty);
@@ -52,11 +52,8 @@ fn implied_outlives_bounds<'a, 'tcx>(
5252
let mut canonical_var_values = OriginalQueryValues::default();
5353
let input = ImpliedOutlivesBounds { ty };
5454
let canonical = infcx.canonicalize_query(param_env.and(input), &mut canonical_var_values);
55-
let implied_bounds_result = if compat {
56-
infcx.tcx.implied_outlives_bounds_compat(canonical)
57-
} else {
58-
infcx.tcx.implied_outlives_bounds(canonical)
59-
};
55+
let implied_bounds_result =
56+
infcx.tcx.implied_outlives_bounds((canonical, disable_implied_bounds_hack));
6057
let Ok(canonical_result) = implied_bounds_result else {
6158
return vec![];
6259
};
@@ -110,14 +107,15 @@ fn implied_outlives_bounds<'a, 'tcx>(
110107
impl<'tcx> InferCtxt<'tcx> {
111108
/// Do *NOT* call this directly. You probably want to construct a `OutlivesEnvironment`
112109
/// instead if you're interested in the implied bounds for a given signature.
113-
fn implied_bounds_tys_with_compat<Tys: IntoIterator<Item = Ty<'tcx>>>(
110+
fn implied_bounds_tys<Tys: IntoIterator<Item = Ty<'tcx>>>(
114111
&self,
115112
body_id: LocalDefId,
116113
param_env: ParamEnv<'tcx>,
117114
tys: Tys,
118-
compat: bool,
115+
disable_implied_bounds_hack: bool,
119116
) -> impl Iterator<Item = OutlivesBound<'tcx>> {
120-
tys.into_iter()
121-
.flat_map(move |ty| implied_outlives_bounds(self, param_env, body_id, ty, compat))
117+
tys.into_iter().flat_map(move |ty| {
118+
implied_outlives_bounds(self, param_env, body_id, ty, disable_implied_bounds_hack)
119+
})
122120
}
123121
}

0 commit comments

Comments
 (0)