Skip to content

Use token description in "expected/found" parse messages #55455

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 2 commits into from
Nov 4, 2018
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
48 changes: 25 additions & 23 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,7 @@ impl<'a> Parser<'a> {
t if t.is_special_ident() => "reserved identifier",
t if t.is_used_keyword() => "keyword",
t if t.is_unused_keyword() => "reserved keyword",
token::DocComment(..) => "doc comment",
_ => return None,
})
}
Expand Down Expand Up @@ -644,8 +645,8 @@ impl<'a> Parser<'a> {
Ok(())
} else {
let token_str = pprust::token_to_string(t);
let this_token_str = self.this_token_to_string();
let mut err = self.fatal(&format!("expected `{}`, found `{}`",
let this_token_str = self.this_token_descr();
let mut err = self.fatal(&format!("expected `{}`, found {}",
token_str,
this_token_str));

Expand Down Expand Up @@ -1444,17 +1445,17 @@ impl<'a> Parser<'a> {
Some(body)
}
_ => {
let token_str = self.this_token_to_string();
let mut err = self.fatal(&format!("expected `;` or `{{`, found `{}`",
let token_str = self.this_token_descr();
let mut err = self.fatal(&format!("expected `;` or `{{`, found {}",
token_str));
err.span_label(self.span, "expected `;` or `{`");
return Err(err);
}
}
}
_ => {
let token_str = self.this_token_to_string();
let mut err = self.fatal(&format!("expected `;` or `{{`, found `{}`",
let token_str = self.this_token_descr();
let mut err = self.fatal(&format!("expected `;` or `{{`, found {}",
token_str));
err.span_label(self.span, "expected `;` or `{`");
return Err(err);
Expand Down Expand Up @@ -3917,8 +3918,8 @@ impl<'a> Parser<'a> {
etc_span = Some(etc_sp);
break;
}
let token_str = self.this_token_to_string();
let mut err = self.fatal(&format!("expected `}}`, found `{}`", token_str));
let token_str = self.this_token_descr();
let mut err = self.fatal(&format!("expected `}}`, found {}", token_str));

err.span_label(self.span, "expected `}`");
let mut comma_sp = None;
Expand Down Expand Up @@ -4680,8 +4681,8 @@ impl<'a> Parser<'a> {
} else {
""
};
let tok_str = self.this_token_to_string();
let mut err = self.fatal(&format!("expected {}`(` or `{{`, found `{}`",
let tok_str = self.this_token_descr();
let mut err = self.fatal(&format!("expected {}`(` or `{{`, found {}",
ident_str,
tok_str));
err.span_label(self.span, format!("expected {}`(` or `{{`", ident_str));
Expand Down Expand Up @@ -4817,8 +4818,8 @@ impl<'a> Parser<'a> {

if !self.eat(&token::OpenDelim(token::Brace)) {
let sp = self.span;
let tok = self.this_token_to_string();
let mut e = self.span_fatal(sp, &format!("expected `{{`, found `{}`", tok));
let tok = self.this_token_descr();
let mut e = self.span_fatal(sp, &format!("expected `{{`, found {}", tok));
let do_not_suggest_help =
self.token.is_keyword(keywords::In) || self.token == token::Colon;

Expand Down Expand Up @@ -4880,6 +4881,7 @@ impl<'a> Parser<'a> {
}
_ => ()
}
e.span_label(sp, "expected `{`");
return Err(e);
}

Expand Down Expand Up @@ -4975,7 +4977,7 @@ impl<'a> Parser<'a> {

fn warn_missing_semicolon(&self) {
self.diagnostic().struct_span_warn(self.span, {
&format!("expected `;`, found `{}`", self.this_token_to_string())
&format!("expected `;`, found {}", self.this_token_descr())
}).note({
"This was erroneously allowed and will become a hard error in a future release"
}).emit();
Expand Down Expand Up @@ -6014,9 +6016,9 @@ impl<'a> Parser<'a> {
self.expect(&token::Semi)?;
body
} else {
let token_str = self.this_token_to_string();
let token_str = self.this_token_descr();
let mut err = self.fatal(&format!(
"expected `where`, `{{`, `(`, or `;` after struct name, found `{}`",
"expected `where`, `{{`, `(`, or `;` after struct name, found {}",
token_str
));
err.span_label(self.span, "expected `where`, `{`, `(`, or `;` after struct name");
Expand All @@ -6038,9 +6040,9 @@ impl<'a> Parser<'a> {
} else if self.token == token::OpenDelim(token::Brace) {
VariantData::Struct(self.parse_record_struct_body()?, ast::DUMMY_NODE_ID)
} else {
let token_str = self.this_token_to_string();
let token_str = self.this_token_descr();
let mut err = self.fatal(&format!(
"expected `where` or `{{` after union name, found `{}`", token_str));
"expected `where` or `{{` after union name, found {}", token_str));
err.span_label(self.span, "expected `where` or `{` after union name");
return Err(err);
};
Expand Down Expand Up @@ -6088,9 +6090,9 @@ impl<'a> Parser<'a> {
}
self.eat(&token::CloseDelim(token::Brace));
} else {
let token_str = self.this_token_to_string();
let token_str = self.this_token_descr();
let mut err = self.fatal(&format!(
"expected `where`, or `{{` after struct name, found `{}`", token_str));
"expected `where`, or `{{` after struct name, found {}", token_str));
err.span_label(self.span, "expected `where`, or `{` after struct name");
return Err(err);
}
Expand Down Expand Up @@ -6166,8 +6168,8 @@ impl<'a> Parser<'a> {
}
_ => {
let sp = self.sess.source_map().next_point(self.prev_span);
let mut err = self.struct_span_err(sp, &format!("expected `,`, or `}}`, found `{}`",
self.this_token_to_string()));
let mut err = self.struct_span_err(sp, &format!("expected `,`, or `}}`, found {}",
self.this_token_descr()));
if self.token.is_ident() {
// This is likely another field; emit the diagnostic and keep going
err.span_suggestion_with_applicability(
Expand Down Expand Up @@ -6303,8 +6305,8 @@ impl<'a> Parser<'a> {
}

if !self.eat(term) {
let token_str = self.this_token_to_string();
let mut err = self.fatal(&format!("expected item, found `{}`", token_str));
let token_str = self.this_token_descr();
let mut err = self.fatal(&format!("expected item, found {}", token_str));
if token_str == ";" {
let msg = "consider removing this semicolon";
err.span_suggestion_short_with_applicability(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ error: expected `{`, found `;`
LL | if not // lack of braces is [sic]
| -- this `if` statement has a condition, but no block
LL | println!("Then when?");
| ^
| ^ expected `{`

error: unexpected `2` after identifier
--> $DIR/issue-46836-identifier-not-instead-of-negation.rs:36:24
Expand Down
1 change: 1 addition & 0 deletions src/test/ui/if/if-without-block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ fn main() {
}
}
//~^ ERROR expected `{`, found `}`
//~| NOTE expected `{`
2 changes: 1 addition & 1 deletion src/test/ui/if/if-without-block.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ LL | if 5 == {
| -- this `if` statement has a condition, but no block
...
LL | }
| ^
| ^ expected `{`

error: aborting due to previous error

2 changes: 1 addition & 1 deletion src/test/ui/issue-51602.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: expected `{`, found `in`
error: expected `{`, found keyword `in`
--> $DIR/issue-51602.rs:12:10
|
LL | if i in 1..10 {
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-39848.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | if $tgt.has_$field() {}
| this `if` statement has a condition, but no block
...
LL | get_opt!(bar, foo);
| ^^^
| ^^^ expected `{`

error: aborting due to previous error

4 changes: 1 addition & 3 deletions src/test/ui/issues/issue-46186.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ error: expected item, found `;`
--> $DIR/issue-46186.rs:13:2
|
LL | }; //~ ERROR expected item, found `;`
| ^ help: consider removing this semicolon
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also a regression. Not sure what caused it.

|
= help: braced struct declarations are not followed by a semicolon
| ^ expected item

error: aborting due to previous error

2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-49040.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error: expected item, found `;`
--> $DIR/issue-49040.rs:11:28
|
LL | #![allow(unused_variables)]; //~ ERROR expected item, found `;`
| ^ help: consider removing this semicolon
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a regression, don't approve yet.

| ^ expected item

error: aborting due to previous error

2 changes: 2 additions & 0 deletions src/test/ui/label/label_break_value_illegal_uses.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ error: expected `{`, found `'b`
LL | if true 'b: {} //~ ERROR expected `{`, found `'b`
| -- ^^----
| | |
| | expected `{`
| | help: try placing this code inside a block: `{ 'b: { } }`
| this `if` statement has a condition, but no block

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

error: expected one of `.`, `?`, `{`, or an operator, found `'b`
Expand Down
3 changes: 2 additions & 1 deletion src/test/ui/missing/missing-block-hint.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error: expected `{`, found `=>`
--> $DIR/missing-block-hint.rs:13:18
|
LL | if (foo) => {} //~ ERROR expected `{`, found `=>`
| -- ^^
| -- ^^ expected `{`
| |
| this `if` statement has a condition, but no block

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

error: aborting due to 2 previous errors
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/missing/missing-semicolon-warning.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
warning: expected `;`, found `let`
warning: expected `;`, found keyword `let`
--> $DIR/missing-semicolon-warning.rs:16:12
|
LL | $( let x = $e1 )*; //~ WARN expected `;`
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/parser/doc-before-identifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

fn /// document
foo() {}
//~^^ ERROR expected identifier, found `/// document`
//~^^ ERROR expected identifier, found doc comment `/// document`

fn main() {
foo();
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/parser/doc-before-identifier.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error: expected identifier, found `/// document`
error: expected identifier, found doc comment `/// document`
--> $DIR/doc-before-identifier.rs:13:4
|
LL | fn /// document
| ^^^^^^^^^^^^ expected identifier
| ^^^^^^^^^^^^ expected identifier, found doc comment

error: aborting due to previous error

4 changes: 4 additions & 0 deletions src/test/ui/parser/doc-comment-in-if-statement.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main() {
if true /*!*/ {}
//~^ ERROR expected `{`, found doc comment `/*!*/`
}
10 changes: 10 additions & 0 deletions src/test/ui/parser/doc-comment-in-if-statement.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
error: expected `{`, found doc comment `/*!*/`
--> $DIR/doc-comment-in-if-statement.rs:2:13
|
LL | if true /*!*/ {}
| -- ^^^^^ expected `{`
| |
| this `if` statement has a condition, but no block

error: aborting due to previous error

2 changes: 1 addition & 1 deletion src/test/ui/parser/import-from-rename.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: expected `;`, found `as`
error: expected `;`, found keyword `as`
--> $DIR/import-from-rename.rs:15:16
|
LL | use foo::{bar} as baz;
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/parser/import-glob-rename.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: expected `;`, found `as`
error: expected `;`, found keyword `as`
--> $DIR/import-glob-rename.rs:15:12
|
LL | use foo::* as baz;
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/parser/issue-17904-2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@

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

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

fn main() {}
4 changes: 2 additions & 2 deletions src/test/ui/parser/issue-17904-2.stderr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
error: expected item, found `where`
error: expected item, found keyword `where`
--> $DIR/issue-17904-2.rs:13:24
|
LL | struct Bar<T> { x: T } where T: Copy //~ ERROR expected item, found `where`
LL | struct Bar<T> { x: T } where T: Copy //~ ERROR expected item, found keyword `where`
| ^^^^^ expected item

error: aborting due to previous error
Expand Down
3 changes: 2 additions & 1 deletion src/test/ui/parser/unsized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

// Test syntax checks for `type` keyword.

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

pub fn main() {
}
4 changes: 2 additions & 2 deletions src/test/ui/parser/unsized.stderr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
error: expected `where`, `{`, `(`, or `;` after struct name, found `for`
error: expected `where`, `{`, `(`, or `;` after struct name, found keyword `for`
--> $DIR/unsized.rs:15:11
|
LL | struct S1 for type; //~ ERROR expected `where`, `{`, `(`, or `;` after struct name, found `for`
LL | struct S1 for type;
| ^^^ expected `where`, `{`, `(`, or `;` after struct name

error: aborting due to previous error
Expand Down
3 changes: 2 additions & 1 deletion src/test/ui/parser/virtual-structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

// Test diagnostics for the removed struct inheritance feature.

virtual struct SuperStruct { //~ ERROR expected item, found `virtual`
virtual struct SuperStruct {
//~^ ERROR expected item, found reserved keyword `virtual`
f1: isize,
}

Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/parser/virtual-structs.stderr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
error: expected item, found `virtual`
error: expected item, found reserved keyword `virtual`
--> $DIR/virtual-structs.rs:15:1
|
LL | virtual struct SuperStruct { //~ ERROR expected item, found `virtual`
LL | virtual struct SuperStruct {
| ^^^^^^^ expected item

error: aborting due to previous error
Expand Down