Skip to content

Commit b78c0d8

Browse files
committed
Review comments
1 parent 4568e7d commit b78c0d8

File tree

2 files changed

+33
-36
lines changed

2 files changed

+33
-36
lines changed

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,6 @@ struct AstValidator<'a> {
8181
is_assoc_ty_bound_banned: bool,
8282

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

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

1230-
self.visit_ty(&bound_pred.bounded_ty);
1231-
1232-
self.trait_ref_hack = !bound_pred.bound_generic_params.is_empty();
1233-
walk_list!(self, visit_param_bound, &bound_pred.bounds);
1234-
walk_list!(self, visit_generic_param, &bound_pred.bound_generic_params);
1235-
self.trait_ref_hack = false;
1236-
}
1237-
_ => {
1238-
self.visit_where_predicate(predicate);
1223+
// This is slightly complicated. Our representation for poly-trait-refs contains a single
1224+
// binder and thus we only allow a single level of quantification. However,
1225+
// the syntax of Rust permits quantification in two places in where clauses,
1226+
// e.g., `T: for <'a> Foo<'a>` and `for <'a, 'b> &'b T: Foo<'a>`. If both are
1227+
// defined, then error.
1228+
if !bound_pred.bound_generic_params.is_empty() {
1229+
for bound in &bound_pred.bounds {
1230+
match bound {
1231+
GenericBound::Trait(t, _) => {
1232+
if !t.bound_generic_params.is_empty() {
1233+
struct_span_err!(
1234+
self.err_handler(),
1235+
t.span,
1236+
E0316,
1237+
"nested quantification of lifetimes"
1238+
)
1239+
.emit();
1240+
}
1241+
}
1242+
GenericBound::Outlives(_) => {}
1243+
}
1244+
}
1245+
}
12391246
}
1247+
_ => {}
12401248
}
1249+
self.visit_where_predicate(predicate);
12411250
}
12421251
}
12431252

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

12901299
fn visit_poly_trait_ref(&mut self, t: &'a PolyTraitRef, m: &'a TraitBoundModifier) {
12911300
self.check_late_bound_lifetime_defs(&t.bound_generic_params);
1292-
if self.trait_ref_hack && !t.bound_generic_params.is_empty() {
1293-
struct_span_err!(
1294-
self.err_handler(),
1295-
t.span,
1296-
E0316,
1297-
"nested quantification of lifetimes"
1298-
)
1299-
.emit();
1300-
}
1301-
let trait_ref_hack = self.trait_ref_hack;
1302-
self.trait_ref_hack = false;
13031301
visit::walk_poly_trait_ref(self, t, m);
1304-
self.trait_ref_hack = trait_ref_hack;
13051302
}
13061303

13071304
fn visit_variant_data(&mut self, s: &'a VariantData) {
@@ -1520,7 +1517,6 @@ pub fn check_crate(session: &Session, krate: &Crate, lints: &mut LintBuffer) ->
15201517
is_impl_trait_banned: false,
15211518
is_assoc_ty_bound_banned: false,
15221519
lint_buffer: lints,
1523-
trait_ref_hack: false,
15241520
};
15251521
visit::walk_crate(&mut validator, krate);
15261522

compiler/rustc_resolve/src/late/lifetimes.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1323,14 +1323,15 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
13231323
// of "if there isn't a Binder scope above us, add one", but I
13241324
// imagine there's a better way to go about this.
13251325
let mut scope = self.scope;
1326-
let trait_ref_hack = loop {
1326+
let (binders, scope_type) = loop {
13271327
match scope {
13281328
Scope::TraitRefBoundary { .. } | Scope::Body { .. } | Scope::Root => {
1329-
break false;
1329+
break (vec![], BinderScopeType::PolyTraitRef);
13301330
}
13311331

1332-
Scope::Binder { .. } => {
1333-
break true;
1332+
Scope::Binder { hir_id, .. } => {
1333+
let binders = self.map.late_bound_vars.entry(*hir_id).or_default().clone();
1334+
break (binders, BinderScopeType::Concatenating);
13341335
}
13351336

13361337
Scope::Elision { s, .. }
@@ -1341,16 +1342,16 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
13411342
}
13421343
};
13431344
match bound {
1344-
hir::GenericBound::LangItemTrait(_, _, hir_id, _) if !trait_ref_hack => {
1345-
self.map.late_bound_vars.insert(*hir_id, vec![]);
1345+
hir::GenericBound::LangItemTrait(_, _, hir_id, _) => {
1346+
self.map.late_bound_vars.insert(*hir_id, binders);
13461347
let scope = Scope::Binder {
13471348
hir_id: *hir_id,
13481349
lifetimes: FxHashMap::default(),
13491350
s: self.scope,
13501351
next_early_index: self.next_early_index(),
13511352
track_lifetime_uses: true,
13521353
opaque_type_parent: false,
1353-
scope_type: BinderScopeType::Other,
1354+
scope_type,
13541355
};
13551356
self.with(scope, |_, this| {
13561357
intravisit::walk_param_bound(this, bound);

0 commit comments

Comments
 (0)