Skip to content

Commit

Permalink
guard against skip_binder errors during FlagComputation
Browse files Browse the repository at this point in the history
  • Loading branch information
lcnr committed Sep 18, 2020
1 parent fd22e87 commit 7652b4b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 35 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/context.rs
Expand Up @@ -134,7 +134,7 @@ impl<'tcx> CtxtInterners<'tcx> {
fn intern_predicate(&self, kind: PredicateKind<'tcx>) -> &'tcx PredicateInner<'tcx> {
self.predicate
.intern(kind, |kind| {
let flags = super::flags::FlagComputation::for_predicate(&kind);
let flags = super::flags::FlagComputation::for_predicate(kind);

let predicate_struct = PredicateInner {
kind,
Expand Down
66 changes: 32 additions & 34 deletions compiler/rustc_middle/src/ty/flags.rs
Expand Up @@ -22,7 +22,7 @@ impl FlagComputation {
result
}

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

/// Adds the flags/depth from a set of types that appear within the current type, but within a
/// region binder.
fn add_bound_computation(&mut self, computation: FlagComputation) {
fn bound_computation<T, F>(&mut self, value: ty::Binder<T>, f: F)
where
F: FnOnce(&mut Self, T),
{
let mut computation = FlagComputation::new();

f(&mut computation, value.skip_binder());

self.add_flags(computation.flags);

// The types that contributed to `computation` occurred within
Expand Down Expand Up @@ -101,9 +108,7 @@ impl FlagComputation {
}

&ty::GeneratorWitness(ts) => {
let mut computation = FlagComputation::new();
computation.add_tys(ts.skip_binder());
self.add_bound_computation(computation);
self.bound_computation(ts, |flags, ts| flags.add_tys(ts));
}

&ty::Closure(_, substs) => {
Expand Down Expand Up @@ -154,18 +159,21 @@ impl FlagComputation {
self.add_substs(substs);
}

&ty::Dynamic(ref obj, r) => {
let mut computation = FlagComputation::new();
for predicate in obj.skip_binder().iter() {
match predicate {
ty::ExistentialPredicate::Trait(tr) => computation.add_substs(tr.substs),
ty::ExistentialPredicate::Projection(p) => {
computation.add_existential_projection(&p);
&ty::Dynamic(obj, r) => {
self.bound_computation(obj, |computation, obj| {
for predicate in obj.iter() {
match predicate {
ty::ExistentialPredicate::Trait(tr) => {
computation.add_substs(tr.substs)
}
ty::ExistentialPredicate::Projection(p) => {
computation.add_existential_projection(&p);
}
ty::ExistentialPredicate::AutoTrait(_) => {}
}
ty::ExistentialPredicate::AutoTrait(_) => {}
}
}
self.add_bound_computation(computation);
});

self.add_region(r);
}

Expand Down Expand Up @@ -193,22 +201,21 @@ impl FlagComputation {
self.add_substs(substs);
}

&ty::FnPtr(f) => {
self.add_fn_sig(f);
}
&ty::FnPtr(fn_sig) => self.bound_computation(fn_sig, |computation, fn_sig| {
computation.add_tys(fn_sig.inputs());
computation.add_ty(fn_sig.output());
}),
}
}

fn add_predicate_kind(&mut self, kind: &ty::PredicateKind<'_>) {
fn add_predicate_kind(&mut self, kind: ty::PredicateKind<'_>) {
match kind {
ty::PredicateKind::ForAll(binder) => {
let mut computation = FlagComputation::new();

computation.add_predicate_atom(binder.skip_binder());

self.add_bound_computation(computation);
self.bound_computation(binder, |computation, atom| {
computation.add_predicate_atom(atom)
});
}
&ty::PredicateKind::Atom(atom) => self.add_predicate_atom(atom),
ty::PredicateKind::Atom(atom) => self.add_predicate_atom(atom),
}
}

Expand Down Expand Up @@ -264,15 +271,6 @@ impl FlagComputation {
}
}

fn add_fn_sig(&mut self, fn_sig: ty::PolyFnSig<'_>) {
let mut computation = FlagComputation::new();

computation.add_tys(fn_sig.skip_binder().inputs());
computation.add_ty(fn_sig.skip_binder().output());

self.add_bound_computation(computation);
}

fn add_region(&mut self, r: ty::Region<'_>) {
self.add_flags(r.type_flags());
if let ty::ReLateBound(debruijn, _) = *r {
Expand Down

0 comments on commit 7652b4b

Please sign in to comment.