Skip to content

Commit f308550

Browse files
committed
Rollup merge of rust-lang#22527 - dotdash:if-loop, r=huonw
In `if loop {} {}`, the `if` is actually unreachable, but we didn't handle that correctly and when trying to translate the `if` we tried to branch on the \"return value\" of the loop expression, which is not an `i1` and therefore triggered an LLVM assertion.
2 parents b7ac60a + 07c0faa commit f308550

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/librustc_trans/trans/controlflow.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use llvm::ValueRef;
1212
use middle::def;
1313
use middle::lang_items::{PanicFnLangItem, PanicBoundsCheckFnLangItem};
1414
use trans::base::*;
15+
use trans::basic_block::BasicBlock;
1516
use trans::build::*;
1617
use trans::callee;
1718
use trans::cleanup::CleanupMethods;
@@ -280,6 +281,12 @@ pub fn trans_loop<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
280281

281282
fcx.pop_loop_cleanup_scope(loop_expr.id);
282283

284+
// If there are no predecessors for the next block, we just translated an endless loop and the
285+
// next block is unreachable
286+
if BasicBlock(next_bcx_in.llbb).pred_iter().next().is_none() {
287+
Unreachable(next_bcx_in);
288+
}
289+
283290
return next_bcx_in;
284291
}
285292

src/test/compile-fail/if-loop.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2015 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+
#![feature(rustc_attrs)]
12+
#![allow(warnings)]
13+
14+
// This used to ICE because the "if" being unreachable was not handled correctly
15+
fn err() {
16+
if loop {} {}
17+
}
18+
19+
#[rustc_error]
20+
fn main() {} //~ ERROR compilation successful

0 commit comments

Comments
 (0)