Skip to content

Commit f5c3dfd

Browse files
committed
Auto merge of #105085 - oli-obk:stop_promoting_all_the_things, r=RalfJung
Stop promoting all the things fixes #91009 r? `@RalfJung`
2 parents 8766bbd + b4a6047 commit f5c3dfd

10 files changed

+419
-8
lines changed

compiler/rustc_const_eval/src/transform/promote_consts.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -216,12 +216,6 @@ impl<'tcx> Validator<'_, 'tcx> {
216216
return Err(Unpromotable);
217217
}
218218

219-
// We cannot promote things that need dropping, since the promoted value
220-
// would not get dropped.
221-
if self.qualif_local::<qualifs::NeedsDrop>(place.local) {
222-
return Err(Unpromotable);
223-
}
224-
225219
Ok(())
226220
}
227221
_ => bug!(),
@@ -262,13 +256,17 @@ impl<'tcx> Validator<'_, 'tcx> {
262256
}
263257
}
264258
} else {
265-
let span = self.body.local_decls[local].source_info.span;
266-
span_bug!(span, "{:?} not promotable, qualif_local shouldn't have been called", local);
259+
false
267260
}
268261
}
269262

270263
fn validate_local(&mut self, local: Local) -> Result<(), Unpromotable> {
271264
if let TempState::Defined { location: loc, uses, valid } = self.temps[local] {
265+
// We cannot promote things that need dropping, since the promoted value
266+
// would not get dropped.
267+
if self.qualif_local::<qualifs::NeedsDrop>(local) {
268+
return Err(Unpromotable);
269+
}
272270
valid.or_else(|_| {
273271
let ok = {
274272
let block = &self.body[loc.block];
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#![feature(const_mut_refs)]
2+
#![feature(const_trait_impl)]
3+
struct Panic;
4+
impl const Drop for Panic { fn drop(&mut self) { panic!(); } }
5+
pub const fn id<T>(x: T) -> T { x }
6+
pub const C: () = {
7+
let _: &'static _ = &id(&Panic);
8+
//~^ ERROR: temporary value dropped while borrowed
9+
//~| ERROR: temporary value dropped while borrowed
10+
};
11+
12+
fn main() {
13+
let _: &'static _ = &id(&Panic);
14+
//~^ ERROR: temporary value dropped while borrowed
15+
//~| ERROR: temporary value dropped while borrowed
16+
let _: &'static _ = &&(Panic, 0).1;
17+
//~^ ERROR: temporary value dropped while borrowed
18+
//~| ERROR: temporary value dropped while borrowed
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
error[E0716]: temporary value dropped while borrowed
2+
--> $DIR/promoted_const_call.rs:7:26
3+
|
4+
LL | let _: &'static _ = &id(&Panic);
5+
| ---------- ^^^^^^^^^^ creates a temporary value which is freed while still in use
6+
| |
7+
| type annotation requires that borrow lasts for `'static`
8+
...
9+
LL | };
10+
| - temporary value is freed at the end of this statement
11+
12+
error[E0716]: temporary value dropped while borrowed
13+
--> $DIR/promoted_const_call.rs:7:30
14+
|
15+
LL | let _: &'static _ = &id(&Panic);
16+
| ---------- ^^^^^ - temporary value is freed at the end of this statement
17+
| | |
18+
| | creates a temporary value which is freed while still in use
19+
| type annotation requires that borrow lasts for `'static`
20+
21+
error[E0716]: temporary value dropped while borrowed
22+
--> $DIR/promoted_const_call.rs:13:26
23+
|
24+
LL | let _: &'static _ = &id(&Panic);
25+
| ---------- ^^^^^^^^^^ creates a temporary value which is freed while still in use
26+
| |
27+
| type annotation requires that borrow lasts for `'static`
28+
...
29+
LL | }
30+
| - temporary value is freed at the end of this statement
31+
32+
error[E0716]: temporary value dropped while borrowed
33+
--> $DIR/promoted_const_call.rs:13:30
34+
|
35+
LL | let _: &'static _ = &id(&Panic);
36+
| ---------- ^^^^^ - temporary value is freed at the end of this statement
37+
| | |
38+
| | creates a temporary value which is freed while still in use
39+
| type annotation requires that borrow lasts for `'static`
40+
41+
error[E0716]: temporary value dropped while borrowed
42+
--> $DIR/promoted_const_call.rs:16:26
43+
|
44+
LL | let _: &'static _ = &&(Panic, 0).1;
45+
| ---------- ^^^^^^^^^^^^^ creates a temporary value which is freed while still in use
46+
| |
47+
| type annotation requires that borrow lasts for `'static`
48+
...
49+
LL | }
50+
| - temporary value is freed at the end of this statement
51+
52+
error[E0716]: temporary value dropped while borrowed
53+
--> $DIR/promoted_const_call.rs:16:27
54+
|
55+
LL | let _: &'static _ = &&(Panic, 0).1;
56+
| ---------- ^^^^^^^^^^ creates a temporary value which is freed while still in use
57+
| |
58+
| type annotation requires that borrow lasts for `'static`
59+
...
60+
LL | }
61+
| - temporary value is freed at the end of this statement
62+
63+
error: aborting due to 6 previous errors
64+
65+
For more information about this error, try `rustc --explain E0716`.
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#![feature(const_precise_live_drops)]
2+
pub const fn id<T>(x: T) -> T { x }
3+
pub const C: () = {
4+
let _: &'static _ = &id(&String::new());
5+
//~^ ERROR: temporary value dropped while borrowed
6+
//~| ERROR: temporary value dropped while borrowed
7+
//~| ERROR: destructor of `String` cannot be evaluated at compile-time
8+
};
9+
10+
fn main() {
11+
let _: &'static _ = &id(&String::new());
12+
//~^ ERROR: temporary value dropped while borrowed
13+
//~| ERROR: temporary value dropped while borrowed
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
error[E0716]: temporary value dropped while borrowed
2+
--> $DIR/promoted_const_call2.rs:4:26
3+
|
4+
LL | let _: &'static _ = &id(&String::new());
5+
| ---------- ^^^^^^^^^^^^^^^^^^ creates a temporary value which is freed while still in use
6+
| |
7+
| type annotation requires that borrow lasts for `'static`
8+
...
9+
LL | };
10+
| - temporary value is freed at the end of this statement
11+
12+
error[E0716]: temporary value dropped while borrowed
13+
--> $DIR/promoted_const_call2.rs:4:30
14+
|
15+
LL | let _: &'static _ = &id(&String::new());
16+
| ---------- ^^^^^^^^^^^^^ - temporary value is freed at the end of this statement
17+
| | |
18+
| | creates a temporary value which is freed while still in use
19+
| type annotation requires that borrow lasts for `'static`
20+
21+
error[E0716]: temporary value dropped while borrowed
22+
--> $DIR/promoted_const_call2.rs:11:26
23+
|
24+
LL | let _: &'static _ = &id(&String::new());
25+
| ---------- ^^^^^^^^^^^^^^^^^^ creates a temporary value which is freed while still in use
26+
| |
27+
| type annotation requires that borrow lasts for `'static`
28+
...
29+
LL | }
30+
| - temporary value is freed at the end of this statement
31+
32+
error[E0716]: temporary value dropped while borrowed
33+
--> $DIR/promoted_const_call2.rs:11:30
34+
|
35+
LL | let _: &'static _ = &id(&String::new());
36+
| ---------- ^^^^^^^^^^^^^ - temporary value is freed at the end of this statement
37+
| | |
38+
| | creates a temporary value which is freed while still in use
39+
| type annotation requires that borrow lasts for `'static`
40+
41+
error[E0493]: destructor of `String` cannot be evaluated at compile-time
42+
--> $DIR/promoted_const_call2.rs:4:30
43+
|
44+
LL | let _: &'static _ = &id(&String::new());
45+
| ^^^^^^^^^^^^^ the destructor for this type cannot be evaluated in constants
46+
47+
error: aborting due to 5 previous errors
48+
49+
Some errors have detailed explanations: E0493, E0716.
50+
For more information about an error, try `rustc --explain E0493`.
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
pub const fn id<T>(x: T) -> T { x }
2+
pub const C: () = {
3+
let _: &'static _ = &String::new();
4+
//~^ ERROR: destructor of `String` cannot be evaluated at compile-time
5+
//~| ERROR: temporary value dropped while borrowed
6+
7+
let _: &'static _ = &id(&String::new());
8+
//~^ ERROR: destructor of `String` cannot be evaluated at compile-time
9+
//~| ERROR: temporary value dropped while borrowed
10+
//~| ERROR: temporary value dropped while borrowed
11+
12+
let _: &'static _ = &std::mem::ManuallyDrop::new(String::new());
13+
//~^ ERROR: temporary value dropped while borrowed
14+
};
15+
16+
fn main() {
17+
let _: &'static _ = &String::new();
18+
//~^ ERROR: temporary value dropped while borrowed
19+
20+
let _: &'static _ = &id(&String::new());
21+
//~^ ERROR: temporary value dropped while borrowed
22+
//~| ERROR: temporary value dropped while borrowed
23+
24+
let _: &'static _ = &std::mem::ManuallyDrop::new(String::new());
25+
//~^ ERROR: temporary value dropped while borrowed
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
error[E0493]: destructor of `String` cannot be evaluated at compile-time
2+
--> $DIR/promoted_const_call3.rs:7:30
3+
|
4+
LL | let _: &'static _ = &id(&String::new());
5+
| ^^^^^^^^^^^^^ - value is dropped here
6+
| |
7+
| the destructor for this type cannot be evaluated in constants
8+
9+
error[E0493]: destructor of `String` cannot be evaluated at compile-time
10+
--> $DIR/promoted_const_call3.rs:3:26
11+
|
12+
LL | let _: &'static _ = &String::new();
13+
| ^^^^^^^^^^^^^ the destructor for this type cannot be evaluated in constants
14+
...
15+
LL | };
16+
| - value is dropped here
17+
18+
error[E0716]: temporary value dropped while borrowed
19+
--> $DIR/promoted_const_call3.rs:3:26
20+
|
21+
LL | let _: &'static _ = &String::new();
22+
| ---------- ^^^^^^^^^^^^^ creates a temporary value which is freed while still in use
23+
| |
24+
| type annotation requires that borrow lasts for `'static`
25+
...
26+
LL | };
27+
| - temporary value is freed at the end of this statement
28+
29+
error[E0716]: temporary value dropped while borrowed
30+
--> $DIR/promoted_const_call3.rs:7:26
31+
|
32+
LL | let _: &'static _ = &id(&String::new());
33+
| ---------- ^^^^^^^^^^^^^^^^^^ creates a temporary value which is freed while still in use
34+
| |
35+
| type annotation requires that borrow lasts for `'static`
36+
...
37+
LL | };
38+
| - temporary value is freed at the end of this statement
39+
40+
error[E0716]: temporary value dropped while borrowed
41+
--> $DIR/promoted_const_call3.rs:7:30
42+
|
43+
LL | let _: &'static _ = &id(&String::new());
44+
| ---------- ^^^^^^^^^^^^^ - temporary value is freed at the end of this statement
45+
| | |
46+
| | creates a temporary value which is freed while still in use
47+
| type annotation requires that borrow lasts for `'static`
48+
49+
error[E0716]: temporary value dropped while borrowed
50+
--> $DIR/promoted_const_call3.rs:12:26
51+
|
52+
LL | let _: &'static _ = &std::mem::ManuallyDrop::new(String::new());
53+
| ---------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ creates a temporary value which is freed while still in use
54+
| |
55+
| type annotation requires that borrow lasts for `'static`
56+
LL |
57+
LL | };
58+
| - temporary value is freed at the end of this statement
59+
60+
error[E0716]: temporary value dropped while borrowed
61+
--> $DIR/promoted_const_call3.rs:17:26
62+
|
63+
LL | let _: &'static _ = &String::new();
64+
| ---------- ^^^^^^^^^^^^^ creates a temporary value which is freed while still in use
65+
| |
66+
| type annotation requires that borrow lasts for `'static`
67+
...
68+
LL | }
69+
| - temporary value is freed at the end of this statement
70+
71+
error[E0716]: temporary value dropped while borrowed
72+
--> $DIR/promoted_const_call3.rs:20:26
73+
|
74+
LL | let _: &'static _ = &id(&String::new());
75+
| ---------- ^^^^^^^^^^^^^^^^^^ creates a temporary value which is freed while still in use
76+
| |
77+
| type annotation requires that borrow lasts for `'static`
78+
...
79+
LL | }
80+
| - temporary value is freed at the end of this statement
81+
82+
error[E0716]: temporary value dropped while borrowed
83+
--> $DIR/promoted_const_call3.rs:20:30
84+
|
85+
LL | let _: &'static _ = &id(&String::new());
86+
| ---------- ^^^^^^^^^^^^^ - temporary value is freed at the end of this statement
87+
| | |
88+
| | creates a temporary value which is freed while still in use
89+
| type annotation requires that borrow lasts for `'static`
90+
91+
error[E0716]: temporary value dropped while borrowed
92+
--> $DIR/promoted_const_call3.rs:24:26
93+
|
94+
LL | let _: &'static _ = &std::mem::ManuallyDrop::new(String::new());
95+
| ---------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ creates a temporary value which is freed while still in use
96+
| |
97+
| type annotation requires that borrow lasts for `'static`
98+
LL |
99+
LL | }
100+
| - temporary value is freed at the end of this statement
101+
102+
error: aborting due to 10 previous errors
103+
104+
Some errors have detailed explanations: E0493, E0716.
105+
For more information about an error, try `rustc --explain E0493`.
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// run-pass
2+
3+
use std::sync::atomic::*;
4+
5+
static FLAG: AtomicBool = AtomicBool::new(false);
6+
7+
struct NoisyDrop(&'static str);
8+
impl Drop for NoisyDrop {
9+
fn drop(&mut self) {
10+
FLAG.store(true, Ordering::SeqCst);
11+
}
12+
}
13+
fn main() {
14+
{
15+
let _val = &&(NoisyDrop("drop!"), 0).1;
16+
}
17+
assert!(FLAG.load(Ordering::SeqCst));
18+
}
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#![feature(rustc_attrs)]
2+
#![feature(staged_api)]
3+
#![stable(feature = "a", since = "1.0.0")]
4+
5+
#[rustc_promotable]
6+
#[stable(feature = "a", since = "1.0.0")]
7+
#[rustc_const_stable(feature = "a", since = "1.0.0")]
8+
pub const fn id<T>(x: &'static T) -> &'static T { x }
9+
10+
#[rustc_promotable]
11+
#[stable(feature = "a", since = "1.0.0")]
12+
#[rustc_const_stable(feature = "a", since = "1.0.0")]
13+
pub const fn new_string() -> String {
14+
String::new()
15+
}
16+
17+
#[rustc_promotable]
18+
#[stable(feature = "a", since = "1.0.0")]
19+
#[rustc_const_stable(feature = "a", since = "1.0.0")]
20+
pub const fn new_manually_drop<T>(t: T) -> std::mem::ManuallyDrop<T> {
21+
std::mem::ManuallyDrop::new(t)
22+
}
23+
24+
25+
const C: () = {
26+
let _: &'static _ = &id(&new_string());
27+
//~^ ERROR destructor of `String` cannot be evaluated at compile-time
28+
//~| ERROR: temporary value dropped while borrowed
29+
//~| ERROR: temporary value dropped while borrowed
30+
31+
let _: &'static _ = &new_manually_drop(new_string());
32+
//~^ ERROR: temporary value dropped while borrowed
33+
};
34+
35+
fn main() {
36+
let _: &'static _ = &id(&new_string());
37+
//~^ ERROR: temporary value dropped while borrowed
38+
//~| ERROR: temporary value dropped while borrowed
39+
40+
let _: &'static _ = &new_manually_drop(new_string());
41+
//~^ ERROR: temporary value dropped while borrowed
42+
}

0 commit comments

Comments
 (0)