Skip to content

Commit

Permalink
promote-not: add test that distinguishes promotion from outer scope rule
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Mar 11, 2024
1 parent 279465b commit fb802f2
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 22 deletions.
12 changes: 11 additions & 1 deletion tests/ui/consts/promote-not.rs
Expand Up @@ -40,7 +40,16 @@ const TEST_INTERIOR_MUT: () = {
let _val: &'static _ = &(Cell::new(1), 2).1; //~ ERROR temporary value dropped while borrowed
};

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


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

// No promotion of temporaries that need to be dropped.
const TEST_DROP: String = String::new();
let _val: &'static _ = &TEST_DROP;
//~^ ERROR temporary value dropped while borrowed
let _val: &'static _ = &&TEST_DROP;
Expand Down
52 changes: 31 additions & 21 deletions tests/ui/consts/promote-not.stderr
Expand Up @@ -38,6 +38,15 @@ LL | let _val: &'static _ = &(Cell::new(1), 2).1;
LL | };
| - temporary value is freed at the end of this statement

error[E0493]: destructor of `String` cannot be evaluated at compile-time
--> $DIR/promote-not.rs:47:14
|
LL | let x = &String::new();
| ^^^^^^^^^^^^^ the destructor for this type cannot be evaluated in constants
...
LL | };
| - value is dropped here

error[E0716]: temporary value dropped while borrowed
--> $DIR/promote-not.rs:21:32
|
Expand All @@ -59,7 +68,7 @@ LL | }
| - temporary value is freed at the end of this statement

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

error: aborting due to 25 previous errors
error: aborting due to 26 previous errors

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

0 comments on commit fb802f2

Please sign in to comment.