Skip to content

Commit fb802f2

Browse files
committed
promote-not: add test that distinguishes promotion from outer scope rule
1 parent 279465b commit fb802f2

File tree

2 files changed

+42
-22
lines changed

2 files changed

+42
-22
lines changed

tests/ui/consts/promote-not.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,16 @@ const TEST_INTERIOR_MUT: () = {
4040
let _val: &'static _ = &(Cell::new(1), 2).1; //~ ERROR temporary value dropped while borrowed
4141
};
4242

43-
const TEST_DROP: String = String::new();
43+
// This gets accepted by the "outer scope" rule, not promotion.
44+
const TEST_DROP_OUTER_SCOPE: &String = &String::new();
45+
// To demonstrate that, we can rewrite it as follows. If this was promotion it would still work.
46+
const TEST_DROP_NOT_PROMOTE: &String = {
47+
let x = &String::new(); //~ ERROR destructor of `String` cannot be evaluated at compile-time
48+
// The "dropped while borrowed" error seems to be suppressed, but the key point is that this
49+
// fails to compile.
50+
x
51+
};
52+
4453

4554
fn main() {
4655
// We must not promote things with interior mutability. Not even if we "project it away".
@@ -59,6 +68,7 @@ fn main() {
5968
let _val: &'static _ = &([1,2,3][4]+1); //~ ERROR temporary value dropped while borrowed
6069

6170
// No promotion of temporaries that need to be dropped.
71+
const TEST_DROP: String = String::new();
6272
let _val: &'static _ = &TEST_DROP;
6373
//~^ ERROR temporary value dropped while borrowed
6474
let _val: &'static _ = &&TEST_DROP;

tests/ui/consts/promote-not.stderr

+31-21
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ LL | let _val: &'static _ = &(Cell::new(1), 2).1;
3838
LL | };
3939
| - temporary value is freed at the end of this statement
4040

41+
error[E0493]: destructor of `String` cannot be evaluated at compile-time
42+
--> $DIR/promote-not.rs:47:14
43+
|
44+
LL | let x = &String::new();
45+
| ^^^^^^^^^^^^^ the destructor for this type cannot be evaluated in constants
46+
...
47+
LL | };
48+
| - value is dropped here
49+
4150
error[E0716]: temporary value dropped while borrowed
4251
--> $DIR/promote-not.rs:21:32
4352
|
@@ -59,7 +68,7 @@ LL | }
5968
| - temporary value is freed at the end of this statement
6069

6170
error[E0716]: temporary value dropped while borrowed
62-
--> $DIR/promote-not.rs:47:29
71+
--> $DIR/promote-not.rs:56:29
6372
|
6473
LL | let _val: &'static _ = &(Cell::new(1), 2).0;
6574
| ---------- ^^^^^^^^^^^^^^^^^ creates a temporary value which is freed while still in use
@@ -70,7 +79,7 @@ LL | }
7079
| - temporary value is freed at the end of this statement
7180

7281
error[E0716]: temporary value dropped while borrowed
73-
--> $DIR/promote-not.rs:48:29
82+
--> $DIR/promote-not.rs:57:29
7483
|
7584
LL | let _val: &'static _ = &(Cell::new(1), 2).1;
7685
| ---------- ^^^^^^^^^^^^^^^^^ creates a temporary value which is freed while still in use
@@ -81,7 +90,7 @@ LL | }
8190
| - temporary value is freed at the end of this statement
8291

8392
error[E0716]: temporary value dropped while borrowed
84-
--> $DIR/promote-not.rs:51:29
93+
--> $DIR/promote-not.rs:60:29
8594
|
8695
LL | let _val: &'static _ = &(1/0);
8796
| ---------- ^^^^^ creates a temporary value which is freed while still in use
@@ -92,7 +101,7 @@ LL | }
92101
| - temporary value is freed at the end of this statement
93102

94103
error[E0716]: temporary value dropped while borrowed
95-
--> $DIR/promote-not.rs:52:29
104+
--> $DIR/promote-not.rs:61:29
96105
|
97106
LL | let _val: &'static _ = &(1/(1-1));
98107
| ---------- ^^^^^^^^^ creates a temporary value which is freed while still in use
@@ -103,7 +112,7 @@ LL | }
103112
| - temporary value is freed at the end of this statement
104113

105114
error[E0716]: temporary value dropped while borrowed
106-
--> $DIR/promote-not.rs:53:29
115+
--> $DIR/promote-not.rs:62:29
107116
|
108117
LL | let _val: &'static _ = &((1+1)/(1-1));
109118
| ---------- ^^^^^^^^^^^^^ creates a temporary value which is freed while still in use
@@ -114,7 +123,7 @@ LL | }
114123
| - temporary value is freed at the end of this statement
115124

116125
error[E0716]: temporary value dropped while borrowed
117-
--> $DIR/promote-not.rs:54:29
126+
--> $DIR/promote-not.rs:63:29
118127
|
119128
LL | let _val: &'static _ = &(i32::MIN/-1);
120129
| ---------- ^^^^^^^^^^^^^ creates a temporary value which is freed while still in use
@@ -125,7 +134,7 @@ LL | }
125134
| - temporary value is freed at the end of this statement
126135

127136
error[E0716]: temporary value dropped while borrowed
128-
--> $DIR/promote-not.rs:55:29
137+
--> $DIR/promote-not.rs:64:29
129138
|
130139
LL | let _val: &'static _ = &(i32::MIN/(0-1));
131140
| ---------- ^^^^^^^^^^^^^^^^ creates a temporary value which is freed while still in use
@@ -136,7 +145,7 @@ LL | }
136145
| - temporary value is freed at the end of this statement
137146

