Skip to content

Commit 085a9d4

Browse files
committed
Don't use a wildcard to prevent future mistakes
1 parent 8b0e806 commit 085a9d4

File tree

2 files changed

+57
-31
lines changed

2 files changed

+57
-31
lines changed

bytecode/src/bytecode.rs

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ impl CodeFlags {
146146
#[derive(Serialize, Debug, Deserialize, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
147147
#[repr(transparent)]
148148
// XXX: if you add a new instruction that stores a Label, make sure to add it in
149-
// compile::CodeInfo::finalize_code
149+
// compile::CodeInfo::finalize_code and CodeObject::label_targets
150150
pub struct Label(pub usize);
151151
impl fmt::Display for Label {
152152
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@@ -598,25 +598,38 @@ impl<C: Constant> CodeObject<C> {
598598
pub fn label_targets(&self) -> BTreeSet<Label> {
599599
let mut label_targets = BTreeSet::new();
600600
for instruction in &self.instructions {
601-
let label = match instruction {
602-
Jump { target } => target,
603-
JumpIfTrue { target } => target,
604-
JumpIfFalse { target } => target,
605-
JumpIfTrueOrPop { target } => target,
606-
JumpIfFalseOrPop { target } => target,
607-
ForIter { target } => target,
601+
match instruction {
602+
Jump { target: l }
603+
| JumpIfTrue { target: l }
604+
| JumpIfFalse { target: l }
605+
| JumpIfTrueOrPop { target: l }
606+
| JumpIfFalseOrPop { target: l }
607+
| ForIter { target: l }
608+
| SetupFinally { handler: l }
609+
| SetupExcept { handler: l }
610+
| SetupWith { end: l }
611+
| SetupAsyncWith { end: l } => {
612+
label_targets.insert(*l);
613+
}
608614
SetupLoop { start, end } => {
609615
label_targets.insert(*start);
610616
label_targets.insert(*end);
611-
continue;
612617
}
613-
SetupFinally { handler } => handler,
614-
SetupExcept { handler } => handler,
615-
SetupWith { end } => end,
616-
SetupAsyncWith { end } => end,
617-
_ => continue,
618-
};
619-
label_targets.insert(*label);
618+
619+
#[rustfmt::skip]
620+
Import { .. } | ImportStar | ImportFrom { .. } | LoadName { .. } | StoreName { .. }
621+
| DeleteName { .. } | Subscript | StoreSubscript | DeleteSubscript
622+
| StoreAttr { .. } | DeleteAttr { .. } | LoadConst { .. } | UnaryOperation { .. }
623+
| BinaryOperation { .. } | LoadAttr { .. } | CompareOperation { .. } | Pop
624+
| Rotate { .. } | Duplicate | GetIter | Continue | Break | MakeFunction
625+
| CallFunction { .. } | ReturnValue | YieldValue | YieldFrom | SetupAnnotation
626+
| EnterFinally | EndFinally | WithCleanupStart | WithCleanupFinish | PopBlock
627+
| Raise { .. } | BuildString { .. } | BuildTuple { .. } | BuildList { .. }
628+
| BuildSet { .. } | BuildMap { .. } | BuildSlice { .. } | ListAppend { .. }
629+
| SetAdd { .. } | MapAdd { .. } | PrintExpr | LoadBuildClass | UnpackSequence { .. }
630+
| UnpackEx { .. } | FormatValue { .. } | PopException | Reverse { .. }
631+
| GetAwaitable | BeforeAsyncWith | GetAIter | GetANext | MapAddRev { .. } => {}
632+
}
620633
}
621634
label_targets
622635
}

compiler/src/compile.rs

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,25 +36,38 @@ impl CodeInfo {
3636
// this is a little bit hacky, as until now the data stored inside Labels in
3737
// Instructions is just bookkeeping, but I think it's the best way to do this
3838
// XXX: any new instruction that uses a label has to be added here
39-
let label = match instruction {
40-
Jump { target } => target,
41-
JumpIfTrue { target } => target,
42-
JumpIfFalse { target } => target,
43-
JumpIfTrueOrPop { target } => target,
44-
JumpIfFalseOrPop { target } => target,
45-
ForIter { target } => target,
39+
match instruction {
40+
Jump { target: l }
41+
| JumpIfTrue { target: l }
42+
| JumpIfFalse { target: l }
43+
| JumpIfTrueOrPop { target: l }
44+
| JumpIfFalseOrPop { target: l }
45+
| ForIter { target: l }
46+
| SetupFinally { handler: l }
47+
| SetupExcept { handler: l }
48+
| SetupWith { end: l }
49+
| SetupAsyncWith { end: l } => {
50+
*l = label_map[l.0].expect("label never set");
51+
}
4652
SetupLoop { start, end } => {
4753
*start = label_map[start.0].expect("label never set");
4854
*end = label_map[end.0].expect("label never set");
49-
continue;
5055
}
51-
SetupFinally { handler } => handler,
52-
SetupExcept { handler } => handler,
53-
SetupWith { end } => end,
54-
SetupAsyncWith { end } => end,
55-
_ => continue,
56-
};
57-
*label = label_map[label.0].expect("label never set");
56+
57+
#[rustfmt::skip]
58+
Import { .. } | ImportStar | ImportFrom { .. } | LoadName { .. } | StoreName { .. }
59+
| DeleteName { .. } | Subscript | StoreSubscript | DeleteSubscript
60+
| StoreAttr { .. } | DeleteAttr { .. } | LoadConst { .. } | UnaryOperation { .. }
61+
| BinaryOperation { .. } | LoadAttr { .. } | CompareOperation { .. } | Pop
62+
| Rotate { .. } | Duplicate | GetIter | Continue | Break | MakeFunction
63+
| CallFunction { .. } | ReturnValue | YieldValue | YieldFrom | SetupAnnotation
64+
| EnterFinally | EndFinally | WithCleanupStart | WithCleanupFinish | PopBlock
65+
| Raise { .. } | BuildString { .. } | BuildTuple { .. } | BuildList { .. }
66+
| BuildSet { .. } | BuildMap { .. } | BuildSlice { .. } | ListAppend { .. }
67+
| SetAdd { .. } | MapAdd { .. } | PrintExpr | LoadBuildClass | UnpackSequence { .. }
68+
| UnpackEx { .. } | FormatValue { .. } | PopException | Reverse { .. }
69+
| GetAwaitable | BeforeAsyncWith | GetAIter | GetANext | MapAddRev { .. } => {}
70+
}
5871
}
5972
code
6073
}

0 commit comments

Comments
 (0)