Commit 19d173b2

19d173b29c70Merged

Yusuke Endoh <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified

Description

feat(codegen): module_function + bare-class lookup in module method body

Two intertwined fixes for module-body method dispatch:

  1. module_function (no args) flips subsequent def name into class-method dispatch, parallel to def self.name. Without this, Mod.foo calls fell through to the int default and warned "cannot resolve 'foo' on int". Spinel only needs the class-method shape; the full Ruby semantics also installs private instance methods (for include-mixin) — unmodeled.

  2. Bare class refs inside a module's method body (def self.load ...; Video.new(conf); end) need current_lexical_scope_name set so resolve_const_read_name walks up and finds Top_Video instead of bare Video. The top-level method loop in infer_all_returns now pins @current_method_name to the method's full name so the lexical scope can peel <Mod>_cls_<m> correctly.

Both fire together in Optcarrot::Driver.load shape and let the Palette / Driver bare-named module dispatch compile.

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
19d173b29c70
Parent
a0864f361051

spinel_codegen.rbmodifiedView file

@@ -7357,6 +7357,7 @@
73577357 end
73587358 }
73597359
7360+ in_module_function = 0
73607361 body_stmts.each { |sid|
73617362 if @nd_type[sid] == "ConstantWriteNode"
73627363 collect_scoped_constant(mname, sid)
@@ -7365,28 +7366,41 @@
73657366 if @nd_type[sid] == "MultiWriteNode"
73667367 collect_scoped_multi_const(mname, sid)
73677368 end
7369+ # `module_function` (no args) flips subsequent `def name`
7370+ # into class-method dispatch, parallel to `def self.name`.
7371+ # Spinel only needs the class-method shape; the full Ruby
7372+ # semantics also installs the methods as private instance
7373+ # methods (for include-mixin), unmodeled here.
7374+ if @nd_type[sid] == "CallNode" && @nd_receiver[sid] < 0 && @nd_name[sid] == "module_function"
7375+ args_id_mf = @nd_arguments[sid]
7376+ if args_id_mf < 0 || get_args(args_id_mf).length == 0
7377+ in_module_function = 1
7378+ end
7379+ end
73687380 # Collect module class methods (def self.xxx) as top-level functions
73697381 if @nd_type[sid] == "DefNode"
7370- if @nd_receiver[sid] >= 0
7371- if @nd_type[@nd_receiver[sid]] == "SelfNode"
7372- dmname = @nd_name[sid]
7373- # Create as top-level method with module prefix for dispatch
7374- @meth_names.push(mname + "_cls_" + dmname)
7375- @meth_param_names.push(collect_params_str(sid))
7376- @meth_param_types.push(collect_ptypes_str(sid, -1))
7377- @meth_param_empty.push("")
7378- @meth_return_types.push("int")
7379- @meth_body_ids.push(@nd_body[sid])
7380- @meth_has_yield.push(0)
7381- # Issue #239: capture default-arg expressions so call
7382- # sites that omit trailing args get them filled in by
7383- # compile_call_args_with_defaults. Pre-fix this slot
7384- # was hardcoded to "0", which `compile_expr` lowered to
7385- # the literal `0` regardless of the actual default
7386- # (string literals etc.).
7387- @meth_has_defaults.push(collect_defaults_str(sid))
7388- @meth_rest_index.push(collect_rest_index(sid))
7389- end
7382+ is_self_def = 0
7383+ if @nd_receiver[sid] >= 0 && @nd_type[@nd_receiver[sid]] == "SelfNode"
7384+ is_self_def = 1
7385+ end
7386+ if is_self_def == 1 || (in_module_function == 1 && @nd_receiver[sid] < 0)
7387+ dmname = @nd_name[sid]
7388+ # Create as top-level method with module prefix for dispatch
7389+ @meth_names.push(mname + "_cls_" + dmname)
7390+ @meth_param_names.push(collect_params_str(sid))
7391+ @meth_param_types.push(collect_ptypes_str(sid, -1))
7392+ @meth_param_empty.push("")
7393+ @meth_return_types.push("int")
7394+ @meth_body_ids.push(@nd_body[sid])
7395+ @meth_has_yield.push(0)
7396+ # Issue #239: capture default-arg expressions so call
7397+ # sites that omit trailing args get them filled in by
7398+ # compile_call_args_with_defaults. Pre-fix this slot
7399+ # was hardcoded to "0", which `compile_expr` lowered to
7400+ # the literal `0` regardless of the actual default
7401+ # (string literals etc.).
7402+ @meth_has_defaults.push(collect_defaults_str(sid))
7403+ @meth_rest_index.push(collect_rest_index(sid))
73907404 end
73917405 end
73927406 # Collect module-level ivar writes as global statics
@@ -10542,6 +10556,12 @@
1054210556 push_scope
1054310557 # Open class self type
1054410558 mfn = @meth_names[i]
10559+ # Pin @current_method_name so current_lexical_scope_name can
10560+ # peel `<Mod>_cls_<m>` and resolve bare class refs in the body
10561+ # (e.g. `Video.new` inside `Top::Drv.load` resolves Video to
10562+ # Top_Video instead of bare Video).
10563+ saved_meth_ar = @current_method_name
10564+ @current_method_name = mfn
1054510565 if mfn.start_with?("__oc_Integer_")
1054610566 declare_var("__self_type", "int")
1054710567 end
@@ -10601,6 +10621,7 @@
1060110621 end
1060210622 rt = infer_body_return(@meth_body_ids[i])
1060310623 @meth_return_types[i] = rt
10624+ @current_method_name = saved_meth_ar
1060410625 pop_scope
1060510626 i = i + 1
1060610627 end