From 7a8560ba382af4b3955b14757518f748e0d67709 Mon Sep 17 00:00:00 2001 From: DJMcNab <36049421+DJMcNab@users.noreply.github.com> Date: Wed, 19 Dec 2018 18:12:19 +0000 Subject: Fix expression parsing by bailing out upon a macro block being found TODO: Fix this when the block like macro is in expression position E.g. `test(test!{})` currently parses --- crates/ra_syntax/src/grammar/expressions.rs | 26 ++++++++++++-------------- crates/ra_syntax/tests/test.rs | 3 --- 2 files changed, 12 insertions(+), 17 deletions(-) (limited to 'crates') diff --git a/crates/ra_syntax/src/grammar/expressions.rs b/crates/ra_syntax/src/grammar/expressions.rs index 043f41b98..9d75bfb90 100644 --- a/crates/ra_syntax/src/grammar/expressions.rs +++ b/crates/ra_syntax/src/grammar/expressions.rs @@ -158,13 +158,14 @@ fn current_op(p: &Parser) -> (u8, Op) { // Parses expression with binding power of at least bp. fn expr_bp(p: &mut Parser, r: Restrictions, bp: u8) -> BlockLike { let mut lhs = match lhs(p, r) { - (Some(lhs), blocklike) => { + (Some(lhs), macro_blocklike) => { // test stmt_bin_expr_ambiguity // fn foo() { // let _ = {1} & 2; // {1} &2; // } - if r.prefer_stmt && (is_block(lhs.kind()) || blocklike == Some(BlockLike::Block)) { + if r.prefer_stmt && (is_block(lhs.kind()) || macro_blocklike == Some(BlockLike::Block)) + { return BlockLike::Block; } lhs @@ -251,11 +252,11 @@ fn lhs(p: &mut Parser, r: Restrictions) -> (Option, Option { let (lhs_marker, macro_block_like) = atom::atom_expr(p, r); + if macro_block_like == Some(BlockLike::Block) { + return (lhs_marker, macro_block_like); + } if let Some(lhs_marker) = lhs_marker { - return ( - Some(postfix_expr(p, r, lhs_marker, macro_block_like)), - macro_block_like, - ); + return (Some(postfix_expr(p, r, lhs_marker)), macro_block_like); } else { return (None, None); } @@ -265,14 +266,11 @@ fn lhs(p: &mut Parser, r: Restrictions) -> (Option, Option, -) -> CompletedMarker { - let mut allow_calls = - !r.prefer_stmt || !is_block(lhs.kind()) || macro_block_like != Some(BlockLike::Block); +fn postfix_expr(p: &mut Parser, r: Restrictions, mut lhs: CompletedMarker) -> CompletedMarker { + // Calls are disallowed if the type is a block and we prefer statements because the call cannot be disambiguated from a tuple + // E.g. `while true {break}();` is parsed as + // `while true {break}; ();` + let mut allow_calls = !r.prefer_stmt || !is_block(lhs.kind()); loop { lhs = match p.current() { // test stmt_postfix_expr_ambiguity diff --git a/crates/ra_syntax/tests/test.rs b/crates/ra_syntax/tests/test.rs index 7f385f86f..a07855768 100644 --- a/crates/ra_syntax/tests/test.rs +++ b/crates/ra_syntax/tests/test.rs @@ -43,7 +43,6 @@ fn parser_fuzz_tests() { fn self_hosting_parsing() { let empty_vec = vec![]; let dir = project_dir(); - let mut count = 0u32; for entry in walkdir::WalkDir::new(dir) .into_iter() .filter_entry(|entry| { @@ -64,7 +63,6 @@ fn self_hosting_parsing() { !entry.path().is_dir() && (entry.path().extension() == Some(std::ffi::OsStr::new("rs"))) }) { - count += 1; let text = read_text(entry.path()); let node = SourceFileNode::parse(&text); let errors = node.errors(); @@ -74,7 +72,6 @@ fn self_hosting_parsing() { entry ); } - panic!("{}", count) } /// Read file and normalize newlines. /// -- cgit v1.2.3