diff --git a/crates/hir_def/src/macro_expansion_tests.rs b/crates/hir_def/src/macro_expansion_tests.rs index c66c75c1436..e892ab52686 100644 --- a/crates/hir_def/src/macro_expansion_tests.rs +++ b/crates/hir_def/src/macro_expansion_tests.rs @@ -121,6 +121,7 @@ fn pretty_print_macro_expansion(expn: SyntaxNode) -> String { (T![;] | T!['{'] | T!['}'], _) => "\n", (_, T!['}']) => "\n", (IDENT | LIFETIME_IDENT, IDENT | LIFETIME_IDENT) => " ", + _ if prev_kind.is_keyword() && curr_kind.is_keyword() => " ", (IDENT, _) if curr_kind.is_keyword() => " ", (_, IDENT) if prev_kind.is_keyword() => " ", (T![>], IDENT) => " ", diff --git a/crates/hir_def/src/macro_expansion_tests/mbe.rs b/crates/hir_def/src/macro_expansion_tests/mbe.rs index ae93ce2ae8d..958f2a1c331 100644 --- a/crates/hir_def/src/macro_expansion_tests/mbe.rs +++ b/crates/hir_def/src/macro_expansion_tests/mbe.rs @@ -704,3 +704,77 @@ fn foo() { "#]], ); } + +#[test] +fn test_expr() { + check( + r#" +macro_rules! m { + ($e:expr) => { fn bar() { $e; } } +} + +m! { 2 + 2 * baz(3).quux() } +"#, + expect![[r#" +macro_rules! m { + ($e:expr) => { fn bar() { $e; } } +} + +fn bar() { + 2+2*baz(3).quux(); +} +"#]], + ) +} + +#[test] +fn test_last_expr() { + check( + r#" +macro_rules! vec { + ($($item:expr),*) => {{ + let mut v = Vec::new(); + $( v.push($item); )* + v + }}; +} + +fn f() { + vec![1,2,3]; +} +"#, + expect![[r#" +macro_rules! vec { + ($($item:expr),*) => {{ + let mut v = Vec::new(); + $( v.push($item); )* + v + }}; +} + +fn f() { + { + let mut v = Vec::new(); + v.push(1); + v.push(2); + v.push(3); + v + }; +} +"#]], + ); +} + +#[test] +fn test_expr_with_attr() { + check( + r#" +macro_rules! m { ($a:expr) => { x!(); } } +m!(#[allow(a)]()); +"#, + expect![[r#" +macro_rules! m { ($a:expr) => { x!(); } } +x!(); +"#]], + ) +} diff --git a/crates/mbe/src/tests/expand.rs b/crates/mbe/src/tests/expand.rs index b3e30391f7c..01b55335226 100644 --- a/crates/mbe/src/tests/expand.rs +++ b/crates/mbe/src/tests/expand.rs @@ -101,58 +101,6 @@ fn test_attr_to_token_tree() { ); } -#[test] -fn test_expr() { - parse_macro( - r#" - macro_rules! foo { - ($ i:expr) => { - fn bar() { $ i; } - } - } -"#, - ) - .assert_expand_items( - "foo! { 2 + 2 * baz(3).quux() }", - "fn bar () {2 + 2 * baz (3) . quux () ;}", - ); -} - -#[test] -fn test_last_expr() { - parse_macro( - r#" - macro_rules! vec { - ($($item:expr),*) => { - { - let mut v = Vec::new(); - $( - v.push($item); - )* - v - } - }; - } -"#, - ) - .assert_expand_items( - "vec!(1,2,3);", - "{let mut v = Vec :: new () ; v . push (1) ; v . push (2) ; v . push (3) ; v}", - ); -} - -#[test] -fn test_expr_with_attr() { - parse_macro( - r#" -macro_rules! m { - ($a:expr) => {0} -} -"#, - ) - .assert_expand_items("m!(#[allow(a)]())", "0"); -} - #[test] fn test_ty() { parse_macro(