Skip to content

Commit

Permalink
Merge pull request #18625 from Veykril/push-npnxwpxuzlqz
Browse files Browse the repository at this point in the history
fix: Fix parser getting stuck for bad asm expressions
  • Loading branch information
Veykril authored Dec 6, 2024
2 parents 150b0e7 + 4a24e72 commit cddaf74
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 4 deletions.
18 changes: 14 additions & 4 deletions crates/parser/src/grammar/expressions/atom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,10 +345,7 @@ fn parse_asm_expr(p: &mut Parser<'_>, m: Marker) -> Option<CompletedMarker> {
name(p);
p.bump(T![=]);
allow_templates = false;
true
} else {
false
};
}

let op = p.start();
let dir_spec = p.start();
Expand Down Expand Up @@ -399,6 +396,19 @@ fn parse_asm_expr(p: &mut Parser<'_>, m: Marker) -> Option<CompletedMarker> {
op.abandon(p);
op_n.abandon(p);
p.err_and_bump("expected asm operand");

// improves error recovery and handles err_and_bump recovering from `{` which gets
// the parser stuck here
if p.at(T!['{']) {
// test_err bad_asm_expr
// fn foo() {
// builtin#asm(
// label crashy = { return; }
// );
// }
expr(p);
}

if p.at(T!['}']) {
break;
}
Expand Down
2 changes: 2 additions & 0 deletions crates/parser/test_data/generated/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,8 @@ mod err {
run_and_expect_errors("test_data/parser/inline/err/async_without_semicolon.rs");
}
#[test]
fn bad_asm_expr() { run_and_expect_errors("test_data/parser/inline/err/bad_asm_expr.rs"); }
#[test]
fn comma_after_functional_update_syntax() {
run_and_expect_errors(
"test_data/parser/inline/err/comma_after_functional_update_syntax.rs",
Expand Down
50 changes: 50 additions & 0 deletions crates/parser/test_data/parser/inline/err/bad_asm_expr.rast
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
SOURCE_FILE
FN
FN_KW "fn"
WHITESPACE " "
NAME
IDENT "foo"
PARAM_LIST
L_PAREN "("
R_PAREN ")"
WHITESPACE " "
BLOCK_EXPR
STMT_LIST
L_CURLY "{"
WHITESPACE "\n "
EXPR_STMT
ASM_EXPR
BUILTIN_KW "builtin"
POUND "#"
ASM_KW "asm"
L_PAREN "("
WHITESPACE "\n "
PATH_EXPR
PATH
PATH_SEGMENT
NAME_REF
IDENT "label"
WHITESPACE " "
NAME
IDENT "crashy"
WHITESPACE " "
EQ "="
WHITESPACE " "
BLOCK_EXPR
STMT_LIST
L_CURLY "{"
WHITESPACE " "
EXPR_STMT
RETURN_EXPR
RETURN_KW "return"
SEMICOLON ";"
WHITESPACE " "
R_CURLY "}"
WHITESPACE "\n "
R_PAREN ")"
SEMICOLON ";"
WHITESPACE "\n"
R_CURLY "}"
WHITESPACE "\n"
error 41: expected COMMA
error 50: expected asm operand
5 changes: 5 additions & 0 deletions crates/parser/test_data/parser/inline/err/bad_asm_expr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
fn foo() {
builtin#asm(
label crashy = { return; }
);
}

0 comments on commit cddaf74

Please sign in to comment.