about summary refs log tree commit diff
diff options
context:
space:
mode:
authorhkalbasi <hamidrezakalbasi@protonmail.com>2023-05-12 12:36:57 +0330
committerhkalbasi <hamidrezakalbasi@protonmail.com>2023-05-12 12:36:57 +0330
commit7da80d4f679ee2120717af295fc12c99d1110e40 (patch)
tree7df3ff66624ab41c8cac4e4ccbf73b6e2511bf5c
parent9b3387454d7c70ec768114871682ee2946ec88a8 (diff)
downloadrust-7da80d4f679ee2120717af295fc12c99d1110e40.tar.gz
rust-7da80d4f679ee2120717af295fc12c99d1110e40.zip
Use double reference in debug derive
-rw-r--r--crates/hir-def/src/macro_expansion_tests/builtin_derive_macro.rs2
-rw-r--r--crates/hir-expand/src/builtin_derive_macro.rs4
-rw-r--r--crates/ide-diagnostics/src/handlers/type_mismatch.rs20
-rw-r--r--crates/test-utils/src/minicore.rs73
4 files changed, 91 insertions, 8 deletions
diff --git a/crates/hir-def/src/macro_expansion_tests/builtin_derive_macro.rs b/crates/hir-def/src/macro_expansion_tests/builtin_derive_macro.rs
index 9ea688a8c1a..80474bc154d 100644
--- a/crates/hir-def/src/macro_expansion_tests/builtin_derive_macro.rs
+++ b/crates/hir-def/src/macro_expansion_tests/builtin_derive_macro.rs
@@ -387,7 +387,7 @@ impl < > core::fmt::Debug for Command< > where {
             Command::Move {
                 x: x, y: y,
             }
-            =>f.debug_struct("Move").field("x", x).field("y", y).finish(), Command::Do(f0, )=>f.debug_tuple("Do").field(f0).finish(), Command::Jump=>f.write_str("Jump"),
+            =>f.debug_struct("Move").field("x", &x).field("y", &y).finish(), Command::Do(f0, )=>f.debug_tuple("Do").field(&f0).finish(), Command::Jump=>f.write_str("Jump"),
         }
     }
 }"#]],
diff --git a/crates/hir-expand/src/builtin_derive_macro.rs b/crates/hir-expand/src/builtin_derive_macro.rs
index 4ce71e9774b..54706943ac4 100644
--- a/crates/hir-expand/src/builtin_derive_macro.rs
+++ b/crates/hir-expand/src/builtin_derive_macro.rs
@@ -519,7 +519,7 @@ fn debug_expand(
                 let for_fields = fields.iter().map(|x| {
                     let x_string = x.to_string();
                     quote! {
-                        .field(#x_string, #x)
+                        .field(#x_string, & #x)
                     }
                 });
                 quote! {
@@ -529,7 +529,7 @@ fn debug_expand(
             VariantShape::Tuple(n) => {
                 let for_fields = tuple_field_iterator(*n).map(|x| {
                     quote! {
-                        .field(#x)
+                        .field( & #x)
                     }
                 });
                 quote! {
diff --git a/crates/ide-diagnostics/src/handlers/type_mismatch.rs b/crates/ide-diagnostics/src/handlers/type_mismatch.rs
index fee160c3e7f..c462a16e362 100644
--- a/crates/ide-diagnostics/src/handlers/type_mismatch.rs
+++ b/crates/ide-diagnostics/src/handlers/type_mismatch.rs
@@ -661,4 +661,24 @@ fn f() {
 "#,
         );
     }
+
+    #[test]
+    fn regression_14768() {
+        check_diagnostics(
+            r#"
+//- minicore: derive, fmt, slice, coerce_unsized, builtin_impls
+use core::fmt::Debug;
+
+#[derive(Debug)]
+struct Foo(u8, u16, [u8]);
+
+#[derive(Debug)]
+struct Bar {
+    f1: u8,
+    f2: &[u16],
+    f3: dyn Debug,
+}
+"#,
+        );
+    }
 }
diff --git a/crates/test-utils/src/minicore.rs b/crates/test-utils/src/minicore.rs
index c693235f344..8b831bdcef4 100644
--- a/crates/test-utils/src/minicore.rs
+++ b/crates/test-utils/src/minicore.rs
@@ -766,6 +766,38 @@ pub mod fmt {
     pub struct Error;
     pub type Result = Result<(), Error>;
     pub struct Formatter<'a>;
+    pub struct DebugTuple;
+    pub struct DebugStruct;
+    impl Formatter<'_> {
+        pub fn debug_tuple(&mut self, name: &str) -> DebugTuple {
+            DebugTuple
+        }
+
+        pub fn debug_struct(&mut self, name: &str) -> DebugStruct {
+            DebugStruct
+        }
+    }
+
+    impl DebugTuple {
+        pub fn field(&mut self, value: &dyn Debug) -> &mut Self {
+            self
+        }
+
+        pub fn finish(&mut self) -> Result {
+            Ok(())
+        }
+    }
+
+    impl DebugStruct {
+        pub fn field(&mut self, name: &str, value: &dyn Debug) -> &mut Self {
+            self
+        }
+
+        pub fn finish(&mut self) -> Result {
+            Ok(())
+        }
+    }
+
     pub trait Debug {
         fn fmt(&self, f: &mut Formatter<'_>) -> Result;
     }
@@ -777,6 +809,39 @@ pub mod fmt {
     #[rustc_builtin_macro]
     pub macro Debug($item:item) {}
     // endregion:derive
+
+    // region:builtin_impls
+    macro_rules! impl_debug {
+        ($($t:ty)*) => {
+            $(
+                impl const Debug for $t {
+                    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
+                        Ok(())
+                    }
+                }
+            )*
+        }
+    }
+
+    impl_debug! {
+        usize u8 u16 u32 u64 u128
+        isize i8 i16 i32 i64 i128
+        f32 f64
+        bool char
+    }
+
+    impl<T: Debug> Debug for [T] {
+        fn fmt(&self, f: &mut Formatter<'_>) -> Result {
+            Ok(())
+        }
+    }
+
+    impl<T: Debug + ?Sized> Debug for &T {
+        fn fmt(&self, f: &mut Formatter<'_>) -> Result {
+            (&**self).fmt(f)
+        }
+    }
+    // endregion:builtin_impls
 }
 // endregion:fmt
 
@@ -1075,10 +1140,8 @@ pub mod iter {
 
 // region:panic
 mod panic {
-    pub macro panic_2021 {
-        ($($t:tt)+) => (
-            /* Nothing yet */
-        ),
+    pub macro panic_2021($($t:tt)+) {
+        /* Nothing yet */
     }
 }
 // endregion:panic
@@ -1158,8 +1221,8 @@ pub mod prelude {
             ops::Drop,                          // :drop
             ops::{Fn, FnMut, FnOnce},           // :fn
             option::Option::{self, None, Some}, // :option
-            result::Result::{self, Err, Ok},    // :result
             panic,                              // :panic
+            result::Result::{self, Err, Ok},    // :result
         };
     }