Skip to content

Commit af6bf26

Browse files
authored
Rollup merge of rust-lang#140580 - jdonszelmann:variables-external-macros, r=m-ou-se
Don't name variables from external macros in borrow errors. This came up as part of the expansion of format_args. However, it's a more general problem (and now solution). I noticed that this does change another test, moving out of fields in derives on packed struct. However, I think this is a better error simply because it used to refer to `other.0` which is an implementation detail which doesn't really make sense. cc `@m-ou-se`
2 parents 679b6ce + 5424e4f commit af6bf26

11 files changed

+114
-49
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -2954,12 +2954,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
29542954
}
29552955
}
29562956

2957-
let name = if borrow_span.in_external_macro(self.infcx.tcx.sess.source_map()) {
2958-
// Don't name local variables in external macros.
2959-
"value".to_string()
2960-
} else {
2961-
format!("`{name}`")
2962-
};
2957+
let name = format!("`{name}`");
29632958

29642959
let mut err = self.path_does_not_live_long_enough(borrow_span, &name);
29652960

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);

tests/ui/derives/deriving-with-repr-packed-move-errors.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ use std::cmp::Ordering;
1111
#[repr(packed)]
1212
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Default)]
1313
struct StructA(String);
14-
//~^ ERROR: cannot move out of `self` which is behind a shared reference
15-
//~| ERROR: cannot move out of `self` which is behind a shared reference
16-
//~| ERROR: cannot move out of `other` which is behind a shared reference
17-
//~| ERROR: cannot move out of `self` which is behind a shared reference
18-
//~| ERROR: cannot move out of `other` which is behind a shared reference
19-
//~| ERROR: cannot move out of `self` which is behind a shared reference
20-
//~| ERROR: cannot move out of `other` which is behind a shared reference
21-
//~| ERROR: cannot move out of `self` which is behind a shared reference
22-
//~| ERROR: cannot move out of `self` which is behind a shared reference
14+
//~^ ERROR: cannot move out of a shared reference [E0507]
15+
//~| ERROR: cannot move out of a shared reference [E0507]
16+
//~| ERROR: cannot move out of a shared reference [E0507]
17+
//~| ERROR: cannot move out of a shared reference [E0507]
18+
//~| ERROR: cannot move out of a shared reference [E0507]
19+
//~| ERROR: cannot move out of a shared reference [E0507]
20+
//~| ERROR: cannot move out of a shared reference [E0507]
21+
//~| ERROR: cannot move out of a shared reference [E0507]
22+
//~| ERROR: cannot move out of a shared reference [E0507]
2323

2424

2525
// Unrelated impl: additinal diagnostic should NOT be emitted

tests/ui/derives/deriving-with-repr-packed-move-errors.stderr

+21-18
Original file line numberDiff line numberDiff line change
@@ -1,122 +1,125 @@
1-
error[E0507]: cannot move out of `self` which is behind a shared reference
1+
error[E0507]: cannot move out of a shared reference
22
--> $DIR/deriving-with-repr-packed-move-errors.rs:13:16
33
|
44
LL | #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Default)]
55
| ----- in this derive macro expansion
66
LL | struct StructA(String);
7-
| ^^^^^^ move occurs because `self.0` has type `String`, which does not implement the `Copy` trait
7+
| ^^^^^^ move occurs because value has type `String`, which does not implement the `Copy` trait
88
|
99
= note: `#[derive(Debug)]` triggers a move because taking references to the fields of a packed struct is undefined behaviour
1010
help: consider cloning the value if the performance cost is acceptable
1111
|
1212
LL | struct StructA(String.clone());
1313
| ++++++++
1414

