Skip to content

Commit ddd4b19

Browse files
committed
Auto merge of #55262 - oli-obk:dangling_alloc_id_ice, r=RalfJung
Change the ICE from #55223 to a hard error cc @SimonSapin r? @RalfJung fixes #55287
2 parents 15d7704 + e70b634 commit ddd4b19

File tree

5 files changed

+56
-0
lines changed

5 files changed

+56
-0
lines changed

src/librustc_mir/interpret/memory.rs

+5
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,11 @@ where
730730
if self.alloc_map.contains_key(&alloc) {
731731
// Not yet interned, so proceed recursively
732732
self.intern_static(alloc, mutability)?;
733+
} else if self.dead_alloc_map.contains_key(&alloc) {
734+
// dangling pointer
735+
return err!(ValidationFailure(
736+
"encountered dangling pointer in final constant".into(),
737+
))
733738
}
734739
}
735740
Ok(())
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)