From e252277fe9b44ed9a913aeeb9f55dc85eaadace4 Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Wed, 17 Jul 2013 03:13:23 -0500 Subject: [PATCH] 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. --- src/librustc/middle/trans/base.rs | 2 +- src/librustc/middle/trans/build.rs | 18 ++++++++++-------- src/librustc/middle/trans/datum.rs | 2 +- src/test/run-pass/issue-7344.rs | 28 ++++++++++++++++++++++++++++ 4 files changed, 40 insertions(+), 10 deletions(-) create mode 100644 src/test/run-pass/issue-7344.rs 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); + 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)); + 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 or the MIT license +// , 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(); +}