Skip to content

Commit b0fed64

Browse files
committed
Handle plain enum values
1 parent e90d647 commit b0fed64

File tree

4 files changed

+47
-16
lines changed

4 files changed

+47
-16
lines changed

compiler/rustc_mir_build/src/builder/scope.rs

+35-4
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,15 @@ that contains only loops and breakable blocks. It tracks where a `break`,
8383

8484
use std::mem;
8585

86+
use rustc_abi::Size;
8687
use rustc_data_structures::fx::FxHashMap;
8788
use rustc_hir::HirId;
8889
use rustc_index::{IndexSlice, IndexVec};
8990
use rustc_middle::middle::region;
9091
use rustc_middle::mir::*;
91-
use rustc_middle::thir::{ArmId, ExprId, LintLevel};
92-
use rustc_middle::{bug, span_bug};
92+
use rustc_middle::thir::{AdtExpr, AdtExprBase, ArmId, ExprId, ExprKind, LintLevel};
93+
use rustc_middle::ty::ValTree;
94+
use rustc_middle::{bug, span_bug, ty};
9395
use rustc_pattern_analysis::rustc::RustcPatCtxt;
9496
use rustc_session::lint::Level;
9597
use rustc_span::source_map::Spanned;
@@ -761,8 +763,37 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
761763
span_bug!(span, "break value must be a scope")
762764
};
763765

764-
// FIXME accept bare MyEnum::Foo as constant
765-
let constant = self.as_constant(&self.thir[value]);
766+
let constant = match &self.thir[value].kind {
767+
ExprKind::Adt(box AdtExpr { variant_index, fields, base, .. }) => {
768+
assert!(matches!(base, AdtExprBase::None));
769+
assert!(fields.is_empty());
770+
ConstOperand {
771+
span: self.thir[value].span,
772+
user_ty: None,
773+
const_: Const::Ty(
774+
self.thir[value].ty,
775+
ty::Const::new(
776+
self.tcx,
777+
ty::ConstKind::Value(ty::Value {
778+
ty: self.thir[value].ty,
779+
valtree: ValTree::from_branches(
780+
self.tcx,
781+
Some(ValTree::from_scalar_int(
782+
self.tcx,
783+
ty::ScalarInt::try_from_uint(
784+
variant_index.as_u32(),
785+
Size::from_bits(32),
786+
)
787+
.unwrap(),
788+
)),
789+
),
790+
}),
791+
),
792+
),
793+
}
794+
}
795+
_ => self.as_constant(&self.thir[value]),
796+
};
766797

767798
let break_index = self
768799
.scopes

tests/ui/loop-match/loop-match.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ fn main() {
1919
match state {
2020
State::A => {
2121
#[const_continue]
22-
break 'blk const { State::B };
22+
break 'blk State::B;
2323
}
2424
State::B => {
2525
// without special logic, the compiler believes this is a re-assignment to
@@ -29,10 +29,10 @@ fn main() {
2929

3030
if true {
3131
#[const_continue]
32-
break 'blk const { State::C };
32+
break 'blk State::C;
3333
} else {
3434
#[const_continue]
35-
break 'blk const { State::A };
35+
break 'blk State::A;
3636
}
3737
}
3838
State::C => break 'a,

tests/ui/loop-match/nested.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ fn run() -> String {
3737
State1::A => {
3838
accum.push('a');
3939
#[const_continue]
40-
break 'blk1 const { State1::B };
40+
break 'blk1 State1::B;
4141
}
4242
State1::B => {
4343
accum.push('b');
@@ -48,22 +48,22 @@ fn run() -> String {
4848
State2::X => {
4949
accum.push('x');
5050
#[const_continue]
51-
break 'blk2 const { State2::Y };
51+
break 'blk2 State2::Y;
5252
}
5353
State2::Y => {
5454
accum.push('y');
5555
#[const_continue]
56-
break 'blk2 const { State2::Z };
56+
break 'blk2 State2::Z;
5757
}
5858
State2::Z => {
5959
accum.push('z');
6060
if first {
6161
first = false;
6262
#[const_continue]
63-
break 'blk2 const { State2::X };
63+
break 'blk2 State2::X;
6464
} else {
6565
#[const_continue]
66-
break 'blk1 const { State1::C };
66+
break 'blk1 State1::C;
6767
}
6868
}
6969
}

tests/ui/loop-match/or-patterns.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,21 @@ fn main() {
2525
states.push(state);
2626
if first {
2727
#[const_continue]
28-
break 'blk const { State::B };
28+
break 'blk State::B;
2929
} else {
3030
#[const_continue]
31-
break 'blk const { State::D };
31+
break 'blk State::D;
3232
}
3333
}
3434
State::B | State::D => {
3535
states.push(state);
3636
if first {
3737
first = false;
3838
#[const_continue]
39-
break 'blk const { State::A };
39+
break 'blk State::A;
4040
} else {
4141
#[const_continue]
42-
break 'blk const { State::C };
42+
break 'blk State::C;
4343
}
4444
}
4545
State::C => {

0 commit comments

Comments
 (0)