Skip to content
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

refactor expr & stmt parsing + improve recovery #66994

Merged
merged 34 commits into from
Dec 21, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
666ff8f
reduce repetition in stmt parsing
Centril Dec 2, 2019
b75a93a
extract parse_sttmt_mac
Centril Dec 3, 2019
690815b
inline parse_stmt_ into parse_stmt
Centril Dec 3, 2019
c54c9ef
parser: early return for item stmt
Centril Dec 3, 2019
2ddea30
extract suggest_slice_pat
Centril Dec 3, 2019
74d9c4b
parse_stmt_mac: add a comment
Centril Dec 3, 2019
467c86f
parse_stmt_without_recovery: readability!
Centril Dec 3, 2019
cdca5cf
parser: extract error_outer_attrs
Centril Dec 3, 2019
be463cb
extract: error_block_no_opening_brace
Centril Dec 3, 2019
903c9df
extract should_continue_as_assoc_expr
Centril Dec 3, 2019
52acaa6
implement recovery in check_assoc_op
Centril Dec 3, 2019
dd15904
parse_bottom_expr: use else if
Centril Dec 3, 2019
0b7908c
Add a UI test for correct parsing
achan1989 Sep 16, 2019
0c32ee1
Clean up `parse_bottom_expr`
achan1989 Sep 16, 2019
9cb2b08
extract parse_tuple_parens_expr
Centril Dec 3, 2019
cb985ba
extract parse_array_or_repeat_expr
Centril Dec 3, 2019
5f0f86b
extract parse_path_start_expr
Centril Dec 3, 2019
3ed5ba7
extract parse_labeled_expr
Centril Dec 3, 2019
4e01b70
add recovery to parse_labeled_expr
Centril Dec 3, 2019
32ac9d0
pass attr as param in new methods
Centril Dec 3, 2019
327641e
recover on 'do catch { .. }'
Centril Dec 3, 2019
2f9b191
extract parse_{expr_opt, return_expr, yield_expr}
Centril Dec 3, 2019
a3c0ef1
refactor parse_incorrect_await_syntax
Centril Dec 3, 2019
e9a4d94
extract parse_break_expr
Centril Dec 3, 2019
4311a4d
extract parse_lit_expr and simplify
Centril Dec 3, 2019
948ff67
use mk_expr_err more
Centril Dec 3, 2019
3d5dbcb
simplify parse_bottom_expr more
Centril Dec 3, 2019
a0d2093
introduce 'type AttrVec'
Centril Dec 3, 2019
c9e1f13
recover on 'mut', 'var', 'auto'
Centril Dec 3, 2019
66470d3
recover `#[attr] if expr {}`
Centril Dec 4, 2019
9b53c5b
fix bug in parse_tuple_parens_expr + related refactoring
Centril Dec 4, 2019
19db2d2
ast_stmt_expr_attr -> pretty & ui tests
Centril Dec 4, 2019
4982684
use .span_suggestion_short for &&
Centril Dec 4, 2019
621661f
tweak var/auto/mut recovery
Centril Dec 4, 2019
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
Prev Previous commit
tweak var/auto/mut recovery
  • Loading branch information
Centril committed Dec 20, 2019
commit 621661f8a63f2118f3add5c3d686d9a2b6f62e5e
14 changes: 7 additions & 7 deletions src/librustc_parse/parser/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,16 @@ impl<'a> Parser<'a> {
return self.parse_local_mk(lo, attrs.into()).map(Some)
}
if self.is_kw_followed_by_ident(kw::Mut) {
return self.recover_stmt_local(lo, attrs.into(), "missing `let`", "let mut");
return self.recover_stmt_local(lo, attrs.into(), "missing keyword", "let mut");
}
if self.is_kw_followed_by_ident(kw::Auto) {
self.bump(); // `auto`
let msg = "to introduce a variable, write `let` instead of `auto`";
let msg = "write `let` instead of `auto` to introduce a new variable";
return self.recover_stmt_local(lo, attrs.into(), msg, "let");
}
if self.is_kw_followed_by_ident(sym::var) {
self.bump(); // `var`
let msg = "to introduce a variable, write `let` instead of `var`";
let msg = "write `let` instead of `var` to introduce a new variable";
return self.recover_stmt_local(lo, attrs.into(), msg, "let");
}

