Skip to content

Commit 3e22e0c

Browse files
committed
Use token description in "expected/found" parse messages
1 parent 3e6f30e commit 3e22e0c

23 files changed

+67
-47
lines changed

src/libsyntax/parse/parser.rs

+25-23
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,7 @@ impl<'a> Parser<'a> {
611611
t if t.is_special_ident() => "reserved identifier",
612612
t if t.is_used_keyword() => "keyword",
613613
t if t.is_unused_keyword() => "reserved keyword",
614+
token::DocComment(..) => "doc comment",
614615
_ => return None,
615616
})
616617
}
@@ -644,8 +645,8 @@ impl<'a> Parser<'a> {
644645
Ok(())
645646
} else {
646647
let token_str = pprust::token_to_string(t);
647-
let this_token_str = self.this_token_to_string();
648-
let mut err = self.fatal(&format!("expected `{}`, found `{}`",
648+
let this_token_str = self.this_token_descr();
649+
let mut err = self.fatal(&format!("expected `{}`, found {}",
649650
token_str,
650651
this_token_str));
651652

@@ -1444,17 +1445,17 @@ impl<'a> Parser<'a> {
14441445
Some(body)
14451446
}
14461447
_ => {
1447-
let token_str = self.this_token_to_string();
1448-
let mut err = self.fatal(&format!("expected `;` or `{{`, found `{}`",
1448+
let token_str = self.this_token_descr();
1449+
let mut err = self.fatal(&format!("expected `;` or `{{`, found {}",
14491450
token_str));
14501451
err.span_label(self.span, "expected `;` or `{`");
14511452
return Err(err);
14521453
}
14531454
}
14541455
}
14551456
_ => {
1456-
let token_str = self.this_token_to_string();
1457-
let mut err = self.fatal(&format!("expected `;` or `{{`, found `{}`",
1457+
let token_str = self.this_token_descr();
1458+
let mut err = self.fatal(&format!("expected `;` or `{{`, found {}",
14581459
token_str));
14591460
err.span_label(self.span, "expected `;` or `{`");
14601461
return Err(err);
@@ -3917,8 +3918,8 @@ impl<'a> Parser<'a> {
39173918
etc_span = Some(etc_sp);
39183919
break;
39193920
}
3920-
let token_str = self.this_token_to_string();
3921-
let mut err = self.fatal(&format!("expected `}}`, found `{}`", token_str));
3921+
let token_str = self.this_token_descr();
3922+
let mut err = self.fatal(&format!("expected `}}`, found {}", token_str));
39223923

39233924
err.span_label(self.span, "expected `}`");
39243925
let mut comma_sp = None;
@@ -4680,8 +4681,8 @@ impl<'a> Parser<'a> {
46804681
} else {
46814682
""
46824683
};
4683-
let tok_str = self.this_token_to_string();
4684-
let mut err = self.fatal(&format!("expected {}`(` or `{{`, found `{}`",
4684+
let tok_str = self.this_token_descr();
4685+
let mut err = self.fatal(&format!("expected {}`(` or `{{`, found {}",
46854686
ident_str,
46864687
tok_str));
46874688
err.span_label(self.span, format!("expected {}`(` or `{{`", ident_str));
@@ -4817,8 +4818,8 @@ impl<'a> Parser<'a> {
48174818

48184819
if !self.eat(&token::OpenDelim(token::Brace)) {
48194820
let sp = self.span;
4820-
let tok = self.this_token_to_string();
4821-
let mut e = self.span_fatal(sp, &format!("expected `{{`, found `{}`", tok));
4821+
let tok = self.this_token_descr();
4822+
let mut e = self.span_fatal(sp, &format!("expected `{{`, found {}", tok));
48224823
let do_not_suggest_help =
48234824
self.token.is_keyword(keywords::In) || self.token == token::Colon;
48244825

@@ -4880,6 +4881,7 @@ impl<'a> Parser<'a> {
48804881
}
48814882
_ => ()
48824883
}
4884+
e.span_label(sp, "expected `{`");
48834885
return Err(e);
48844886
}
48854887

@@ -4975,7 +4977,7 @@ impl<'a> Parser<'a> {
49754977

49764978
fn warn_missing_semicolon(&self) {
49774979
self.diagnostic().struct_span_warn(self.span, {
4978-
&format!("expected `;`, found `{}`", self.this_token_to_string())
4980+
&format!("expected `;`, found {}", self.this_token_descr())
49794981
}).note({
49804982
"This was erroneously allowed and will become a hard error in a future release"
49814983
}).emit();
@@ -6014,9 +6016,9 @@ impl<'a> Parser<'a> {
60146016
self.expect(&token::Semi)?;
60156017
body
60166018
} else {
6017-
let token_str = self.this_token_to_string();
6019+
let token_str = self.this_token_descr();
60186020
let mut err = self.fatal(&format!(
6019-
"expected `where`, `{{`, `(`, or `;` after struct name, found `{}`",
6021+
"expected `where`, `{{`, `(`, or `;` after struct name, found {}",
60206022
token_str
60216023
));
60226024
err.span_label(self.span, "expected `where`, `{`, `(`, or `;` after struct name");
@@ -6038,9 +6040,9 @@ impl<'a> Parser<'a> {
60386040
} else if self.token == token::OpenDelim(token::Brace) {
60396041
VariantData::Struct(self.parse_record_struct_body()?, ast::DUMMY_NODE_ID)
60406042
} else {
6041-
let token_str = self.this_token_to_string();
6043+
let token_str = self.this_token_descr();
60426044
let mut err = self.fatal(&format!(
6043-
"expected `where` or `{{` after union name, found `{}`", token_str));
6045+
"expected `where` or `{{` after union name, found {}", token_str));
60446046
err.span_label(self.span, "expected `where` or `{` after union name");
60456047
return Err(err);
60466048
};
@@ -6088,9 +6090,9 @@ impl<'a> Parser<'a> {
60886090
}
60896091
self.eat(&token::CloseDelim(token::Brace));
60906092
} else {
6091-
let token_str = self.this_token_to_string();
6093+
let token_str = self.this_token_descr();
60926094
let mut err = self.fatal(&format!(
6093-
"expected `where`, or `{{` after struct name, found `{}`", token_str));
6095+
"expected `where`, or `{{` after struct name, found {}", token_str));
60946096
err.span_label(self.span, "expected `where`, or `{` after struct name");
60956097
return Err(err);
60966098
}
@@ -6166,8 +6168,8 @@ impl<'a> Parser<'a> {
61666168
}
61676169
_ => {
61686170
let sp = self.sess.source_map().next_point(self.prev_span);
6169-
let mut err = self.struct_span_err(sp, &format!("expected `,`, or `}}`, found `{}`",
6170-
self.this_token_to_string()));
6171+
let mut err = self.struct_span_err(sp, &format!("expected `,`, or `}}`, found {}",
6172+
self.this_token_descr()));
61716173
if self.token.is_ident() {
61726174
// This is likely another field; emit the diagnostic and keep going
61736175
err.span_suggestion_with_applicability(
@@ -6303,8 +6305,8 @@ impl<'a> Parser<'a> {
63036305
}
63046306

63056307
if !self.eat(term) {
6306-
let token_str = self.this_token_to_string();
6307-
let mut err = self.fatal(&format!("expected item, found `{}`", token_str));
6308+
let token_str = self.this_token_descr();
6309+
let mut err = self.fatal(&format!("expected item, found {}", token_str));
63086310
if token_str == ";" {
63096311
let msg = "consider removing this semicolon";
63106312
err.span_suggestion_short_with_applicability(

src/test/ui/did_you_mean/issue-46836-identifier-not-instead-of-negation.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ error: expected `{`, found `;`
2828
LL | if not // lack of braces is [sic]
2929
| -- this `if` statement has a condition, but no block
3030
LL | println!("Then when?");
31-
| ^
31+
| ^ expected `{`
3232

3333
error: unexpected `2` after identifier
3434
--> $DIR/issue-46836-identifier-not-instead-of-negation.rs:36:24

src/test/ui/if/if-without-block.rs

+1
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ fn main() {
1616
}
1717
}
1818
//~^ ERROR expected `{`, found `}`
19+
//~| NOTE expected `{`

src/test/ui/if/if-without-block.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | if 5 == {
55
| -- this `if` statement has a condition, but no block
66
...
77
LL | }
8-
| ^
8+
| ^ expected `{`
99

1010
error: aborting due to previous error
1111

src/test/ui/issue-51602.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: expected `{`, found `in`
1+
error: expected `{`, found keyword `in`
22
--> $DIR/issue-51602.rs:12:10
33
|
44
LL | if i in 1..10 {

src/test/ui/issues/issue-39848.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | if $tgt.has_$field() {}
77
| this `if` statement has a condition, but no block
88
...
99
LL | get_opt!(bar, foo);
10-
| ^^^
10+
| ^^^ expected `{`
1111

1212
error: aborting due to previous error
1313

src/test/ui/issues/issue-46186.stderr

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ error: expected item, found `;`
22
--> $DIR/issue-46186.rs:13:2
33
|
44
LL | }; //~ ERROR expected item, found `;`
5-
| ^ help: consider removing this semicolon
6-
|
7-
= help: braced struct declarations are not followed by a semicolon
5+
| ^ expected item
86

97
error: aborting due to previous error
108

src/test/ui/issues/issue-49040.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: expected item, found `;`
22
--> $DIR/issue-49040.rs:11:28
33
|
44
LL | #![allow(unused_variables)]; //~ ERROR expected item, found `;`
5-
| ^ help: consider removing this semicolon
5+
| ^ expected item
66

77
error: aborting due to previous error
88

src/test/ui/label/label_break_value_illegal_uses.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ error: expected `{`, found `'b`
1010
LL | if true 'b: {} //~ ERROR expected `{`, found `'b`
1111
| -- ^^----
1212
| | |
13+
| | expected `{`
1314
| | help: try placing this code inside a block: `{ 'b: { } }`
1415
| this `if` statement has a condition, but no block
1516

@@ -19,6 +20,7 @@ error: expected `{`, found `'b`
1920
LL | if true {} else 'b: {} //~ ERROR expected `{`, found `'b`
2021
| ^^----
2122
| |
23+
| expected `{`
2224
| help: try placing this code inside a block: `{ 'b: { } }`
2325

2426
error: expected one of `.`, `?`, `{`, or an operator, found `'b`

src/test/ui/missing/missing-block-hint.stderr

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: expected `{`, found `=>`
22
--> $DIR/missing-block-hint.rs:13:18
33
|
44
LL | if (foo) => {} //~ ERROR expected `{`, found `=>`
5-
| -- ^^
5+
| -- ^^ expected `{`
66
| |
77
| this `if` statement has a condition, but no block
88

@@ -14,6 +14,7 @@ LL | if (foo)
1414
LL | bar; //~ ERROR expected `{`, found `bar`
1515
| ^^^-
1616
| |
17+
| expected `{`
1718
| help: try placing this code inside a block: `{ bar; }`
1819

1920
error: aborting due to 2 previous errors

src/test/ui/missing/missing-semicolon-warning.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
warning: expected `;`, found `let`
1+
warning: expected `;`, found keyword `let`
22
--> $DIR/missing-semicolon-warning.rs:16:12
33
|
44
LL | $( let x = $e1 )*; //~ WARN expected `;`

src/test/ui/parser/doc-before-identifier.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
fn /// document
1414
foo() {}
15-
//~^^ ERROR expected identifier, found `/// document`
15+
//~^^ ERROR expected identifier, found doc comment `/// document`
1616

1717
fn main() {
1818
foo();
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: expected identifier, found `/// document`
1+
error: expected identifier, found doc comment `/// document`
22
--> $DIR/doc-before-identifier.rs:13:4
33
|
44
LL | fn /// document
5-
| ^^^^^^^^^^^^ expected identifier
5+
| ^^^^^^^^^^^^ expected identifier, found doc comment
66

77
error: aborting due to previous error
88

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
fn main() {
2+
if true /*!*/ {}
3+
//~^ ERROR expected `{`, found doc comment `/*!*/`
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: expected `{`, found doc comment `/*!*/`
2+
--> $DIR/doc-comment-in-if-statement.rs:2:13
3+
|
4+
LL | if true /*!*/ {}
5+
| -- ^^^^^ expected `{`
6+
| |
7+
| this `if` statement has a condition, but no block
8+
9+
error: aborting due to previous error
10+

src/test/ui/parser/import-from-rename.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: expected `;`, found `as`
1+
error: expected `;`, found keyword `as`
22
--> $DIR/import-from-rename.rs:15:16
33
|
44
LL | use foo::{bar} as baz;

src/test/ui/parser/import-glob-rename.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: expected `;`, found `as`
1+
error: expected `;`, found keyword `as`
22
--> $DIR/import-glob-rename.rs:15:12
33
|
44
LL | use foo::* as baz;

src/test/ui/parser/issue-17904-2.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010

1111
// compile-flags: -Z parse-only -Z continue-parse-after-error
1212

13-
struct Bar<T> { x: T } where T: Copy //~ ERROR expected item, found `where`
13+
struct Bar<T> { x: T } where T: Copy //~ ERROR expected item, found keyword `where`
1414

1515
fn main() {}

src/test/ui/parser/issue-17904-2.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
error: expected item, found `where`
1+
error: expected item, found keyword `where`
22
--> $DIR/issue-17904-2.rs:13:24
33
|
4-
LL | struct Bar<T> { x: T } where T: Copy //~ ERROR expected item, found `where`
4+
LL | struct Bar<T> { x: T } where T: Copy //~ ERROR expected item, found keyword `where`
55
| ^^^^^ expected item
66

77
error: aborting due to previous error

src/test/ui/parser/unsized.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212

1313
// Test syntax checks for `type` keyword.
1414

15-
struct S1 for type; //~ ERROR expected `where`, `{`, `(`, or `;` after struct name, found `for`
15+
struct S1 for type;
16+
//~^ ERROR expected `where`, `{`, `(`, or `;` after struct name, found keyword `for`
1617

1718
pub fn main() {
1819
}

src/test/ui/parser/unsized.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
error: expected `where`, `{`, `(`, or `;` after struct name, found `for`
1+
error: expected `where`, `{`, `(`, or `;` after struct name, found keyword `for`
22
--> $DIR/unsized.rs:15:11
33
|
4-
LL | struct S1 for type; //~ ERROR expected `where`, `{`, `(`, or `;` after struct name, found `for`
4+
LL | struct S1 for type;
55
| ^^^ expected `where`, `{`, `(`, or `;` after struct name
66

77
error: aborting due to previous error

src/test/ui/parser/virtual-structs.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212

1313
// Test diagnostics for the removed struct inheritance feature.
1414

15-
virtual struct SuperStruct { //~ ERROR expected item, found `virtual`
15+
virtual struct SuperStruct {
16+
//~^ ERROR expected item, found reserved keyword `virtual`
1617
f1: isize,
1718
}
1819

src/test/ui/parser/virtual-structs.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
error: expected item, found `virtual`
1+
error: expected item, found reserved keyword `virtual`
22
--> $DIR/virtual-structs.rs:15:1
33
|
4-
LL | virtual struct SuperStruct { //~ ERROR expected item, found `virtual`
4+
LL | virtual struct SuperStruct {
55
| ^^^^^^^ expected item
66

77
error: aborting due to previous error

0 commit comments

Comments
 (0)