Skip to content

Commit 6a95896

Browse files
committed
add comment and test: we do not do value-based reasoning for promotion of unions
1 parent d3d145e commit 6a95896

File tree

3 files changed

+46
-25
lines changed

3 files changed

+46
-25
lines changed

Diff for: compiler/rustc_const_eval/src/transform/check_consts/qualifs.rs

+1
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ where
284284
if Q::in_adt_inherently(cx, def, args) {
285285
return true;
286286
}
287+
// Don't do any value-based reasoning for unions.
287288
if def.is_union() && Q::in_any_value_of_ty(cx, rvalue.ty(cx.body, cx.tcx)) {
288289
return true;
289290
}

Diff for: tests/ui/consts/promote-not.rs

+9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#![allow(unconditional_panic)]
44

55
use std::cell::Cell;
6+
use std::mem::ManuallyDrop;
67

78
// We do not promote mutable references.
89
static mut TEST1: Option<&mut [i32]> = Some(&mut [1, 2, 3]); //~ ERROR temporary value dropped while borrowed
@@ -69,4 +70,12 @@ fn main() {
6970
let _val: &'static _ = &[&TEST_DROP; 1];
7071
//~^ ERROR temporary value dropped while borrowed
7172
//~| ERROR temporary value dropped while borrowed
73+
74+
// Make sure there is no value-based reasoning for unions.
75+
union UnionWithCell {
76+
f1: i32,
77+
f2: ManuallyDrop<Cell<i32>>,
78+
}
79+
let x: &'static _ = &UnionWithCell { f1: 0 };
80+
//~^ ERROR temporary value dropped while borrowed
7281
}

Diff for: tests/ui/consts/promote-not.stderr

+36-25
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0716]: temporary value dropped while borrowed
2-
--> $DIR/promote-not.rs:8:50
2+
--> $DIR/promote-not.rs:9:50
33
|
44
LL | static mut TEST1: Option<&mut [i32]> = Some(&mut [1, 2, 3]);
55
| ----------^^^^^^^^^-
@@ -9,7 +9,7 @@ LL | static mut TEST1: Option<&mut [i32]> = Some(&mut [1, 2, 3]);
99
| using this value as a static requires that borrow lasts for `'static`
1010

1111
error[E0716]: temporary value dropped while borrowed
12-
--> $DIR/promote-not.rs:11:18
12+
--> $DIR/promote-not.rs:12:18
1313
|
1414
LL | let x = &mut [1,2,3];
1515
| ^^^^^^^ creates a temporary value which is freed while still in use
@@ -19,7 +19,7 @@ LL | };
1919
| - temporary value is freed at the end of this statement
2020

2121
error[E0716]: temporary value dropped while borrowed
22-
--> $DIR/promote-not.rs:33:29
22+
--> $DIR/promote-not.rs:34:29
2323
|
2424
LL | let _x: &'static i32 = &unsafe { U { x: 0 }.x };
2525
| ------------ ^^^^^^^^^^^^^^^^^^^^^^^ creates a temporary value which is freed while still in use
@@ -29,7 +29,7 @@ LL | };
2929
| - temporary value is freed at the end of this statement
3030

3131
error[E0716]: temporary value dropped while borrowed
32-
--> $DIR/promote-not.rs:39:29
32+
--> $DIR/promote-not.rs:40:29
3333
|
3434
LL | let _val: &'static _ = &(Cell::new(1), 2).1;
3535
| ---------- ^^^^^^^^^^^^^^^^^ creates a temporary value which is freed while still in use
@@ -39,7 +39,7 @@ LL | };
3939
| - temporary value is freed at the end of this statement
4040

4141
error[E0716]: temporary value dropped while borrowed
42-
--> $DIR/promote-not.rs:20:32
42+
--> $DIR/promote-not.rs:21:32
4343
|
4444
LL | let _x: &'static () = &foo();
4545
| ----------- ^^^^^ creates a temporary value which is freed while still in use
@@ -49,7 +49,7 @@ LL | }
4949
| - temporary value is freed at the end of this statement
5050

5151
error[E0716]: temporary value dropped while borrowed
52-
--> $DIR/promote-not.rs:28:29
52+
--> $DIR/promote-not.rs:29:29
5353
|
5454
LL | let _x: &'static i32 = &unsafe { U { x: 0 }.x };
5555
| ------------ ^^^^^^^^^^^^^^^^^^^^^^^ creates a temporary value which is freed while still in use
@@ -59,7 +59,7 @@ LL | }
5959
| - temporary value is freed at the end of this statement
6060

