@@ -3,7 +3,7 @@ use std::cell::RefCell;
33use std:: vec;
44use anyhow:: Error ;
55use ruff_text_size:: { Ranged , TextRange , TextSize } ;
6- use ruff_python_ast:: { Alias , AnyRootNodeRef , CmpOp , Expr , ExprNamed , ExprTuple , FStringPart , Identifier , Pattern , Stmt , StmtAnnAssign , StmtAssign , StmtClassDef , StmtFor , StmtFunctionDef , StmtIf , StmtMatch , StmtTry , StmtWhile , StmtWith } ;
6+ use ruff_python_ast:: { Alias , AnyRootNodeRef , CmpOp , Expr , ExprNamed , ExprTuple , FStringPart , Identifier , Parameters , Pattern , Stmt , StmtAnnAssign , StmtAssign , StmtClassDef , StmtFor , StmtFunctionDef , StmtIf , StmtMatch , StmtTry , StmtWhile , StmtWith } ;
77use lsp_types:: Diagnostic ;
88use tracing:: { trace, warn} ;
99use weak_table:: traits:: WeakElement ;
@@ -456,12 +456,16 @@ impl PythonArchBuilder {
456456 expr_slice. lower . as_ref ( ) . map ( |lower_expr| self . visit_expr ( session, & lower_expr) ) ;
457457 } ,
458458 // Expressions that cannot contained a named expressions are not traversed
459- Expr :: Lambda ( _todo_lambda_expr) => {
460- // Lambdas can have named expressions, but it is not a common use
461- // Like lambda vals: vals[(x := 0): x + 3]
462- // However x is only in scope in the lambda expression only
463- // It needs adding a new function, ast_indexes, then add the variable inside
464- // I deem it currently unnecessary
459+ Expr :: Lambda ( lambda_expr) => {
460+ let sym = self . sym_stack . last ( ) . unwrap ( ) . borrow_mut ( ) . add_new_function (
461+ session, & S ! ( "<lambda>" ) , & lambda_expr. range , & lambda_expr. body . range ( ) . start ( )
462+ ) ;
463+ if let Some ( parameters) = & lambda_expr. parameters {
464+ PythonArchBuilder :: handle_func_args ( & sym, session, & parameters) ;
465+ }
466+ self . sym_stack . push ( sym. clone ( ) ) ;
467+ self . visit_expr ( session, & lambda_expr. body ) ;
468+ self . sym_stack . pop ( ) ;
465469 } ,
466470 Expr :: Generator ( _todo_expr_generator) => {
467471 // generators are lazily evaluated,
@@ -661,6 +665,67 @@ impl PythonArchBuilder {
661665 }
662666 }
663667
668+ fn handle_func_args ( fun_sym : & Rc < RefCell < Symbol > > , session : & mut SessionInfo , parameters : & Parameters ) {
669+ for arg in parameters. posonlyargs . iter ( ) {
670+ let param = fun_sym. borrow_mut ( ) . add_new_variable ( session, oyarn ! ( "{}" , arg. parameter. name. id) , & arg. range ) ;
671+ param. borrow_mut ( ) . as_variable_mut ( ) . is_parameter = true ;
672+ let mut default = None ;
673+ if arg. default . is_some ( ) {
674+ default = Some ( Evaluation :: new_none ( ) ) ; //TODO evaluate default? actually only used to know if there is a default or not
675+ }
676+ fun_sym. borrow_mut ( ) . as_func_mut ( ) . args . push ( Argument {
677+ symbol : Rc :: downgrade ( & param) ,
678+ default_value : default,
679+ arg_type : ArgumentType :: POS_ONLY ,
680+ annotation : arg. parameter . annotation . clone ( ) ,
681+ } ) ;
682+ }
683+ for arg in parameters. args . iter ( ) {
684+ let param = fun_sym. borrow_mut ( ) . add_new_variable ( session, oyarn ! ( "{}" , arg. parameter. name. id) , & arg. range ) ;
685+ param. borrow_mut ( ) . as_variable_mut ( ) . is_parameter = true ;
686+ let mut default = None ;
687+ if arg. default . is_some ( ) {
688+ default = Some ( Evaluation :: new_none ( ) ) ; //TODO evaluate default? actually only used to know if there is a default or not
689+ }
690+ fun_sym. borrow_mut ( ) . as_func_mut ( ) . args . push ( Argument {
691+ symbol : Rc :: downgrade ( & param) ,
692+ default_value : default,
693+ arg_type : ArgumentType :: ARG ,
694+ annotation : arg. parameter . annotation . clone ( ) ,
695+ } ) ;
696+ }
697+ if let Some ( arg) = & parameters. vararg {
698+ let param = fun_sym. borrow_mut ( ) . add_new_variable ( session, oyarn ! ( "{}" , arg. name. id) , & arg. range ) ;
699+ param. borrow_mut ( ) . as_variable_mut ( ) . is_parameter = true ;
700+ fun_sym. borrow_mut ( ) . as_func_mut ( ) . args . push ( Argument {
701+ symbol : Rc :: downgrade ( & param) ,
702+ default_value : None ,
703+ arg_type : ArgumentType :: VARARG ,
704+ annotation : arg. annotation . clone ( ) ,
705+ } ) ;
706+ }
707+ for arg in parameters. kwonlyargs . iter ( ) {
708+ let param = fun_sym. borrow_mut ( ) . add_new_variable ( session, oyarn ! ( "{}" , arg. parameter. name. id) , & arg. range ) ;
709+ param. borrow_mut ( ) . as_variable_mut ( ) . is_parameter = true ;
710+ fun_sym. borrow_mut ( ) . as_func_mut ( ) . args . push ( Argument {
711+ symbol : Rc :: downgrade ( & param) ,
712+ default_value : arg. default . as_ref ( ) . map ( |_default| Evaluation :: new_none ( ) ) ,
713+ arg_type : ArgumentType :: KWORD_ONLY ,
714+ annotation : arg. parameter . annotation . clone ( ) ,
715+ } ) ;
716+ }
717+ if let Some ( arg) = & parameters. kwarg {
718+ let param = fun_sym. borrow_mut ( ) . add_new_variable ( session, oyarn ! ( "{}" , arg. name. id) , & arg. range ) ;
719+ param. borrow_mut ( ) . as_variable_mut ( ) . is_parameter = true ;
720+ fun_sym. borrow_mut ( ) . as_func_mut ( ) . args . push ( Argument {
721+ symbol : Rc :: downgrade ( & param) ,
722+ default_value : None ,
723+ arg_type : ArgumentType :: KWARG ,
724+ annotation : arg. annotation . clone ( ) ,
725+ } ) ;
726+ }
727+ }
728+
664729 fn visit_func_def ( & mut self , session : & mut SessionInfo , func_def : & StmtFunctionDef ) -> Result < ( ) , Error > {
665730 if func_def. body . is_empty ( ) {
666731 return Ok ( ( ) ) //if body is empty, it usually means that the ast of the class is invalid. Skip it
@@ -706,64 +771,7 @@ impl PythonArchBuilder {
706771 }
707772 drop ( sym_bw) ;
708773 //add params
709- for arg in func_def. parameters . posonlyargs . iter ( ) {
710- let param = sym. borrow_mut ( ) . add_new_variable ( session, oyarn ! ( "{}" , arg. parameter. name. id) , & arg. range ) ;
711- param. borrow_mut ( ) . as_variable_mut ( ) . is_parameter = true ;
712- let mut default = None ;
713- if arg. default . is_some ( ) {
714- default = Some ( Evaluation :: new_none ( ) ) ; //TODO evaluate default? actually only used to know if there is a default or not
715- }
716- sym. borrow_mut ( ) . as_func_mut ( ) . args . push ( Argument {
717- symbol : Rc :: downgrade ( & param) ,
718- default_value : default,
719- arg_type : ArgumentType :: POS_ONLY ,
720- annotation : arg. parameter . annotation . clone ( ) ,
721- } ) ;
722- }
723- for arg in func_def. parameters . args . iter ( ) {
724- let param = sym. borrow_mut ( ) . add_new_variable ( session, oyarn ! ( "{}" , arg. parameter. name. id) , & arg. range ) ;
725- param. borrow_mut ( ) . as_variable_mut ( ) . is_parameter = true ;
726- let mut default = None ;
727- if arg. default . is_some ( ) {
728- default = Some ( Evaluation :: new_none ( ) ) ; //TODO evaluate default? actually only used to know if there is a default or not
729- }
730- sym. borrow_mut ( ) . as_func_mut ( ) . args . push ( Argument {
731- symbol : Rc :: downgrade ( & param) ,
732- default_value : default,
733- arg_type : ArgumentType :: ARG ,
734- annotation : arg. parameter . annotation . clone ( ) ,
735- } ) ;
736- }
737- if let Some ( arg) = & func_def. parameters . vararg {
738- let param = sym. borrow_mut ( ) . add_new_variable ( session, oyarn ! ( "{}" , arg. name. id) , & arg. range ) ;
739- param. borrow_mut ( ) . as_variable_mut ( ) . is_parameter = true ;
740- sym. borrow_mut ( ) . as_func_mut ( ) . args . push ( Argument {
741- symbol : Rc :: downgrade ( & param) ,
742- default_value : None ,
743- arg_type : ArgumentType :: VARARG ,
744- annotation : arg. annotation . clone ( ) ,
745- } ) ;
746- }
747- for arg in func_def. parameters . kwonlyargs . iter ( ) {
748- let param = sym. borrow_mut ( ) . add_new_variable ( session, oyarn ! ( "{}" , arg. parameter. name. id) , & arg. range ) ;
749- param. borrow_mut ( ) . as_variable_mut ( ) . is_parameter = true ;
750- sym. borrow_mut ( ) . as_func_mut ( ) . args . push ( Argument {
751- symbol : Rc :: downgrade ( & param) ,
752- default_value : arg. default . as_ref ( ) . map ( |_default| Evaluation :: new_none ( ) ) ,
753- arg_type : ArgumentType :: KWORD_ONLY ,
754- annotation : arg. parameter . annotation . clone ( ) ,
755- } ) ;
756- }
757- if let Some ( arg) = & func_def. parameters . kwarg {
758- let param = sym. borrow_mut ( ) . add_new_variable ( session, oyarn ! ( "{}" , arg. name. id) , & arg. range ) ;
759- param. borrow_mut ( ) . as_variable_mut ( ) . is_parameter = true ;
760- sym. borrow_mut ( ) . as_func_mut ( ) . args . push ( Argument {
761- symbol : Rc :: downgrade ( & param) ,
762- default_value : None ,
763- arg_type : ArgumentType :: KWARG ,
764- annotation : arg. annotation . clone ( ) ,
765- } ) ;
766- }
774+ PythonArchBuilder :: handle_func_args ( & sym, session, & func_def. parameters ) ;
767775 let mut add_noqa = false ;
768776 if let Some ( noqa_bloc) = self . file_info . as_ref ( ) . unwrap ( ) . borrow ( ) . noqas_blocs . get ( & func_def. range . start ( ) . to_u32 ( ) ) {
769777 session. noqas_stack . push ( noqa_bloc. clone ( ) ) ;
0 commit comments