mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-13 22:06:56 +00:00
Parse try
as a keyword only in edition 2018 and up
This commit is contained in:
parent
713c47f25b
commit
d235d09bf9
8 changed files with 24 additions and 42 deletions
|
@ -844,9 +844,9 @@ fn f1() -> DynTrait<Vec<Error>> {foo()}
|
||||||
#[test]
|
#[test]
|
||||||
fn replace_macro_invocations() {
|
fn replace_macro_invocations() {
|
||||||
assert_ssr_transform(
|
assert_ssr_transform(
|
||||||
"try!($a) ==>> $a?",
|
"try_!($a) ==>> $a?",
|
||||||
"macro_rules! try {() => {}} fn f1() -> Result<(), E> {bar(try!(foo()));}",
|
"macro_rules! try_ {() => {}} fn f1() -> Result<(), E> {bar(try_!(foo()));}",
|
||||||
expect![["macro_rules! try {() => {}} fn f1() -> Result<(), E> {bar(foo()?);}"]],
|
expect![["macro_rules! try_ {() => {}} fn f1() -> Result<(), E> {bar(foo()?);}"]],
|
||||||
);
|
);
|
||||||
// FIXME: Figure out why this doesn't work anymore
|
// FIXME: Figure out why this doesn't work anymore
|
||||||
// assert_ssr_transform(
|
// assert_ssr_transform(
|
||||||
|
|
|
@ -101,6 +101,8 @@ pub(super) fn atom_expr(
|
||||||
}
|
}
|
||||||
T![loop] => loop_expr(p, None),
|
T![loop] => loop_expr(p, None),
|
||||||
T![while] => while_expr(p, None),
|
T![while] => while_expr(p, None),
|
||||||
|
// test try_macro_fallback 2015
|
||||||
|
// fn foo() { try!(Ok(())); }
|
||||||
T![try] => try_block_expr(p, None),
|
T![try] => try_block_expr(p, None),
|
||||||
T![match] => match_expr(p),
|
T![match] => match_expr(p),
|
||||||
T![return] => return_expr(p),
|
T![return] => return_expr(p),
|
||||||
|
@ -767,24 +769,6 @@ fn break_expr(p: &mut Parser<'_>, r: Restrictions) -> CompletedMarker {
|
||||||
fn try_block_expr(p: &mut Parser<'_>, m: Option<Marker>) -> CompletedMarker {
|
fn try_block_expr(p: &mut Parser<'_>, m: Option<Marker>) -> CompletedMarker {
|
||||||
assert!(p.at(T![try]));
|
assert!(p.at(T![try]));
|
||||||
let m = m.unwrap_or_else(|| p.start());
|
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 macro_call = p.start();
|
|
||||||
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);
|
|
||||||
macro_call.complete(p, MACRO_CALL);
|
|
||||||
return m.complete(p, MACRO_EXPR);
|
|
||||||
}
|
|
||||||
|
|
||||||
p.bump(T![try]);
|
p.bump(T![try]);
|
||||||
if p.at(T!['{']) {
|
if p.at(T!['{']) {
|
||||||
stmt_list(p);
|
stmt_list(p);
|
||||||
|
|
|
@ -230,13 +230,8 @@ fn opt_item_without_modifiers(p: &mut Parser<'_>, m: Marker) -> Result<(), Marke
|
||||||
IDENT if p.at_contextual_kw(T![union]) && p.nth(1) == IDENT => adt::union(p, m),
|
IDENT if p.at_contextual_kw(T![union]) && p.nth(1) == IDENT => adt::union(p, m),
|
||||||
|
|
||||||
T![macro] => macro_def(p, m),
|
T![macro] => macro_def(p, m),
|
||||||
// check if current token is "macro_rules" followed by "!" followed by an identifier or "try"
|
// check if current token is "macro_rules" followed by "!" followed by an identifier
|
||||||
// try is keyword since the 2018 edition and the parser is not edition aware (yet!)
|
IDENT if p.at_contextual_kw(T![macro_rules]) && p.nth_at(1, BANG) && p.nth_at(2, IDENT) => {
|
||||||
IDENT
|
|
||||||
if p.at_contextual_kw(T![macro_rules])
|
|
||||||
&& p.nth_at(1, BANG)
|
|
||||||
&& (p.nth_at(2, IDENT) || p.nth_at(2, T![try])) =>
|
|
||||||
{
|
|
||||||
macro_rules(p, m)
|
macro_rules(p, m)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -334,23 +329,14 @@ pub(crate) fn extern_item_list(p: &mut Parser<'_>) {
|
||||||
m.complete(p, EXTERN_ITEM_LIST);
|
m.complete(p, EXTERN_ITEM_LIST);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// test try_macro_rules 2015
|
||||||
|
// macro_rules! try { () => {} }
|
||||||
fn macro_rules(p: &mut Parser<'_>, m: Marker) {
|
fn macro_rules(p: &mut Parser<'_>, m: Marker) {
|
||||||
assert!(p.at_contextual_kw(T![macro_rules]));
|
assert!(p.at_contextual_kw(T![macro_rules]));
|
||||||
p.bump_remap(T![macro_rules]);
|
p.bump_remap(T![macro_rules]);
|
||||||
p.expect(T![!]);
|
p.expect(T![!]);
|
||||||
|
|
||||||
// Special-case `macro_rules! try`.
|
name(p);
|
||||||
// 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);
|
|
||||||
} else {
|
|
||||||
name(p);
|
|
||||||
}
|
|
||||||
|
|
||||||
match p.current() {
|
match p.current() {
|
||||||
// test macro_rules_non_brace
|
// test macro_rules_non_brace
|
||||||
|
|
|
@ -559,11 +559,17 @@ mod ok {
|
||||||
fn try_expr() { run_and_expect_no_errors("test_data/parser/inline/ok/try_expr.rs"); }
|
fn try_expr() { run_and_expect_no_errors("test_data/parser/inline/ok/try_expr.rs"); }
|
||||||
#[test]
|
#[test]
|
||||||
fn try_macro_fallback() {
|
fn try_macro_fallback() {
|
||||||
run_and_expect_no_errors("test_data/parser/inline/ok/try_macro_fallback.rs");
|
run_and_expect_no_errors_with_edition(
|
||||||
|
"test_data/parser/inline/ok/try_macro_fallback.rs",
|
||||||
|
crate::Edition::Edition2015,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn try_macro_rules() {
|
fn try_macro_rules() {
|
||||||
run_and_expect_no_errors("test_data/parser/inline/ok/try_macro_rules.rs");
|
run_and_expect_no_errors_with_edition(
|
||||||
|
"test_data/parser/inline/ok/try_macro_rules.rs",
|
||||||
|
crate::Edition::Edition2015,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn tuple_attrs() { run_and_expect_no_errors("test_data/parser/inline/ok/tuple_attrs.rs"); }
|
fn tuple_attrs() { run_and_expect_no_errors("test_data/parser/inline/ok/tuple_attrs.rs"); }
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
SOURCE_FILE
|
SOURCE_FILE
|
||||||
FN
|
FN
|
||||||
|
COMMENT "// 2015"
|
||||||
|
WHITESPACE "\n"
|
||||||
FN_KW "fn"
|
FN_KW "fn"
|
||||||
WHITESPACE " "
|
WHITESPACE " "
|
||||||
NAME
|
NAME
|
||||||
|
|
|
@ -1 +1,2 @@
|
||||||
|
// 2015
|
||||||
fn foo() { try!(Ok(())); }
|
fn foo() { try!(Ok(())); }
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
SOURCE_FILE
|
SOURCE_FILE
|
||||||
MACRO_RULES
|
MACRO_RULES
|
||||||
|
COMMENT "// 2015"
|
||||||
|
WHITESPACE "\n"
|
||||||
MACRO_RULES_KW "macro_rules"
|
MACRO_RULES_KW "macro_rules"
|
||||||
BANG "!"
|
BANG "!"
|
||||||
WHITESPACE " "
|
WHITESPACE " "
|
||||||
|
|
|
@ -1 +1,2 @@
|
||||||
|
// 2015
|
||||||
macro_rules! try { () => {} }
|
macro_rules! try { () => {} }
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue