From b21244e080f0abc489be0f75c5047e6671b588d7 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 9 Oct 2021 16:19:19 +0300 Subject: [PATCH 1/6] internal: move test --- crates/hir_def/src/macro_expansion_tests.rs | 4 +-- .../mbe/tt_conversion.rs | 18 +++++++++++++ crates/mbe/src/tests/expand.rs | 27 ------------------- 3 files changed, 20 insertions(+), 29 deletions(-) diff --git a/crates/hir_def/src/macro_expansion_tests.rs b/crates/hir_def/src/macro_expansion_tests.rs index 76bd0299fb4..83765bfb513 100644 --- a/crates/hir_def/src/macro_expansion_tests.rs +++ b/crates/hir_def/src/macro_expansion_tests.rs @@ -20,7 +20,7 @@ use stdx::format_to; use syntax::{ ast::{self, edit::IndentLevel}, AstNode, - SyntaxKind::{self, IDENT}, + SyntaxKind::{self, IDENT, LIFETIME_IDENT}, SyntaxNode, T, }; @@ -103,7 +103,7 @@ fn pretty_print_macro_expansion(expn: SyntaxNode) -> String { _ if prev_kind.is_trivia() || curr_kind.is_trivia() => "", (T![=], _) | (_, T![=]) => " ", (T![;], _) => "\n", - (IDENT, IDENT) => " ", + (IDENT | LIFETIME_IDENT, IDENT | LIFETIME_IDENT) => " ", (IDENT, _) if curr_kind.is_keyword() => " ", (_, IDENT) if prev_kind.is_keyword() => " ", _ => "", diff --git a/crates/hir_def/src/macro_expansion_tests/mbe/tt_conversion.rs b/crates/hir_def/src/macro_expansion_tests/mbe/tt_conversion.rs index a24fe9bee2c..3450cda3fda 100644 --- a/crates/hir_def/src/macro_expansion_tests/mbe/tt_conversion.rs +++ b/crates/hir_def/src/macro_expansion_tests/mbe/tt_conversion.rs @@ -63,6 +63,24 @@ fn f() { ); } +#[test] +fn roundtrip_lifetime() { + check( + r#" +macro_rules! m { + ($($t:tt)*) => { $($t)*} +} +m!(static bar: &'static str = "hello";); +"#, + expect![[r#" +macro_rules! m { + ($($t:tt)*) => { $($t)*} +} +static bar: & 'static str = "hello"; +"#]], + ); +} + #[test] fn broken_parenthesis_sequence() { check( diff --git a/crates/mbe/src/tests/expand.rs b/crates/mbe/src/tests/expand.rs index df374aea981..360d24db570 100644 --- a/crates/mbe/src/tests/expand.rs +++ b/crates/mbe/src/tests/expand.rs @@ -166,33 +166,6 @@ SUBTREE $ ); } -#[test] -fn test_lifetime_split() { - parse_macro( - r#" -macro_rules! foo { - ($($t:tt)*) => { $($t)*} -} -"#, - ) - .assert_expand( - r#"foo!(static bar: &'static str = "hello";);"#, - r#" -SUBTREE $ - IDENT static 17 - IDENT bar 18 - PUNCH : [alone] 19 - PUNCH & [alone] 20 - PUNCH ' [joint] 21 - IDENT static 22 - IDENT str 23 - PUNCH = [alone] 24 - LITERAL "hello" 25 - PUNCH ; [joint] 26 -"#, - ); -} - #[test] fn test_expr_order() { let expanded = parse_macro( From 574df660e480360764d40d5174c510302500162c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 9 Oct 2021 16:22:42 +0300 Subject: [PATCH 2/6] move test --- crates/hir_def/src/macro_expansion_tests.rs | 1 + .../hir_def/src/macro_expansion_tests/mbe.rs | 26 +++++++++++++++++++ crates/mbe/src/tests/expand.rs | 22 ---------------- 3 files changed, 27 insertions(+), 22 deletions(-) diff --git a/crates/hir_def/src/macro_expansion_tests.rs b/crates/hir_def/src/macro_expansion_tests.rs index 83765bfb513..a899b2c1f38 100644 --- a/crates/hir_def/src/macro_expansion_tests.rs +++ b/crates/hir_def/src/macro_expansion_tests.rs @@ -102,6 +102,7 @@ fn pretty_print_macro_expansion(expn: SyntaxNode) -> String { let space = match (prev_kind, curr_kind) { _ if prev_kind.is_trivia() || curr_kind.is_trivia() => "", (T![=], _) | (_, T![=]) => " ", + (_, T!['{']) => " ", (T![;], _) => "\n", (IDENT | LIFETIME_IDENT, IDENT | LIFETIME_IDENT) => " ", (IDENT, _) if curr_kind.is_keyword() => " ", diff --git a/crates/hir_def/src/macro_expansion_tests/mbe.rs b/crates/hir_def/src/macro_expansion_tests/mbe.rs index cc6551f8aa6..1e3d554b83a 100644 --- a/crates/hir_def/src/macro_expansion_tests/mbe.rs +++ b/crates/hir_def/src/macro_expansion_tests/mbe.rs @@ -46,3 +46,29 @@ macro_rules! m { "#]], ); } + +#[test] +fn tries_all_branches_matching_token_literally() { + check( + r#" +macro_rules! m { + ($ i:ident) => ( mod $ i {} ); + (= $ i:ident) => ( fn $ i() {} ); + (+ $ i:ident) => ( struct $ i; ) +} +m! { foo } +m! { = bar } +m! { + Baz } +"#, + expect![[r#" +macro_rules! m { + ($ i:ident) => ( mod $ i {} ); + (= $ i:ident) => ( fn $ i() {} ); + (+ $ i:ident) => ( struct $ i; ) +} +mod foo {} +fn bar() {} +struct Baz; +"#]], + ) +} diff --git a/crates/mbe/src/tests/expand.rs b/crates/mbe/src/tests/expand.rs index 360d24db570..48143f0a60b 100644 --- a/crates/mbe/src/tests/expand.rs +++ b/crates/mbe/src/tests/expand.rs @@ -209,28 +209,6 @@ fn test_expr_order() { ); } -#[test] -fn test_fail_match_pattern_by_first_token() { - parse_macro( - r#" - macro_rules! foo { - ($ i:ident) => ( - mod $ i {} - ); - (= $ i:ident) => ( - fn $ i() {} - ); - (+ $ i:ident) => ( - struct $ i; - ) - } -"#, - ) - .assert_expand_items("foo! { foo }", "mod foo {}") - .assert_expand_items("foo! { = bar }", "fn bar () {}") - .assert_expand_items("foo! { + Baz }", "struct Baz ;"); -} - #[test] fn test_fail_match_pattern_by_last_token() { parse_macro( From 0dd1b354795500b34bd4f6a76ef1e364c7b1e5c8 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 9 Oct 2021 16:25:37 +0300 Subject: [PATCH 3/6] move test --- .../hir_def/src/macro_expansion_tests/mbe.rs | 30 +++++++++++++++++-- crates/mbe/src/tests/expand.rs | 22 -------------- 2 files changed, 28 insertions(+), 24 deletions(-) diff --git a/crates/hir_def/src/macro_expansion_tests/mbe.rs b/crates/hir_def/src/macro_expansion_tests/mbe.rs index 1e3d554b83a..09688e16b63 100644 --- a/crates/hir_def/src/macro_expansion_tests/mbe.rs +++ b/crates/hir_def/src/macro_expansion_tests/mbe.rs @@ -48,7 +48,7 @@ macro_rules! m { } #[test] -fn tries_all_branches_matching_token_literally() { +fn tries_all_branches_matching_first_token_literally() { check( r#" macro_rules! m { @@ -70,5 +70,31 @@ mod foo {} fn bar() {} struct Baz; "#]], - ) + ); +} + +#[test] +fn tries_all_branches_matching_last_token_literally() { + check( + r#" +macro_rules! m { + ($ i:ident) => ( mod $ i {} ); + ($ i:ident =) => ( fn $ i() {} ); + ($ i:ident +) => ( struct $ i; ) +} +m! { foo } +m! { bar = } +m! { Baz + } +"#, + expect![[r#" +macro_rules! m { + ($ i:ident) => ( mod $ i {} ); + ($ i:ident =) => ( fn $ i() {} ); + ($ i:ident +) => ( struct $ i; ) +} +mod foo {} +fn bar() {} +struct Baz; +"#]], + ); } diff --git a/crates/mbe/src/tests/expand.rs b/crates/mbe/src/tests/expand.rs index 48143f0a60b..f746eb5a1a6 100644 --- a/crates/mbe/src/tests/expand.rs +++ b/crates/mbe/src/tests/expand.rs @@ -209,28 +209,6 @@ fn test_expr_order() { ); } -#[test] -fn test_fail_match_pattern_by_last_token() { - parse_macro( - r#" - macro_rules! foo { - ($ i:ident) => ( - mod $ i {} - ); - ($ i:ident =) => ( - fn $ i() {} - ); - ($ i:ident +) => ( - struct $ i; - ) - } -"#, - ) - .assert_expand_items("foo! { foo }", "mod foo {}") - .assert_expand_items("foo! { bar = }", "fn bar () {}") - .assert_expand_items("foo! { Baz + }", "struct Baz ;"); -} - #[test] fn test_fail_match_pattern_by_word_token() { parse_macro( From 75b0ce17cfa57daff824fcc2b99b3aaba66866a7 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 9 Oct 2021 16:27:19 +0300 Subject: [PATCH 4/6] move test --- .../hir_def/src/macro_expansion_tests/mbe.rs | 26 +++++++++++++++++++ crates/mbe/src/tests/expand.rs | 22 ---------------- 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/crates/hir_def/src/macro_expansion_tests/mbe.rs b/crates/hir_def/src/macro_expansion_tests/mbe.rs index 09688e16b63..32ecd7a4ea2 100644 --- a/crates/hir_def/src/macro_expansion_tests/mbe.rs +++ b/crates/hir_def/src/macro_expansion_tests/mbe.rs @@ -98,3 +98,29 @@ struct Baz; "#]], ); } + +#[test] +fn tries_all_branches_matching_ident() { + check( + r#" +macro_rules! m { + ($ i:ident) => ( mod $ i {} ); + (spam $ i:ident) => ( fn $ i() {} ); + (eggs $ i:ident) => ( struct $ i; ) +} +m! { foo } +m! { spam bar } +m! { eggs Baz } +"#, + expect![[r#" +macro_rules! m { + ($ i:ident) => ( mod $ i {} ); + (spam $ i:ident) => ( fn $ i() {} ); + (eggs $ i:ident) => ( struct $ i; ) +} +mod foo {} +fn bar() {} +struct Baz; +"#]], + ); +} diff --git a/crates/mbe/src/tests/expand.rs b/crates/mbe/src/tests/expand.rs index f746eb5a1a6..33bdcbf0f8d 100644 --- a/crates/mbe/src/tests/expand.rs +++ b/crates/mbe/src/tests/expand.rs @@ -209,28 +209,6 @@ fn test_expr_order() { ); } -#[test] -fn test_fail_match_pattern_by_word_token() { - parse_macro( - r#" - macro_rules! foo { - ($ i:ident) => ( - mod $ i {} - ); - (spam $ i:ident) => ( - fn $ i() {} - ); - (eggs $ i:ident) => ( - struct $ i; - ) - } -"#, - ) - .assert_expand_items("foo! { foo }", "mod foo {}") - .assert_expand_items("foo! { spam bar }", "fn bar () {}") - .assert_expand_items("foo! { eggs Baz }", "struct Baz ;"); -} - #[test] fn test_match_group_pattern_by_separator_token() { parse_macro( From 59c86ff30099f4aaac084e19637889e8fe0a3dbe Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 9 Oct 2021 16:28:11 +0300 Subject: [PATCH 5/6] rename --- crates/hir_def/src/macro_expansion_tests/mbe.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/hir_def/src/macro_expansion_tests/mbe.rs b/crates/hir_def/src/macro_expansion_tests/mbe.rs index 32ecd7a4ea2..bb39dd44926 100644 --- a/crates/hir_def/src/macro_expansion_tests/mbe.rs +++ b/crates/hir_def/src/macro_expansion_tests/mbe.rs @@ -48,7 +48,7 @@ macro_rules! m { } #[test] -fn tries_all_branches_matching_first_token_literally() { +fn match_by_first_token_literally() { check( r#" macro_rules! m { @@ -74,7 +74,7 @@ struct Baz; } #[test] -fn tries_all_branches_matching_last_token_literally() { +fn match_by_last_token_literally() { check( r#" macro_rules! m { @@ -100,7 +100,7 @@ struct Baz; } #[test] -fn tries_all_branches_matching_ident() { +fn match_by_ident() { check( r#" macro_rules! m { From 959da8caa1a80c64b697863f0d61402ac19cf8ba Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 9 Oct 2021 16:31:26 +0300 Subject: [PATCH 6/6] internal: move test --- crates/hir_def/src/macro_expansion_tests.rs | 2 +- .../hir_def/src/macro_expansion_tests/mbe.rs | 35 +++++++++++++++++++ crates/mbe/src/tests/expand.rs | 23 ------------ 3 files changed, 36 insertions(+), 24 deletions(-) diff --git a/crates/hir_def/src/macro_expansion_tests.rs b/crates/hir_def/src/macro_expansion_tests.rs index a899b2c1f38..96957471c83 100644 --- a/crates/hir_def/src/macro_expansion_tests.rs +++ b/crates/hir_def/src/macro_expansion_tests.rs @@ -103,7 +103,7 @@ fn pretty_print_macro_expansion(expn: SyntaxNode) -> String { _ if prev_kind.is_trivia() || curr_kind.is_trivia() => "", (T![=], _) | (_, T![=]) => " ", (_, T!['{']) => " ", - (T![;], _) => "\n", + (T![;] | T!['}'], _) => "\n", (IDENT | LIFETIME_IDENT, IDENT | LIFETIME_IDENT) => " ", (IDENT, _) if curr_kind.is_keyword() => " ", (_, IDENT) if prev_kind.is_keyword() => " ", diff --git a/crates/hir_def/src/macro_expansion_tests/mbe.rs b/crates/hir_def/src/macro_expansion_tests/mbe.rs index bb39dd44926..c57e9cd838d 100644 --- a/crates/hir_def/src/macro_expansion_tests/mbe.rs +++ b/crates/hir_def/src/macro_expansion_tests/mbe.rs @@ -124,3 +124,38 @@ struct Baz; "#]], ); } + +#[test] +fn match_by_separator_token() { + check( + r#" +macro_rules! m { + ($ ($ i:ident),*) => ($ ( mod $ i {} )*); + ($ ($ i:ident)#*) => ($ ( fn $ i() {} )*); + ($ i:ident ,# $ j:ident) => ( struct $ i; struct $ j; ) +} + +m! { foo, bar } + +m! { foo# bar } + +m! { Foo,# Bar } +"#, + expect![[r##" +macro_rules! m { + ($ ($ i:ident),*) => ($ ( mod $ i {} )*); + ($ ($ i:ident)#*) => ($ ( fn $ i() {} )*); + ($ i:ident ,# $ j:ident) => ( struct $ i; struct $ j; ) +} + +mod foo {} +mod bar {} + +fn foo() {} +fn bar() {} + +struct Foo; +struct Bar; +"##]], + ); +} diff --git a/crates/mbe/src/tests/expand.rs b/crates/mbe/src/tests/expand.rs index 33bdcbf0f8d..7becaa6658f 100644 --- a/crates/mbe/src/tests/expand.rs +++ b/crates/mbe/src/tests/expand.rs @@ -209,29 +209,6 @@ fn test_expr_order() { ); } -#[test] -fn test_match_group_pattern_by_separator_token() { - parse_macro( - r#" - macro_rules! foo { - ($ ($ i:ident),*) => ($ ( - mod $ i {} - )*); - ($ ($ i:ident)#*) => ($ ( - fn $ i() {} - )*); - ($ i:ident ,# $ j:ident) => ( - struct $ i; - struct $ j; - ) - } -"#, - ) - .assert_expand_items("foo! { foo, bar }", "mod foo {} mod bar {}") - .assert_expand_items("foo! { foo# bar }", "fn foo () {} fn bar () {}") - .assert_expand_items("foo! { Foo,# Bar }", "struct Foo ; struct Bar ;"); -} - #[test] fn test_match_group_pattern_with_multiple_defs() { parse_macro(