Repository: rust-lang/rust-analyzer
Pull request #12990 (2022-08-10 08:31:32)

fix: Improve whitespace insertion in mbe
Related:  https://github.com/rust-lang/rust-analyzer/issues/12260#issuecomment-1126957162


Patch:
diff --git a/crates/hir-expand/src/builtin_fn_macro.rs b/crates/hir-expand/src/builtin_fn_macro.rs
index c21b35cdc0..8befa7f7da 100644
--- a/crates/hir-expand/src/builtin_fn_macro.rs
+++ b/crates/hir-expand/src/builtin_fn_macro.rs
@@ -251,9 +251,13 @@ fn format_args_expand(
     }
     for arg in &mut args {
         // Remove `key =`.
-        if matches!(arg.token_trees.get(1), Some(tt::TokenTree::Leaf(tt::Leaf::Punct(p))) if p.char == '=' && p.spacing != tt::Spacing::Joint)
+        if matches!(arg.token_trees.get(1), Some(tt::TokenTree::Leaf(tt::Leaf::Punct(p))) if p.char == '=')
         {
-            arg.token_trees.drain(..2);
+            // but not with `==`
+            if !matches!(arg.token_trees.get(2), Some(tt::TokenTree::Leaf(tt::Leaf::Punct(p))) if p.char == '=' )
+            {
+                arg.token_trees.drain(..2);
+            }
         }
     }
     let _format_string = args.remove(0);
diff --git a/crates/hir-expand/src/fixup.rs b/crates/hir-expand/src/fixup.rs
index 46257b6bc4..893e6fe4b8 100644
--- a/crates/hir-expand/src/fixup.rs
+++ b/crates/hir-expand/src/fixup.rs
@@ -468,7 +468,7 @@ fn foo() {
 }
 "#,
             expect![[r#"
-fn foo () {a . __ra_fixup}
+fn foo () {a .__ra_fixup}
 "#]],
         )
     }
@@ -478,11 +478,11 @@ fn foo () {a . __ra_fixup}
         check(
             r#"
 fn foo() {
-    a. ;
+    a.;
 }
 "#,
             expect![[r#"
-fn foo () {a . __ra_fixup ;}
+fn foo () {a .__ra_fixup ;}
 "#]],
         )
     }
@@ -492,12 +492,12 @@ fn foo () {a . __ra_fixup ;}
         check(
             r#"
 fn foo() {
-    a. ;
+    a.;
     bar();
 }
 "#,
             expect![[r#"
-fn foo () {a . __ra_fixup ; bar () ;}
+fn foo () {a .__ra_fixup ; bar () ;}
 "#]],
         )
     }
@@ -525,7 +525,7 @@ fn foo() {
 }
 "#,
             expect![[r#"
-fn foo () {let x = a . __ra_fixup ;}
+fn foo () {let x = a .__ra_fixup ;}
 "#]],
         )
     }
@@ -541,7 +541,7 @@ fn foo() {
 }
 "#,
             expect![[r#"
-fn foo () {a . b ; bar () ;}
+fn foo () {a .b ; bar () ;}
 "#]],
         )
     }
diff --git a/crates/ide-assists/src/handlers/add_missing_impl_members.rs b/crates/ide-assists/src/handlers/add_missing_impl_members.rs
index c808c010c6..62cf5ab4f3 100644
--- a/crates/ide-assists/src/handlers/add_missing_impl_members.rs
+++ b/crates/ide-assists/src/handlers/add_missing_impl_members.rs
@@ -944,7 +944,7 @@ foo!();
 struct Foo(usize);
 
 impl FooB for Foo {
-    $0fn foo< 'lt>(& 'lt self){}
+    $0fn foo<'lt>(&'lt self){}
 }
 "#,
         )