6161
error[E0716]: temporary value dropped while borrowed
62-
--> $DIR/promote-not.rs:46:29
62+
--> $DIR/promote-not.rs:47:29
6363
|
6464
LL | let _val: &'static _ = &(Cell::new(1), 2).0;
6565
| ---------- ^^^^^^^^^^^^^^^^^ creates a temporary value which is freed while still in use
@@ -70,7 +70,7 @@ LL | }
7070
| - temporary value is freed at the end of this statement
7171

7272
error[E0716]: temporary value dropped while borrowed
73-
--> $DIR/promote-not.rs:47:29
73+
--> $DIR/promote-not.rs:48:29
7474
|
7575
LL | let _val: &'static _ = &(Cell::new(1), 2).1;
7676
| ---------- ^^^^^^^^^^^^^^^^^ creates a temporary value which is freed while still in use
@@ -81,7 +81,7 @@ LL | }
8181
| - temporary value is freed at the end of this statement
8282

8383
error[E0716]: temporary value dropped while borrowed
84-
--> $DIR/promote-not.rs:50:29
84+
--> $DIR/promote-not.rs:51:29
8585
|
8686
LL | let _val: &'static _ = &(1/0);
8787
| ---------- ^^^^^ creates a temporary value which is freed while still in use
@@ -92,7 +92,7 @@ LL | }
9292
| - temporary value is freed at the end of this statement
9393

9494
error[E0716]: temporary value dropped while borrowed
95-
--> $DIR/promote-not.rs:51:29
95+
--> $DIR/promote-not.rs:52:29
9696
|
9797
LL | let _val: &'static _ = &(1/(1-1));
9898
| ---------- ^^^^^^^^^ creates a temporary value which is freed while still in use
@@ -103,7 +103,7 @@ LL | }
103103
| - temporary value is freed at the end of this statement
104104

105105
error[E0716]: temporary value dropped while borrowed
106-
--> $DIR/promote-not.rs:52:29
106+
--> $DIR/promote-not.rs:53:29
107107
|
108108
LL | let _val: &'static _ = &((1+1)/(1-1));
109109
| ---------- ^^^^^^^^^^^^^ creates a temporary value which is freed while still in use
@@ -114,7 +114,7 @@ LL | }
114114
| - temporary value is freed at the end of this statement
115115

116116
error[E0716]: temporary value dropped while borrowed
117-
--> $DIR/promote-not.rs:53:29
117+
--> $DIR/promote-not.rs:54:29
118118
|
119119
LL | let _val: &'static _ = &(i32::MIN/-1);
120120
| ---------- ^^^^^^^^^^^^^ creates a temporary value which is freed while still in use
@@ -125,7 +125,7 @@ LL | }
125125
| - temporary value is freed at the end of this statement
126126

127127
error[E0716]: temporary value dropped while borrowed
128-
--> $DIR/promote-not.rs:54:29
128+
--> $DIR/promote-not.rs:55:29
129129
|
130130
LL | let _val: &'static _ = &(i32::MIN/(0-1));
131131
| ---------- ^^^^^^^^^^^^^^^^ creates a temporary value which is freed while still in use
@@ -136,7 +136,7 @@ LL | }
136136
| - temporary value is freed at the end of this statement
137137

138138
error[E0716]: temporary value dropped while borrowed
139-
--> $DIR/promote-not.rs:55:29
139+
--> $DIR/promote-not.rs:56:29
140140
|
141141
LL | let _val: &'static _ = &(-128i8/-1);
142142
| ---------- ^^^^^^^^^^^ creates a temporary value which is freed while still in use
@@ -147,7 +147,7 @@ LL | }
147147
| - temporary value is freed at the end of this statement
148148

149149
error[E0716]: temporary value dropped while borrowed
150-
--> $DIR/promote-not.rs:56:29
150+
--> $DIR/promote-not.rs:57:29
151151
|
152152
LL | let _val: &'static _ = &(1%0);
153153
| ---------- ^^^^^ creates a temporary value which is freed while still in use
@@ -158,7 +158,7 @@ LL | }
158158
| - temporary value is freed at the end of this statement
159159

160160
error[E0716]: temporary value dropped while borrowed
161-
--> $DIR/promote-not.rs:57:29
161+
--> $DIR/promote-not.rs:58:29
162162
|
163163
LL | let _val: &'static _ = &(1%(1-1));
164164
| ---------- ^^^^^^^^^ creates a temporary value which is freed while still in use
@@ -169,7 +169,7 @@ LL | }
169169
| - temporary value is freed at the end of this statement
170170

