Skip to content

Commit fadb9cc

Browse files
committed
Test that promotion follows references when looking for drop
Noticed that this wasn't covered by any of existing tests. The const checking and const qualification, which currently shares the implementation with promotion, will likely need a different behaviour here (see issue rust-lang#90193).
1 parent 38b01d9 commit fadb9cc

File tree

2 files changed

+100
-9
lines changed

2 files changed

+100
-9
lines changed

src/test/ui/consts/promote-not.rs

+15
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ const TEST_INTERIOR_MUT: () = {
3939
let _val: &'static _ = &(Cell::new(1), 2).1; //~ ERROR temporary value dropped while borrowed
4040
};
4141

42+
const TEST_DROP: String = String::new();
43+
4244
fn main() {
4345
// We must not promote things with interior mutability. Not even if we "project it away".
4446
let _val: &'static _ = &(Cell::new(1), 2).0; //~ ERROR temporary value dropped while borrowed
@@ -50,4 +52,17 @@ fn main() {
5052
let _val: &'static _ = &(1%0); //~ ERROR temporary value dropped while borrowed
5153
let _val: &'static _ = &(1%(1-1)); //~ ERROR temporary value dropped while borrowed
5254
let _val: &'static _ = &([1,2,3][4]+1); //~ ERROR temporary value dropped while borrowed
55+
56+
// No promotion of temporaries that need to be dropped.
57+
let _val: &'static _ = &TEST_DROP;
58+
//~^ ERROR temporary value dropped while borrowed
59+
let _val: &'static _ = &&TEST_DROP;
60+
//~^ ERROR temporary value dropped while borrowed
61+
//~| ERROR temporary value dropped while borrowed
62+
let _val: &'static _ = &(&TEST_DROP,);
63+
//~^ ERROR temporary value dropped while borrowed
64+
//~| ERROR temporary value dropped while borrowed
65+
let _val: &'static _ = &[&TEST_DROP; 1];
66+
//~^ ERROR temporary value dropped while borrowed
67+
//~| ERROR temporary value dropped while borrowed
5368
}

src/test/ui/consts/promote-not.stderr

+85-9
Original file line numberDiff line numberDiff line change
@@ -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:44:29
62+
--> $DIR/promote-not.rs:46:29
6363
|
6464
LL | let _val: &'static _ = &(Cell::new(1), 2).0;
6565
| ---------- ^^^^^^^^^^^^^^^^^ creates a temporary 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:45:29
73+
--> $DIR/promote-not.rs:47:29
7474
|
7575
LL | let _val: &'static _ = &(Cell::new(1), 2).1;
7676
| ---------- ^^^^^^^^^^^^^^^^^ creates a temporary 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:48:29
84+
--> $DIR/promote-not.rs:50:29
8585
|
8686
LL | let _val: &'static _ = &(1/0);
8787
| ---------- ^^^^^ creates a temporary 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:49:29
95+
--> $DIR/promote-not.rs:51:29
9696
|
9797
LL | let _val: &'static _ = &(1/(1-1));
9898
| ---------- ^^^^^^^^^ creates a temporary 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:50:29
106+
--> $DIR/promote-not.rs:52:29
107107
|
108108
LL | let _val: &'static _ = &(1%0);
109109
| ---------- ^^^^^ creates a temporary which is freed while still in use
@@ -114,26 +114,102 @@ 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:51:29
117+
--> $DIR/promote-not.rs:53:29
118118
|
119119
LL | let _val: &'static _ = &(1%(1-1));
120120
| ---------- ^^^^^^^^^ creates a temporary which is freed while still in use
121121
| |
122122
| type annotation requires that borrow lasts for `'static`
123-
LL | let _val: &'static _ = &([1,2,3][4]+1);
123+
...
124124
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:52:29
128+
--> $DIR/promote-not.rs:54:29
129129
|
130130
LL | let _val: &'static _ = &([1,2,3][4]+1);
131131
| ---------- ^^^^^^^^^^^^^^ creates a temporary which is freed while still in use
132132
| |
133133
| type annotation requires that borrow lasts for `'static`
134+
...
135+
LL | }
136+
| - temporary value is freed at the end of this statement
137+
138+
error[E0716]: temporary value dropped while borrowed
139+
--> $DIR/promote-not.rs:57:29
140+
|
141+
LL | let _val: &'static _ = &TEST_DROP;
142+
| ---------- ^^^^^^^^^ creates a temporary which is freed while still in use
143+
| |
144+
| type annotation requires that borrow lasts for `'static`
145+
...
146+
LL | }
147+
| - temporary value is freed at the end of this statement
148+
149+
error[E0716]: temporary value dropped while borrowed
150+
--> $DIR/promote-not.rs:59:29
151+
|
152+
LL | let _val: &'static _ = &&TEST_DROP;
153+
| ---------- ^^^^^^^^^^ creates a temporary which is freed while still in use
154+
| |
155+
| type annotation requires that borrow lasts for `'static`
156+
...
157+
LL | }
158+
| - temporary value is freed at the end of this statement
159+
160+
error[E0716]: temporary value dropped while borrowed
161+
--> $DIR/promote-not.rs:59:30
162+
|
163+
LL | let _val: &'static _ = &&TEST_DROP;
164+
| ---------- ^^^^^^^^^ creates a temporary which is freed while still in use
165+
| |
166+
| type annotation requires that borrow lasts for `'static`
167+
...
168+
LL | }
169+
| - temporary value is freed at the end of this statement
170+
171+
error[E0716]: temporary value dropped while borrowed
172+
--> $DIR/promote-not.rs:62:29
173+
|
174+
LL | let _val: &'static _ = &(&TEST_DROP,);
175+
| ---------- ^^^^^^^^^^^^^ creates a temporary which is freed while still in use
176+
| |
177+
| type annotation requires that borrow lasts for `'static`
178+
...
134179
LL | }
135180
| - temporary value is freed at the end of this statement
136181

137-
error: aborting due to 13 previous errors
182+
error[E0716]: temporary value dropped while borrowed
183+
--> $DIR/promote-not.rs:62:31
184+
|
185+
LL | let _val: &'static _ = &(&TEST_DROP,);
186+
| ---------- ^^^^^^^^^ creates a temporary which is freed while still in use
187+
| |
188+
| type annotation requires that borrow lasts for `'static`
189+
...
190+
LL | }
191+
| - temporary value is freed at the end of this statement
192+
193+
error[E0716]: temporary value dropped while borrowed
194+
--> $DIR/promote-not.rs:65:29
195+
|
196+
LL | let _val: &'static _ = &[&TEST_DROP; 1];
197+
| ---------- ^^^^^^^^^^^^^^^ creates a temporary which is freed while still in use
198+
| |
199+
| type annotation requires that borrow lasts for `'static`
200+
...
201+
LL | }
202+
| - temporary value is freed at the end of this statement
203+
204+
error[E0716]: temporary value dropped while borrowed
205+
--> $DIR/promote-not.rs:65:31
206+
|
207+
LL | let _val: &'static _ = &[&TEST_DROP; 1];
208+
| ---------- ^^^^^^^^^ - temporary value is freed at the end of this statement
209+
| | |
210+
| | creates a temporary which is freed while still in use
211+
| type annotation requires that borrow lasts for `'static`
212+
213+
error: aborting due to 20 previous errors
138214

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

0 commit comments

Comments
 (0)