@@ -145,7 +145,7 @@ impl CodeFlags {
145145#[ derive( Serialize , Debug , Deserialize , Clone , Copy , PartialEq , Eq , Hash , Ord , PartialOrd ) ]
146146#[ repr( transparent) ]
147147// XXX: if you add a new instruction that stores a Label, make sure to add it in
148- // compile::CodeInfo::finalize_code and CodeObject::label_targets
148+ // Instruction::label_arg{,_mut}
149149pub struct Label ( pub usize ) ;
150150impl fmt:: Display for Label {
151151 fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
@@ -589,23 +589,8 @@ impl<C: Constant> CodeObject<C> {
589589 pub fn label_targets ( & self ) -> BTreeSet < Label > {
590590 let mut label_targets = BTreeSet :: new ( ) ;
591591 for instruction in & * self . instructions {
592- match instruction {
593- Jump { target : l }
594- | JumpIfTrue { target : l }
595- | JumpIfFalse { target : l }
596- | JumpIfTrueOrPop { target : l }
597- | JumpIfFalseOrPop { target : l }
598- | ForIter { target : l }
599- | SetupFinally { handler : l }
600- | SetupExcept { handler : l }
601- | SetupWith { end : l }
602- | SetupAsyncWith { end : l }
603- | SetupLoop { end : l }
604- | Continue { target : l } => {
605- label_targets. insert ( * l) ;
606- }
607-
608- _ => { }
592+ if let Some ( l) = instruction. label_arg ( ) {
593+ label_targets. insert ( * l) ;
609594 }
610595 }
611596 label_targets
@@ -753,6 +738,46 @@ impl<C: Constant> fmt::Display for CodeObject<C> {
753738}
754739
755740impl Instruction {
741+ /// Gets the label stored inside this instruction, if it exists
742+ #[ inline]
743+ pub fn label_arg ( & self ) -> Option < & Label > {
744+ match self {
745+ Jump { target : l }
746+ | JumpIfTrue { target : l }
747+ | JumpIfFalse { target : l }
748+ | JumpIfTrueOrPop { target : l }
749+ | JumpIfFalseOrPop { target : l }
750+ | ForIter { target : l }
751+ | SetupFinally { handler : l }
752+ | SetupExcept { handler : l }
753+ | SetupWith { end : l }
754+ | SetupAsyncWith { end : l }
755+ | SetupLoop { end : l }
756+ | Continue { target : l } => Some ( l) ,
757+ _ => None ,
758+ }
759+ }
760+
761+ /// Gets a mutable reference to the label stored inside this instruction, if it exists
762+ #[ inline]
763+ pub fn label_arg_mut ( & mut self ) -> Option < & mut Label > {
764+ match self {
765+ Jump { target : l }
766+ | JumpIfTrue { target : l }
767+ | JumpIfFalse { target : l }
768+ | JumpIfTrueOrPop { target : l }
769+ | JumpIfFalseOrPop { target : l }
770+ | ForIter { target : l }
771+ | SetupFinally { handler : l }
772+ | SetupExcept { handler : l }
773+ | SetupWith { end : l }
774+ | SetupAsyncWith { end : l }
775+ | SetupLoop { end : l }
776+ | Continue { target : l } => Some ( l) ,
777+ _ => None ,
778+ }
779+ }
780+
756781 #[ allow( clippy:: too_many_arguments) ]
757782 fn fmt_dis < C : Constant > (
758783 & self ,
0 commit comments