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); attributes::outer_attrs(p);
match p.current() { match p.current() {
IDENT | INT_NUMBER => { IDENT | INT_NUMBER if p.nth_at(1, T![::]) => {
// test_err record_literal_missing_ellipsis_recovery // test_err record_literal_missing_ellipsis_recovery
// fn main() { // fn main() {
// S { S::default() } // S { S::default() }
// } // }
if p.nth_at(1, T![::]) { m.abandon(p);
m.abandon(p); p.expect(T![..]);
p.expect(T![..]); expr(p);
expr(p); }
} else { IDENT | INT_NUMBER => {
if p.nth_at(1, T![..]) {
// test_err record_literal_before_ellipsis_recovery // test_err record_literal_before_ellipsis_recovery
// fn main() { // fn main() {
// S { field ..S::default() } // S { field ..S::default() }
// } // }
if p.nth_at(1, T![:]) || p.nth_at(1, T![..]) { name_ref_or_index(p);
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); name_ref_or_index(p);
p.expect(T![:]); p.bump(T![:]);
} else if p.nth_at(1, T![=]) {
name_ref_or_index(p);
p.err_and_bump("expected `:`");
} }
expr(p); expr(p);
m.complete(p, RECORD_EXPR_FIELD);
} }
m.complete(p, RECORD_EXPR_FIELD);
} }
T![.] if p.at(T![..]) => { T![.] if p.at(T![..]) => {
m.abandon(p); m.abandon(p);

View File

@ -323,6 +323,15 @@ fn record_pat_field(p: &mut Parser<'_>) {
p.bump(T![:]); p.bump(T![:]);
pattern(p); 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] => { T![box] => {
// FIXME: not all box patterns should be allowed // FIXME: not all box patterns should be allowed
box_pat(p); box_pat(p);

View File

@ -24,26 +24,26 @@ SOURCE_FILE
RECORD_EXPR_FIELD RECORD_EXPR_FIELD
NAME_REF NAME_REF
IDENT "field" IDENT "field"
WHITESPACE " " WHITESPACE " "
RANGE_EXPR DOT2 ".."
DOT2 ".." CALL_EXPR
CALL_EXPR PATH_EXPR
PATH_EXPR PATH
PATH PATH
PATH PATH_SEGMENT
PATH_SEGMENT NAME_REF
NAME_REF IDENT "S"
IDENT "S" COLON2 "::"
COLON2 "::" PATH_SEGMENT
PATH_SEGMENT NAME_REF
NAME_REF IDENT "default"
IDENT "default" ARG_LIST
ARG_LIST L_PAREN "("
L_PAREN "(" R_PAREN ")"
R_PAREN ")"
WHITESPACE " " WHITESPACE " "
R_CURLY "}" R_CURLY "}"
WHITESPACE "\n" WHITESPACE "\n"
R_CURLY "}" R_CURLY "}"
WHITESPACE "\n" 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 };
}