Skip to content

Improve skip_binder usage during FlagComputation #76893

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
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
68 changes: 32 additions & 36 deletions compiler/rustc_middle/src/ty/flags.rs
Original file line number Diff line number Diff line change
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,20 +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) => {
let mut proj_computation = FlagComputation::new();
proj_computation.add_existential_projection(&p);
self.add_bound_computation(proj_computation);
&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 @@ -195,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 @@ -266,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