15-
error[E0507]: cannot move out of `self` which is behind a shared reference
15+
error[E0507]: cannot move out of a shared reference
1616
--> $DIR/deriving-with-repr-packed-move-errors.rs:13:16
1717
|
1818
LL | #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Default)]
1919
| --------- in this derive macro expansion
2020
LL | struct StructA(String);
21-
| ^^^^^^ move occurs because `self.0` has type `String`, which does not implement the `Copy` trait
21+
| ^^^^^^ move occurs because value has type `String`, which does not implement the `Copy` trait
2222
|
2323
= note: `#[derive(PartialEq)]` triggers a move because taking references to the fields of a packed struct is undefined behaviour
2424
help: consider cloning the value if the performance cost is acceptable
2525
|
2626
LL | struct StructA(String.clone());
2727
| ++++++++
2828

29-
error[E0507]: cannot move out of `other` which is behind a shared reference
29+
error[E0507]: cannot move out of a shared reference
3030
--> $DIR/deriving-with-repr-packed-move-errors.rs:13:16
3131
|
3232
LL | #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Default)]
3333
| --------- in this derive macro expansion
3434
LL | struct StructA(String);
35-
| ^^^^^^ move occurs because `other.0` has type `String`, which does not implement the `Copy` trait
35+
| ^^^^^^ move occurs because value has type `String`, which does not implement the `Copy` trait
3636
|
3737
= note: `#[derive(PartialEq)]` triggers a move because taking references to the fields of a packed struct is undefined behaviour
38+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
3839
help: consider cloning the value if the performance cost is acceptable
3940
|
4041
LL | struct StructA(String.clone());
4142
| ++++++++
4243

43-
error[E0507]: cannot move out of `self` which is behind a shared reference
44+
error[E0507]: cannot move out of a shared reference
4445
--> $DIR/deriving-with-repr-packed-move-errors.rs:13:16
4546
|
4647
LL | #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Default)]
4748
| ---------- in this derive macro expansion
4849
LL | struct StructA(String);
49-
| ^^^^^^ move occurs because `self.0` has type `String`, which does not implement the `Copy` trait
50+
| ^^^^^^ move occurs because value has type `String`, which does not implement the `Copy` trait
5051
|
5152
= note: `#[derive(PartialOrd)]` triggers a move because taking references to the fields of a packed struct is undefined behaviour
5253
help: consider cloning the value if the performance cost is acceptable
5354
|
5455
LL | struct StructA(String.clone());
5556
| ++++++++
5657

57-
error[E0507]: cannot move out of `other` which is behind a shared reference
58+
error[E0507]: cannot move out of a shared reference
5859
--> $DIR/deriving-with-repr-packed-move-errors.rs:13:16
5960
|
6061
LL | #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Default)]
6162
| ---------- in this derive macro expansion
6263
LL | struct StructA(String);
63-
| ^^^^^^ move occurs because `other.0` has type `String`, which does not implement the `Copy` trait
64+
| ^^^^^^ move occurs because value has type `String`, which does not implement the `Copy` trait
6465
|
6566
= note: `#[derive(PartialOrd)]` triggers a move because taking references to the fields of a packed struct is undefined behaviour
67+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
6668
help: consider cloning the value if the performance cost is acceptable
6769
|
6870
LL | struct StructA(String.clone());
6971
| ++++++++
7072

71-
error[E0507]: cannot move out of `self` which is behind a shared reference
73+
error[E0507]: cannot move out of a shared reference
7274
--> $DIR/deriving-with-repr-packed-move-errors.rs:13:16
7375
|
7476
LL | #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Default)]
7577
| --- in this derive macro expansion
7678
LL | struct StructA(String);
77-
| ^^^^^^ move occurs because `self.0` has type `String`, which does not implement the `Copy` trait
79+
| ^^^^^^ move occurs because value has type `String`, which does not implement the `Copy` trait
7880
|
7981
= note: `#[derive(Ord)]` triggers a move because taking references to the fields of a packed struct is undefined behaviour
8082
help: consider cloning the value if the performance cost is acceptable
8183
|
8284
LL | struct StructA(String.clone());
8385
| ++++++++
8486