138147
error[E0716]: temporary value dropped while borrowed
139-
--> $DIR/promote-not.rs:56:29
148+
--> $DIR/promote-not.rs:65:29
140149
|
141150
LL | let _val: &'static _ = &(-128i8/-1);
142151
| ---------- ^^^^^^^^^^^ creates a temporary value which is freed while still in use
@@ -147,7 +156,7 @@ LL | }
147156
| - temporary value is freed at the end of this statement
148157

149158
error[E0716]: temporary value dropped while borrowed
150-
--> $DIR/promote-not.rs:57:29
159+
--> $DIR/promote-not.rs:66:29
151160
|
152161
LL | let _val: &'static _ = &(1%0);
153162
| ---------- ^^^^^ creates a temporary value which is freed while still in use
@@ -158,7 +167,7 @@ LL | }
158167
| - temporary value is freed at the end of this statement
159168

160169
error[E0716]: temporary value dropped while borrowed
161-
--> $DIR/promote-not.rs:58:29
170+
--> $DIR/promote-not.rs:67:29
162171
|
163172
LL | let _val: &'static _ = &(1%(1-1));
164173
| ---------- ^^^^^^^^^ creates a temporary value which is freed while still in use
@@ -169,7 +178,7 @@ LL | }
169178
| - temporary value is freed at the end of this statement
170179

171180
error[E0716]: temporary value dropped while borrowed
172-
--> $DIR/promote-not.rs:59:29
181+
--> $DIR/promote-not.rs:68:29
173182
|
174183
LL | let _val: &'static _ = &([1,2,3][4]+1);
175184
| ---------- ^^^^^^^^^^^^^^ creates a temporary value which is freed while still in use
@@ -180,7 +189,7 @@ LL | }
180189
| - temporary value is freed at the end of this statement
181190

182191
error[E0716]: temporary value dropped while borrowed
183-
--> $DIR/promote-not.rs:62:29
192+
--> $DIR/promote-not.rs:72:29
184193
|
185194
LL | let _val: &'static _ = &TEST_DROP;
186195
| ---------- ^^^^^^^^^ creates a temporary value which is freed while still in use
@@ -191,7 +200,7 @@ LL | }
191200
| - temporary value is freed at the end of this statement
192201

193202
error[E0716]: temporary value dropped while borrowed
194-
--> $DIR/promote-not.rs:64:29
203+
--> $DIR/promote-not.rs:74:29
195204
|
196205
LL | let _val: &'static _ = &&TEST_DROP;
197206
| ---------- ^^^^^^^^^^ creates a temporary value which is freed while still in use
@@ -202,7 +211,7 @@ LL | }
202211
| - temporary value is freed at the end of this statement
203212

204213
error[E0716]: temporary value dropped while borrowed
205-
--> $DIR/promote-not.rs:64:30
214+
--> $DIR/promote-not.rs:74:30
206215
|
207216
LL | let _val: &'static _ = &&TEST_DROP;
208217
| ---------- ^^^^^^^^^ creates a temporary value which is freed while still in use
@@ -213,7 +222,7 @@ LL | }
213222
| - temporary value is freed at the end of this statement
214223

215224
error[E0716]: temporary value dropped while borrowed
216-
--> $DIR/promote-not.rs:67:29
225+
--> $DIR/promote-not.rs:77:29
217226
|
218227
LL | let _val: &'static _ = &(&TEST_DROP,);
219228
| ---------- ^^^^^^^^^^^^^ creates a temporary value which is freed while still in use
@@ -224,7 +233,7 @@ LL | }
224233
| - temporary value is freed at the end of this statement
225234

226235
error[E0716]: temporary value dropped while borrowed
227-
--> $DIR/promote-not.rs:67:31
236+
--> $DIR/promote-not.rs:77:31
228237
|
229238
LL | let _val: &'static _ = &(&TEST_DROP,);
230239
| ---------- ^^^^^^^^^ creates a temporary value which is freed while still in use
@@ -235,7 +244,7 @@ LL | }
235244
| - temporary value is freed at the end of this statement
236245

237246
error[E0716]: temporary value dropped while borrowed
238-
--> $DIR/promote-not.rs:70:29
247+
--> $DIR/promote-not.rs:80:29
239248
|
240249
LL | let _val: &'static _ = &[&TEST_DROP; 1];
241250
| ---------- ^^^^^^^^^^^^^^^ creates a temporary value which is freed while still in use
@@ -246,7 +255,7 @@ LL | }
246255
| - temporary value is freed at the end of this statement
247256

248257
error[E0716]: temporary value dropped while borrowed
249-
--> $DIR/promote-not.rs:70:31
258+
--> $DIR/promote-not.rs:80:31
250259
|
251260
LL | let _val: &'static _ = &[&TEST_DROP; 1];
252261
| ---------- ^^^^^^^^^ - temporary value is freed at the end of this statement
@@ -255,7 +264,7 @@ LL | let _val: &'static _ = &[&TEST_DROP; 1];
255264
| type annotation requires that borrow lasts for `'static`
256265

257266
error[E0716]: temporary value dropped while borrowed
258-
--> $DIR/promote-not.rs:79:26
267+
--> $DIR/promote-not.rs:89:26
259268
|
260269
LL | let x: &'static _ = &UnionWithCell { f1: 0 };
261270
| ---------- ^^^^^^^^^^^^^^^^^^^^^^^ creates a temporary value which is freed while still in use
@@ -265,6 +274,7 @@ LL |
265274
LL | }
266275
| - temporary value is freed at the end of this statement
267276

268-
error: aborting due to 25 previous errors
277+
error: aborting due to 26 previous errors
269278

270-
For more information about this error, try `rustc --explain E0716`.
279+
Some errors have detailed explanations: E0493, E0716.
280+
For more information about an error, try `rustc --explain E0493`.

0 commit comments

Comments
 (0)