Skip to content

Commit 96c7b35

Browse files
committed
Handle plain enum values
1 parent 9e1d62f commit 96c7b35

File tree

5 files changed

+39
-18
lines changed

5 files changed

+39
-18
lines changed

compiler/rustc_mir_build/src/builder/scope.rs

+25-4
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,9 @@ use rustc_hir::HirId;
8888
use rustc_index::{IndexSlice, IndexVec};
8989
use rustc_middle::middle::region;
9090
use rustc_middle::mir::*;
91-
use rustc_middle::thir::{ArmId, ExprId, LintLevel};
92-
use rustc_middle::{bug, span_bug};
91+
use rustc_middle::thir::{AdtExpr, AdtExprBase, ArmId, ExprId, ExprKind, LintLevel};
92+
use rustc_middle::ty::ValTree;
93+
use rustc_middle::{bug, span_bug, ty};
9394
use rustc_pattern_analysis::rustc::RustcPatCtxt;
9495
use rustc_session::lint::Level;
9596
use rustc_span::source_map::Spanned;
@@ -824,8 +825,28 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
824825
span_bug!(span, "break value must be a scope")
825826
};
826827

827-
// FIXME accept bare MyEnum::Foo as constant
828-
let constant = self.as_constant(&self.thir[value]);
828+
let constant = match &self.thir[value].kind {
829+
ExprKind::Adt(box AdtExpr { variant_index, fields, base, .. }) => {
830+
assert!(matches!(base, AdtExprBase::None));
831+
assert!(fields.is_empty());
832+
ConstOperand {
833+
span: self.thir[value].span,
834+
user_ty: None,
835+
const_: Const::Ty(
836+
self.thir[value].ty,
837+
ty::Const::new_value(
838+
self.tcx,
839+
ValTree::from_branches(
840+
self.tcx,
841+
[ValTree::from_scalar_int(self.tcx, variant_index.as_u32().into())],
842+
),
843+
self.thir[value].ty,
844+
),
845+
),
846+
}
847+
}
848+
_ => self.as_constant(&self.thir[value]),
849+
};
829850

830851
let break_index = self
831852
.scopes

tests/ui/feature-gates/feature-gate-loop-match.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ fn main() {
1313
State::A => {
1414
#[const_continue]
1515
//~^ ERROR the `#[const_continue]` attribute is an experimental feature
16-
break 'blk const { State::B };
16+
break 'blk State::B;
1717
}
1818
State::B => {
1919
#[const_continue]
2020
//~^ ERROR the `#[const_continue]` attribute is an experimental feature
21-
break 'blk const { State::C };
21+
break 'blk State::C;
2222
}
2323
State::C => break 'a,
2424
}

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)