Skip to content

Commit 58928f4

Browse files
committed
Clean up parsers related to generic bounds
1 parent eb3e0d4 commit 58928f4

File tree

8 files changed

+86
-102
lines changed

8 files changed

+86
-102
lines changed

compiler/rustc_parse/messages.ftl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -748,9 +748,6 @@ parse_parentheses_with_struct_fields = invalid `struct` delimiters or `fn` call
748748
.suggestion_braces_for_struct = if `{$type}` is a struct, use braces as delimiters
749749
.suggestion_no_fields_for_fn = if `{$type}` is a function, use the arguments directly
750750
751-
parse_parenthesized_lifetime = parenthesized lifetime bounds are not supported
752-
parse_parenthesized_lifetime_suggestion = remove the parentheses
753-
754751
parse_path_double_colon = path separator must be a double colon
755752
.suggestion = use a double colon instead
756753

compiler/rustc_parse/src/errors.rs

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3136,27 +3136,6 @@ pub(crate) struct ModifierLifetime {
31363136
pub modifier: &'static str,
31373137
}
31383138

3139-
#[derive(Subdiagnostic)]
3140-
#[multipart_suggestion(
3141-
parse_parenthesized_lifetime_suggestion,
3142-
applicability = "machine-applicable"
3143-
)]
3144-
pub(crate) struct RemoveParens {
3145-
#[suggestion_part(code = "")]
3146-
pub lo: Span,
3147-
#[suggestion_part(code = "")]
3148-
pub hi: Span,
3149-
}
3150-
3151-
#[derive(Diagnostic)]
3152-
#[diag(parse_parenthesized_lifetime)]
3153-
pub(crate) struct ParenthesizedLifetime {
3154-
#[primary_span]
3155-
pub span: Span,
3156-
#[subdiagnostic]
3157-
pub sugg: RemoveParens,
3158-
}
3159-
31603139
#[derive(Diagnostic)]
31613140
#[diag(parse_underscore_literal_suffix)]
31623141
pub(crate) struct UnderscoreLiteralSuffix {

compiler/rustc_parse/src/parser/expr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2397,12 +2397,12 @@ impl<'a> Parser<'a> {
23972397
let before = self.prev_token;
23982398
let binder = if self.check_keyword(exp!(For)) {
23992399
let lo = self.token.span;
2400-
let (lifetime_defs, _) = self.parse_late_bound_lifetime_defs()?;
2400+
let (bound_vars, _) = self.parse_higher_ranked_binder()?;
24012401
let span = lo.to(self.prev_token.span);
24022402

24032403
self.psess.gated_spans.gate(sym::closure_lifetime_binder, span);
24042404

2405-
ClosureBinder::For { span, generic_params: lifetime_defs }
2405+
ClosureBinder::For { span, generic_params: bound_vars }
24062406
} else {
24072407
ClosureBinder::NotPresent
24082408
};

compiler/rustc_parse/src/parser/generics.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,11 @@ impl<'a> Parser<'a> {
172172
})
173173
}
174174

