66//! <https://github.com/micropython/micropython/blob/master/py/compile.c>
77
88use crate :: {
9- error:: { CompileError , CompileErrorType } ,
9+ error:: { CodegenError , CodegenErrorType } ,
1010 ir,
1111 symboltable:: { self , SymbolScope , SymbolTable } ,
1212 IndexSet ,
@@ -20,7 +20,7 @@ use std::borrow::Cow;
2020
2121pub use rustpython_bytecode:: Mode ;
2222
23- type CompileResult < T > = Result < T , CompileError > ;
23+ type CompileResult < T > = Result < T , CodegenError > ;
2424
2525#[ derive( PartialEq , Eq , Clone , Copy ) ]
2626enum NameUsage {
@@ -129,7 +129,7 @@ fn compile_impl<Ast: ?Sized>(
129129) -> CompileResult < CodeObject > {
130130 let symbol_table = match make_symbol_table ( ast) {
131131 Ok ( x) => x,
132- Err ( e) => return Err ( e. into_compile_error ( source_path) ) ,
132+ Err ( e) => return Err ( e. into_codegen_error ( source_path) ) ,
133133 } ;
134134
135135 let mut compiler = Compiler :: new ( opts, source_path, "<module>" . to_owned ( ) ) ;
@@ -234,11 +234,11 @@ impl Compiler {
234234 }
235235 }
236236
237- fn error ( & self , error : CompileErrorType ) -> CompileError {
237+ fn error ( & self , error : CodegenErrorType ) -> CodegenError {
238238 self . error_loc ( error, self . current_source_location )
239239 }
240- fn error_loc ( & self , error : CompileErrorType , location : ast:: Location ) -> CompileError {
241- CompileError {
240+ fn error_loc ( & self , error : CodegenErrorType , location : ast:: Location ) -> CodegenError {
241+ CodegenError {
242242 error,
243243 location,
244244 source_path : self . source_path . clone ( ) ,
@@ -455,7 +455,7 @@ impl Compiler {
455455 NameUsage :: Delete if is_forbidden_name ( name) => "cannot delete" ,
456456 _ => return Ok ( ( ) ) ,
457457 } ;
458- Err ( self . error ( CompileErrorType :: SyntaxError ( format ! ( "{} {}" , msg, name) ) ) )
458+ Err ( self . error ( CodegenErrorType :: SyntaxError ( format ! ( "{} {}" , msg, name) ) ) )
459459 }
460460
461461 fn compile_name ( & mut self , name : & str , usage : NameUsage ) -> CompileResult < ( ) > {
@@ -580,7 +580,7 @@ impl Compiler {
580580 let from_list = if import_star {
581581 if self . ctx . in_func ( ) {
582582 return Err ( self
583- . error_loc ( CompileErrorType :: FunctionImportStar , statement. location ) ) ;
583+ . error_loc ( CodegenErrorType :: FunctionImportStar , statement. location ) ) ;
584584 }
585585 vec ! [ ConstantData :: Str {
586586 value: "*" . to_owned( ) ,
@@ -769,7 +769,7 @@ impl Compiler {
769769 self . emit ( Instruction :: Break { target : end } ) ;
770770 }
771771 None => {
772- return Err ( self . error_loc ( CompileErrorType :: InvalidBreak , statement. location ) ) ;
772+ return Err ( self . error_loc ( CodegenErrorType :: InvalidBreak , statement. location ) ) ;
773773 }
774774 } ,
775775 Continue => match self . ctx . loop_data {
@@ -778,13 +778,13 @@ impl Compiler {
778778 }
779779 None => {
780780 return Err (
781- self . error_loc ( CompileErrorType :: InvalidContinue , statement. location )
781+ self . error_loc ( CodegenErrorType :: InvalidContinue , statement. location )
782782 ) ;
783783 }
784784 } ,
785785 Return { value } => {
786786 if !self . ctx . in_func ( ) {
787- return Err ( self . error_loc ( CompileErrorType :: InvalidReturn , statement. location ) ) ;
787+ return Err ( self . error_loc ( CodegenErrorType :: InvalidReturn , statement. location ) ) ;
788788 }
789789 match value {
790790 Some ( v) => {
@@ -795,7 +795,7 @@ impl Compiler {
795795 . contains ( bytecode:: CodeFlags :: IS_GENERATOR )
796796 {
797797 return Err ( self . error_loc (
798- CompileErrorType :: AsyncReturnValue ,
798+ CodegenErrorType :: AsyncReturnValue ,
799799 statement. location ,
800800 ) ) ;
801801 }
@@ -857,9 +857,9 @@ impl Compiler {
857857 }
858858 }
859859 ast:: ExprKind :: BinOp { .. } | ast:: ExprKind :: UnaryOp { .. } => {
860- return Err ( self . error ( CompileErrorType :: Delete ( "expression" ) ) )
860+ return Err ( self . error ( CodegenErrorType :: Delete ( "expression" ) ) )
861861 }
862- _ => return Err ( self . error ( CompileErrorType :: Delete ( expression. node . name ( ) ) ) ) ,
862+ _ => return Err ( self . error ( CodegenErrorType :: Delete ( expression. node . name ( ) ) ) ) ,
863863 }
864864 Ok ( ( ) )
865865 }
@@ -922,7 +922,7 @@ impl Compiler {
922922 . chain ( & args. kwonlyargs ) ;
923923 for name in args_iter {
924924 if Compiler :: is_forbidden_arg_name ( & name. node . arg ) {
925- return Err ( self . error ( CompileErrorType :: SyntaxError ( format ! (
925+ return Err ( self . error ( CodegenErrorType :: SyntaxError ( format ! (
926926 "cannot assign to {}" ,
927927 & name. node. arg
928928 ) ) ) ) ;
@@ -1401,7 +1401,7 @@ impl Compiler {
14011401 let ( item, items) = if let Some ( parts) = items. split_first ( ) {
14021402 parts
14031403 } else {
1404- return Err ( self . error ( CompileErrorType :: EmptyWithItems ) ) ;
1404+ return Err ( self . error ( CodegenErrorType :: EmptyWithItems ) ) ;
14051405 } ;
14061406
14071407 let final_block = {
@@ -1433,7 +1433,7 @@ impl Compiler {
14331433
14341434 if items. is_empty ( ) {
14351435 if body. is_empty ( ) {
1436- return Err ( self . error ( CompileErrorType :: EmptyWithBody ) ) ;
1436+ return Err ( self . error ( CodegenErrorType :: EmptyWithBody ) ) ;
14371437 }
14381438 self . compile_statements ( body) ?;
14391439 } else {
@@ -1529,7 +1529,7 @@ impl Compiler {
15291529 ) -> CompileResult < ( ) > {
15301530 eprintln ! ( "match subject: {subject:?}" ) ;
15311531 eprintln ! ( "match cases: {cases:?}" ) ;
1532- Err ( self . error ( CompileErrorType :: NotImplementedYet ) )
1532+ Err ( self . error ( CodegenErrorType :: NotImplementedYet ) )
15331533 }
15341534
15351535 fn compile_chained_comparison (
@@ -1699,15 +1699,15 @@ impl Compiler {
16991699 for ( i, element) in elts. iter ( ) . enumerate ( ) {
17001700 if let ast:: ExprKind :: Starred { .. } = & element. node {
17011701 if seen_star {
1702- return Err ( self . error ( CompileErrorType :: MultipleStarArgs ) ) ;
1702+ return Err ( self . error ( CodegenErrorType :: MultipleStarArgs ) ) ;
17031703 } else {
17041704 seen_star = true ;
17051705 let before = i;
17061706 let after = elts. len ( ) - i - 1 ;
17071707 let ( before, after) = ( || Some ( ( before. to_u8 ( ) ?, after. to_u8 ( ) ?) ) ) ( )
17081708 . ok_or_else ( || {
17091709 self . error_loc (
1710- CompileErrorType :: TooManyStarUnpack ,
1710+ CodegenErrorType :: TooManyStarUnpack ,
17111711 target. location ,
17121712 )
17131713 } ) ?;
@@ -1732,10 +1732,10 @@ impl Compiler {
17321732 }
17331733 _ => {
17341734 return Err ( self . error ( match target. node {
1735- ast:: ExprKind :: Starred { .. } => CompileErrorType :: SyntaxError (
1735+ ast:: ExprKind :: Starred { .. } => CodegenErrorType :: SyntaxError (
17361736 "starred assignment target must be in a list or tuple" . to_owned ( ) ,
17371737 ) ,
1738- _ => CompileErrorType :: Assign ( target. node . name ( ) ) ,
1738+ _ => CodegenErrorType :: Assign ( target. node . name ( ) ) ,
17391739 } ) ) ;
17401740 }
17411741 }
@@ -1776,7 +1776,7 @@ impl Compiler {
17761776 AugAssignKind :: Attr { idx }
17771777 }
17781778 _ => {
1779- return Err ( self . error ( CompileErrorType :: Assign ( target. node . name ( ) ) ) ) ;
1779+ return Err ( self . error ( CodegenErrorType :: Assign ( target. node . name ( ) ) ) ) ;
17801780 }
17811781 } ;
17821782
@@ -2049,7 +2049,7 @@ impl Compiler {
20492049 }
20502050 Yield { value } => {
20512051 if !self . ctx . in_func ( ) {
2052- return Err ( self . error ( CompileErrorType :: InvalidYield ) ) ;
2052+ return Err ( self . error ( CodegenErrorType :: InvalidYield ) ) ;
20532053 }
20542054 self . mark_generator ( ) ;
20552055 match value {
@@ -2060,7 +2060,7 @@ impl Compiler {
20602060 }
20612061 Await { value } => {
20622062 if self . ctx . func != FunctionContext :: AsyncFunction {
2063- return Err ( self . error ( CompileErrorType :: InvalidAwait ) ) ;
2063+ return Err ( self . error ( CodegenErrorType :: InvalidAwait ) ) ;
20642064 }
20652065 self . compile_expression ( value) ?;
20662066 self . emit ( Instruction :: GetAwaitable ) ;
@@ -2070,10 +2070,10 @@ impl Compiler {
20702070 YieldFrom { value } => {
20712071 match self . ctx . func {
20722072 FunctionContext :: NoFunction => {
2073- return Err ( self . error ( CompileErrorType :: InvalidYieldFrom ) ) ;
2073+ return Err ( self . error ( CodegenErrorType :: InvalidYieldFrom ) ) ;
20742074 }
20752075 FunctionContext :: AsyncFunction => {
2076- return Err ( self . error ( CompileErrorType :: AsyncYieldFrom ) ) ;
2076+ return Err ( self . error ( CodegenErrorType :: AsyncYieldFrom ) ) ;
20772077 }
20782078 FunctionContext :: Function => { }
20792079 }
@@ -2208,7 +2208,7 @@ impl Compiler {
22082208 } ) ?;
22092209 }
22102210 Starred { .. } => {
2211- return Err ( self . error ( CompileErrorType :: InvalidStarExpr ) ) ;
2211+ return Err ( self . error ( CodegenErrorType :: InvalidStarExpr ) ) ;
22122212 }
22132213 IfExp { test, body, orelse } => {
22142214 let else_block = self . new_block ( ) ;
@@ -2417,8 +2417,8 @@ impl Compiler {
24172417
24182418 fn compile_comprehension_element ( & mut self , element : & ast:: Expr ) -> CompileResult < ( ) > {
24192419 self . compile_expression ( element) . map_err ( |e| {
2420- if let CompileErrorType :: InvalidStarExpr = e. error {
2421- self . error ( CompileErrorType :: SyntaxError (
2420+ if let CodegenErrorType :: InvalidStarExpr = e. error {
2421+ self . error ( CodegenErrorType :: SyntaxError (
24222422 "iterable unpacking cannot be used in comprehension" . to_owned ( ) ,
24232423 ) )
24242424 } else {
@@ -2547,9 +2547,9 @@ impl Compiler {
25472547 Ok ( ( ) )
25482548 }
25492549
2550- fn compile_future_features ( & mut self , features : & [ ast:: Alias ] ) -> Result < ( ) , CompileError > {
2550+ fn compile_future_features ( & mut self , features : & [ ast:: Alias ] ) -> Result < ( ) , CodegenError > {
25512551 if self . done_with_future_stmts {
2552- return Err ( self . error ( CompileErrorType :: InvalidFuturePlacement ) ) ;
2552+ return Err ( self . error ( CodegenErrorType :: InvalidFuturePlacement ) ) ;
25532553 }
25542554 for feature in features {
25552555 match & * feature. node . name {
@@ -2559,7 +2559,7 @@ impl Compiler {
25592559 // "generator_stop" => {}
25602560 "annotations" => self . future_annotations = true ,
25612561 other => {
2562- return Err ( self . error ( CompileErrorType :: InvalidFutureFeature ( other. to_owned ( ) ) ) )
2562+ return Err ( self . error ( CodegenErrorType :: InvalidFutureFeature ( other. to_owned ( ) ) ) )
25632563 }
25642564 }
25652565 }
0 commit comments