Skip to content

Commit 805fcb6

Browse files
Rollup merge of rust-lang#55262 - oli-obk:dangling_alloc_id_ice, r=RalfJung
Change the ICE from rust-lang#55223 to a hard error cc @SimonSapin r? @RalfJung
2 parents b3740b1 + 599eb12 commit 805fcb6

8 files changed

+107
-0
lines changed

src/librustc_mir/interpret/memory.rs

+5
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,11 @@ where
737737
if self.alloc_map.contains_key(&alloc) {
738738
// Not yet interned, so proceed recursively
739739
self.intern_static(alloc, mutability)?;
740+
} else if self.dead_alloc_map.contains_key(&alloc) {
741+
// dangling pointer
742+
return err!(ValidationFailure(
743+
"encountered dangling pointer in final constant".into(),
744+
))
740745
}
741746
}
742747
Ok(())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
warning[E0716]: temporary value dropped while borrowed
2+
--> $DIR/dangling-alloc-id-ice-2.rs:5:28
3+
|
4+
LL | static MAP: Slice = Slice(&[
5+
| ___________________________-^
6+
| |___________________________|
7+
| ||
8+
LL | || b"CloseEvent" as &'static [u8],
9+
LL | || ]);
10+
| || -- temporary value is freed at the end of this statement
11+
| ||_|
12+
| |__creates a temporary which is freed while still in use
13+
| cast requires that borrow lasts for `'static`
14+
|
15+
= warning: This error has been downgraded to a warning for backwards compatibility with previous releases.
16+
It represents potential unsoundness in your code.
17+
This warning will become a hard error in the future.
18+
19+
error[E0080]: could not evaluate static initializer
20+
--> $DIR/dangling-alloc-id-ice-2.rs:5:1
21+
|
22+
LL | / static MAP: Slice = Slice(&[
23+
LL | | b"CloseEvent" as &'static [u8],
24+
LL | | ]);
25+
| |___^ type validation failed: encountered dangling pointer in final constant
26+
27+
error: aborting due to previous error
28+
29+
Some errors occurred: E0080, E0716.
30+
For more information about an error, try `rustc --explain E0080`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// FIXME(#55223) this is just a reproduction test showing the wrong behavior
2+
3+
struct Slice(&'static [&'static [u8]]);
4+
5+
static MAP: Slice = Slice(&[
6+
b"CloseEvent" as &'static [u8],
7+
]);
8+
9+
10+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0080]: could not evaluate static initializer
2+
--> $DIR/dangling-alloc-id-ice-2.rs:5:1
3+
|
4+
LL | / static MAP: Slice = Slice(&[
5+
LL | | b"CloseEvent" as &'static [u8],
6+
LL | | ]);
7+
| |___^ type validation failed: encountered dangling pointer in final constant
8+
9+
error: aborting due to previous error
10+
11+
For more information about this error, try `rustc --explain E0080`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// https://github.com/rust-lang/rust/issues/55223
2+
3+
#![feature(const_let)]
4+
5+
union Foo<'a> {
6+
y: &'a (),
7+
long_live_the_unit: &'static (),
8+
}
9+
10+
const FOO: &() = { //~ ERROR any use of this value will cause an error
11+
let y = ();
12+
unsafe { Foo { y: &y }.long_live_the_unit }
13+
};
14+
15+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error: any use of this value will cause an error
2+
--> $DIR/dangling-alloc-id-ice.rs:10:1
3+
|
4+
LL | / const FOO: &() = { //~ ERROR any use of this value will cause an error
5+
LL | | let y = ();
6+
LL | | unsafe { Foo { y: &y }.long_live_the_unit }
7+
LL | | };
8+
| |__^ type validation failed: encountered dangling pointer in final constant
9+
|
10+
= note: #[deny(const_err)] on by default
11+
12+
error: aborting due to previous error
13+
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![feature(const_let)]
2+
3+
const FOO: *const u32 = { //~ ERROR any use of this value will cause an error
4+
let x = 42;
5+
&x
6+
};
7+
8+
fn main() {
9+
let x = FOO;
10+
}
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error: any use of this value will cause an error
2+
--> $DIR/dangling_raw_ptr.rs:3:1
3+
|
4+
LL | / const FOO: *const u32 = { //~ ERROR any use of this value will cause an error
5+
LL | | let x = 42;
6+
LL | | &x
7+
LL | | };
8+
| |__^ type validation failed: encountered dangling pointer in final constant
9+
|
10+
= note: #[deny(const_err)] on by default
11+
12+
error: aborting due to previous error
13+

0 commit comments

Comments
 (0)