Check types of statics in MIR typeck

This commit is contained in:
Matthew Jasper 2020-02-13 20:29:30 +00:00
parent cd9f5ff2a1
commit ddc25456c5
5 changed files with 98 additions and 3 deletions

View file

@ -0,0 +1,30 @@
// Check that borrowck ensures that `static mut` items have the expected type.
static FOO: u8 = 42;
static mut BAR: &'static u8 = &FOO;
static mut BAR_ELIDED: &u8 = &FOO;
fn main() {
unsafe {
println!("{} {}", BAR, BAR_ELIDED);
set_bar();
set_bar_elided();
println!("{} {}", BAR, BAR_ELIDED);
}
}
fn set_bar() {
let n = 42;
unsafe {
BAR = &n;
//~^ ERROR does not live long enough
}
}
fn set_bar_elided() {
let n = 42;
unsafe {
BAR_ELIDED = &n;
//~^ ERROR does not live long enough
}
}

View file

@ -0,0 +1,27 @@
error[E0597]: `n` does not live long enough
--> $DIR/issue-69114-static-mut-ty.rs:19:15
|
LL | BAR = &n;
| ------^^
| | |
| | borrowed value does not live long enough
| assignment requires that `n` is borrowed for `'static`
...
LL | }
| - `n` dropped here while still borrowed
error[E0597]: `n` does not live long enough
--> $DIR/issue-69114-static-mut-ty.rs:27:22
|
LL | BAR_ELIDED = &n;
| -------------^^
| | |
| | borrowed value does not live long enough
| assignment requires that `n` is borrowed for `'static`
...
LL | }
| - `n` dropped here while still borrowed
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0597`.

View file

@ -0,0 +1,9 @@
// Check that borrowck ensures that `static` items have the expected type.
static FOO: &'static (dyn Fn(&'static u8) + Send + Sync) = &drop;
fn main() {
let n = 42;
FOO(&n);
//~^ ERROR does not live long enough
}

View file

@ -0,0 +1,15 @@
error[E0597]: `n` does not live long enough
--> $DIR/issue-69114-static-ty.rs:7:9
|
LL | FOO(&n);
| ----^^-
| | |
| | borrowed value does not live long enough
| argument requires that `n` is borrowed for `'static`
LL |
LL | }
| - `n` dropped here while still borrowed
error: aborting due to previous error
For more information about this error, try `rustc --explain E0597`.