Auto merge of #16553 - Veykril:field-lit-recovery, r=Veykril

fix: Imrpove recover on `=` for record field initializer and pattern
This commit is contained in:
bors 2024-02-13 17:28:36 +00:00
commit 2c05da15a9
7 changed files with 137 additions and 27 deletions

View File

@ -678,27 +678,38 @@ pub(crate) fn record_expr_field_list(p: &mut Parser<'_>) {
attributes::outer_attrs(p);
match p.current() {
IDENT | INT_NUMBER => {
IDENT | INT_NUMBER if p.nth_at(1, T![::]) => {
// test_err record_literal_missing_ellipsis_recovery
// fn main() {
// S { S::default() }
// }
if p.nth_at(1, T![::]) {
m.abandon(p);
p.expect(T![..]);
expr(p);
} else {
}
IDENT | INT_NUMBER => {
if p.nth_at(1, T![..]) {
// test_err record_literal_before_ellipsis_recovery
// fn main() {
// S { field ..S::default() }
// }
if p.nth_at(1, T![:]) || p.nth_at(1, T![..]) {
name_ref_or_index(p);
p.expect(T![:]);
p.error("expected `:`");
} else {
// test_err record_literal_field_eq_recovery
// fn main() {
// S { field = foo }
// }
if p.nth_at(1, T![:]) {
name_ref_or_index(p);
p.bump(T![:]);
} else if p.nth_at(1, T![=]) {
name_ref_or_index(p);
p.err_and_bump("expected `:`");
}
expr(p);
m.complete(p, RECORD_EXPR_FIELD);
}
m.complete(p, RECORD_EXPR_FIELD);
}
T![.] if p.at(T![..]) => {
m.abandon(p);

View File

@ -323,6 +323,15 @@ fn record_pat_field(p: &mut Parser<'_>) {
p.bump(T![:]);
pattern(p);
}
// test_err record_pat_field_eq_recovery
// fn main() {
// let S { field = foo };
// }
IDENT | INT_NUMBER if p.nth(1) == T![=] => {
name_ref_or_index(p);
p.err_and_bump("expected `:`");
pattern(p);
}
T![box] => {
// FIXME: not all box patterns should be allowed
box_pat(p);

View File

@ -25,7 +25,6 @@ SOURCE_FILE
NAME_REF
IDENT "field"
WHITESPACE " "
RANGE_EXPR
DOT2 ".."
CALL_EXPR
PATH_EXPR
@ -46,4 +45,5 @@ SOURCE_FILE
WHITESPACE "\n"
R_CURLY "}"
WHITESPACE "\n"
error 25: expected COLON
error 25: expected `:`
error 25: expected COMMA

View File

@ -0,0 +1,41 @@
SOURCE_FILE
FN
FN_KW "fn"
WHITESPACE " "
NAME
IDENT "main"
PARAM_LIST
L_PAREN "("
R_PAREN ")"
WHITESPACE " "
BLOCK_EXPR
STMT_LIST
L_CURLY "{"
WHITESPACE "\n "
RECORD_EXPR
PATH
PATH_SEGMENT
NAME_REF
IDENT "S"
WHITESPACE " "
RECORD_EXPR_FIELD_LIST
L_CURLY "{"
WHITESPACE " "
RECORD_EXPR_FIELD
NAME_REF
IDENT "field"
WHITESPACE " "
ERROR
EQ "="
WHITESPACE " "
PATH_EXPR
PATH
PATH_SEGMENT
NAME_REF
IDENT "foo"
WHITESPACE " "
R_CURLY "}"
WHITESPACE "\n"
R_CURLY "}"
WHITESPACE "\n"
error 26: expected `:`

View File

@ -0,0 +1,3 @@
fn main() {
S { field = foo }
}

View File

@ -0,0 +1,43 @@
SOURCE_FILE
FN
FN_KW "fn"
WHITESPACE " "
NAME
IDENT "main"
PARAM_LIST
L_PAREN "("
R_PAREN ")"
WHITESPACE " "
BLOCK_EXPR
STMT_LIST
L_CURLY "{"
WHITESPACE "\n "
LET_STMT
LET_KW "let"
WHITESPACE " "
RECORD_PAT
PATH
PATH_SEGMENT
NAME_REF
IDENT "S"
WHITESPACE " "
RECORD_PAT_FIELD_LIST
L_CURLY "{"
WHITESPACE " "
RECORD_PAT_FIELD
NAME_REF
IDENT "field"
WHITESPACE " "
ERROR
EQ "="
WHITESPACE " "
IDENT_PAT
NAME
IDENT "foo"
WHITESPACE " "
R_CURLY "}"
SEMICOLON ";"
WHITESPACE "\n"
R_CURLY "}"
WHITESPACE "\n"
error 30: expected `:`

View File

@ -0,0 +1,3 @@
fn main() {
let S { field = foo };
}