]> git.lizzy.rs Git - rust.git/commitdiff
Don't access a static just for its size and alignment
authorOliver Scherer <github35764891676564198441@oli-obk.de>
Thu, 25 Jul 2019 17:29:48 +0000 (19:29 +0200)
committerOliver Scherer <github35764891676564198441@oli-obk.de>
Thu, 25 Jul 2019 17:29:48 +0000 (19:29 +0200)
src/librustc_mir/interpret/memory.rs
src/test/ui/consts/static-cycle-error.rs [new file with mode: 0644]

index 3f2a76a77be36b8f6f847482de41871418849830..674ae29070644f4ac866911e1a1ec9079a094583 100644 (file)
@@ -535,6 +535,19 @@ pub fn get_size_and_align(
         id: AllocId,
         liveness: AllocCheck,
     ) -> InterpResult<'static, (Size, Align)> {
+        // Allocations of `static` items
+        // Can't do this in the match argument, we may get cycle errors since the lock would
+        // be held throughout the match.
+        let alloc = self.tcx.alloc_map.lock().get(id);
+        match alloc {
+            Some(GlobalAlloc::Static(did)) => {
+                // Use size and align of the type
+                let ty = self.tcx.type_of(did);
+                let layout = self.tcx.layout_of(ParamEnv::empty().and(ty)).unwrap();
+                return Ok((layout.size, layout.align.abi));
+            }
+            _ => {}
+        }
         // Regular allocations.
         if let Ok(alloc) = self.get(id) {
             return Ok((Size::from_bytes(alloc.bytes.len() as u64), alloc.align));
@@ -548,20 +561,6 @@ pub fn get_size_and_align(
                 Ok((Size::ZERO, Align::from_bytes(1).unwrap()))
             };
         }
-        // Foreign statics.
-        // Can't do this in the match argument, we may get cycle errors since the lock would
-        // be held throughout the match.
-        let alloc = self.tcx.alloc_map.lock().get(id);
-        match alloc {
-            Some(GlobalAlloc::Static(did)) => {
-                assert!(self.tcx.is_foreign_item(did));
-                // Use size and align of the type
-                let ty = self.tcx.type_of(did);
-                let layout = self.tcx.layout_of(ParamEnv::empty().and(ty)).unwrap();
-                return Ok((layout.size, layout.align.abi));
-            }
-            _ => {}
-        }
         // The rest must be dead.
         if let AllocCheck::MaybeDead = liveness {
             // Deallocated pointers are allowed, we should be able to find
diff --git a/src/test/ui/consts/static-cycle-error.rs b/src/test/ui/consts/static-cycle-error.rs
new file mode 100644 (file)
index 0000000..9ce050a
--- /dev/null
@@ -0,0 +1,11 @@
+// check-pass
+
+struct Foo {
+    foo: Option<&'static Foo>
+}
+
+static FOO: Foo = Foo {
+    foo: Some(&FOO),
+};
+
+fn main() {}