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.
This commit is contained in:
Huon Wilson 2013-07-17 03:13:23 -05:00
parent e4f7561bcd
commit e252277fe9
4 changed files with 40 additions and 10 deletions

View File

@ -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());

View File

@ -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));
}
}
}

View File

@ -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. */

View File

@ -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();
}