1
Fork 0

Use token description in "expected/found" parse messages

This commit is contained in:
Esteban Küber 2018-10-28 16:05:07 -07:00
parent 3e6f30ec3e
commit 3e22e0c3bc
23 changed files with 67 additions and 47 deletions

View file

@ -611,6 +611,7 @@ impl<'a> Parser<'a> {
t if t.is_special_ident() => "reserved identifier", t if t.is_special_ident() => "reserved identifier",
t if t.is_used_keyword() => "keyword", t if t.is_used_keyword() => "keyword",
t if t.is_unused_keyword() => "reserved keyword", t if t.is_unused_keyword() => "reserved keyword",
token::DocComment(..) => "doc comment",
_ => return None, _ => return None,
}) })
} }
@ -644,8 +645,8 @@ impl<'a> Parser<'a> {
Ok(()) Ok(())
} else { } else {
let token_str = pprust::token_to_string(t); let token_str = pprust::token_to_string(t);
let this_token_str = self.this_token_to_string(); let this_token_str = self.this_token_descr();
let mut err = self.fatal(&format!("expected `{}`, found `{}`", let mut err = self.fatal(&format!("expected `{}`, found {}",
token_str, token_str,
this_token_str)); this_token_str));
@ -1444,8 +1445,8 @@ impl<'a> Parser<'a> {
Some(body) Some(body)
} }
_ => { _ => {
let token_str = self.this_token_to_string(); let token_str = self.this_token_descr();
let mut err = self.fatal(&format!("expected `;` or `{{`, found `{}`", let mut err = self.fatal(&format!("expected `;` or `{{`, found {}",
token_str)); token_str));
err.span_label(self.span, "expected `;` or `{`"); err.span_label(self.span, "expected `;` or `{`");
return Err(err); return Err(err);
@ -1453,8 +1454,8 @@ impl<'a> Parser<'a> {
} }
} }
_ => { _ => {
let token_str = self.this_token_to_string(); let token_str = self.this_token_descr();
let mut err = self.fatal(&format!("expected `;` or `{{`, found `{}`", let mut err = self.fatal(&format!("expected `;` or `{{`, found {}",
token_str)); token_str));
err.span_label(self.span, "expected `;` or `{`"); err.span_label(self.span, "expected `;` or `{`");
return Err(err); return Err(err);
@ -3917,8 +3918,8 @@ impl<'a> Parser<'a> {
etc_span = Some(etc_sp); etc_span = Some(etc_sp);
break; break;
} }
let token_str = self.this_token_to_string(); let token_str = self.this_token_descr();
let mut err = self.fatal(&format!("expected `}}`, found `{}`", token_str)); let mut err = self.fatal(&format!("expected `}}`, found {}", token_str));
err.span_label(self.span, "expected `}`"); err.span_label(self.span, "expected `}`");
let mut comma_sp = None; let mut comma_sp = None;
@ -4680,8 +4681,8 @@ impl<'a> Parser<'a> {
} else { } else {
"" ""
}; };
let tok_str = self.this_token_to_string(); let tok_str = self.this_token_descr();
let mut err = self.fatal(&format!("expected {}`(` or `{{`, found `{}`", let mut err = self.fatal(&format!("expected {}`(` or `{{`, found {}",
ident_str, ident_str,
tok_str)); tok_str));
err.span_label(self.span, format!("expected {}`(` or `{{`", ident_str)); err.span_label(self.span, format!("expected {}`(` or `{{`", ident_str));
@ -4817,8 +4818,8 @@ impl<'a> Parser<'a> {
if !self.eat(&token::OpenDelim(token::Brace)) { if !self.eat(&token::OpenDelim(token::Brace)) {
let sp = self.span; let sp = self.span;
let tok = self.this_token_to_string(); let tok = self.this_token_descr();
let mut e = self.span_fatal(sp, &format!("expected `{{`, found `{}`", tok)); let mut e = self.span_fatal(sp, &format!("expected `{{`, found {}", tok));
let do_not_suggest_help = let do_not_suggest_help =
self.token.is_keyword(keywords::In) || self.token == token::Colon; self.token.is_keyword(keywords::In) || self.token == token::Colon;
@ -4880,6 +4881,7 @@ impl<'a> Parser<'a> {
} }
_ => () _ => ()
} }
e.span_label(sp, "expected `{`");
return Err(e); return Err(e);
} }
@ -4975,7 +4977,7 @@ impl<'a> Parser<'a> {
fn warn_missing_semicolon(&self) { fn warn_missing_semicolon(&self) {
self.diagnostic().struct_span_warn(self.span, { self.diagnostic().struct_span_warn(self.span, {
&format!("expected `;`, found `{}`", self.this_token_to_string()) &format!("expected `;`, found {}", self.this_token_descr())
}).note({ }).note({
"This was erroneously allowed and will become a hard error in a future release" "This was erroneously allowed and will become a hard error in a future release"
}).emit(); }).emit();
@ -6014,9 +6016,9 @@ impl<'a> Parser<'a> {
self.expect(&token::Semi)?; self.expect(&token::Semi)?;
body body
} else { } else {
let token_str = self.this_token_to_string(); let token_str = self.this_token_descr();
let mut err = self.fatal(&format!( let mut err = self.fatal(&format!(
"expected `where`, `{{`, `(`, or `;` after struct name, found `{}`", "expected `where`, `{{`, `(`, or `;` after struct name, found {}",
token_str token_str
)); ));
err.span_label(self.span, "expected `where`, `{`, `(`, or `;` after struct name"); err.span_label(self.span, "expected `where`, `{`, `(`, or `;` after struct name");
@ -6038,9 +6040,9 @@ impl<'a> Parser<'a> {
} else if self.token == token::OpenDelim(token::Brace) { } else if self.token == token::OpenDelim(token::Brace) {
VariantData::Struct(self.parse_record_struct_body()?, ast::DUMMY_NODE_ID) VariantData::Struct(self.parse_record_struct_body()?, ast::DUMMY_NODE_ID)
} else { } else {
let token_str = self.this_token_to_string(); let token_str = self.this_token_descr();
let mut err = self.fatal(&format!( 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"); err.span_label(self.span, "expected `where` or `{` after union name");
return Err(err); return Err(err);
}; };
@ -6088,9 +6090,9 @@ impl<'a> Parser<'a> {
} }
self.eat(&token::CloseDelim(token::Brace)); self.eat(&token::CloseDelim(token::Brace));
} else { } else {
let token_str = self.this_token_to_string(); let token_str = self.this_token_descr();
let mut err = self.fatal(&format!( 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"); err.span_label(self.span, "expected `where`, or `{` after struct name");
return Err(err); return Err(err);
} }
@ -6166,8 +6168,8 @@ impl<'a> Parser<'a> {
} }
_ => { _ => {
let sp = self.sess.source_map().next_point(self.prev_span); let sp = self.sess.source_map().next_point(self.prev_span);
let mut err = self.struct_span_err(sp, &format!("expected `,`, or `}}`, found `{}`", let mut err = self.struct_span_err(sp, &format!("expected `,`, or `}}`, found {}",
self.this_token_to_string())); self.this_token_descr()));
if self.token.is_ident() { if self.token.is_ident() {
// This is likely another field; emit the diagnostic and keep going // This is likely another field; emit the diagnostic and keep going
err.span_suggestion_with_applicability( err.span_suggestion_with_applicability(
@ -6303,8 +6305,8 @@ impl<'a> Parser<'a> {
} }
if !self.eat(term) { if !self.eat(term) {
let token_str = self.this_token_to_string(); let token_str = self.this_token_descr();
let mut err = self.fatal(&format!("expected item, found `{}`", token_str)); let mut err = self.fatal(&format!("expected item, found {}", token_str));
if token_str == ";" { if token_str == ";" {
let msg = "consider removing this semicolon"; let msg = "consider removing this semicolon";
err.span_suggestion_short_with_applicability( err.span_suggestion_short_with_applicability(

View file

@ -28,7 +28,7 @@ error: expected `{`, found `;`
LL | if not // lack of braces is [sic] LL | if not // lack of braces is [sic]
| -- this `if` statement has a condition, but no block | -- this `if` statement has a condition, but no block
LL | println!("Then when?"); LL | println!("Then when?");
| ^ | ^ expected `{`
error: unexpected `2` after identifier error: unexpected `2` after identifier
--> $DIR/issue-46836-identifier-not-instead-of-negation.rs:36:24 --> $DIR/issue-46836-identifier-not-instead-of-negation.rs:36:24

View file

@ -16,3 +16,4 @@ fn main() {
} }
} }
//~^ ERROR expected `{`, found `}` //~^ ERROR expected `{`, found `}`
//~| NOTE expected `{`

View file

@ -5,7 +5,7 @@ LL | if 5 == {
| -- this `if` statement has a condition, but no block | -- this `if` statement has a condition, but no block
... ...
LL | } LL | }
| ^ | ^ expected `{`
error: aborting due to previous error error: aborting due to previous error

View file

@ -1,4 +1,4 @@
error: expected `{`, found `in` error: expected `{`, found keyword `in`
--> $DIR/issue-51602.rs:12:10 --> $DIR/issue-51602.rs:12:10
| |
LL | if i in 1..10 { LL | if i in 1..10 {

View file

@ -7,7 +7,7 @@ LL | if $tgt.has_$field() {}
| this `if` statement has a condition, but no block | this `if` statement has a condition, but no block
... ...
LL | get_opt!(bar, foo); LL | get_opt!(bar, foo);
| ^^^ | ^^^ expected `{`
error: aborting due to previous error error: aborting due to previous error

View file

@ -2,9 +2,7 @@ error: expected item, found `;`
--> $DIR/issue-46186.rs:13:2 --> $DIR/issue-46186.rs:13:2
| |
LL | }; //~ ERROR expected item, found `;` LL | }; //~ ERROR expected item, found `;`
| ^ help: consider removing this semicolon | ^ expected item
|
= help: braced struct declarations are not followed by a semicolon
error: aborting due to previous error error: aborting due to previous error

View file

@ -2,7 +2,7 @@ error: expected item, found `;`
--> $DIR/issue-49040.rs:11:28 --> $DIR/issue-49040.rs:11:28
| |
LL | #![allow(unused_variables)]; //~ ERROR expected item, found `;` LL | #![allow(unused_variables)]; //~ ERROR expected item, found `;`
| ^ help: consider removing this semicolon | ^ expected item
error: aborting due to previous error error: aborting due to previous error

View file

@ -10,6 +10,7 @@ error: expected `{`, found `'b`
LL | if true 'b: {} //~ ERROR expected `{`, found `'b` LL | if true 'b: {} //~ ERROR expected `{`, found `'b`
| -- ^^---- | -- ^^----
| | | | | |
| | expected `{`
| | help: try placing this code inside a block: `{ 'b: { } }` | | help: try placing this code inside a block: `{ 'b: { } }`
| this `if` statement has a condition, but no block | this `if` statement has a condition, but no block
@ -19,6 +20,7 @@ error: expected `{`, found `'b`
LL | if true {} else 'b: {} //~ ERROR expected `{`, found `'b` LL | if true {} else 'b: {} //~ ERROR expected `{`, found `'b`
| ^^---- | ^^----
| | | |
| expected `{`
| help: try placing this code inside a block: `{ 'b: { } }` | help: try placing this code inside a block: `{ 'b: { } }`
error: expected one of `.`, `?`, `{`, or an operator, found `'b` error: expected one of `.`, `?`, `{`, or an operator, found `'b`

View file

@ -2,7 +2,7 @@ error: expected `{`, found `=>`
--> $DIR/missing-block-hint.rs:13:18 --> $DIR/missing-block-hint.rs:13:18
| |
LL | if (foo) => {} //~ ERROR expected `{`, found `=>` LL | if (foo) => {} //~ ERROR expected `{`, found `=>`
| -- ^^ | -- ^^ expected `{`
| | | |
| this `if` statement has a condition, but no block | this `if` statement has a condition, but no block
@ -14,6 +14,7 @@ LL | if (foo)
LL | bar; //~ ERROR expected `{`, found `bar` LL | bar; //~ ERROR expected `{`, found `bar`
| ^^^- | ^^^-
| | | |
| expected `{`
| help: try placing this code inside a block: `{ bar; }` | help: try placing this code inside a block: `{ bar; }`
error: aborting due to 2 previous errors error: aborting due to 2 previous errors

View file

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

View file

@ -12,7 +12,7 @@
fn /// document fn /// document
foo() {} foo() {}
//~^^ ERROR expected identifier, found `/// document` //~^^ ERROR expected identifier, found doc comment `/// document`
fn main() { fn main() {
foo(); foo();

View file

@ -1,8 +1,8 @@
error: expected identifier, found `/// document` error: expected identifier, found doc comment `/// document`
--> $DIR/doc-before-identifier.rs:13:4 --> $DIR/doc-before-identifier.rs:13:4
| |
LL | fn /// document LL | fn /// document
| ^^^^^^^^^^^^ expected identifier | ^^^^^^^^^^^^ expected identifier, found doc comment
error: aborting due to previous error error: aborting due to previous error

View file

@ -0,0 +1,4 @@
fn main() {
if true /*!*/ {}
//~^ ERROR expected `{`, found doc comment `/*!*/`
}

View file

@ -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

View file

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

View file

@ -1,4 +1,4 @@
error: expected `;`, found `as` error: expected `;`, found keyword `as`
--> $DIR/import-glob-rename.rs:15:12 --> $DIR/import-glob-rename.rs:15:12
| |
LL | use foo::* as baz; LL | use foo::* as baz;

View file

@ -10,6 +10,6 @@
// compile-flags: -Z parse-only -Z continue-parse-after-error // 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() {} fn main() {}

View file

@ -1,7 +1,7 @@
error: expected item, found `where` error: expected item, found keyword `where`
--> $DIR/issue-17904-2.rs:13:24 --> $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 | ^^^^^ expected item
error: aborting due to previous error error: aborting due to previous error

View file

@ -12,7 +12,8 @@
// Test syntax checks for `type` keyword. // 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() { pub fn main() {
} }

View file

@ -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 --> $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 | ^^^ expected `where`, `{`, `(`, or `;` after struct name
error: aborting due to previous error error: aborting due to previous error

View file

@ -12,7 +12,8 @@
// Test diagnostics for the removed struct inheritance feature. // 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, f1: isize,
} }

View file

@ -1,7 +1,7 @@
error: expected item, found `virtual` error: expected item, found reserved keyword `virtual`
--> $DIR/virtual-structs.rs:15:1 --> $DIR/virtual-structs.rs:15:1
| |
LL | virtual struct SuperStruct { //~ ERROR expected item, found `virtual` LL | virtual struct SuperStruct {
| ^^^^^^^ expected item | ^^^^^^^ expected item
error: aborting due to previous error error: aborting due to previous error