@@ -31,6 +31,7 @@ def g() -> int:
3131from mypyc .irbuild .builder import IRBuilder , SymbolTarget
3232from mypyc .irbuild .context import FuncInfo , GeneratorClass , ImplicitClass
3333from mypyc .irbuild .targets import AssignmentTargetAttr
34+ from mypyc .irbuild .util import get_func_target_symbol
3435
3536
3637def setup_env_class (builder : IRBuilder ) -> ClassIR :
@@ -141,7 +142,7 @@ def load_env_registers(builder: IRBuilder, prefix: str = "") -> None:
141142 # If this is a FuncDef, then make sure to load the FuncDef into its own environment
142143 # class so that the function can be called recursively.
143144 if isinstance (fitem , FuncDef ) and fn_info .add_nested_funcs_to_env :
144- setup_func_for_recursive_call (builder , fitem , fn_info .callable_class , prefix = prefix )
145+ setup_func_for_recursive_call (builder , fitem , fn_info .callable_class )
145146
146147
147148def load_outer_env (
@@ -298,8 +299,13 @@ def add_vars_to_env(builder: IRBuilder, prefix: str = "") -> None:
298299 )
299300
300301 if fn_info .fitem in builder .encapsulating_funcs :
302+ added : set [SymbolNode ] = set ()
301303 for nested_fn in builder .encapsulating_funcs [fn_info .fitem ]:
302304 if isinstance (nested_fn , FuncDef ):
305+ target_symbol = get_func_target_symbol (nested_fn )
306+ if target_symbol in added :
307+ continue
308+ added .add (target_symbol )
303309 # The return type is 'object' instead of an RInstance of the
304310 # callable class because differently defined functions with
305311 # the same name and signature across conditional blocks
@@ -308,18 +314,18 @@ def add_vars_to_env(builder: IRBuilder, prefix: str = "") -> None:
308314 nested_prefix = prefix
309315 if nested_fn .is_generator or nested_fn .is_coroutine :
310316 nested_prefix = GENERATOR_ATTRIBUTE_PREFIX
311- keep_alive = is_free_variable (builder , nested_fn )
312- if fn_info .is_generator and not builder .is_captured_by_nested_func (nested_fn ):
317+ keep_alive = is_free_variable (builder , target_symbol )
318+ if fn_info .is_generator and not builder .is_captured_by_nested_func (target_symbol ):
313319 builder .add_var_to_generator_frame (
314- nested_fn ,
320+ target_symbol ,
315321 object_rprimitive ,
316322 fn_info .generator_class .self_reg ,
317323 reassign = False ,
318324 keep_alive_on_completion = keep_alive ,
319325 )
320326 else :
321327 builder .add_var_to_env_class (
322- nested_fn ,
328+ target_symbol ,
323329 object_rprimitive ,
324330 env_for_func ,
325331 reassign = False ,
@@ -328,20 +334,26 @@ def add_vars_to_env(builder: IRBuilder, prefix: str = "") -> None:
328334 )
329335
330336
331- def setup_func_for_recursive_call (
332- builder : IRBuilder , fdef : FuncDef , base : ImplicitClass , prefix : str = ""
333- ) -> None :
337+ def setup_func_for_recursive_call (builder : IRBuilder , fdef : FuncDef , base : ImplicitClass ) -> None :
334338 """Enable calling a nested function (with a callable class) recursively.
335339
336340 Adds the instance of the callable class representing the given
337341 FuncDef to a register in the environment so that the function can
338342 be called recursively. Note that this needs to be done only for
339343 nested functions.
340344 """
341- # First, set the attribute of the environment class so that GetAttr can be called on it.
342345 prev_env = builder .fn_infos [- 2 ].env_class
343- attr_name = prefix + fdef .name
344- prev_env .attributes [attr_name ] = builder .type_to_rtype (fdef .type )
346+ target_symbol = get_func_target_symbol (fdef )
347+ target = builder .symtables [- 2 ].get (target_symbol )
348+ if (
349+ not isinstance (target_symbol , FuncDef )
350+ or not isinstance (target , AssignmentTargetAttr )
351+ or not isinstance (target .obj .type , RInstance )
352+ or target .obj .type .class_ir is not prev_env
353+ ):
354+ return
355+
356+ attr_name = target .attr
345357 line = fdef .line
346358
347359 if isinstance (base , GeneratorClass ):
@@ -355,7 +367,7 @@ def setup_func_for_recursive_call(
355367 # Obtain the instance of the callable class representing the FuncDef, and add it to the
356368 # current environment.
357369 val = builder .add (GetAttr (prev_env_reg , attr_name , line ))
358- target = builder .add_local_reg (fdef , object_rprimitive )
370+ target = builder .add_local_reg (target_symbol , object_rprimitive )
359371 builder .assign (target , val , line )
360372
361373
0 commit comments