Skip to content

Commit 867b4c9

Browse files
committed
Test that names of variables in external macros are not shown on a borrow error
1 parent f97b3c6 commit 867b4c9

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed

compiler/rustc_borrowck/src/diagnostics/mod.rs

+8
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,14 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
317317
opt: DescribePlaceOpt,
318318
) -> Option<String> {
319319
let local = place.local;
320+
if self.body.local_decls[local]
321+
.source_info
322+
.span
323+
.in_external_macro(self.infcx.tcx.sess.source_map())
324+
{
325+
return None;
326+
}
327+
320328
let mut autoderef_index = None;
321329
let mut buf = String::new();
322330
let mut ok = self.append_local_to_string(local, &mut buf);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#![feature(super_let)]
2+
3+
#[macro_export]
4+
macro_rules! foo {
5+
() => {
6+
{
7+
super let args = 1;
8+
&args
9+
}
10+
};
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//@ aux-crate: ret_from_ext=return_from_external_macro.rs
2+
3+
#![feature(super_let)]
4+
extern crate ret_from_ext;
5+
6+
fn foo() -> impl Sized {
7+
drop(|| ret_from_ext::foo!());
8+
//~^ ERROR cannot return reference to local binding
9+
10+
ret_from_ext::foo!()
11+
//~^ ERROR temporary value dropped while borrowed
12+
}
13+
//~^ NOTE temporary value is freed at the end of this statement
14+
15+
fn main() {
16+
foo();
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
error[E0515]: cannot return reference to local binding
2+
--> $DIR/return_from_external_macro.rs:7:13
3+
|
4+
LL | drop(|| ret_from_ext::foo!());
5+
| ^^^^^^^^^^^^^^^^^^^^
6+
| |
7+
| returns a reference to data owned by the current function
8+
| local binding introduced here
9+
|
10+
= note: this error originates in the macro `ret_from_ext::foo` (in Nightly builds, run with -Z macro-backtrace for more info)
11+
12+
error[E0716]: temporary value dropped while borrowed
13+
--> $DIR/return_from_external_macro.rs:10:5
14+
|
15+
LL | ret_from_ext::foo!()
16+
| ^^^^^^^^^^^^^^^^^^^^
17+
| |
18+
| creates a temporary value which is freed while still in use
19+
| opaque type requires that borrow lasts for `'static`
20+
LL |
21+
LL | }
22+
| - temporary value is freed at the end of this statement
23+
|
24+
= note: this error originates in the macro `ret_from_ext::foo` (in Nightly builds, run with -Z macro-backtrace for more info)
25+
26+
error: aborting due to 2 previous errors
27+
28+
Some errors have detailed explanations: E0515, E0716.
29+
For more information about an error, try `rustc --explain E0515`.

0 commit comments

Comments
 (0)