Skip to content

Commit 9f0a352

Browse files
committed
Turn ICE for dangling pointers into error
1 parent a66dc8a commit 9f0a352

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

src/librustc_mir/interpret/memory.rs

+5
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,11 @@ where
732732
if self.alloc_map.contains_key(&alloc) {
733733
// Not yet interned, so proceed recursively
734734
self.intern_static(alloc, mutability)?;
735+
} else if self.dead_alloc_map.contains_key(&alloc) {
736+
// danging pointer
737+
return err!(ValidationFailure(
738+
"encountered dangling pointer in final constant".into(),
739+
))
735740
}
736741
}
737742
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 this constant cannot be used
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: this constant cannot be used
2+
--> $DIR/dangling-alloc-id-ice.rs:10:1
3+
|
4+
LL | / const FOO: &() = { //~ ERROR this constant cannot be used
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+

0 commit comments

Comments
 (0)