diff --git a/crates/mbe/src/syntax_bridge.rs b/crates/mbe/src/syntax_bridge.rs
index aca6ecd424..e4c56565b9 100644
--- a/crates/mbe/src/syntax_bridge.rs
+++ b/crates/mbe/src/syntax_bridge.rs
@@ -228,16 +228,7 @@ fn convert_tokens<C: TokenConvertor>(conv: &mut C) -> tt::Subtree {
             }
 
             let spacing = match conv.peek().map(|next| next.kind(conv)) {
-                Some(kind)
-                    if !kind.is_trivia()
-                        && kind.is_punct()
-                        && kind != T!['[']
-                        && kind != T!['{']
-                        && kind != T!['(']
-                        && kind != UNDERSCORE =>
-                {
-                    tt::Spacing::Joint
-                }
+                Some(kind) if !kind.is_trivia() => tt::Spacing::Joint,
                 _ => tt::Spacing::Alone,
             };
             let char = match token.to_char(conv) {


Test patch:
diff --git a/crates/hir-def/src/macro_expansion_tests/mbe.rs b/crates/hir-def/src/macro_expansion_tests/mbe.rs
index 30d39d52f3..457e43925c 100644
--- a/crates/hir-def/src/macro_expansion_tests/mbe.rs
+++ b/crates/hir-def/src/macro_expansion_tests/mbe.rs
@@ -885,7 +885,7 @@ macro_rules! m {
     ($t:ty) => ( fn bar() -> $ t {} )
 }
 
-fn bar() -> & 'a Baz<u8> {}
+fn bar() -> &'a Baz<u8> {}
 
 fn bar() -> extern "Rust"fn() -> Ret {}
 "#]],
@@ -1578,7 +1578,7 @@ macro_rules !register_methods {
             ($$($val: expr), *) = > {
                 struct Foo;
                 impl Foo {
-                    $(fn $method()-> & 'static[u32] {
+                    $(fn $method()-> &'static[u32] {
                         &[$$($$val), *]
                     }
                     )*
@@ -1591,10 +1591,10 @@ macro_rules !implement_methods {
     ($($val: expr), *) = > {
         struct Foo;
         impl Foo {
-            fn alpha()-> & 'static[u32] {
+            fn alpha()-> &'static[u32] {
                 &[$($val), *]
             }
-            fn beta()-> & 'static[u32] {
+            fn beta()-> &'static[u32] {
                 &[$($val), *]
             }
         }
@@ -1602,10 +1602,10 @@ macro_rules !implement_methods {
 }
 struct Foo;
 impl Foo {
-    fn alpha() -> & 'static[u32] {
+    fn alpha() -> &'static[u32] {
         &[1, 2, 3]
     }
-    fn beta() -> & 'static[u32] {
+    fn beta() -> &'static[u32] {
         &[1, 2, 3]
     }
 }
diff --git a/crates/hir-def/src/macro_expansion_tests/mbe/regression.rs b/crates/hir-def/src/macro_expansion_tests/mbe/regression.rs
index 2dff4adf2e..d2505e7caf 100644
--- a/crates/hir-def/src/macro_expansion_tests/mbe/regression.rs
+++ b/crates/hir-def/src/macro_expansion_tests/mbe/regression.rs
@@ -166,7 +166,7 @@ macro_rules! int_base {
     }
 }
 #[stable(feature = "rust1", since = "1.0.0")] impl fmt::Binary for isize {
-    fn fmt(&self , f: &mut fmt::Formatter< '_>) -> fmt::Result {
+    fn fmt(&self , f: &mut fmt::Formatter<'_>) -> fmt::Result {
         Binary.fmt_int(*self as usize, f)
     }
 }
@@ -724,7 +724,7 @@ macro_rules! delegate_impl {
         }
     }
 }
-impl <> Data for & 'amut G where G: Data {}
+impl <> Data for &'amut G where G: Data {}
 "##]],
     );
 }
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 0710b1ac3d..b8d2ca687c 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
@@ -78,7 +78,7 @@ m!(static bar: &'static str = "hello";);
 macro_rules! m {
     ($($t:tt)*) => { $($t)*}
 }
-static bar: & 'static str = "hello";
+static bar: &'static str = "hello";
 "#]],
     );
 }
diff --git a/crates/hir-def/src/macro_expansion_tests/proc_macros.rs b/crates/hir-def/src/macro_expansion_tests/proc_macros.rs
index 72c44a0fbc..029821e5e8 100644
--- a/crates/hir-def/src/macro_expansion_tests/proc_macros.rs
+++ b/crates/hir-def/src/macro_expansion_tests/proc_macros.rs
@@ -87,7 +87,7 @@ fn foo() { bar.; blub }
 fn foo() { bar.; blub }
 
 fn foo() {
-    bar. ;
+    bar.;
     blub
 }"##]],
     );
diff --git a/crates/proc-macro-srv/src/tests/mod.rs b/crates/proc-macro-srv/src/tests/mod.rs
index 07222907f0..6339d56d01 100644
--- a/crates/proc-macro-srv/src/tests/mod.rs
+++ b/crates/proc-macro-srv/src/tests/mod.rs
@@ -19,7 +19,7 @@ fn test_derive_error() {
         expect![[r##"
             SUBTREE $
               IDENT   compile_error 4294967295
-              PUNCH   ! [alone] 4294967295
+              PUNCH   ! [joint] 4294967295
               SUBTREE () 4294967295
                 LITERAL "#[derive(DeriveError)] struct S ;" 4294967295
               PUNCH   ; [alone] 4294967295"##]],
@@ -109,7 +109,7 @@ fn test_fn_like_macro_clone_literals() {
               PUNCH   , [alone] 4294967295
               LITERAL 2_u32 4294967295
               PUNCH   , [alone] 4294967295
-              PUNCH   - [alone] 4294967295
+              PUNCH   - [joint] 4294967295
               LITERAL 4i64 4294967295
               PUNCH   , [alone] 4294967295
               LITERAL 3.14f32 4294967295
@@ -130,7 +130,7 @@ fn test_attr_macro() {
         expect![[r##"
             SUBTREE $
               IDENT   compile_error 4294967295
-              PUNCH   ! [alone] 4294967295
+              PUNCH   ! [joint] 4294967295
               SUBTREE () 4294967295
                 LITERAL "#[attr_error(some arguments)] mod m {}" 4294967295
               PUNCH   ; [alone] 4294967295"##]],