about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2018-11-06 23:36:13 +0000
committerbors <bors@rust-lang.org>2018-11-06 23:36:13 +0000
commitddd4b194a0c28065a277c1f964aa931cc021b5c6 (patch)
treec523387f32304340c0d53d7e218fc0086a686d88
parent15d770400eed9018f18bddf83dd65cb7789280a5 (diff)
parente70b63458aafdc0f0e2e350c72317e53b0afa71d (diff)
downloadrust-ddd4b194a0c28065a277c1f964aa931cc021b5c6.tar.gz
rust-ddd4b194a0c28065a277c1f964aa931cc021b5c6.zip
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 https://github.com/rust-lang/rust/issues/55287
-rw-r--r--src/librustc_mir/interpret/memory.rs5
-rw-r--r--src/test/ui/consts/dangling-alloc-id-ice.rs15
-rw-r--r--src/test/ui/consts/dangling-alloc-id-ice.stderr13
-rw-r--r--src/test/ui/consts/dangling_raw_ptr.rs10
-rw-r--r--src/test/ui/consts/dangling_raw_ptr.stderr13
5 files changed, 56 insertions, 0 deletions
diff --git a/src/librustc_mir/interpret/memory.rs b/src/librustc_mir/interpret/memory.rs
index a0231f3feb1..6a109efe3c4 100644
--- a/src/librustc_mir/interpret/memory.rs
+++ b/src/librustc_mir/interpret/memory.rs
@@ -730,6 +730,11 @@ where
             if self.alloc_map.contains_key(&alloc) {
                 // Not yet interned, so proceed recursively
                 self.intern_static(alloc, mutability)?;
+            } else if self.dead_alloc_map.contains_key(&alloc) {
+                // dangling pointer
+                return err!(ValidationFailure(
+                    "encountered dangling pointer in final constant".into(),
+                ))
             }
         }
         Ok(())
diff --git a/src/test/ui/consts/dangling-alloc-id-ice.rs b/src/test/ui/consts/dangling-alloc-id-ice.rs
new file mode 100644
index 00000000000..695d33b6908
--- /dev/null
+++ b/src/test/ui/consts/dangling-alloc-id-ice.rs
@@ -0,0 +1,15 @@
+// https://github.com/rust-lang/rust/issues/55223
+
+#![feature(const_let)]
+
+union Foo<'a> {
+    y: &'a (),
+    long_live_the_unit: &'static (),
+}
+
+const FOO: &() = { //~ ERROR any use of this value will cause an error
+    let y = ();
+    unsafe { Foo { y: &y }.long_live_the_unit }
+};
+
+fn main() {}
diff --git a/src/test/ui/consts/dangling-alloc-id-ice.stderr b/src/test/ui/consts/dangling-alloc-id-ice.stderr
new file mode 100644
index 00000000000..a5fa88e5e68
--- /dev/null
+++ b/src/test/ui/consts/dangling-alloc-id-ice.stderr
@@ -0,0 +1,13 @@
+error: any use of this value will cause an error
+  --> $DIR/dangling-alloc-id-ice.rs:10:1
+   |
+LL | / const FOO: &() = { //~ ERROR any use of this value will cause an error
+LL | |     let y = ();
+LL | |     unsafe { Foo { y: &y }.long_live_the_unit }
+LL | | };
+   | |__^ type validation failed: encountered dangling pointer in final constant
+   |
+   = note: #[deny(const_err)] on by default
+
+error: aborting due to previous error
+
diff --git a/src/test/ui/consts/dangling_raw_ptr.rs b/src/test/ui/consts/dangling_raw_ptr.rs
new file mode 100644
index 00000000000..7fc773412f2
--- /dev/null
+++ b/src/test/ui/consts/dangling_raw_ptr.rs
@@ -0,0 +1,10 @@
+#![feature(const_let)]
+
+const FOO: *const u32 = { //~ ERROR any use of this value will cause an error
+    let x = 42;
+    &x
+};
+
+fn main() {
+    let x = FOO;
+}
diff --git a/src/test/ui/consts/dangling_raw_ptr.stderr b/src/test/ui/consts/dangling_raw_ptr.stderr
new file mode 100644
index 00000000000..3b20936f8ae
--- /dev/null
+++ b/src/test/ui/consts/dangling_raw_ptr.stderr
@@ -0,0 +1,13 @@
+error: any use of this value will cause an error
+  --> $DIR/dangling_raw_ptr.rs:3:1
+   |
+LL | / const FOO: *const u32 = { //~ ERROR any use of this value will cause an error
+LL | |     let x = 42;
+LL | |     &x
+LL | | };
+   | |__^ type validation failed: encountered dangling pointer in final constant
+   |
+   = note: #[deny(const_err)] on by default
+
+error: aborting due to previous error
+