Rollup merge of #53373 - estebank:unclosed, r=petrochenkov

Tweak unclosed delimiter parser error
This commit is contained in:
kennytm 2018-08-17 00:13:24 +08:00 committed by GitHub
commit 098d80d44a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 58 additions and 61 deletions

View File

@ -67,6 +67,7 @@ pub struct StringReader<'a> {
span_src_raw: Span,
open_braces: Vec<(token::DelimToken, Span)>,
crate override_span: Option<Span>,
last_unclosed_found_span: Option<Span>,
}
impl<'a> StringReader<'a> {
@ -216,6 +217,7 @@ fn new_raw_internal(sess: &'a ParseSess, filemap: Lrc<syntax_pos::FileMap>,
span_src_raw: syntax_pos::DUMMY_SP,
open_braces: Vec::new(),
override_span,
last_unclosed_found_span: None,
}
}

View File

@ -81,16 +81,23 @@ fn parse_token_tree(&mut self) -> PResult<'a, TokenStream> {
// Incorrect delimiter.
token::CloseDelim(other) => {
let token_str = token_to_string(&self.token);
let msg = format!("incorrect close delimiter: `{}`", token_str);
let mut err = self.sess.span_diagnostic.struct_span_err(self.span, &msg);
// This is a conservative error: only report the last unclosed delimiter.
// The previous unclosed delimiters could actually be closed! The parser
// just hasn't gotten to them yet.
if let Some(&(_, sp)) = self.open_braces.last() {
err.span_note(sp, "unclosed delimiter");
};
err.emit();
if self.last_unclosed_found_span != Some(self.span) {
// do not complain about the same unclosed delimiter multiple times
self.last_unclosed_found_span = Some(self.span);
let msg = format!("incorrect close delimiter: `{}`", token_str);
let mut err = self.sess.span_diagnostic.struct_span_err(
self.span,
&msg,
);
err.span_label(self.span, "incorrect close delimiter");
// This is a conservative error: only report the last unclosed
// delimiter. The previous unclosed delimiters could actually be
// closed! The parser just hasn't gotten to them yet.
if let Some(&(_, sp)) = self.open_braces.last() {
err.span_label(sp, "unclosed delimiter");
};
err.emit();
}
self.open_braces.pop().unwrap();
// If the incorrect delimiter matches an earlier opening
@ -122,7 +129,8 @@ fn parse_token_tree(&mut self) -> PResult<'a, TokenStream> {
// matching opening delimiter).
let token_str = token_to_string(&self.token);
let msg = format!("unexpected close delimiter: `{}`", token_str);
let err = self.sess.span_diagnostic.struct_span_err(self.span, &msg);
let mut err = self.sess.span_diagnostic.struct_span_err(self.span, &msg);
err.span_label(self.span, "unexpected close delimiter");
Err(err)
},
_ => {

View File

@ -2,7 +2,7 @@ error: unexpected close delimiter: `)`
--> $DIR/invalid-punct-ident-4.rs:16:1
|
LL | lexer_failure!(); //~ ERROR proc macro panicked
| ^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^ unexpected close delimiter
error: proc macro panicked
--> $DIR/invalid-punct-ident-4.rs:16:1

View File

@ -12,4 +12,8 @@
struct Obj { //~ NOTE: unclosed delimiter
member: usize
) //~ ERROR: incorrect close delimiter
)
//~^ ERROR incorrect close delimiter
//~| NOTE incorrect close delimiter
fn main() {}

View File

@ -0,0 +1,11 @@
error: incorrect close delimiter: `)`
--> $DIR/issue-10636-1.rs:15:1
|
LL | struct Obj { //~ NOTE: unclosed delimiter
| - unclosed delimiter
LL | member: usize
LL | )
| ^ incorrect close delimiter
error: aborting due to previous error

View File

@ -1,14 +1,11 @@
error: incorrect close delimiter: `)`
--> $DIR/parser-recovery-2.rs:18:5
|
LL | ) //~ ERROR incorrect close delimiter: `)`
| ^
|
note: unclosed delimiter
--> $DIR/parser-recovery-2.rs:16:14
|
LL | fn bar() {
| ^
| - unclosed delimiter
LL | let x = foo(); //~ ERROR cannot find function `foo` in this scope
LL | ) //~ ERROR incorrect close delimiter: `)`
| ^ incorrect close delimiter
error: unexpected token: `;`
--> $DIR/parser-recovery-2.rs:22:15

View File

@ -1,14 +1,11 @@
error: incorrect close delimiter: `)`
--> $DIR/token-error-correct-2.rs:16:5
|
LL | ) //~ ERROR: incorrect close delimiter: `)`
| ^
|
note: unclosed delimiter
--> $DIR/token-error-correct-2.rs:14:12
|
LL | if foo {
| ^
| - unclosed delimiter
LL | //~^ ERROR: cannot find value `foo`
LL | ) //~ ERROR: incorrect close delimiter: `)`
| ^ incorrect close delimiter
error[E0425]: cannot find value `foo` in this scope
--> $DIR/token-error-correct-2.rs:14:8

View File

@ -1,14 +1,11 @@
error: incorrect close delimiter: `}`
--> $DIR/token-error-correct-3.rs:30:9
|
LL | } else { //~ ERROR: incorrect close delimiter: `}`
| ^
|
note: unclosed delimiter
--> $DIR/token-error-correct-3.rs:24:21
|
LL | callback(path.as_ref(); //~ ERROR expected one of
| ^
| - unclosed delimiter
...
LL | } else { //~ ERROR: incorrect close delimiter: `}`
| ^ incorrect close delimiter
error: expected one of `)`, `,`, `.`, `?`, or an operator, found `;`
--> $DIR/token-error-correct-3.rs:24:35

View File

@ -15,4 +15,3 @@ fn main() {
//~^ ERROR: expected expression, found `;`
}
//~^ ERROR: incorrect close delimiter: `}`
//~| ERROR: incorrect close delimiter: `}`

View File

@ -1,26 +1,11 @@
error: incorrect close delimiter: `}`
--> $DIR/token-error-correct.rs:16:1
|
LL | }
| ^
|
note: unclosed delimiter
--> $DIR/token-error-correct.rs:14:12
|
LL | foo(bar(;
| ^
error: incorrect close delimiter: `}`
--> $DIR/token-error-correct.rs:16:1
|
| - unclosed delimiter
LL | //~^ ERROR: expected expression, found `;`
LL | }
| ^
|
note: unclosed delimiter
--> $DIR/token-error-correct.rs:14:8
|
LL | foo(bar(;
| ^
| ^ incorrect close delimiter
error: expected expression, found `;`
--> $DIR/token-error-correct.rs:14:13
@ -28,5 +13,5 @@ error: expected expression, found `;`
LL | foo(bar(;
| ^ expected expression
error: aborting due to 3 previous errors
error: aborting due to 2 previous errors

View File

@ -1,14 +1,11 @@
error: incorrect close delimiter: `}`
--> $DIR/issue-10636-2.rs:18:1
|
LL | } //~ ERROR: incorrect close delimiter
| ^
|
note: unclosed delimiter
--> $DIR/issue-10636-2.rs:15:15
|
LL | option.map(|some| 42;
| ^
| - unclosed delimiter
...
LL | } //~ ERROR: incorrect close delimiter
| ^ incorrect close delimiter
error: expected one of `)`, `,`, `.`, `?`, or an operator, found `;`
--> $DIR/issue-10636-2.rs:15:25