85-
error[E0507]: cannot move out of `other` which is behind a shared reference
87+
error[E0507]: cannot move out of a shared reference
8688
--> $DIR/deriving-with-repr-packed-move-errors.rs:13:16
8789
|
8890
LL | #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Default)]
8991
| --- in this derive macro expansion
9092
LL | struct StructA(String);
91-
| ^^^^^^ move occurs because `other.0` has type `String`, which does not implement the `Copy` trait
93+
| ^^^^^^ move occurs because value has type `String`, which does not implement the `Copy` trait
9294
|
9395
= note: `#[derive(Ord)]` triggers a move because taking references to the fields of a packed struct is undefined behaviour
96+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
9497
help: consider cloning the value if the performance cost is acceptable
9598
|
9699
LL | struct StructA(String.clone());
97100
| ++++++++
98101

99-
error[E0507]: cannot move out of `self` which is behind a shared reference
102+
error[E0507]: cannot move out of a shared reference
100103
--> $DIR/deriving-with-repr-packed-move-errors.rs:13:16
101104
|
102105
LL | #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Default)]
103106
| ---- in this derive macro expansion
104107
LL | struct StructA(String);
105-
| ^^^^^^ move occurs because `self.0` has type `String`, which does not implement the `Copy` trait
108+
| ^^^^^^ move occurs because value has type `String`, which does not implement the `Copy` trait
106109
|
107110
= note: `#[derive(Hash)]` triggers a move because taking references to the fields of a packed struct is undefined behaviour
108111
help: consider cloning the value if the performance cost is acceptable
109112
|
110113
LL | struct StructA(String.clone());
111114
| ++++++++
112115

113-
error[E0507]: cannot move out of `self` which is behind a shared reference
116+
error[E0507]: cannot move out of a shared reference
114117
--> $DIR/deriving-with-repr-packed-move-errors.rs:13:16
115118
|
116119
LL | #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Default)]
117120
| ----- in this derive macro expansion
118121
LL | struct StructA(String);
119-
| ^^^^^^ move occurs because `self.0` has type `String`, which does not implement the `Copy` trait
122+
| ^^^^^^ move occurs because value has type `String`, which does not implement the `Copy` trait
120123
|
121124
= note: `#[derive(Clone)]` triggers a move because taking references to the fields of a packed struct is undefined behaviour
122125
help: consider cloning the value if the performance cost is acceptable

tests/ui/derives/deriving-with-repr-packed.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ struct Y(usize);
2020
#[derive(Debug, Default)]
2121
#[repr(packed)]
2222
struct X(Y);
23-
//~^ ERROR cannot move out of `self` which is behind a shared reference
23+
//~^ ERROR cannot move out of a shared reference [E0507]
2424

2525
#[derive(Debug)]
2626
#[repr(packed)]

tests/ui/derives/deriving-with-repr-packed.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
error[E0507]: cannot move out of `self` which is behind a shared reference
1+
error[E0507]: cannot move out of a shared reference
22
--> $DIR/deriving-with-repr-packed.rs:22:10
33
|
44
LL | #[derive(Debug, Default)]
55
| ----- in this derive macro expansion
66
LL | #[repr(packed)]
77
LL | struct X(Y);
8-
| ^ move occurs because `self.0` has type `Y`, which does not implement the `Copy` trait
8+
| ^ move occurs because value has type `Y`, which does not implement the `Copy` trait
99
|
1010
note: if `Y` implemented `Clone`, you could clone the value
1111
--> $DIR/deriving-with-repr-packed.rs:16:1
@@ -23,14 +23,14 @@ error[E0161]: cannot move a value of type `[u8]`
2323
LL | data: [u8],
2424
| ^^^^^^^^^^ the size of `[u8]` cannot be statically determined
2525

26-
error[E0507]: cannot move out of `self.data` which is behind a shared reference
26+
error[E0507]: cannot move out of a shared reference
2727
--> $DIR/deriving-with-repr-packed.rs:29:5
2828
|
2929
LL | #[derive(Debug)]
3030
| ----- in this derive macro expansion
3131
...
3232
LL | data: [u8],
33-
| ^^^^^^^^^^ move occurs because `self.data` has type `[u8]`, which does not implement the `Copy` trait
33+
| ^^^^^^^^^^ move occurs because value has type `[u8]`, which does not implement the `Copy` trait
3434
|
3535
= note: `#[derive(Debug)]` triggers a move because taking references to the fields of a packed struct is undefined behaviour
3636

