@@ -223,6 +223,12 @@ pub struct PatternContext {
223223 pub on_top : usize ,
224224}
225225
226+ impl Default for PatternContext {
227+ fn default ( ) -> Self {
228+ Self :: new ( )
229+ }
230+ }
231+
226232impl PatternContext {
227233 pub fn new ( ) -> Self {
228234 PatternContext {
@@ -1919,7 +1925,7 @@ impl Compiler<'_> {
19191925 emit ! (
19201926 self ,
19211927 Instruction :: Swap {
1922- index: count as u32
1928+ index: u32 :: try_from ( count ) . unwrap ( )
19231929 }
19241930 ) ;
19251931 count -= 1 ;
@@ -1977,8 +1983,8 @@ impl Compiler<'_> {
19771983 // return self.compiler_error(loc, "too many expressions in star-unpacking sequence pattern");
19781984 }
19791985 let args = UnpackExArgs {
1980- before : i as u8 ,
1981- after : ( n - i - 1 ) as u8 ,
1986+ before : u8 :: try_from ( i ) . unwrap ( ) ,
1987+ after : u8 :: try_from ( n - i - 1 ) . unwrap ( ) ,
19821988 } ;
19831989 emit ! ( self , Instruction :: UnpackEx { args } ) ;
19841990 seen_star = true ;
@@ -1990,7 +1996,12 @@ impl Compiler<'_> {
19901996 }
19911997 }
19921998 if !seen_star {
1993- emit ! ( self , Instruction :: UnpackSequence { size: n as u32 } ) ;
1999+ emit ! (
2000+ self ,
2001+ Instruction :: UnpackSequence {
2002+ size: u32 :: try_from( n) . unwrap( )
2003+ }
2004+ ) ;
19942005 }
19952006 Ok ( ( ) )
19962007 }
@@ -2005,7 +2016,7 @@ impl Compiler<'_> {
20052016 self . pattern_unpack_helper ( patterns) ?;
20062017 let size = patterns. len ( ) ;
20072018 // Increase the on_top counter for the newly unpacked subjects.
2008- pc. on_top += size as usize ;
2019+ pc. on_top += size;
20092020 // For each unpacked subject, compile its subpattern.
20102021 for pattern in patterns {
20112022 // Decrement on_top for each subject as it is consumed.
@@ -2023,9 +2034,7 @@ impl Compiler<'_> {
20232034 ) -> CompileResult < ( ) > {
20242035 // Keep the subject around for extracting elements.
20252036 pc. on_top += 1 ;
2026- let size = patterns. len ( ) ;
2027- for i in 0 ..size {
2028- let pattern = & patterns[ i] ;
2037+ for ( i, pattern) in patterns. iter ( ) . enumerate ( ) {
20292038 // if pattern.is_wildcard() {
20302039 // continue;
20312040 // }
@@ -2035,7 +2044,7 @@ impl Compiler<'_> {
20352044 continue ;
20362045 }
20372046 // Duplicate the subject.
2038- emit ! ( self , Instruction :: CopyItem { index: 1 as u32 } ) ;
2047+ emit ! ( self , Instruction :: CopyItem { index: 1_u32 } ) ;
20392048 if i < star {
20402049 // For indices before the star, use a nonnegative index equal to i.
20412050 self . emit_load_const ( ConstantData :: Integer { value : i. into ( ) } ) ;
@@ -2044,7 +2053,7 @@ impl Compiler<'_> {
20442053 // index = len(subject) - (size - i)
20452054 emit ! ( self , Instruction :: GetLen ) ;
20462055 self . emit_load_const ( ConstantData :: Integer {
2047- value : ( size - 1 ) . into ( ) ,
2056+ value : ( patterns . len ( ) - 1 ) . into ( ) ,
20482057 } ) ;
20492058 // Subtract to compute the correct index.
20502059 emit ! (
@@ -2108,7 +2117,7 @@ impl Compiler<'_> {
21082117
21092118 // Otherwise, there is a sub-pattern. Duplicate the object on top of the stack.
21102119 pc. on_top += 1 ;
2111- emit ! ( self , Instruction :: CopyItem { index: 1 as u32 } ) ;
2120+ emit ! ( self , Instruction :: CopyItem { index: 1_u32 } ) ;
21122121 // Compile the sub-pattern.
21132122 self . compile_pattern ( p. pattern . as_ref ( ) . unwrap ( ) , pc) ?;
21142123 // After success, decrement the on_top counter.
@@ -2143,8 +2152,8 @@ impl Compiler<'_> {
21432152 return Err ( self . compile_error_forbidden_name ( attr) ) ;
21442153 }
21452154 // Check for duplicates: compare with every subsequent attribute.
2146- for j in ( i + 1 ) ..nattrs {
2147- let other = attrs [ j ] . as_str ( ) ;
2155+ for ident in attrs . iter ( ) . take ( nattrs ) . skip ( i + 1 ) {
2156+ let other = ident . as_str ( ) ;
21482157 if attr == other {
21492158 todo ! ( ) ;
21502159 // return Err(self.compiler_error(
@@ -2221,7 +2230,7 @@ impl Compiler<'_> {
22212230 elements : attr_names,
22222231 } ) ;
22232232 // 2. Emit MATCH_CLASS with nargs.
2224- emit ! ( self , Instruction :: MatchClass ( nargs as u32 ) ) ;
2233+ emit ! ( self , Instruction :: MatchClass ( u32 :: try_from ( nargs ) . unwrap ( ) ) ) ;
22252234 // 3. Duplicate the top of the stack.
22262235 emit ! ( self , Instruction :: CopyItem { index: 1_u32 } ) ;
22272236 // 4. Load None.
@@ -2235,7 +2244,12 @@ impl Compiler<'_> {
22352244
22362245 // Unpack the tuple into (nargs + nattrs) items.
22372246 let total = nargs + nattrs;
2238- emit ! ( self , Instruction :: UnpackSequence { size: total as u32 } ) ;
2247+ emit ! (
2248+ self ,
2249+ Instruction :: UnpackSequence {
2250+ size: u32 :: try_from( total) . unwrap( )
2251+ }
2252+ ) ;
22392253 pc. on_top += total;
22402254 pc. on_top -= 1 ;
22412255
@@ -2405,7 +2419,7 @@ impl Compiler<'_> {
24052419 pc. fail_pop . clear ( ) ;
24062420 pc. on_top = 0 ;
24072421 // Emit a COPY(1) instruction before compiling the alternative.
2408- emit ! ( self , Instruction :: CopyItem { index: 1 as u32 } ) ;
2422+ emit ! ( self , Instruction :: CopyItem { index: 1_u32 } ) ;
24092423 self . compile_pattern ( alt, pc) ?;
24102424
24112425 let nstores = pc. stores . len ( ) ;
@@ -2558,9 +2572,9 @@ impl Compiler<'_> {
25582572 // Patterns like: [] / [_] / [_, _] / [*_] / [_, *_] / [_, _, *_] / etc.
25592573 emit ! ( self , Instruction :: Pop ) ;
25602574 } else if star_wildcard {
2561- self . pattern_helper_sequence_subscr ( & patterns, star. unwrap ( ) , pc) ?;
2575+ self . pattern_helper_sequence_subscr ( patterns, star. unwrap ( ) , pc) ?;
25622576 } else {
2563- self . pattern_helper_sequence_unpack ( & patterns, star, pc) ?;
2577+ self . pattern_helper_sequence_unpack ( patterns, star, pc) ?;
25642578 }
25652579 Ok ( ( ) )
25662580 }
@@ -2656,12 +2670,10 @@ impl Compiler<'_> {
26562670 let has_default = cases. iter ( ) . last ( ) . unwrap ( ) . pattern . is_match_star ( ) && num_cases > 1 ;
26572671
26582672 let case_count = num_cases - if has_default { 1 } else { 0 } ;
2659- for i in 0 ..case_count {
2660- let m = & cases[ i] ;
2661-
2673+ for ( i, m) in cases. iter ( ) . enumerate ( ) . take ( case_count) {
26622674 // Only copy the subject if not on the last case
26632675 if i != case_count - 1 {
2664- emit ! ( self , Instruction :: CopyItem { index: 1 as u32 } ) ;
2676+ emit ! ( self , Instruction :: CopyItem { index: 1_u32 } ) ;
26652677 }
26662678
26672679 pattern_context. stores = Vec :: with_capacity ( 1 ) ;
0 commit comments