Skip to content

Followup to #83944 #84377

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 7 commits into from
Apr 22, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Review comments
  • Loading branch information
jackh726 committed Apr 21, 2021
commit b78c0d8a4d5af91a4a55d029293e3ecb879ec142
54 changes: 25 additions & 29 deletions compiler/rustc_ast_passes/src/ast_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,6 @@ struct AstValidator<'a> {
is_assoc_ty_bound_banned: bool,

lint_buffer: &'a mut LintBuffer,

/// This is slightly complicated. Our representation for poly-trait-refs contains a single
/// binder and thus we only allow a single level of quantification. However,
/// the syntax of Rust permits quantification in two places in where clauses,
/// e.g., `T: for <'a> Foo<'a>` and `for <'a, 'b> &'b T: Foo<'a>`. If both are
/// defined, then error.
trait_ref_hack: bool,
}

impl<'a> AstValidator<'a> {
Expand Down Expand Up @@ -1227,17 +1220,33 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
// A type binding, eg `for<'c> Foo: Send+Clone+'c`
self.check_late_bound_lifetime_defs(&bound_pred.bound_generic_params);

self.visit_ty(&bound_pred.bounded_ty);

self.trait_ref_hack = !bound_pred.bound_generic_params.is_empty();
walk_list!(self, visit_param_bound, &bound_pred.bounds);
walk_list!(self, visit_generic_param, &bound_pred.bound_generic_params);
self.trait_ref_hack = false;
}
_ => {
self.visit_where_predicate(predicate);
// This is slightly complicated. Our representation for poly-trait-refs contains a single
// binder and thus we only allow a single level of quantification. However,
// the syntax of Rust permits quantification in two places in where clauses,
// e.g., `T: for <'a> Foo<'a>` and `for <'a, 'b> &'b T: Foo<'a>`. If both are
// defined, then error.
if !bound_pred.bound_generic_params.is_empty() {
for bound in &bound_pred.bounds {
match bound {
GenericBound::Trait(t, _) => {
if !t.bound_generic_params.is_empty() {
struct_span_err!(
self.err_handler(),
t.span,
E0316,
"nested quantification of lifetimes"
)
.emit();
}
}
GenericBound::Outlives(_) => {}
}
}
}
}
_ => {}
}
self.visit_where_predicate(predicate);
}
}

Expand Down Expand Up @@ -1289,19 +1298,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {

fn visit_poly_trait_ref(&mut self, t: &'a PolyTraitRef, m: &'a TraitBoundModifier) {
self.check_late_bound_lifetime_defs(&t.bound_generic_params);
if self.trait_ref_hack && !t.bound_generic_params.is_empty() {
struct_span_err!(
self.err_handler(),
t.span,
E0316,
"nested quantification of lifetimes"
)
.emit();
}
let trait_ref_hack = self.trait_ref_hack;
self.trait_ref_hack = false;
visit::walk_poly_trait_ref(self, t, m);
self.trait_ref_hack = trait_ref_hack;
}

fn visit_variant_data(&mut self, s: &'a VariantData) {
Expand Down Expand Up @@ -1520,7 +1517,6 @@ pub fn check_crate(session: &Session, krate: &Crate, lints: &mut LintBuffer) ->
is_impl_trait_banned: false,
is_assoc_ty_bound_banned: false,
lint_buffer: lints,
trait_ref_hack: false,
};
visit::walk_crate(&mut validator, krate);

Expand Down
15 changes: 8 additions & 7 deletions compiler/rustc_resolve/src/late/lifetimes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1323,14 +1323,15 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
// of "if there isn't a Binder scope above us, add one", but I
// imagine there's a better way to go about this.
let mut scope = self.scope;
let trait_ref_hack = loop {
let (binders, scope_type) = loop {
match scope {
Scope::TraitRefBoundary { .. } | Scope::Body { .. } | Scope::Root => {
break false;
break (vec![], BinderScopeType::PolyTraitRef);
}

Scope::Binder { .. } => {
break true;
Scope::Binder { hir_id, .. } => {
let binders = self.map.late_bound_vars.entry(*hir_id).or_default().clone();
break (binders, BinderScopeType::Concatenating);
}

Scope::Elision { s, .. }
Expand All @@ -1341,16 +1342,16 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
}
};
match bound {
hir::GenericBound::LangItemTrait(_, _, hir_id, _) if !trait_ref_hack => {
self.map.late_bound_vars.insert(*hir_id, vec![]);
hir::GenericBound::LangItemTrait(_, _, hir_id, _) => {
self.map.late_bound_vars.insert(*hir_id, binders);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Except that I don't quite get why we need any code here at all. It seems like the LangItemTrait is basically a "poly trait ref" that binds no variables at all. We could probably pull out a helper from visit_poly_trait_ref that does the exact same setup code -- which would be good -- or else delete this code altogether, because it's not really needed. I can't quite see any case where introducing a Binder here changes behavior.

I guess the only change could be that there is a PolyTraitRef boundary (with no variables) instead of a TraitRefBoundary binder. Are those equivalent? I would think so (does that suggest a further simplification is possible?).

let scope = Scope::Binder {
hir_id: *hir_id,
lifetimes: FxHashMap::default(),
s: self.scope,
next_early_index: self.next_early_index(),
track_lifetime_uses: true,
opaque_type_parent: false,
scope_type: BinderScopeType::Other,
scope_type,
};
self.with(scope, |_, this| {
intravisit::walk_param_bound(this, bound);
Expand Down