From c51c8bfb840d35709ee1cec190620c98b4fc3590 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 30 Apr 2020 14:17:14 +0200 Subject: Special-case try macro to better support 2015 edition --- crates/ra_parser/src/grammar/expressions/atom.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'crates/ra_parser/src/grammar') diff --git a/crates/ra_parser/src/grammar/expressions/atom.rs b/crates/ra_parser/src/grammar/expressions/atom.rs index 0d277a586..166dfc472 100644 --- a/crates/ra_parser/src/grammar/expressions/atom.rs +++ b/crates/ra_parser/src/grammar/expressions/atom.rs @@ -535,6 +535,22 @@ fn break_expr(p: &mut Parser, r: Restrictions) -> CompletedMarker { fn try_block_expr(p: &mut Parser, m: Option) -> CompletedMarker { assert!(p.at(T![try])); let m = m.unwrap_or_else(|| p.start()); + // Special-case `try!` as macro. + // This is a hack until we do proper edition support + if p.nth_at(1, T![!]) { + // test try_macro_fallback + // fn foo() { try!(Ok(())); } + let path = p.start(); + let path_segment = p.start(); + let name_ref = p.start(); + p.bump_remap(IDENT); + name_ref.complete(p, NAME_REF); + path_segment.complete(p, PATH_SEGMENT); + path.complete(p, PATH); + let _block_like = items::macro_call_after_excl(p); + return m.complete(p, MACRO_CALL); + } + p.bump(T![try]); block(p); m.complete(p, TRY_EXPR) -- cgit v1.2.3 From 45c4f620b1c5b8e462875b6e372db0e849bd6170 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Thu, 30 Apr 2020 22:07:46 +0800 Subject: Special-case try macro_rules --- crates/ra_parser/src/grammar/items.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'crates/ra_parser/src/grammar') diff --git a/crates/ra_parser/src/grammar/items.rs b/crates/ra_parser/src/grammar/items.rs index 433ed6812..1503a8730 100644 --- a/crates/ra_parser/src/grammar/items.rs +++ b/crates/ra_parser/src/grammar/items.rs @@ -415,6 +415,17 @@ pub(super) fn macro_call_after_excl(p: &mut Parser) -> BlockLike { if p.at(IDENT) { name(p); } + // Special-case `macro_rules! try`. + // This is a hack until we do proper edition support + + // test try_macro_rules + // macro_rules! try { () => {} } + if p.at(T![try]) { + let m = p.start(); + p.bump_remap(IDENT); + m.complete(p, NAME); + } + match p.current() { T!['{'] => { token_tree(p); -- cgit v1.2.3 From 513a3615f6d462852c0135dc4ac30a2086e25c5a Mon Sep 17 00:00:00 2001 From: John Renner Date: Thu, 30 Apr 2020 10:41:24 -0700 Subject: Report invalid, nested, multi-segment crate-paths Specifically, things like: use foo::{crate::bar}; Are now being caught, when before we only caught: use foo::{crate}; --- crates/ra_parser/src/grammar/items/use_item.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_parser/src/grammar') diff --git a/crates/ra_parser/src/grammar/items/use_item.rs b/crates/ra_parser/src/grammar/items/use_item.rs index e3b991c8c..3a0c7a31a 100644 --- a/crates/ra_parser/src/grammar/items/use_item.rs +++ b/crates/ra_parser/src/grammar/items/use_item.rs @@ -47,7 +47,7 @@ fn use_tree(p: &mut Parser, top_level: bool) { // use {crate::path::from::root, or::path::from::crate_name}; // Rust 2018 (with a crate named `or`) // use {path::from::root}; // Rust 2015 // use ::{some::arbritrary::path}; // Rust 2015 - // use ::{{{crate::export}}}; // Nonsensical but perfectly legal nestnig + // use ::{{{root::export}}}; // Nonsensical but perfectly legal nesting T!['{'] => { use_tree_list(p); } -- cgit v1.2.3 From 292ba6a1f81fee4170c3081f74499fe8c3ddedd4 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 30 Apr 2020 22:41:14 +0200 Subject: Remove dead code, which elaborately pretends to be alive --- crates/ra_parser/src/grammar/expressions/atom.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'crates/ra_parser/src/grammar') diff --git a/crates/ra_parser/src/grammar/expressions/atom.rs b/crates/ra_parser/src/grammar/expressions/atom.rs index 166dfc472..76aa601cb 100644 --- a/crates/ra_parser/src/grammar/expressions/atom.rs +++ b/crates/ra_parser/src/grammar/expressions/atom.rs @@ -84,7 +84,7 @@ pub(super) fn atom_expr(p: &mut Parser, r: Restrictions) -> Option<(CompletedMar T![box] => box_expr(p, None), T![for] => for_expr(p, None), T![while] => while_expr(p, None), - T![try] => try_block_expr(p, None), + T![try] => try_expr(p, None), LIFETIME if la == T![:] => { let m = p.start(); label(p); @@ -134,7 +134,7 @@ pub(super) fn atom_expr(p: &mut Parser, r: Restrictions) -> Option<(CompletedMar } }; let blocklike = match done.kind() { - IF_EXPR | WHILE_EXPR | FOR_EXPR | LOOP_EXPR | MATCH_EXPR | BLOCK_EXPR | TRY_BLOCK_EXPR => { + IF_EXPR | WHILE_EXPR | FOR_EXPR | LOOP_EXPR | MATCH_EXPR | BLOCK_EXPR | TRY_EXPR => { BlockLike::Block } _ => BlockLike::NotBlock, @@ -532,7 +532,7 @@ fn break_expr(p: &mut Parser, r: Restrictions) -> CompletedMarker { // fn foo() { // let _ = try {}; // } -fn try_block_expr(p: &mut Parser, m: Option) -> CompletedMarker { +fn try_expr(p: &mut Parser, m: Option) -> CompletedMarker { assert!(p.at(T![try])); let m = m.unwrap_or_else(|| p.start()); // Special-case `try!` as macro. -- cgit v1.2.3 From fd030f9450ed6910677e30f8fa65b06e71fcffa2 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 2 May 2020 01:12:37 +0200 Subject: Revert "Merge #4233" This reverts commit a5f2b16366f027ad60c58266a66eb7fbdcbda9f9, reversing changes made to c96b2180c1c4206a0a98c280b4d30897eb116336. --- crates/ra_parser/src/grammar/expressions/atom.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'crates/ra_parser/src/grammar') diff --git a/crates/ra_parser/src/grammar/expressions/atom.rs b/crates/ra_parser/src/grammar/expressions/atom.rs index 76aa601cb..166dfc472 100644 --- a/crates/ra_parser/src/grammar/expressions/atom.rs +++ b/crates/ra_parser/src/grammar/expressions/atom.rs @@ -84,7 +84,7 @@ pub(super) fn atom_expr(p: &mut Parser, r: Restrictions) -> Option<(CompletedMar T![box] => box_expr(p, None), T![for] => for_expr(p, None), T![while] => while_expr(p, None), - T![try] => try_expr(p, None), + T![try] => try_block_expr(p, None), LIFETIME if la == T![:] => { let m = p.start(); label(p); @@ -134,7 +134,7 @@ pub(super) fn atom_expr(p: &mut Parser, r: Restrictions) -> Option<(CompletedMar } }; let blocklike = match done.kind() { - IF_EXPR | WHILE_EXPR | FOR_EXPR | LOOP_EXPR | MATCH_EXPR | BLOCK_EXPR | TRY_EXPR => { + IF_EXPR | WHILE_EXPR | FOR_EXPR | LOOP_EXPR | MATCH_EXPR | BLOCK_EXPR | TRY_BLOCK_EXPR => { BlockLike::Block } _ => BlockLike::NotBlock, @@ -532,7 +532,7 @@ fn break_expr(p: &mut Parser, r: Restrictions) -> CompletedMarker { // fn foo() { // let _ = try {}; // } -fn try_expr(p: &mut Parser, m: Option) -> CompletedMarker { +fn try_block_expr(p: &mut Parser, m: Option) -> CompletedMarker { assert!(p.at(T![try])); let m = m.unwrap_or_else(|| p.start()); // Special-case `try!` as macro. -- cgit v1.2.3 From 4f2134cc33f07c09fe166cec42971828843bc0ef Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 2 May 2020 01:18:19 +0200 Subject: Introduce EffectExpr --- crates/ra_parser/src/grammar/expressions.rs | 13 ++--------- crates/ra_parser/src/grammar/expressions/atom.rs | 29 +++++++++++++++--------- 2 files changed, 20 insertions(+), 22 deletions(-) (limited to 'crates/ra_parser/src/grammar') diff --git a/crates/ra_parser/src/grammar/expressions.rs b/crates/ra_parser/src/grammar/expressions.rs index cb30b25a8..a23dbcacf 100644 --- a/crates/ra_parser/src/grammar/expressions.rs +++ b/crates/ra_parser/src/grammar/expressions.rs @@ -59,16 +59,7 @@ pub(crate) fn block(p: &mut Parser) { p.error("expected a block"); return; } - atom::block_expr(p, None); -} - -pub(crate) fn naked_block(p: &mut Parser) { - assert!(p.at(T!['{'])); - let m = p.start(); - p.bump(T!['{']); - expr_block_contents(p); - p.expect(T!['}']); - m.complete(p, BLOCK); + atom::block_expr(p); } fn is_expr_stmt_attr_allowed(kind: SyntaxKind) -> bool { @@ -197,7 +188,7 @@ pub(super) fn stmt(p: &mut Parser, with_semi: StmtWithSemi) { } } -pub(crate) fn expr_block_contents(p: &mut Parser) { +pub(super) fn expr_block_contents(p: &mut Parser) { // This is checked by a validator attributes::inner_attributes(p); diff --git a/crates/ra_parser/src/grammar/expressions/atom.rs b/crates/ra_parser/src/grammar/expressions/atom.rs index 166dfc472..c76b7330c 100644 --- a/crates/ra_parser/src/grammar/expressions/atom.rs +++ b/crates/ra_parser/src/grammar/expressions/atom.rs @@ -92,7 +92,10 @@ pub(super) fn atom_expr(p: &mut Parser, r: Restrictions) -> Option<(CompletedMar T![loop] => loop_expr(p, Some(m)), T![for] => for_expr(p, Some(m)), T![while] => while_expr(p, Some(m)), - T!['{'] => block_expr(p, Some(m)), + T!['{'] => { + block_expr(p); + m.complete(p, EFFECT_EXPR) + } _ => { // test_err misplaced_label_err // fn main() { @@ -108,13 +111,15 @@ pub(super) fn atom_expr(p: &mut Parser, r: Restrictions) -> Option<(CompletedMar let m = p.start(); p.bump(T![async]); p.eat(T![move]); - block_expr(p, Some(m)) + block_expr(p); + m.complete(p, EFFECT_EXPR) } T![match] => match_expr(p), T![unsafe] if la == T!['{'] => { let m = p.start(); p.bump(T![unsafe]); - block_expr(p, Some(m)) + block_expr(p); + m.complete(p, EFFECT_EXPR) } T!['{'] => { // test for_range_from @@ -123,7 +128,7 @@ pub(super) fn atom_expr(p: &mut Parser, r: Restrictions) -> Option<(CompletedMar // break; // } // } - block_expr(p, None) + block_expr(p) } T![return] => return_expr(p), T![continue] => continue_expr(p), @@ -134,7 +139,7 @@ pub(super) fn atom_expr(p: &mut Parser, r: Restrictions) -> Option<(CompletedMar } }; let blocklike = match done.kind() { - IF_EXPR | WHILE_EXPR | FOR_EXPR | LOOP_EXPR | MATCH_EXPR | BLOCK_EXPR | TRY_BLOCK_EXPR => { + IF_EXPR | WHILE_EXPR | FOR_EXPR | LOOP_EXPR | MATCH_EXPR | BLOCK_EXPR | EFFECT_EXPR => { BlockLike::Block } _ => BlockLike::NotBlock, @@ -234,7 +239,7 @@ fn lambda_expr(p: &mut Parser) -> CompletedMarker { if p.at(T!['{']) { // test lambda_ret_block // fn main() { || -> i32 { 92 }(); } - block_expr(p, None); + block_expr(p); } else { p.error("expected `{`"); } @@ -464,10 +469,12 @@ fn match_guard(p: &mut Parser) -> CompletedMarker { // unsafe {}; // 'label: {}; // } -pub(super) fn block_expr(p: &mut Parser, m: Option) -> CompletedMarker { +pub(super) fn block_expr(p: &mut Parser) -> CompletedMarker { assert!(p.at(T!['{'])); - let m = m.unwrap_or_else(|| p.start()); - naked_block(p); + let m = p.start(); + p.bump(T!['{']); + expr_block_contents(p); + p.expect(T!['}']); m.complete(p, BLOCK_EXPR) } @@ -552,8 +559,8 @@ fn try_block_expr(p: &mut Parser, m: Option) -> CompletedMarker { } p.bump(T![try]); - block(p); - m.complete(p, TRY_EXPR) + block_expr(p); + m.complete(p, EFFECT_EXPR) } // test box_expr -- cgit v1.2.3 From 623faefcda3536185232be1676709b0233bb10ba Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 2 May 2020 01:57:21 +0200 Subject: Cleanup inline tests --- crates/ra_parser/src/grammar/expressions/atom.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'crates/ra_parser/src/grammar') diff --git a/crates/ra_parser/src/grammar/expressions/atom.rs b/crates/ra_parser/src/grammar/expressions/atom.rs index c76b7330c..efb424dae 100644 --- a/crates/ra_parser/src/grammar/expressions/atom.rs +++ b/crates/ra_parser/src/grammar/expressions/atom.rs @@ -92,6 +92,8 @@ pub(super) fn atom_expr(p: &mut Parser, r: Restrictions) -> Option<(CompletedMar T![loop] => loop_expr(p, Some(m)), T![for] => for_expr(p, Some(m)), T![while] => while_expr(p, Some(m)), + // test labeled_block + // fn f() { 'label: {}; } T!['{'] => { block_expr(p); m.complete(p, EFFECT_EXPR) @@ -115,6 +117,8 @@ pub(super) fn atom_expr(p: &mut Parser, r: Restrictions) -> Option<(CompletedMar m.complete(p, EFFECT_EXPR) } T![match] => match_expr(p), + // test unsafe_block + // fn f() { unsafe { } } T![unsafe] if la == T!['{'] => { let m = p.start(); p.bump(T![unsafe]); @@ -466,8 +470,6 @@ fn match_guard(p: &mut Parser) -> CompletedMarker { // test block_expr // fn foo() { // {}; -// unsafe {}; -// 'label: {}; // } pub(super) fn block_expr(p: &mut Parser) -> CompletedMarker { assert!(p.at(T!['{'])); -- cgit v1.2.3 From 359d3be308cab2415218200f5799c5031213c250 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 2 May 2020 14:34:39 +0200 Subject: Fix parsing of blocks without `{` --- crates/ra_parser/src/grammar/expressions.rs | 15 +-------- crates/ra_parser/src/grammar/expressions/atom.rs | 41 +++++++++++++----------- crates/ra_parser/src/grammar/items.rs | 2 +- crates/ra_parser/src/grammar/type_args.rs | 2 +- 4 files changed, 26 insertions(+), 34 deletions(-) (limited to 'crates/ra_parser/src/grammar') diff --git a/crates/ra_parser/src/grammar/expressions.rs b/crates/ra_parser/src/grammar/expressions.rs index a23dbcacf..34f039768 100644 --- a/crates/ra_parser/src/grammar/expressions.rs +++ b/crates/ra_parser/src/grammar/expressions.rs @@ -2,7 +2,7 @@ mod atom; -pub(crate) use self::atom::match_arm_list; +pub(crate) use self::atom::{block_expr, match_arm_list}; pub(super) use self::atom::{literal, LITERAL_FIRST}; use super::*; @@ -49,19 +49,6 @@ fn expr_no_struct(p: &mut Parser) { expr_bp(p, r, 1); } -// test block -// fn a() {} -// fn b() { let _ = 1; } -// fn c() { 1; 2; } -// fn d() { 1; 2 } -pub(crate) fn block(p: &mut Parser) { - if !p.at(T!['{']) { - p.error("expected a block"); - return; - } - atom::block_expr(p); -} - fn is_expr_stmt_attr_allowed(kind: SyntaxKind) -> bool { match kind { BIN_EXPR | RANGE_EXPR | IF_EXPR => false, diff --git a/crates/ra_parser/src/grammar/expressions/atom.rs b/crates/ra_parser/src/grammar/expressions/atom.rs index efb424dae..706a2f796 100644 --- a/crates/ra_parser/src/grammar/expressions/atom.rs +++ b/crates/ra_parser/src/grammar/expressions/atom.rs @@ -132,7 +132,7 @@ pub(super) fn atom_expr(p: &mut Parser, r: Restrictions) -> Option<(CompletedMar // break; // } // } - block_expr(p) + block_expr_unchecked(p) } T![return] => return_expr(p), T![continue] => continue_expr(p), @@ -240,13 +240,9 @@ fn lambda_expr(p: &mut Parser) -> CompletedMarker { p.eat(T![move]); params::param_list_closure(p); if opt_fn_ret_type(p) { - if p.at(T!['{']) { - // test lambda_ret_block - // fn main() { || -> i32 { 92 }(); } - block_expr(p); - } else { - p.error("expected `{`"); - } + // test lambda_ret_block + // fn main() { || -> i32 { 92 }(); } + block_expr(p); } else { if p.at_ts(EXPR_FIRST) { expr(p); @@ -270,13 +266,13 @@ fn if_expr(p: &mut Parser) -> CompletedMarker { let m = p.start(); p.bump(T![if]); cond(p); - block(p); + block_expr(p); if p.at(T![else]) { p.bump(T![else]); if p.at(T![if]) { if_expr(p); } else { - block(p); + block_expr(p); } } m.complete(p, IF_EXPR) @@ -304,7 +300,7 @@ fn loop_expr(p: &mut Parser, m: Option) -> CompletedMarker { assert!(p.at(T![loop])); let m = m.unwrap_or_else(|| p.start()); p.bump(T![loop]); - block(p); + block_expr(p); m.complete(p, LOOP_EXPR) } @@ -319,7 +315,7 @@ fn while_expr(p: &mut Parser, m: Option) -> CompletedMarker { let m = m.unwrap_or_else(|| p.start()); p.bump(T![while]); cond(p); - block(p); + block_expr(p); m.complete(p, WHILE_EXPR) } @@ -334,7 +330,7 @@ fn for_expr(p: &mut Parser, m: Option) -> CompletedMarker { patterns::pattern(p); p.expect(T![in]); expr_no_struct(p); - block(p); + block_expr(p); m.complete(p, FOR_EXPR) } @@ -467,11 +463,20 @@ fn match_guard(p: &mut Parser) -> CompletedMarker { m.complete(p, MATCH_GUARD) } -// test block_expr -// fn foo() { -// {}; -// } -pub(super) fn block_expr(p: &mut Parser) -> CompletedMarker { +// test block +// fn a() {} +// fn b() { let _ = 1; } +// fn c() { 1; 2; } +// fn d() { 1; 2 } +pub(crate) fn block_expr(p: &mut Parser) { + if !p.at(T!['{']) { + p.error("expected a block"); + return; + } + block_expr_unchecked(p); +} + +fn block_expr_unchecked(p: &mut Parser) -> CompletedMarker { assert!(p.at(T!['{'])); let m = p.start(); p.bump(T!['{']); diff --git a/crates/ra_parser/src/grammar/items.rs b/crates/ra_parser/src/grammar/items.rs index 1503a8730..67a924de5 100644 --- a/crates/ra_parser/src/grammar/items.rs +++ b/crates/ra_parser/src/grammar/items.rs @@ -329,7 +329,7 @@ fn fn_def(p: &mut Parser) { if p.at(T![;]) { p.bump(T![;]); } else { - expressions::block(p) + expressions::block_expr(p) } } diff --git a/crates/ra_parser/src/grammar/type_args.rs b/crates/ra_parser/src/grammar/type_args.rs index 33d9973e9..2d61f9d80 100644 --- a/crates/ra_parser/src/grammar/type_args.rs +++ b/crates/ra_parser/src/grammar/type_args.rs @@ -48,7 +48,7 @@ fn type_arg(p: &mut Parser) { m.complete(p, ASSOC_TYPE_ARG); } T!['{'] => { - expressions::block(p); + expressions::block_expr(p); m.complete(p, CONST_ARG); } k if k.is_literal() => { -- cgit v1.2.3