Expand Down Expand Up @@ -208,14 +208,14 @@ impl<'a> Parser<'a> {

fn recover_stmt_local(
&mut self,
span: Span,
lo: Span,
attrs: AttrVec,
msg: &str,
sugg: &str,
) -> PResult<'a, Option<Stmt>> {
let stmt = self.parse_local_mk(span, attrs)?;
self.struct_span_err(stmt.span, "invalid variable declaration")
.span_suggestion_short(span, msg, sugg.to_string(), Applicability::MachineApplicable)
let stmt = self.parse_local_mk(lo, attrs)?;
self.struct_span_err(lo, "invalid variable declaration")
.span_suggestion(lo, msg, sugg.to_string(), Applicability::MachineApplicable)
.emit();
Ok(Some(stmt))
}
Expand Down
12 changes: 6 additions & 6 deletions src/test/ui/parser/issue-65257-invalid-var-decl-recovery.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
fn main() {
auto n = 0;//~ ERROR invalid variable declaration
//~^ HELP to introduce a variable, write `let` instead of `auto`
//~^ HELP write `let` instead of `auto` to introduce a new variable
auto m;//~ ERROR invalid variable declaration
//~^ HELP to introduce a variable, write `let` instead of `auto`
//~^ HELP write `let` instead of `auto` to introduce a new variable
m = 0;

var n = 0;//~ ERROR invalid variable declaration
//~^ HELP to introduce a variable, write `let` instead of `var`
//~^ HELP write `let` instead of `var` to introduce a new variable
var m;//~ ERROR invalid variable declaration
//~^ HELP to introduce a variable, write `let` instead of `var`
//~^ HELP write `let` instead of `var` to introduce a new variable
m = 0;

mut n = 0;//~ ERROR invalid variable declaration
//~^ HELP missing `let`
//~^ HELP missing keyword
mut var;//~ ERROR invalid variable declaration
//~^ HELP missing `let`
//~^ HELP missing keyword
var = 0;

let _recovery_witness: () = 0; //~ ERROR mismatched types
Expand Down
44 changes: 26 additions & 18 deletions src/test/ui/parser/issue-65257-invalid-var-decl-recovery.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,57 @@ error: invalid variable declaration
--> $DIR/issue-65257-invalid-var-decl-recovery.rs:2:5
|
LL | auto n = 0;
| ----^^^^^^
| |
| help: to introduce a variable, write `let` instead of `auto`
| ^^^^
|
help: write `let` instead of `auto` to introduce a new variable
|
LL | let n = 0;
| ^^^

error: invalid variable declaration
--> $DIR/issue-65257-invalid-var-decl-recovery.rs:4:5
|
LL | auto m;
| ----^^
| |
| help: to introduce a variable, write `let` instead of `auto`
| ^^^^
|
help: write `let` instead of `auto` to introduce a new variable
|
LL | let m;
| ^^^

error: invalid variable declaration
--> $DIR/issue-65257-invalid-var-decl-recovery.rs:8:5
|
LL | var n = 0;
| ---^^^^^^
| |
| help: to introduce a variable, write `let` instead of `var`
| ^^^
|
help: write `let` instead of `var` to introduce a new variable
|
LL | let n = 0;
| ^^^

error: invalid variable declaration
--> $DIR/issue-65257-invalid-var-decl-recovery.rs:10:5
|
LL | var m;
| ---^^
| |
| help: to introduce a variable, write `let` instead of `var`
| ^^^
|
help: write `let` instead of `var` to introduce a new variable
|
LL | let m;
| ^^^

error: invalid variable declaration
--> $DIR/issue-65257-invalid-var-decl-recovery.rs:14:5
|
LL | mut n = 0;
| ---^^^^^^
| |
| help: missing `let`
| ^^^ help: missing keyword: `let mut`

error: invalid variable declaration
--> $DIR/issue-65257-invalid-var-decl-recovery.rs:16:5
|
LL | mut var;
| ---^^^^
| |
| help: missing `let`
| ^^^ help: missing keyword: `let mut`

error[E0308]: mismatched types
--> $DIR/issue-65257-invalid-var-decl-recovery.rs:20:33
Expand Down