171171
error[E0716]: temporary value dropped while borrowed
172-
--> $DIR/promote-not.rs:58:29
172+
--> $DIR/promote-not.rs:59:29
173173
|
174174
LL | let _val: &'static _ = &([1,2,3][4]+1);
175175
| ---------- ^^^^^^^^^^^^^^ creates a temporary value which is freed while still in use
@@ -180,7 +180,7 @@ LL | }
180180
| - temporary value is freed at the end of this statement
181181

182182
error[E0716]: temporary value dropped while borrowed
183-
--> $DIR/promote-not.rs:61:29
183+
--> $DIR/promote-not.rs:62:29
184184
|
185185
LL | let _val: &'static _ = &TEST_DROP;
186186
| ---------- ^^^^^^^^^ creates a temporary value which is freed while still in use
@@ -191,7 +191,7 @@ LL | }
191191
| - temporary value is freed at the end of this statement
192192

193193
error[E0716]: temporary value dropped while borrowed
194-
--> $DIR/promote-not.rs:63:29
194+
--> $DIR/promote-not.rs:64:29
195195
|
196196
LL | let _val: &'static _ = &&TEST_DROP;
197197
| ---------- ^^^^^^^^^^ creates a temporary value which is freed while still in use
@@ -202,7 +202,7 @@ LL | }
202202
| - temporary value is freed at the end of this statement
203203

204204
error[E0716]: temporary value dropped while borrowed
205-
--> $DIR/promote-not.rs:63:30
205+
--> $DIR/promote-not.rs:64:30
206206
|
207207
LL | let _val: &'static _ = &&TEST_DROP;
208208
| ---------- ^^^^^^^^^ creates a temporary value which is freed while still in use
@@ -213,7 +213,7 @@ LL | }
213213
| - temporary value is freed at the end of this statement
214214

215215
error[E0716]: temporary value dropped while borrowed
216-
--> $DIR/promote-not.rs:66:29
216+
--> $DIR/promote-not.rs:67:29
217217
|
218218
LL | let _val: &'static _ = &(&TEST_DROP,);
219219
| ---------- ^^^^^^^^^^^^^ creates a temporary value which is freed while still in use
@@ -224,7 +224,7 @@ LL | }
224224
| - temporary value is freed at the end of this statement
225225

226226
error[E0716]: temporary value dropped while borrowed
227-
--> $DIR/promote-not.rs:66:31
227+
--> $DIR/promote-not.rs:67:31
228228
|
229229
LL | let _val: &'static _ = &(&TEST_DROP,);
230230
| ---------- ^^^^^^^^^ creates a temporary value which is freed while still in use
@@ -235,7 +235,7 @@ LL | }
235235
| - temporary value is freed at the end of this statement
236236

237237
error[E0716]: temporary value dropped while borrowed
238-
--> $DIR/promote-not.rs:69:29
238+
--> $DIR/promote-not.rs:70:29
239239
|
240240
LL | let _val: &'static _ = &[&TEST_DROP; 1];
241241
| ---------- ^^^^^^^^^^^^^^^ creates a temporary value which is freed while still in use
@@ -246,14 +246,25 @@ LL | }
246246
| - temporary value is freed at the end of this statement
247247

248248
error[E0716]: temporary value dropped while borrowed
249-
--> $DIR/promote-not.rs:69:31
249+
--> $DIR/promote-not.rs:70:31
250250
|
251251
LL | let _val: &'static _ = &[&TEST_DROP; 1];
252252
| ---------- ^^^^^^^^^ - temporary value is freed at the end of this statement
253253
| | |
254254
| | creates a temporary value which is freed while still in use
255255
| type annotation requires that borrow lasts for `'static`
256256

257-
error: aborting due to 24 previous errors
257+
error[E0716]: temporary value dropped while borrowed
258+
--> $DIR/promote-not.rs:79:26
259+
|
260+
LL | let x: &'static _ = &UnionWithCell { f1: 0 };
261+
| ---------- ^^^^^^^^^^^^^^^^^^^^^^^ creates a temporary value which is freed while still in use
262+
| |
263+
| type annotation requires that borrow lasts for `'static`
264+
LL |
265+
LL | }
266+
| - temporary value is freed at the end of this statement
267+
268+
error: aborting due to 25 previous errors
258269

259270
For more information about this error, try `rustc --explain E0716`.

0 commit comments

Comments
 (0)