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 ++++++++++ .../parser/inline/ok/0159_try_macro_fallback.rast | 35 ++++++++++++++++++++++ .../parser/inline/ok/0159_try_macro_fallback.rs | 1 + 3 files changed, 52 insertions(+) create mode 100644 crates/ra_syntax/test_data/parser/inline/ok/0159_try_macro_fallback.rast create mode 100644 crates/ra_syntax/test_data/parser/inline/ok/0159_try_macro_fallback.rs (limited to 'crates') 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) diff --git a/crates/ra_syntax/test_data/parser/inline/ok/0159_try_macro_fallback.rast b/crates/ra_syntax/test_data/parser/inline/ok/0159_try_macro_fallback.rast new file mode 100644 index 000000000..beb6d8010 --- /dev/null +++ b/crates/ra_syntax/test_data/parser/inline/ok/0159_try_macro_fallback.rast @@ -0,0 +1,35 @@ +SOURCE_FILE@0..27 + FN_DEF@0..26 + 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..26 + BLOCK@9..26 + L_CURLY@9..10 "{" + WHITESPACE@10..11 " " + EXPR_STMT@11..24 + MACRO_CALL@11..23 + PATH@11..14 + PATH_SEGMENT@11..14 + NAME_REF@11..14 + IDENT@11..14 "try" + BANG@14..15 "!" + TOKEN_TREE@15..23 + L_PAREN@15..16 "(" + IDENT@16..18 "Ok" + TOKEN_TREE@18..22 + L_PAREN@18..19 "(" + TOKEN_TREE@19..21 + L_PAREN@19..20 "(" + R_PAREN@20..21 ")" + R_PAREN@21..22 ")" + R_PAREN@22..23 ")" + SEMICOLON@23..24 ";" + WHITESPACE@24..25 " " + R_CURLY@25..26 "}" + WHITESPACE@26..27 "\n" diff --git a/crates/ra_syntax/test_data/parser/inline/ok/0159_try_macro_fallback.rs b/crates/ra_syntax/test_data/parser/inline/ok/0159_try_macro_fallback.rs new file mode 100644 index 000000000..61a6b46a0 --- /dev/null +++ b/crates/ra_syntax/test_data/parser/inline/ok/0159_try_macro_fallback.rs @@ -0,0 +1 @@ +fn foo() { try!(Ok(())); } -- cgit v1.2.3