Skip to content

Commit b7b52cc

Browse files
committed
Auto merge of #46877 - Deewiant:gh46843, r=eddyb
MIR: terminate unreachable blocks in construct_const Fixes #46843. #45821 added unreachable blocks in matches, which were terminated in construct_fn but not in construct_const, causing a panic due to "no terminator on block" when constants involved matching on enums. The "unimplemented expression type" error may go away in the future, the key is that we see the E0015 about using a non-const function and then don't ICE.
2 parents eff3de0 + 8dfc47a commit b7b52cc

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/librustc_mir/build/mod.rs

+7
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,13 @@ fn construct_const<'a, 'gcx, 'tcx>(hir: Cx<'a, 'gcx, 'tcx>,
472472
// Constants can't `return` so a return block should not be created.
473473
assert_eq!(builder.cached_return_block, None);
474474

475+
// Constants may be match expressions in which case an unreachable block may
476+
// be created, so terminate it properly.
477+
if let Some(unreachable_block) = builder.cached_unreachable_block {
478+
builder.cfg.terminate(unreachable_block, source_info,
479+
TerminatorKind::Unreachable);
480+
}
481+
475482
builder.finish(vec![], None)
476483
}
477484

src/test/compile-fail/issue-46843.rs

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2017 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+
enum Thing { This, That }
12+
13+
fn non_const() -> Thing {
14+
Thing::This
15+
}
16+
17+
pub const Q: i32 = match non_const() { //~ ERROR E0015
18+
Thing::This => 1, //~ ERROR unimplemented expression type
19+
Thing::That => 0
20+
};
21+
22+
fn main() {}

0 commit comments

Comments
 (0)