Skip to content

Commit 2dc84ab

Browse files
committed
Parse try as a keyword only in edition 2018 and up
1 parent f4b2bf5 commit 2dc84ab

File tree

8 files changed

+24
-42
lines changed

8 files changed

+24
-42
lines changed

src/tools/rust-analyzer/crates/ide-ssr/src/tests.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -844,9 +844,9 @@ fn f1() -> DynTrait<Vec<Error>> {foo()}
844844
#[test]
845845
fn replace_macro_invocations() {
846846
assert_ssr_transform(
847-
"try!($a) ==>> $a?",
848-
"macro_rules! try {() => {}} fn f1() -> Result<(), E> {bar(try!(foo()));}",
849-
expect![["macro_rules! try {() => {}} fn f1() -> Result<(), E> {bar(foo()?);}"]],
847+
"try_!($a) ==>> $a?",
848+
"macro_rules! try_ {() => {}} fn f1() -> Result<(), E> {bar(try_!(foo()));}",
849+
expect![["macro_rules! try_ {() => {}} fn f1() -> Result<(), E> {bar(foo()?);}"]],
850850
);
851851
// FIXME: Figure out why this doesn't work anymore
852852
// assert_ssr_transform(

src/tools/rust-analyzer/crates/parser/src/grammar/expressions/atom.rs

+2-18
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ pub(super) fn atom_expr(
101101
}
102102
T![loop] => loop_expr(p, None),
103103
T![while] => while_expr(p, None),
104+
// test try_macro_fallback 2015
105+
// fn foo() { try!(Ok(())); }
104106
T![try] => try_block_expr(p, None),
105107
T![match] => match_expr(p),
106108
T![return] => return_expr(p),
@@ -767,24 +769,6 @@ fn break_expr(p: &mut Parser<'_>, r: Restrictions) -> CompletedMarker {
767769
fn try_block_expr(p: &mut Parser<'_>, m: Option<Marker>) -> CompletedMarker {
768770
assert!(p.at(T![try]));
769771
let m = m.unwrap_or_else(|| p.start());
770-
// Special-case `try!` as macro.
771-
// This is a hack until we do proper edition support
772-
if p.nth_at(1, T![!]) {
773-
// test try_macro_fallback
774-
// fn foo() { try!(Ok(())); }
775-
let macro_call = p.start();
776-
let path = p.start();
777-
let path_segment = p.start();
778-
let name_ref = p.start();
779-
p.bump_remap(IDENT);
780-
name_ref.complete(p, NAME_REF);
781-
path_segment.complete(p, PATH_SEGMENT);
782-
path.complete(p, PATH);
783-
let _block_like = items::macro_call_after_excl(p);
784-
macro_call.complete(p, MACRO_CALL);
785-
return m.complete(p, MACRO_EXPR);
786-
}
787-
788772
p.bump(T![try]);
789773
if p.at(T!['{']) {
790774
stmt_list(p);

src/tools/rust-analyzer/crates/parser/src/grammar/items.rs

+5-19
Original file line numberDiff line numberDiff line change
@@ -230,13 +230,8 @@ fn opt_item_without_modifiers(p: &mut Parser<'_>, m: Marker) -> Result<(), Marke
230230
IDENT if p.at_contextual_kw(T![union]) && p.nth(1) == IDENT => adt::union(p, m),
231231

232232
T![macro] => macro_def(p, m),
233-
// check if current token is "macro_rules" followed by "!" followed by an identifier or "try"
234-
// try is keyword since the 2018 edition and the parser is not edition aware (yet!)
235-
IDENT
236-
if p.at_contextual_kw(T![macro_rules])
237-
&& p.nth_at(1, BANG)
238-
&& (p.nth_at(2, IDENT) || p.nth_at(2, T![try])) =>
239-
{
233+
// check if current token is "macro_rules" followed by "!" followed by an identifier
234+
IDENT if p.at_contextual_kw(T![macro_rules]) && p.nth_at(1, BANG) && p.nth_at(2, IDENT) => {
240235
macro_rules(p, m)
241236
}
242237

@@ -334,23 +329,14 @@ pub(crate) fn extern_item_list(p: &mut Parser<'_>) {
334329
m.complete(p, EXTERN_ITEM_LIST);
335330
}
336331

332+
// test try_macro_rules 2015
333+
// macro_rules! try { () => {} }
337334
fn macro_rules(p: &mut Parser<'_>, m: Marker) {
338335
assert!(p.at_contextual_kw(T![macro_rules]));
339336
p.bump_remap(T![macro_rules]);
340337
p.expect(T![!]);
341338

342-
// Special-case `macro_rules! try`.
343-
// This is a hack until we do proper edition support
344-
345-
// test try_macro_rules
346-
// macro_rules! try { () => {} }
347-
if p.at(T![try]) {
348-
let m = p.start();
349-
p.bump_remap(IDENT);
350-
m.complete(p, NAME);
351-
} else {
352-
name(p);
353-
}
339+
name(p);
354340

355341
match p.current() {
356342
// test macro_rules_non_brace

src/tools/rust-analyzer/crates/parser/test_data/generated/runner.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -559,11 +559,17 @@ mod ok {
559559
fn try_expr() { run_and_expect_no_errors("test_data/parser/inline/ok/try_expr.rs"); }
560560
#[test]
561561
fn try_macro_fallback() {
562-
run_and_expect_no_errors("test_data/parser/inline/ok/try_macro_fallback.rs");
562+
run_and_expect_no_errors_with_edition(
563+
"test_data/parser/inline/ok/try_macro_fallback.rs",
564+
crate::Edition::Edition2015,
565+
);
563566
}
564567
#[test]
565568
fn try_macro_rules() {
566-
run_and_expect_no_errors("test_data/parser/inline/ok/try_macro_rules.rs");
569+
run_and_expect_no_errors_with_edition(
570+
"test_data/parser/inline/ok/try_macro_rules.rs",
571+
crate::Edition::Edition2015,
572+
);
567573
}
568574
#[test]
569575
fn tuple_attrs() { run_and_expect_no_errors("test_data/parser/inline/ok/tuple_attrs.rs"); }

src/tools/rust-analyzer/crates/parser/test_data/parser/inline/ok/try_macro_fallback.rast

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
SOURCE_FILE
22
FN
3+
COMMENT "// 2015"
4+
WHITESPACE "\n"
35
FN_KW "fn"
46
WHITESPACE " "
57
NAME
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
// 2015
12
fn foo() { try!(Ok(())); }

src/tools/rust-analyzer/crates/parser/test_data/parser/inline/ok/try_macro_rules.rast

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
SOURCE_FILE
22
MACRO_RULES
3+
COMMENT "// 2015"
4+
WHITESPACE "\n"
35
MACRO_RULES_KW "macro_rules"
46
BANG "!"
57
WHITESPACE " "
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
// 2015
12
macro_rules! try { () => {} }

0 commit comments

Comments
 (0)