Skip to content

Commit e252277

Browse files
committed
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.
1 parent e4f7561 commit e252277

File tree

4 files changed

+41
-11
lines changed

4 files changed

+41
-11
lines changed

src/librustc/middle/trans/base.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1549,7 +1549,7 @@ pub fn alloca_maybe_zeroed(cx: block, ty: Type, name: &str, zero: bool) -> Value
15491549
let _icx = push_ctxt("alloca");
15501550
if cx.unreachable {
15511551
unsafe {
1552-
return llvm::LLVMGetUndef(ty.to_ref());
1552+
return llvm::LLVMGetUndef(ty.ptr_to().to_ref());
15531553
}
15541554
}
15551555
let initcx = base::raw_block(cx.fcx, false, cx.fcx.get_llstaticallocas());

src/librustc/middle/trans/build.rs

+11-9
Original file line numberDiff line numberDiff line change
@@ -569,15 +569,17 @@ pub fn LoadRangeAssert(cx: block, PointerVal: ValueRef, lo: c_ulonglong,
569569
hi: c_ulonglong, signed: lib::llvm::Bool) -> ValueRef {
570570
let value = Load(cx, PointerVal);
571571

572-
unsafe {
573-
let t = llvm::LLVMGetElementType(llvm::LLVMTypeOf(PointerVal));
574-
let min = llvm::LLVMConstInt(t, lo, signed);
575-
let max = llvm::LLVMConstInt(t, hi, signed);
576-
577-
do [min, max].as_imm_buf |ptr, len| {
578-
llvm::LLVMSetMetadata(value, lib::llvm::MD_range as c_uint,
579-
llvm::LLVMMDNodeInContext(cx.fcx.ccx.llcx,
580-
ptr, len as c_uint));
572+
if !cx.unreachable {
573+
unsafe {
574+
let t = llvm::LLVMGetElementType(llvm::LLVMTypeOf(PointerVal));
575+
let min = llvm::LLVMConstInt(t, lo, signed);
576+
let max = llvm::LLVMConstInt(t, hi, signed);
577+
578+
do [min, max].as_imm_buf |ptr, len| {
579+
llvm::LLVMSetMetadata(value, lib::llvm::MD_range as c_uint,
580+
llvm::LLVMMDNodeInContext(cx.fcx.ccx.llcx,
581+
ptr, len as c_uint));
582+
}
581583
}
582584
}
583585

src/librustc/middle/trans/datum.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ impl Datum {
413413
pub fn to_value_datum(&self, bcx: block) -> Datum {
414414
/*!
415415
*
416-
* Yields a by-ref form of this datum. This may involve
416+
* Yields a by-value form of this datum. This may involve
417417
* creation of a temporary stack slot. The value returned by
418418
* this function is not separately rooted from this datum, so
419419
* it will not live longer than the current datum. */

src/test/run-pass/issue-7344.rs

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#[allow(unreachable_code)];
12+
13+
fn foo() -> bool { false }
14+
15+
fn bar() {
16+
return;
17+
!foo();
18+
}
19+
20+
fn baz() {
21+
return;
22+
if "" == "" {}
23+
}
24+
25+
fn main() {
26+
bar();
27+
baz();
28+
}

0 commit comments

Comments
 (0)