@@ -2369,10 +2369,17 @@ function expand_function_generator(ctx, srcref, callex_srcref, func_name, func_n
23692369 push! (gen_arg_types, @ast ctx gen_arg_names[i] " Any" :: K"core" )
23702370 end
23712371 # Code generator definition
2372- gen_func_method_defs = @ast ctx srcref [K " method_defs"
2373- gen_name
2374- method_def_expr (ctx, srcref, callex_srcref, nothing , SyntaxList (ctx), gen_arg_names,
2375- gen_arg_types, gen_body, nothing )
2372+ gen_func_method_defs = @ast ctx srcref [K " block"
2373+ [K " function_decl" gen_name]
2374+ [K " scope_block" (scope_type= :hard )
2375+ [K " method_defs"
2376+ gen_name
2377+ [K " block"
2378+ method_def_expr (ctx, srcref, callex_srcref, nothing , SyntaxList (ctx),
2379+ gen_arg_names, gen_arg_types, gen_body, nothing )
2380+ ]
2381+ ]
2382+ ]
23762383 ]
23772384
23782385 # Extract non-generated body
@@ -2414,7 +2421,7 @@ function expand_function_generator(ctx, srcref, callex_srcref, func_name, func_n
24142421 split_generated (ctx, body, false )
24152422 ]
24162423
2417- return gen_name, gen_func_method_defs, nongen_body
2424+ return gen_func_method_defs, nongen_body
24182425end
24192426
24202427# Generate a method for every number of allowed optional arguments
@@ -2472,8 +2479,8 @@ function scope_nest(ctx, names, values, body)
24722479end
24732480
24742481# Generate body function and `Core.kwcall` overloads for functions taking keywords.
2475- function keyword_function_defs (ctx, srcref, callex_srcref, name_str,
2476- typevar_names, typevar_stmts , arg_names,
2482+ function keyword_function_defs (ctx, srcref, callex_srcref, name_str, typevar_names,
2483+ typevar_stmts, new_typevar_stmts , arg_names,
24772484 arg_types, has_slurp, first_default, arg_defaults,
24782485 keywords, body, ret_var)
24792486 mangled_name = let n = isnothing (name_str) ? " _" : name_str
@@ -2700,23 +2707,29 @@ function keyword_function_defs(ctx, srcref, callex_srcref, name_str,
27002707 check_all_typevars_used (body_arg_types, typevar_names, typevar_stmts)
27012708
27022709 kw_func_method_defs = @ast ctx srcref [K " block"
2703- [K " method_defs"
2704- body_func_name
2705- [K " block"
2706- # TODO : nkw
2707- method_def_expr (ctx, srcref, callex_srcref, " nothing" :: K"core" ,
2708- typevar_names, body_arg_names, body_arg_types,
2709- [K " block"
2710- [K " meta" " nkw" :: K"Symbol" numchildren (keywords):: K"Integer" ]
2711- body
2712- ],
2713- ret_var)
2710+ [K " function_decl" body_func_name]
2711+ [K " scope_block" (scope_type= :hard )
2712+ [K " method_defs"
2713+ body_func_name
2714+ [K " block"
2715+ new_typevar_stmts...
2716+ method_def_expr (ctx, srcref, callex_srcref, " nothing" :: K"core" ,
2717+ typevar_names, body_arg_names, body_arg_types,
2718+ [K " block"
2719+ [K " meta" " nkw" :: K"Symbol" numchildren (keywords):: K"Integer" ]
2720+ body
2721+ ],
2722+ ret_var)
2723+ ]
27142724 ]
27152725 ]
2716- [K " method_defs"
2717- " nothing" :: K"core"
2718- [K " block"
2719- kwcall_method_defs...
2726+ [K " scope_block" (scope_type= :hard )
2727+ [K " method_defs"
2728+ " nothing" :: K"core"
2729+ [K " block"
2730+ new_typevar_stmts...
2731+ kwcall_method_defs...
2732+ ]
27202733 ]
27212734 ]
27222735 ]
@@ -2737,7 +2750,7 @@ function keyword_function_defs(ctx, srcref, callex_srcref, name_str,
27372750 ]
27382751 end
27392752
2740- body_func_name, kw_func_method_defs, body_for_positional_args_only
2753+ kw_func_method_defs, body_for_positional_args_only
27412754end
27422755
27432756# Check valid identifier/function names
@@ -2936,27 +2949,27 @@ function expand_function_def(ctx, ex, docs, rewrite_call=identity, rewrite_body=
29362949 ]
29372950 end
29382951
2939- gen_func_name = nothing
29402952 gen_func_method_defs = nothing
29412953 if is_generated (body)
2942- gen_func_name, gen_func_method_defs, body =
2954+ gen_func_method_defs, body =
29432955 expand_function_generator (ctx, ex, callex, name, name_str, body, arg_names, typevar_names)
29442956
29452957 end
29462958
29472959 if isnothing (keywords)
2948- body_func_name, kw_func_method_defs = (nothing , nothing )
2949- # NB: This check seems good as it statically catches any useless
2950- # typevars which can't be inferred. However it wasn't previously an
2951- # error so we might need to reduce it to a warning?
2960+ kw_func_method_defs = nothing
2961+ # NB: The following check seems good as it statically catches any useless
2962+ # static parameters which can't be bound during method invocation.
2963+ # However it wasn't previously an error so we might need to reduce it
2964+ # to a warning?
29522965 check_all_typevars_used (arg_types, typevar_names, typevar_stmts)
29532966 main_typevar_names = typevar_names
29542967 else
29552968 # Rewrite `body` here so that the positional-only versions dispatch there.
2956- body_func_name, kw_func_method_defs, body =
2969+ kw_func_method_defs, body =
29572970 keyword_function_defs (ctx, ex, callex, name_str, typevar_names, typevar_stmts,
2958- arg_names, arg_types, has_slurp, first_default, arg_defaults ,
2959- keywords, body, ret_var)
2971+ new_typevar_stmts, arg_names, arg_types, has_slurp,
2972+ first_default, arg_defaults, keywords, body, ret_var)
29602973 # The main function (but without keywords) needs its typevars trimmed,
29612974 # as some of them may be for the keywords only.
29622975 main_typevar_names = trim_used_typevars (ctx, arg_types, typevar_names, typevar_stmts)
@@ -2993,28 +3006,22 @@ function expand_function_def(ctx, ex, docs, rewrite_call=identity, rewrite_body=
29933006 end
29943007
29953008 @ast ctx ex [K " block"
2996- if ! isnothing (gen_func_name)
2997- [K " function_decl" (gen_func_name) gen_func_name]
2998- end
2999- if ! isnothing (body_func_name)
3000- [K " function_decl" (body_func_name) body_func_name]
3001- end
30023009 if ! isnothing (bare_func_name)
3010+ # Need the main function type created here before running any code
3011+ # in kw_func_method_defs
30033012 [K " function_decl" (bare_func_name) bare_func_name]
30043013 end
3014+ gen_func_method_defs
3015+ kw_func_method_defs
30053016 [K " scope_block" (scope_type= :hard )
3006- [K " block"
3007- new_typevar_stmts...
3008- gen_func_method_defs
3009- kw_func_method_defs
3010- [K " method_defs"
3011- isnothing (bare_func_name) ? " nothing" :: K"core" : bare_func_name
3012- [K " block"
3013- if ! isnothing (method_table_val)
3014- [K " =" method_table method_table_val]
3015- end
3016- method_stmts...
3017- ]
3017+ [K " method_defs"
3018+ isnothing (bare_func_name) ? " nothing" :: K"core" : bare_func_name
3019+ [K " block"
3020+ new_typevar_stmts...
3021+ if ! isnothing (method_table_val)
3022+ [K " =" method_table method_table_val]
3023+ end
3024+ method_stmts...
30183025 ]
30193026 ]
30203027 ]
0 commit comments