about summary refs log tree commit diff
diff options
context:
space:
mode:
authorHuon Wilson <dbau.pp+github@gmail.com>2013-07-17 03:13:23 -0500
committerHuon Wilson <dbau.pp+github@gmail.com>2013-07-17 03:13:23 +1000
commite252277fe9b44ed9a913aeeb9f55dc85eaadace4 (patch)
treef4c5efcf4e52b091b3053fa438d83035c5c06fb8
parente4f7561bcdf3b54dafefd478b86e1f7610e74348 (diff)
downloadrust-e252277fe9b44ed9a913aeeb9f55dc85eaadace4.tar.gz
rust-e252277fe9b44ed9a913aeeb9f55dc85eaadace4.zip
rustc: handle allocas and LoadRangeAsserts in unreachable blocks correctly.
An alloca in an unreachable block would shortcircuit with Undef, but with type
`Type`, rather than type `*Type` (i.e. a plain value, not a pointer) but it is
expected to return a pointer into the stack, leading to confusion and LLVM
asserts later.

Similarly, attaching the range metadata to a Load in an unreachable block
makes LLVM unhappy, since the Load returns Undef.

Fixes #7344.
-rw-r--r--src/librustc/middle/trans/base.rs2
-rw-r--r--src/librustc/middle/trans/build.rs20
-rw-r--r--src/librustc/middle/trans/datum.rs2
-rw-r--r--src/test/run-pass/issue-7344.rs28
4 files changed, 41 insertions, 11 deletions
diff --git a/src/librustc/middle/trans/base.rs b/src/librustc/middle/trans/base.rs
index 7182f7ff8b7..8b64809df9e 100644
--- a/src/librustc/middle/trans/base.rs
+++ b/src/librustc/middle/trans/base.rs
@@ -1549,7 +1549,7 @@ pub fn alloca_maybe_zeroed(cx: block, ty: Type, name: &str, zero: bool) -> Value
     let _icx = push_ctxt("alloca");
     if cx.unreachable {
         unsafe {
-            return llvm::LLVMGetUndef(ty.to_ref());
+            return llvm::LLVMGetUndef(ty.ptr_to().to_ref());
         }
     }
     let initcx = base::raw_block(cx.fcx, false, cx.fcx.get_llstaticallocas());
diff --git a/src/librustc/middle/trans/build.rs b/src/librustc/middle/trans/build.rs
index db5553ca939..1b92574fd96 100644
--- a/src/librustc/middle/trans/build.rs
+++ b/src/librustc/middle/trans/build.rs
@@ -569,15 +569,17 @@ pub fn LoadRangeAssert(cx: block, PointerVal: ValueRef, lo: c_ulonglong,
                        hi: c_ulonglong, signed: lib::llvm::Bool) -> ValueRef {
     let value = Load(cx, PointerVal);
 
-    unsafe {
-        let t = llvm::LLVMGetElementType(llvm::LLVMTypeOf(PointerVal));
-        let min = llvm::LLVMConstInt(t, lo, signed);
-        let max = llvm::LLVMConstInt(t, hi, signed);
-
-        do [min, max].as_imm_buf |ptr, len| {
-            llvm::LLVMSetMetadata(value, lib::llvm::MD_range as c_uint,
-                                  llvm::LLVMMDNodeInContext(cx.fcx.ccx.llcx,
-                                                            ptr, len as c_uint));
+    if !cx.unreachable {
+        unsafe {
+            let t = llvm::LLVMGetElementType(llvm::LLVMTypeOf(PointerVal));
+            let min = llvm::LLVMConstInt(t, lo, signed);
+            let max = llvm::LLVMConstInt(t, hi, signed);
+
+            do [min, max].as_imm_buf |ptr, len| {
+                llvm::LLVMSetMetadata(value, lib::llvm::MD_range as c_uint,
+                                      llvm::LLVMMDNodeInContext(cx.fcx.ccx.llcx,
+                                                                ptr, len as c_uint));
+            }
         }
     }
 
diff --git a/src/librustc/middle/trans/datum.rs b/src/librustc/middle/trans/datum.rs
index 591e318c731..efd666c8d96 100644
--- a/src/librustc/middle/trans/datum.rs
+++ b/src/librustc/middle/trans/datum.rs
@@ -413,7 +413,7 @@ impl Datum {
     pub fn to_value_datum(&self, bcx: block) -> Datum {
         /*!
          *
-         * Yields a by-ref form of this datum.  This may involve
+         * Yields a by-value form of this datum.  This may involve
          * creation of a temporary stack slot.  The value returned by
          * this function is not separately rooted from this datum, so
          * it will not live longer than the current datum. */
diff --git a/src/test/run-pass/issue-7344.rs b/src/test/run-pass/issue-7344.rs
new file mode 100644
index 00000000000..acf55d2463b
--- /dev/null
+++ b/src/test/run-pass/issue-7344.rs
@@ -0,0 +1,28 @@
+// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+#[allow(unreachable_code)];
+
+fn foo() -> bool { false }
+
+fn bar() {
+    return;
+    !foo();
+}
+
+fn baz() {
+    return;
+    if "" == "" {}
+}
+
+fn main() {
+    bar();
+    baz();
+}