about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJana Dönszelmann <jana@donsz.nl>2025-05-02 13:26:35 +0200
committerJana Dönszelmann <jana@donsz.nl>2025-05-02 18:09:25 +0200
commit867b4c9e489c71246915ebe3e1ceddef66d760a3 (patch)
tree16d98afa35d6a25c13611313adcf5db9e81973dc
parentf97b3c6044a67e0b5d0d0891ca3a6c5d982b2285 (diff)
downloadrust-867b4c9e489c71246915ebe3e1ceddef66d760a3.tar.gz
rust-867b4c9e489c71246915ebe3e1ceddef66d760a3.zip
Test that names of variables in external macros are not shown on a borrow error
-rw-r--r--compiler/rustc_borrowck/src/diagnostics/mod.rs8
-rw-r--r--tests/ui/macros/auxiliary/return_from_external_macro.rs11
-rw-r--r--tests/ui/macros/return_from_external_macro.rs17
-rw-r--r--tests/ui/macros/return_from_external_macro.stderr29
4 files changed, 65 insertions, 0 deletions
diff --git a/compiler/rustc_borrowck/src/diagnostics/mod.rs b/compiler/rustc_borrowck/src/diagnostics/mod.rs
index f9f63ae92a8..5e3f3ffa2ea 100644
--- a/compiler/rustc_borrowck/src/diagnostics/mod.rs
+++ b/compiler/rustc_borrowck/src/diagnostics/mod.rs
@@ -317,6 +317,14 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
         opt: DescribePlaceOpt,
     ) -> Option<String> {
         let local = place.local;
+        if self.body.local_decls[local]
+            .source_info
+            .span
+            .in_external_macro(self.infcx.tcx.sess.source_map())
+        {
+            return None;
+        }
+
         let mut autoderef_index = None;
         let mut buf = String::new();
         let mut ok = self.append_local_to_string(local, &mut buf);
diff --git a/tests/ui/macros/auxiliary/return_from_external_macro.rs b/tests/ui/macros/auxiliary/return_from_external_macro.rs
new file mode 100644
index 00000000000..df59d8bdcb0
--- /dev/null
+++ b/tests/ui/macros/auxiliary/return_from_external_macro.rs
@@ -0,0 +1,11 @@
+#![feature(super_let)]
+
+#[macro_export]
+macro_rules! foo {
+    () => {
+        {
+            super let args = 1;
+            &args
+        }
+    };
+}
diff --git a/tests/ui/macros/return_from_external_macro.rs b/tests/ui/macros/return_from_external_macro.rs
new file mode 100644
index 00000000000..43fe99e63ad
--- /dev/null
+++ b/tests/ui/macros/return_from_external_macro.rs
@@ -0,0 +1,17 @@
+//@ aux-crate: ret_from_ext=return_from_external_macro.rs
+
+#![feature(super_let)]
+extern crate ret_from_ext;
+
+fn foo() -> impl Sized {
+    drop(|| ret_from_ext::foo!());
+    //~^ ERROR cannot return reference to local binding
+
+    ret_from_ext::foo!()
+    //~^ ERROR temporary value dropped while borrowed
+}
+//~^ NOTE temporary value is freed at the end of this statement
+
+fn main() {
+    foo();
+}
diff --git a/tests/ui/macros/return_from_external_macro.stderr b/tests/ui/macros/return_from_external_macro.stderr
new file mode 100644
index 00000000000..b6010b8ec79
--- /dev/null
+++ b/tests/ui/macros/return_from_external_macro.stderr
@@ -0,0 +1,29 @@
+error[E0515]: cannot return reference to local binding
+  --> $DIR/return_from_external_macro.rs:7:13
+   |
+LL |     drop(|| ret_from_ext::foo!());
+   |             ^^^^^^^^^^^^^^^^^^^^
+   |             |
+   |             returns a reference to data owned by the current function
+   |             local binding introduced here
+   |
+   = note: this error originates in the macro `ret_from_ext::foo` (in Nightly builds, run with -Z macro-backtrace for more info)
+
+error[E0716]: temporary value dropped while borrowed
+  --> $DIR/return_from_external_macro.rs:10:5
+   |
+LL |     ret_from_ext::foo!()
+   |     ^^^^^^^^^^^^^^^^^^^^
+   |     |
+   |     creates a temporary value which is freed while still in use
+   |     opaque type requires that borrow lasts for `'static`
+LL |
+LL | }
+   | - temporary value is freed at the end of this statement
+   |
+   = note: this error originates in the macro `ret_from_ext::foo` (in Nightly builds, run with -Z macro-backtrace for more info)
+
+error: aborting due to 2 previous errors
+
+Some errors have detailed explanations: E0515, E0716.
+For more information about an error, try `rustc --explain E0515`.