From 4a745cc8cfab4e9d08a2e513228ce13e33d92a9e Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 11 Mar 2020 12:40:38 +0100 Subject: [PATCH] Fix parsing of stement-ish binary expressions closes #3512 --- crates/ra_parser/src/grammar/expressions.rs | 10 ++++- .../ok/0158_binop_resets_statementness.rs | 3 ++ .../ok/0158_binop_resets_statementness.txt | 38 +++++++++++++++++++ 3 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 crates/ra_syntax/test_data/parser/inline/ok/0158_binop_resets_statementness.rs create mode 100644 crates/ra_syntax/test_data/parser/inline/ok/0158_binop_resets_statementness.txt diff --git a/crates/ra_parser/src/grammar/expressions.rs b/crates/ra_parser/src/grammar/expressions.rs index 4163a2cf51..0c170ac5ec 100644 --- a/crates/ra_parser/src/grammar/expressions.rs +++ b/crates/ra_parser/src/grammar/expressions.rs @@ -278,7 +278,7 @@ fn current_op(p: &Parser) -> (u8, SyntaxKind) { } // Parses expression with binding power of at least bp. -fn expr_bp(p: &mut Parser, r: Restrictions, bp: u8) -> (Option, BlockLike) { +fn expr_bp(p: &mut Parser, mut r: Restrictions, bp: u8) -> (Option, BlockLike) { let mut lhs = match lhs(p, r) { Some((lhs, blocklike)) => { // test stmt_bin_expr_ambiguity @@ -311,6 +311,12 @@ fn expr_bp(p: &mut Parser, r: Restrictions, bp: u8) -> (Option, let m = lhs.precede(p); p.bump(op); + // test binop_resets_statementness + // fn foo() { + // v = {1}&2; + // } + r = Restrictions { prefer_stmt: false, ..r }; + if is_range { // test postfix_range // fn foo() { @@ -327,7 +333,7 @@ fn expr_bp(p: &mut Parser, r: Restrictions, bp: u8) -> (Option, } } - expr_bp(p, r, op_bp + 1); + expr_bp(p, Restrictions { prefer_stmt: false, ..r }, op_bp + 1); lhs = m.complete(p, if is_range { RANGE_EXPR } else { BIN_EXPR }); } (Some(lhs), BlockLike::NotBlock) diff --git a/crates/ra_syntax/test_data/parser/inline/ok/0158_binop_resets_statementness.rs b/crates/ra_syntax/test_data/parser/inline/ok/0158_binop_resets_statementness.rs new file mode 100644 index 0000000000..05acc30f12 --- /dev/null +++ b/crates/ra_syntax/test_data/parser/inline/ok/0158_binop_resets_statementness.rs @@ -0,0 +1,3 @@ +fn foo() { + v = {1}&2; +} diff --git a/crates/ra_syntax/test_data/parser/inline/ok/0158_binop_resets_statementness.txt b/crates/ra_syntax/test_data/parser/inline/ok/0158_binop_resets_statementness.txt new file mode 100644 index 0000000000..d568a1d457 --- /dev/null +++ b/crates/ra_syntax/test_data/parser/inline/ok/0158_binop_resets_statementness.txt @@ -0,0 +1,38 @@ +SOURCE_FILE@[0; 28) + FN_DEF@[0; 27) + FN_KW@[0; 2) "fn" + WHITESPACE@[2; 3) " " + NAME@[3; 6) + IDENT@[3; 6) "foo" + PARAM_LIST@[6; 8) + L_PAREN@[6; 7) "(" + R_PAREN@[7; 8) ")" + WHITESPACE@[8; 9) " " + BLOCK_EXPR@[9; 27) + BLOCK@[9; 27) + L_CURLY@[9; 10) "{" + WHITESPACE@[10; 15) "\n " + EXPR_STMT@[15; 25) + BIN_EXPR@[15; 24) + PATH_EXPR@[15; 16) + PATH@[15; 16) + PATH_SEGMENT@[15; 16) + NAME_REF@[15; 16) + IDENT@[15; 16) "v" + WHITESPACE@[16; 17) " " + EQ@[17; 18) "=" + WHITESPACE@[18; 19) " " + BIN_EXPR@[19; 24) + BLOCK_EXPR@[19; 22) + BLOCK@[19; 22) + L_CURLY@[19; 20) "{" + LITERAL@[20; 21) + INT_NUMBER@[20; 21) "1" + R_CURLY@[21; 22) "}" + AMP@[22; 23) "&" + LITERAL@[23; 24) + INT_NUMBER@[23; 24) "2" + SEMI@[24; 25) ";" + WHITESPACE@[25; 26) "\n" + R_CURLY@[26; 27) "}" + WHITESPACE@[27; 28) "\n"