@@ -40,14 +40,14 @@ error[E0161]: cannot move a value of type `str`
4040
LL | data: str,
4141
| ^^^^^^^^^ the size of `str` cannot be statically determined
4242

43-
error[E0507]: cannot move out of `self.data` which is behind a shared reference
43+
error[E0507]: cannot move out of a shared reference
4444
--> $DIR/deriving-with-repr-packed.rs:38:5
4545
|
4646
LL | #[derive(Debug)]
4747
| ----- in this derive macro expansion
4848
...
4949
LL | data: str,
50-
| ^^^^^^^^^ move occurs because `self.data` has type `str`, which does not implement the `Copy` trait
50+
| ^^^^^^^^^ move occurs because value has type `str`, which does not implement the `Copy` trait
5151
|
5252
= note: `#[derive(Debug)]` triggers a move because taking references to the fields of a packed struct is undefined behaviour
5353

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`.

tests/ui/pin-macro/lifetime_errors_on_promotion_misusage.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ use core::{
99

1010
fn function_call_stops_borrow_extension() {
1111
let phantom_pinned = identity(pin!(PhantomPinned));
12-
//~^ ERROR does not live long enough
12+
//~^ ERROR temporary value dropped while borrowed [E0716]
1313
stuff(phantom_pinned)
1414
}
1515

1616
fn promotion_only_works_for_the_innermost_block() {
1717
let phantom_pinned = {
1818
let phantom_pinned = pin!(PhantomPinned);
19-
//~^ ERROR does not live long enough
19+
//~^ ERROR temporary value dropped while borrowed [E0716]
2020
phantom_pinned
2121
};
2222
stuff(phantom_pinned)
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,31 @@
1-
error[E0597]: value does not live long enough
1+
error[E0716]: temporary value dropped while borrowed
22
--> $DIR/lifetime_errors_on_promotion_misusage.rs:11:35
33
|
44
LL | let phantom_pinned = identity(pin!(PhantomPinned));
5-
| ^^^^^^^^^^^^^^^^^^^ - value dropped here while still borrowed
5+
| ^^^^^^^^^^^^^^^^^^^ - temporary value is freed at the end of this statement
66
| |
7-
| borrowed value does not live long enough
7+
| creates a temporary value which is freed while still in use
88
LL |
99
LL | stuff(phantom_pinned)
1010
| -------------- borrow later used here
1111
|
12+
= note: consider using a `let` binding to create a longer lived value
1213
= note: this error originates in the macro `pin` (in Nightly builds, run with -Z macro-backtrace for more info)
1314

14-
error[E0597]: value does not live long enough
15+
error[E0716]: temporary value dropped while borrowed
1516
--> $DIR/lifetime_errors_on_promotion_misusage.rs:18:30
1617
|
1718
LL | let phantom_pinned = {
1819
| -------------- borrow later stored here
1920
LL | let phantom_pinned = pin!(PhantomPinned);
20-
| ^^^^^^^^^^^^^^^^^^^ borrowed value does not live long enough
21+
| ^^^^^^^^^^^^^^^^^^^ creates a temporary value which is freed while still in use
2122
...
2223
LL | };
23-
| - value dropped here while still borrowed
24+
| - temporary value is freed at the end of this statement
2425
|
26+
= note: consider using a `let` binding to create a longer lived value
2527
= note: this error originates in the macro `pin` (in Nightly builds, run with -Z macro-backtrace for more info)
2628

2729
error: aborting due to 2 previous errors
2830

29-
For more information about this error, try `rustc --explain E0597`.
31+
For more information about this error, try `rustc --explain E0716`.

0 commit comments

Comments
 (0)