175-
/// Parses a (possibly empty) list of lifetime and type parameters, possibly including
176-
/// a trailing comma and erroneous trailing attributes.
175+
/// Parse a (possibly empty) list of generic (lifetime, type, const) parameters.
176+
///
177+
/// ```ebnf
178+
/// GenericParams = (GenericParam ("," GenericParam)* ","?)?
179+
/// ```
177180
pub(super) fn parse_generic_params(&mut self) -> PResult<'a, ThinVec<ast::GenericParam>> {
178181
let mut params = ThinVec::new();
179182
let mut done = false;
@@ -520,15 +523,15 @@ impl<'a> Parser<'a> {
520523
// * `for<'a> Trait1<'a>: Trait2<'a /* ok */>`
521524
// * `(for<'a> Trait1<'a>): Trait2<'a /* not ok */>`
522525
// * `for<'a> for<'b> Trait1<'a, 'b>: Trait2<'a /* ok */, 'b /* not ok */>`
523-
let (lifetime_defs, _) = self.parse_late_bound_lifetime_defs()?;
526+
let (bound_vars, _) = self.parse_higher_ranked_binder()?;
524527

525528
// Parse type with mandatory colon and (possibly empty) bounds,
526529
// or with mandatory equality sign and the second type.
527530
let ty = self.parse_ty_for_where_clause()?;
528531
if self.eat(exp!(Colon)) {
529532
let bounds = self.parse_generic_bounds()?;
530533
Ok(ast::WherePredicateKind::BoundPredicate(ast::WhereBoundPredicate {
531-
bound_generic_params: lifetime_defs,
534+
bound_generic_params: bound_vars,
532535
bounded_ty: ty,
533536
bounds,
534537
}))

compiler/rustc_parse/src/parser/ty.rs

Lines changed: 71 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -307,11 +307,11 @@ impl<'a> Parser<'a> {
307307
// Function pointer type or bound list (trait object type) starting with a poly-trait.
308308
// `for<'lt> [unsafe] [extern "ABI"] fn (&'lt S) -> T`
309309
// `for<'lt> Trait1<'lt> + Trait2 + 'a`
310-
let (lifetime_defs, _) = self.parse_late_bound_lifetime_defs()?;
310+
let (bound_vars, _) = self.parse_higher_ranked_binder()?;
311311
if self.check_fn_front_matter(false, Case::Sensitive) {
312312
self.parse_ty_fn_ptr(
313313
lo,
314-
lifetime_defs,
314+
bound_vars,
315315
Some(self.prev_token.span.shrink_to_lo()),
316316
recover_return_sign,
317317
)?
@@ -325,7 +325,7 @@ impl<'a> Parser<'a> {
325325
let path = self.parse_path(PathStyle::Type)?;
326326
let parse_plus = allow_plus == AllowPlus::Yes && self.check_plus();
327327
let kind = self.parse_remaining_bounds_path(
328-
lifetime_defs,
328+
bound_vars,
329329
path,
330330
lo,
331331
parse_plus,
@@ -358,7 +358,7 @@ impl<'a> Parser<'a> {
358358
let path = self.parse_path(PathStyle::Type)?;
359359
let parse_plus = allow_plus == AllowPlus::Yes && self.check_plus();
360360
self.parse_remaining_bounds_path(
361-
lifetime_defs,
361+
bound_vars,
362362
path,
363363
lo,
364364
parse_plus,
@@ -442,7 +442,7 @@ impl<'a> Parser<'a> {
442442
let ty = ts.into_iter().next().unwrap();
443443
let maybe_bounds = allow_plus == AllowPlus::Yes && self.token.is_like_plus();
444444
match ty.kind {
445-
// `(TY_BOUND_NOPAREN) + BOUND + ...`.
445+
// `"(" BareTraitBound ")" "+" Bound "+" ...`.
446446
TyKind::Path(None, path) if maybe_bounds => self.parse_remaining_bounds_path(
447447
ThinVec::new(),
448448
path,
@@ -847,11 +847,13 @@ impl<'a> Parser<'a> {
847847
Ok(TyKind::ImplTrait(ast::DUMMY_NODE_ID, bounds))
848848
}
849849

850-
fn parse_precise_capturing_args(
851-
&mut self,
852-
lo: Span,
853-
parens: ast::Parens,
854-
) -> PResult<'a, GenericBound> {
850+
/// Parse a use-bound aka precise capturing list.
851+
///
852+
/// ```ebnf
853+
/// UseBound = "use" "<" (PreciseCapture ("," PreciseCapture)* ","?)? ">"
854+
/// PreciseCapture = "Self" | Ident | Lifetime
855+
/// ```
856+
fn parse_use_bound(&mut self, lo: Span, parens: ast::Parens) -> PResult<'a, GenericBound> {
855857
self.expect_lt()?;
856858
let (args, _, _) = self.parse_seq_to_before_tokens(
857859
&[exp!(Gt)],
@@ -881,15 +883,7 @@ impl<'a> Parser<'a> {
881883
if let ast::Parens::Yes = parens {
882884
self.expect(exp!(CloseParen))?;
883885
let hi = self.prev_token.span;
884-
let mut diag = self
885-
.dcx()
886-
.struct_span_err(lo.to(hi), "precise capturing lists may not be parenthesized");
887-
diag.multipart_suggestion(
888-
"remove the parentheses",
889-
vec![(lo, String::new()), (hi, String::new())],
890-
Applicability::MachineApplicable,
891-
);
892-
diag.emit();
886+
self.report_parenthesized_bound(lo, hi, "precise capturing lists");
893887
}
894888

895889
Ok(GenericBound::Use(args, lo.to(self.prev_token.span)))
@@ -950,9 +944,10 @@ impl<'a> Parser<'a> {
950944
self.parse_generic_bounds_common(AllowPlus::Yes)
951945
}
952946

953-
/// Parses bounds of a type parameter `BOUND + BOUND + ...`, possibly with trailing `+`.
947+
/// Parse generic bounds.
954948
///
955-
/// See `parse_generic_bound` for the `BOUND` grammar.
949+
/// Only if `allow_plus` this parses a `+`-separated list of bounds (trailing `+` is admitted).
950+
/// Otherwise, this only parses a single bound or none.
956951
fn parse_generic_bounds_common(&mut self, allow_plus: AllowPlus) -> PResult<'a, GenericBounds> {
957952
let mut bounds = Vec::new();
958953

@@ -998,42 +993,57 @@ impl<'a> Parser<'a> {
998993
|| self.check_keyword(exp!(Use))
999994
}
1000995

1001-
/// Parses a bound according to the grammar:
996+
/// Parse a bound.
997+
///
1002998
/// ```ebnf
1003-
/// BOUND = TY_BOUND | LT_BOUND
999+
/// Bound = LifetimeBound | UseBound | TraitBound
10041000
/// ```
10051001
fn parse_generic_bound(&mut self) -> PResult<'a, GenericBound> {
10061002
let leading_token = self.prev_token;
10071003
let lo = self.token.span;
10081004

1005+
// We only admit parenthesized *trait* bounds. However, we want to gracefully recover from
1006+
// other kinds of parenthesized bounds, so parse the opening parenthesis *here*.
1007+
//
1008+
// In the future we might want to lift this syntactic restriction and
1009+
// introduce "`GenericBound::Paren(Box<GenericBound>)`".
10091010
let parens = if self.eat(exp!(OpenParen)) { ast::Parens::Yes } else { ast::Parens::No };
10101011

10111012
if self.token.is_lifetime() {
1012-
self.parse_generic_lt_bound(lo, parens)
1013+
self.parse_lifetime_bound(lo, parens)
10131014
} else if self.eat_keyword(exp!(Use)) {
1014-
self.parse_precise_capturing_args(lo, parens)
1015+
self.parse_use_bound(lo, parens)
10151016
} else {
1016-
self.parse_generic_ty_bound(lo, parens, &leading_token)
1017+
self.parse_trait_bound(lo, parens, &leading_token)
10171018
}
10181019
}
10191020

1020-
/// Parses a lifetime ("outlives") bound, e.g. `'a`, according to:
1021+
/// Parse a lifetime-bound aka outlives-bound.
1022+
///
10211023
/// ```ebnf
1022-
/// LT_BOUND = LIFETIME
1024+
/// LifetimeBound = Lifetime
10231025
/// ```
1024-
fn parse_generic_lt_bound(
1025-
&mut self,
1026-
lo: Span,
1027-
parens: ast::Parens,
1028-
) -> PResult<'a, GenericBound> {
1026+
fn parse_lifetime_bound(&mut self, lo: Span, parens: ast::Parens) -> PResult<'a, GenericBound> {
10291027
let lt = self.expect_lifetime();
1030-
let bound = GenericBound::Outlives(lt);
1028+
10311029
if let ast::Parens::Yes = parens {
1032-
// FIXME(Centril): Consider not erroring here and accepting `('lt)` instead,
1033-
// possibly introducing `GenericBound::Paren(Box<GenericBound>)`?
1034-
self.recover_paren_lifetime(lo)?;
1030+
self.expect(exp!(CloseParen))?;
1031+
let hi = self.prev_token.span;
1032+
self.report_parenthesized_bound(lo, hi, "lifetime bounds");
10351033
}
1036-
Ok(bound)
1034+
1035+
Ok(GenericBound::Outlives(lt))
1036+
}
1037+
1038+
fn report_parenthesized_bound(&self, lo: Span, hi: Span, kind: &str) -> ErrorGuaranteed {
1039+
let mut diag =
1040+
self.dcx().struct_span_err(lo.to(hi), format!("{kind} may not be parenthesized"));
1041+
diag.multipart_suggestion(
1042+
"remove the parentheses",
1043+
vec![(lo, String::new()), (hi, String::new())],
1044+
Applicability::MachineApplicable,
1045+
);
1046+
diag.emit()
10371047
}
10381048

10391049
/// Emits an error if any trait bound modifiers were present.
@@ -1078,27 +1088,17 @@ impl<'a> Parser<'a> {
10781088
unreachable!("lifetime bound intercepted in `parse_generic_ty_bound` but no modifiers?")
10791089
}
10801090

1081-
/// Recover on `('lifetime)` with `(` already eaten.
1082-
fn recover_paren_lifetime(&mut self, lo: Span) -> PResult<'a, ()> {
1083-
self.expect(exp!(CloseParen))?;
1084-
let span = lo.to(self.prev_token.span);
1085-
let sugg = errors::RemoveParens { lo, hi: self.prev_token.span };
1086-
1087-
self.dcx().emit_err(errors::ParenthesizedLifetime { span, sugg });
1088-
Ok(())
1089-
}
1090-
10911091
/// Parses the modifiers that may precede a trait in a bound, e.g. `?Trait` or `[const] Trait`.
10921092
///
10931093
/// If no modifiers are present, this does not consume any tokens.
10941094
///
10951095
/// ```ebnf
1096-
/// CONSTNESS = [["["] "const" ["]"]]
1097-
/// ASYNCNESS = ["async"]
1098-
/// POLARITY = ["?" | "!"]
1096+
/// Constness = ("const" | "[" "const" "]")?
1097+
/// Asyncness = "async"?
1098+
/// Polarity = ("?" | "!")?
10991099
/// ```
11001100
///
1101-
/// See `parse_generic_ty_bound` for the complete grammar of trait bound modifiers.
1101+
/// See `parse_trait_bound` for more context.
11021102
fn parse_trait_bound_modifiers(&mut self) -> PResult<'a, TraitBoundModifiers> {
11031103
let modifier_lo = self.token.span;
11041104
let constness = self.parse_bound_constness()?;
@@ -1191,20 +1191,21 @@ impl<'a> Parser<'a> {
11911191
})
11921192
}
11931193

1194-
/// Parses a type bound according to:
1194+
/// Parse a trait bound.
1195+
///
11951196
/// ```ebnf
1196-
/// TY_BOUND = TY_BOUND_NOPAREN | (TY_BOUND_NOPAREN)
1197-
/// TY_BOUND_NOPAREN = [for<GENERIC_PARAMS> CONSTNESS ASYNCNESS | POLARITY] SIMPLE_PATH
1197+
/// TraitBound = BareTraitBound | "(" BareTraitBound ")"
1198+
/// BareTraitBound =
1199+
/// (HigherRankedBinder Constness Asyncness | Polarity)?
1200+
/// TypePath
11981201
/// ```
1199-
///
1200-
/// For example, this grammar accepts `for<'a: 'b> [const] ?m::Trait<'a>`.
1201-
fn parse_generic_ty_bound(
1202+
fn parse_trait_bound(
12021203
&mut self,
12031204
lo: Span,
12041205
parens: ast::Parens,
12051206
leading_token: &Token,
12061207
) -> PResult<'a, GenericBound> {
1207-
let (mut lifetime_defs, binder_span) = self.parse_late_bound_lifetime_defs()?;
1208+
let (mut bound_vars, binder_span) = self.parse_higher_ranked_binder()?;
12081209

12091210
let modifiers_lo = self.token.span;
12101211
let modifiers = self.parse_trait_bound_modifiers()?;
@@ -1227,11 +1228,11 @@ impl<'a> Parser<'a> {
12271228
// e.g. `T: for<'a> 'a` or `T: [const] 'a`.
12281229
if self.token.is_lifetime() {
12291230
let _: ErrorGuaranteed = self.error_lt_bound_with_modifiers(modifiers, binder_span);
1230-
return self.parse_generic_lt_bound(lo, parens);
1231+
return self.parse_lifetime_bound(lo, parens);
12311232
}
12321233

1233-
if let (more_lifetime_defs, Some(binder_span)) = self.parse_late_bound_lifetime_defs()? {
1234-
lifetime_defs.extend(more_lifetime_defs);
1234+
if let (more_bound_vars, Some(binder_span)) = self.parse_higher_ranked_binder()? {
1235+
bound_vars.extend(more_bound_vars);
12351236
self.dcx().emit_err(errors::BinderBeforeModifiers { binder_span, modifiers_span });
12361237
}
12371238

@@ -1291,7 +1292,7 @@ impl<'a> Parser<'a> {
12911292
};
12921293

12931294
if self.may_recover() && self.token == TokenKind::OpenParen {
1294-
self.recover_fn_trait_with_lifetime_params(&mut path, &mut lifetime_defs)?;
1295+
self.recover_fn_trait_with_lifetime_params(&mut path, &mut bound_vars)?;
12951296
}
12961297

12971298
if let ast::Parens::Yes = parens {
@@ -1314,7 +1315,7 @@ impl<'a> Parser<'a> {
13141315
}
13151316

13161317
let poly_trait =
1317-
PolyTraitRef::new(lifetime_defs, path, modifiers, lo.to(self.prev_token.span), parens);
1318+
PolyTraitRef::new(bound_vars, path, modifiers, lo.to(self.prev_token.span), parens);
13181319
Ok(GenericBound::Trait(poly_trait))
13191320
}
13201321

@@ -1352,8 +1353,12 @@ impl<'a> Parser<'a> {
13521353
}
13531354
}
13541355

1355-
/// Optionally parses `for<$generic_params>`.
1356-
pub(super) fn parse_late_bound_lifetime_defs(
1356+
/// Parse an optional higher-ranked binder.
1357+
///
1358+
/// ```ebnf
1359+
/// HigherRankedBinder = ("for" "<" GenericParams ">")?
1360+
/// ```
1361+
pub(super) fn parse_higher_ranked_binder(
13571362
&mut self,
13581363
) -> PResult<'a, (ThinVec<GenericParam>, Option<Span>)> {
13591364
if self.eat_keyword(exp!(For)) {

tests/ui/parser/trait-object-lifetime-parens.e2015.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: parenthesized lifetime bounds are not supported
1+
error: lifetime bounds may not be parenthesized
22
--> $DIR/trait-object-lifetime-parens.rs:9:21
33
|
44
LL | fn f<'a, T: Trait + ('a)>() {}
@@ -10,7 +10,7 @@ LL - fn f<'a, T: Trait + ('a)>() {}
1010
LL + fn f<'a, T: Trait + 'a>() {}
1111
|
1212

13-
error: parenthesized lifetime bounds are not supported
13+
error: lifetime bounds may not be parenthesized
1414
--> $DIR/trait-object-lifetime-parens.rs:12:24
1515
|
1616
LL | let _: Box<Trait + ('a)>;

tests/ui/parser/trait-object-lifetime-parens.e2021.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: parenthesized lifetime bounds are not supported
1+
error: lifetime bounds may not be parenthesized
22
--> $DIR/trait-object-lifetime-parens.rs:9:21
33
|
44
LL | fn f<'a, T: Trait + ('a)>() {}
@@ -10,7 +10,7 @@ LL - fn f<'a, T: Trait + ('a)>() {}
1010
LL + fn f<'a, T: Trait + 'a>() {}
1111
|
1212

13-
error: parenthesized lifetime bounds are not supported
13+
error: lifetime bounds may not be parenthesized
1414
--> $DIR/trait-object-lifetime-parens.rs:12:24
1515
|
1616
LL | let _: Box<Trait + ('a)>;

0 commit comments

Comments
 (0)