Skip to content

Commit 9832374

Browse files
committed
Auto merge of #76893 - lcnr:existential-proj, r=estebank
Improve `skip_binder` usage during FlagComputation It looks like there was previously a bug around `ExistentialPredicate::Projection` here, don't know how to best trigger that one to add a regression test though.
2 parents 5546335 + 7652b4b commit 9832374

File tree

2 files changed

+33
-37
lines changed

2 files changed

+33
-37
lines changed

Diff for: compiler/rustc_middle/src/ty/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ impl<'tcx> CtxtInterners<'tcx> {
134134
fn intern_predicate(&self, kind: PredicateKind<'tcx>) -> &'tcx PredicateInner<'tcx> {
135135
self.predicate
136136
.intern(kind, |kind| {
137-
let flags = super::flags::FlagComputation::for_predicate(&kind);
137+
let flags = super::flags::FlagComputation::for_predicate(kind);
138138

139139
let predicate_struct = PredicateInner {
140140
kind,

Diff for: compiler/rustc_middle/src/ty/flags.rs

+32-36
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ impl FlagComputation {
2222
result
2323
}
2424

25-
pub fn for_predicate(kind: &ty::PredicateKind<'_>) -> FlagComputation {
25+
pub fn for_predicate(kind: ty::PredicateKind<'_>) -> FlagComputation {
2626
let mut result = FlagComputation::new();
2727
result.add_predicate_kind(kind);
2828
result
@@ -53,7 +53,14 @@ impl FlagComputation {
5353

5454
/// Adds the flags/depth from a set of types that appear within the current type, but within a
5555
/// region binder.
56-
fn add_bound_computation(&mut self, computation: FlagComputation) {
56+
fn bound_computation<T, F>(&mut self, value: ty::Binder<T>, f: F)
57+
where
58+
F: FnOnce(&mut Self, T),
59+
{
60+
let mut computation = FlagComputation::new();
61+
62+
f(&mut computation, value.skip_binder());
63+
5764
self.add_flags(computation.flags);
5865

5966
// The types that contributed to `computation` occurred within
@@ -101,9 +108,7 @@ impl FlagComputation {
101108
}
102109

103110
&ty::GeneratorWitness(ts) => {
104-
let mut computation = FlagComputation::new();
105-
computation.add_tys(ts.skip_binder());
106-
self.add_bound_computation(computation);
111+
self.bound_computation(ts, |flags, ts| flags.add_tys(ts));
107112
}
108113

109114
&ty::Closure(_, substs) => {
@@ -154,20 +159,21 @@ impl FlagComputation {
154159
self.add_substs(substs);
155160
}
156161

157-
&ty::Dynamic(ref obj, r) => {
158-
let mut computation = FlagComputation::new();
159-
for predicate in obj.skip_binder().iter() {
160-
match predicate {
161-
ty::ExistentialPredicate::Trait(tr) => computation.add_substs(tr.substs),
162-
ty::ExistentialPredicate::Projection(p) => {
163-
let mut proj_computation = FlagComputation::new();
164-
proj_computation.add_existential_projection(&p);
165-
self.add_bound_computation(proj_computation);
162+
&ty::Dynamic(obj, r) => {
163+
self.bound_computation(obj, |computation, obj| {
164+
for predicate in obj.iter() {
165+
match predicate {
166+
ty::ExistentialPredicate::Trait(tr) => {
167+
computation.add_substs(tr.substs)
168+
}
169+
ty::ExistentialPredicate::Projection(p) => {
170+
computation.add_existential_projection(&p);
171+
}
172+
ty::ExistentialPredicate::AutoTrait(_) => {}
166173
}
167-
ty::ExistentialPredicate::AutoTrait(_) => {}
168174
}
169-
}
170-
self.add_bound_computation(computation);
175+
});
176+
171177
self.add_region(r);
172178
}
173179

@@ -195,22 +201,21 @@ impl FlagComputation {
195201
self.add_substs(substs);
196202
}
197203

198-
&ty::FnPtr(f) => {
199-
self.add_fn_sig(f);
200-
}
204+
&ty::FnPtr(fn_sig) => self.bound_computation(fn_sig, |computation, fn_sig| {
205+
computation.add_tys(fn_sig.inputs());
206+
computation.add_ty(fn_sig.output());
207+
}),
201208
}
202209
}
203210

204-
fn add_predicate_kind(&mut self, kind: &ty::PredicateKind<'_>) {
211+
fn add_predicate_kind(&mut self, kind: ty::PredicateKind<'_>) {
205212
match kind {
206213
ty::PredicateKind::ForAll(binder) => {
207-
let mut computation = FlagComputation::new();
208-
209-
computation.add_predicate_atom(binder.skip_binder());
210-
211-
self.add_bound_computation(computation);
214+
self.bound_computation(binder, |computation, atom| {
215+
computation.add_predicate_atom(atom)
216+
});
212217
}
213-
&ty::PredicateKind::Atom(atom) => self.add_predicate_atom(atom),
218+
ty::PredicateKind::Atom(atom) => self.add_predicate_atom(atom),
214219
}
215220
}
216221

@@ -266,15 +271,6 @@ impl FlagComputation {
266271
}
267272
}
268273

269-
fn add_fn_sig(&mut self, fn_sig: ty::PolyFnSig<'_>) {
270-
let mut computation = FlagComputation::new();
271-
272-
computation.add_tys(fn_sig.skip_binder().inputs());
273-
computation.add_ty(fn_sig.skip_binder().output());
274-
275-
self.add_bound_computation(computation);
276-
}
277-
278274
fn add_region(&mut self, r: ty::Region<'_>) {
279275
self.add_flags(r.type_flags());
280276
if let ty::ReLateBound(debruijn, _) = *r {

0 commit comments

Comments
 (0)