Skip to content

Commit

Permalink
Add label to primary span in some parse errors
Browse files Browse the repository at this point in the history
  • Loading branch information
estebank committed Feb 27, 2018
1 parent ba7039c commit 36baa81
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 25 deletions.
85 changes: 64 additions & 21 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1487,7 +1487,10 @@ impl<'a> Parser<'a> {
}
_ => {
let token_str = self.this_token_to_string();
return Err(self.fatal(&format!("expected `;` or `{{`, found `{}`", token_str)));
let mut err = self.fatal(&format!("expected `;` or `{{`, found `{}`",
token_str));
err.span_label(self.span, "expected `;` or `{`");
return Err(err);
}
};
(ident, ast::TraitItemKind::Method(sig, body), generics)
Expand Down Expand Up @@ -2218,7 +2221,12 @@ impl<'a> Parser<'a> {
TokenTree::Delimited(_, delimited) => Ok((delim, delimited.stream().into())),
_ => unreachable!(),
},
_ => Err(self.fatal("expected open delimiter")),
_ => {
let msg = "expected open delimiter";
let mut err = self.fatal(msg);
err.span_label(self.span, msg);
Err(err)
}
}
}

Expand Down Expand Up @@ -2351,7 +2359,10 @@ impl<'a> Parser<'a> {
if self.eat_keyword(keywords::Loop) {
return self.parse_loop_expr(Some(label), lo, attrs)
}
return Err(self.fatal("expected `while`, `for`, or `loop` after a label"))
let msg = "expected `while`, `for`, or `loop` after a label";
let mut err = self.fatal(msg);
err.span_label(self.span, msg);
return Err(err);
}
if self.eat_keyword(keywords::Loop) {
let lo = self.prev_span;
Expand Down Expand Up @@ -2410,6 +2421,7 @@ impl<'a> Parser<'a> {
// Catch this syntax error here, instead of in `parse_ident`, so
// that we can explicitly mention that let is not to be used as an expression
let mut db = self.fatal("expected expression, found statement (`let`)");
db.span_label(self.span, "expected expression");
db.note("variable declaration using `let` is a statement");
return Err(db);
} else if self.token.is_path_start() {
Expand Down Expand Up @@ -2445,7 +2457,9 @@ impl<'a> Parser<'a> {
self.cancel(&mut err);
let msg = format!("expected expression, found {}",
self.this_token_descr());
return Err(self.fatal(&msg));
let mut err = self.fatal(&msg);
err.span_label(self.span, "expected expression");
return Err(err);
}
}
}
Expand Down Expand Up @@ -2735,7 +2749,9 @@ impl<'a> Parser<'a> {
self.look_ahead(1, |t| t.is_ident()) => {
self.bump();
let name = match self.token { token::Ident(ident) => ident, _ => unreachable!() };
self.fatal(&format!("unknown macro variable `{}`", name)).emit();
let mut err = self.fatal(&format!("unknown macro variable `{}`", name));
err.span_label(self.span, "unknown macro variable");
err.emit();
return
}
token::Interpolated(ref nt) => {
Expand Down Expand Up @@ -3760,7 +3776,10 @@ impl<'a> Parser<'a> {
self.expect_and()?;
let mutbl = self.parse_mutability();
if let token::Lifetime(ident) = self.token {
return Err(self.fatal(&format!("unexpected lifetime `{}` in pattern", ident)));
let mut err = self.fatal(&format!("unexpected lifetime `{}` in pattern",
ident));
err.span_label(self.span, "unexpected lifetime");
return Err(err);
}
let subpat = self.parse_pat()?;
pat = PatKind::Ref(subpat, mutbl);
Expand Down Expand Up @@ -3843,7 +3862,10 @@ impl<'a> Parser<'a> {
}
token::OpenDelim(token::Brace) => {
if qself.is_some() {
return Err(self.fatal("unexpected `{` after qualified path"));
let msg = "unexpected `{` after qualified path";
let mut err = self.fatal(msg);
err.span_label(self.span, msg);
return Err(err);
}
// Parse struct pattern
self.bump();
Expand All @@ -3857,7 +3879,10 @@ impl<'a> Parser<'a> {
}
token::OpenDelim(token::Paren) => {
if qself.is_some() {
return Err(self.fatal("unexpected `(` after qualified path"));
let msg = "unexpected `(` after qualified path";
let mut err = self.fatal(msg);
err.span_label(self.span, msg);
return Err(err);
}
// Parse tuple struct or enum pattern
self.bump();
Expand Down Expand Up @@ -3889,7 +3914,9 @@ impl<'a> Parser<'a> {
Err(mut err) => {
self.cancel(&mut err);
let msg = format!("expected pattern, found {}", self.this_token_descr());
return Err(self.fatal(&msg));
let mut err = self.fatal(&msg);
err.span_label(self.span, "expected pattern");
return Err(err);
}
}
}
Expand Down Expand Up @@ -4289,9 +4316,11 @@ impl<'a> Parser<'a> {
""
};
let tok_str = self.this_token_to_string();
return Err(self.fatal(&format!("expected {}`(` or `{{`, found `{}`",
ident_str,
tok_str)))
let mut err = self.fatal(&format!("expected {}`(` or `{{`, found `{}`",
ident_str,
tok_str));
err.span_label(self.span, format!("expected {}`(` or `{{`", ident_str));
return Err(err)
},
};

Expand Down Expand Up @@ -5598,8 +5627,12 @@ impl<'a> Parser<'a> {
body
} else {
let token_str = self.this_token_to_string();
return Err(self.fatal(&format!("expected `where`, `{{`, `(`, or `;` after struct \
name, found `{}`", token_str)))
let mut err = self.fatal(&format!(
"expected `where`, `{{`, `(`, or `;` after struct name, found `{}`",
token_str
));
err.span_label(self.span, "expected `where`, `{`, `(`, or `;` after struct name");
return Err(err);
};

Ok((class_name, ItemKind::Struct(vdata, generics), None))
Expand All @@ -5618,8 +5651,10 @@ impl<'a> Parser<'a> {
VariantData::Struct(self.parse_record_struct_body()?, ast::DUMMY_NODE_ID)
} else {
let token_str = self.this_token_to_string();
return Err(self.fatal(&format!("expected `where` or `{{` after union \
name, found `{}`", token_str)))
let mut err = self.fatal(&format!(
"expected `where` or `{{` after union name, found `{}`", token_str));
err.span_label(self.span, "expected `where` or `{` after union name");
return Err(err);
};

Ok((class_name, ItemKind::Union(vdata, generics), None))
Expand Down Expand Up @@ -5666,9 +5701,10 @@ impl<'a> Parser<'a> {
self.eat(&token::CloseDelim(token::Brace));
} else {
let token_str = self.this_token_to_string();
return Err(self.fatal(&format!("expected `where`, or `{{` after struct \
name, found `{}`",
token_str)));
let mut err = self.fatal(&format!(
"expected `where`, or `{{` after struct name, found `{}`", token_str));
err.span_label(self.span, "expected `where`, or `{` after struct name");
return Err(err);
}

Ok(fields)
Expand Down Expand Up @@ -5841,9 +5877,11 @@ 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 msg = "consider removing this semicolon";
if token_str == ";" {
let msg = "consider removing this semicolon";
err.span_suggestion_short(self.span, msg, "".to_string());
} else {
err.span_label(self.span, "expected item");
}
return Err(err);
}
Expand Down Expand Up @@ -7000,7 +7038,12 @@ impl<'a> Parser<'a> {
self.expect_no_suffix(sp, "string literal", suf);
Ok((s, style))
}
_ => Err(self.fatal("expected string literal"))
_ => {
let msg = "expected string literal";
let mut err = self.fatal(msg);
err.span_label(self.span, msg);
Err(err)
}
}
}
}
2 changes: 1 addition & 1 deletion src/test/ui/cross-file-errors/main.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error: expected expression, found `_`
--> $DIR/underscore.rs:18:9
|
LL | _
| ^
| ^ expected expression
|
::: $DIR/main.rs:15:5
|
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/macro-context.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ error: expected expression, found reserved keyword `typeof`
--> $DIR/macro-context.rs:13:17
|
LL | () => ( i ; typeof ); //~ ERROR expected expression, found reserved keyword `typeof`
| ^^^^^^
| ^^^^^^ expected expression
...
LL | m!();
| ----- in this macro invocation
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/resolve/token-error-correct.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ error: expected expression, found `;`
--> $DIR/token-error-correct.rs:14:13
|
LL | foo(bar(;
| ^
| ^ expected expression

error: aborting due to 3 previous errors

2 changes: 1 addition & 1 deletion src/test/ui/token/issue-10636-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ error: expected expression, found `)`
--> $DIR/issue-10636-2.rs:18:1
|
LL | } //~ ERROR: incorrect close delimiter
| ^
| ^ expected expression

error[E0601]: main function not found

Expand Down

0 comments on commit 36baa81

Please sign in to comment.