Skip to content

Commit 894141b

Browse files
authored
Rollup merge of rust-lang#58198 - igorsdv:suggest-removing-parentheses-surrounding-lifetimes, r=estebank
Suggest removing parentheses surrounding lifetimes Fixes rust-lang#57386. r? @estebank
2 parents 70cc6c9 + f753d30 commit 894141b

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

src/libsyntax/parse/parser.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -5503,6 +5503,7 @@ impl<'a> Parser<'a> {
55035503
if is_bound_start {
55045504
let lo = self.span;
55055505
let has_parens = self.eat(&token::OpenDelim(token::Paren));
5506+
let inner_lo = self.span;
55065507
let question = if self.eat(&token::Question) { Some(self.prev_span) } else { None };
55075508
if self.token.is_lifetime() {
55085509
if let Some(question_span) = question {
@@ -5511,9 +5512,21 @@ impl<'a> Parser<'a> {
55115512
}
55125513
bounds.push(GenericBound::Outlives(self.expect_lifetime()));
55135514
if has_parens {
5515+
let inner_span = inner_lo.to(self.prev_span);
55145516
self.expect(&token::CloseDelim(token::Paren))?;
5515-
self.span_err(self.prev_span,
5516-
"parenthesized lifetime bounds are not supported");
5517+
let mut err = self.struct_span_err(
5518+
lo.to(self.prev_span),
5519+
"parenthesized lifetime bounds are not supported"
5520+
);
5521+
if let Ok(snippet) = self.sess.source_map().span_to_snippet(inner_span) {
5522+
err.span_suggestion_short(
5523+
lo.to(self.prev_span),
5524+
"remove the parentheses",
5525+
snippet.to_owned(),
5526+
Applicability::MachineApplicable
5527+
);
5528+
}
5529+
err.emit();
55175530
}
55185531
} else {
55195532
let lifetime_defs = self.parse_late_bound_lifetime_defs()?;

src/test/ui/parser/trait-object-lifetime-parens.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
error: parenthesized lifetime bounds are not supported
2-
--> $DIR/trait-object-lifetime-parens.rs:5:24
2+
--> $DIR/trait-object-lifetime-parens.rs:5:21
33
|
44
LL | fn f<'a, T: Trait + ('a)>() {} //~ ERROR parenthesized lifetime bounds are not supported
5-
| ^
5+
| ^^^^ help: remove the parentheses
66

77
error: parenthesized lifetime bounds are not supported
8-
--> $DIR/trait-object-lifetime-parens.rs:8:27
8+
--> $DIR/trait-object-lifetime-parens.rs:8:24
99
|
1010
LL | let _: Box<Trait + ('a)>; //~ ERROR parenthesized lifetime bounds are not supported
11-
| ^
11+
| ^^^^ help: remove the parentheses
1212

1313
error: expected type, found `'a`
1414
--> $DIR/trait-object-lifetime-parens.rs:9:17

0 commit comments

Comments
 (0)