55//! <https://github.com/python/cpython/blob/main/Python/compile.c>
66//! <https://github.com/micropython/micropython/blob/master/py/compile.c>
77
8+ #![ deny( clippy:: cast_possible_truncation) ]
9+
810use crate :: {
911 error:: { CodegenError , CodegenErrorType } ,
1012 ir,
@@ -332,7 +334,8 @@ impl Compiler {
332334 let cache = cache ( self . current_codeinfo ( ) ) ;
333335 cache
334336 . get_index_of ( name. as_ref ( ) )
335- . unwrap_or_else ( || cache. insert_full ( name. into_owned ( ) ) . 0 ) as u32
337+ . unwrap_or_else ( || cache. insert_full ( name. into_owned ( ) ) . 0 )
338+ . to_u32 ( )
336339 }
337340
338341 fn compile_program (
@@ -900,7 +903,7 @@ impl Compiler {
900903 let have_defaults = !args. defaults . is_empty ( ) ;
901904 if have_defaults {
902905 // Construct a tuple:
903- let size = args. defaults . len ( ) as u32 ;
906+ let size = args. defaults . len ( ) . to_u32 ( ) ;
904907 for element in & args. defaults {
905908 self . compile_expression ( element) ?;
906909 }
@@ -921,7 +924,7 @@ impl Compiler {
921924 emit ! (
922925 self ,
923926 Instruction :: BuildMap {
924- size: args. kw_defaults. len( ) as u32 ,
927+ size: args. kw_defaults. len( ) . to_u32 ( ) ,
925928 }
926929 ) ;
927930 }
@@ -936,11 +939,9 @@ impl Compiler {
936939
937940 self . push_output (
938941 bytecode:: CodeFlags :: NEW_LOCALS | bytecode:: CodeFlags :: IS_OPTIMIZED ,
939- args. posonlyargs . len ( ) . try_into ( ) . unwrap ( ) ,
940- ( args. posonlyargs . len ( ) + args. args . len ( ) )
941- . try_into ( )
942- . unwrap ( ) ,
943- args. kwonlyargs . len ( ) . try_into ( ) . unwrap ( ) ,
942+ args. posonlyargs . len ( ) . to_u32 ( ) ,
943+ ( args. posonlyargs . len ( ) + args. args . len ( ) ) . to_u32 ( ) ,
944+ args. kwonlyargs . len ( ) . to_u32 ( ) ,
944945 name. to_owned ( ) ,
945946 ) ;
946947
@@ -1269,12 +1270,12 @@ impl Compiler {
12691270 if let SymbolScope :: Free = symbol. scope {
12701271 idx += parent_code. cellvar_cache . len ( ) ;
12711272 }
1272- emit ! ( self , Instruction :: LoadClosure ( idx as u32 ) )
1273+ emit ! ( self , Instruction :: LoadClosure ( idx. to_u32 ( ) ) )
12731274 }
12741275 emit ! (
12751276 self ,
12761277 Instruction :: BuildTuple {
1277- size: code. freevars. len( ) as u32 ,
1278+ size: code. freevars. len( ) . to_u32 ( ) ,
12781279 }
12791280 ) ;
12801281 true
@@ -1370,7 +1371,7 @@ impl Compiler {
13701371 . position ( |var| * var == "__class__" ) ;
13711372
13721373 if let Some ( classcell_idx) = classcell_idx {
1373- emit ! ( self , Instruction :: LoadClosure ( classcell_idx as u32 ) ) ;
1374+ emit ! ( self , Instruction :: LoadClosure ( classcell_idx. to_u32 ( ) ) ) ;
13741375 emit ! ( self , Instruction :: Duplicate ) ;
13751376 let classcell = self . name ( "__classcell__" ) ;
13761377 emit ! ( self , Instruction :: StoreLocal ( classcell) ) ;
@@ -1775,7 +1776,7 @@ impl Compiler {
17751776 emit ! (
17761777 self ,
17771778 Instruction :: UnpackSequence {
1778- size: elts. len( ) as u32 ,
1779+ size: elts. len( ) . to_u32 ( ) ,
17791780 }
17801781 ) ;
17811782 }
@@ -2175,7 +2176,7 @@ impl Compiler {
21752176 emit ! (
21762177 self ,
21772178 Instruction :: BuildString {
2178- size: values. len( ) as u32 ,
2179+ size: values. len( ) . to_u32 ( ) ,
21792180 }
21802181 )
21812182 }
@@ -2244,7 +2245,7 @@ impl Compiler {
22442245 emit ! (
22452246 compiler,
22462247 Instruction :: ListAppend {
2247- i: generators. len( ) as u32 ,
2248+ i: generators. len( ) . to_u32 ( ) ,
22482249 }
22492250 ) ;
22502251 Ok ( ( ) )
@@ -2263,7 +2264,7 @@ impl Compiler {
22632264 emit ! (
22642265 compiler,
22652266 Instruction :: SetAdd {
2266- i: generators. len( ) as u32 ,
2267+ i: generators. len( ) . to_u32 ( ) ,
22672268 }
22682269 ) ;
22692270 Ok ( ( ) )
@@ -2289,7 +2290,7 @@ impl Compiler {
22892290 emit ! (
22902291 compiler,
22912292 Instruction :: MapAdd {
2292- i: generators. len( ) as u32 ,
2293+ i: generators. len( ) . to_u32 ( ) ,
22932294 }
22942295 ) ;
22952296
@@ -2420,7 +2421,7 @@ impl Compiler {
24202421 args : & [ ast:: Expr ] ,
24212422 keywords : & [ ast:: Keyword ] ,
24222423 ) -> CompileResult < CallType > {
2423- let count = ( args. len ( ) + keywords. len ( ) ) as u32 + additional_positional;
2424+ let count = ( args. len ( ) + keywords. len ( ) ) . to_u32 ( ) + additional_positional;
24242425
24252426 // Normal arguments:
24262427 let ( size, unpack) = self . gather_elements ( additional_positional, args) ?;
@@ -2521,7 +2522,7 @@ impl Compiler {
25212522 for element in elements {
25222523 self . compile_expression ( element) ?;
25232524 }
2524- before + elements. len ( ) as u32
2525+ before + elements. len ( ) . to_u32 ( )
25252526 } ;
25262527
25272528 Ok ( ( size, has_stars) )
@@ -2710,7 +2711,7 @@ impl Compiler {
27102711
27112712 fn emit_constant ( & mut self , constant : ConstantData ) {
27122713 let info = self . current_codeinfo ( ) ;
2713- let idx = info. constants . insert_full ( constant) . 0 as u32 ;
2714+ let idx = info. constants . insert_full ( constant) . 0 . to_u32 ( ) ;
27142715 self . emit_arg ( idx, |idx| Instruction :: LoadConst { idx } )
27152716 }
27162717
@@ -2725,7 +2726,7 @@ impl Compiler {
27252726
27262727 fn new_block ( & mut self ) -> ir:: BlockIdx {
27272728 let code = self . current_codeinfo ( ) ;
2728- let idx = ir:: BlockIdx ( code. blocks . len ( ) as u32 ) ;
2729+ let idx = ir:: BlockIdx ( code. blocks . len ( ) . to_u32 ( ) ) ;
27292730 code. blocks . push ( ir:: Block :: default ( ) ) ;
27302731 idx
27312732 }
@@ -2753,7 +2754,7 @@ impl Compiler {
27532754 }
27542755
27552756 fn get_source_line_number ( & self ) -> u32 {
2756- self . current_source_location . row ( ) as u32
2757+ self . current_source_location . row ( ) . to_u32 ( )
27572758 }
27582759
27592760 fn push_qualified_path ( & mut self , name : & str ) {
@@ -2849,6 +2850,17 @@ fn compile_constant(value: &ast::Constant) -> ConstantData {
28492850 }
28502851}
28512852
2853+ // Note: Not a good practice in general. Keep this trait private only for compiler
2854+ trait ToU32 {
2855+ fn to_u32 ( self ) -> u32 ;
2856+ }
2857+
2858+ impl ToU32 for usize {
2859+ fn to_u32 ( self ) -> u32 {
2860+ self . try_into ( ) . unwrap ( )
2861+ }
2862+ }
2863+
28522864#[ cfg( test) ]
28532865mod tests {
28542866 use super :: * ;
0 commit comments