Commit 2e1c2c40

2e1c2c409741Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified

Description

fix(codegen): #314 link gaps — sym_intern emit, abstract-parent super skip, super() arg pad

Three small follow-ups that close the remaining link errors when building the Roundhouse archive against current master:

  1. sp_sym_intern undefined reference. scan_features's to_sym/intern arm only fired @needs_sym_intern = 1 when the receiver statically inferred as string at scan time. Block-param receivers (attrs.each { |k, v| row[k.to_sym] = v } where k's type only resolves through later yield-arg unification) inferred as int (default for un-typed LocalVariableReadNode against an empty scope) and missed the flag — so the call site emitted sp_sym_intern(lv_k) against a function whose definition was conditionally skipped. Broadened the match to also fire on poly and int receivers.

  2. sp_ApplicationRecord_initialize undefined reference. When the immediate parent in a heap class's super chain is an "abstract" intermediate class (no own initialize, e.g. class ApplicationRecord < ActiveRecord::Base), the void _initialize wrapper is never emitted for it. The constructor's super-call lowering at the if @nd_type[sid] == "SuperNode" arm then emitted sp_<imm_parent>_initialize(...) against a missing function. Walk via find_init_class(immediate_parent) to skip over abstract ancestors and call the nearest emitted wrapper.

  3. too few arguments to function 'sp_<Parent>_initialize'. super() (explicit empty parens) emitted no args, but the parent's C signature has no defaults — gcc errored. Pad missing trailing slots with c_default_val(<param_type>) per the parent's ptypes table after the explicit arg loop.

Result: all four Roundhouse archive tests (article/comment/articles_controller/comments_controller) now build and link cleanly. The emitted binaries run (exit 0) — the remaining work to actually pass the minitest assertions is on runtime/framework support, separate from the C-compile/link contract that #314 documents.

Refs #314

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

Details

Commit
2e1c2c409741
Parent
f4be7c7d52be

spinel_codegen.rbmodifiedView file

@@ -13249,8 +13249,19 @@
1324913249 end
1325013250 if mname == "to_sym" || mname == "intern"
1325113251 if @nd_receiver[nid] >= 0
13252+ # Issue #314 follow-up: also fire for poly receivers and
13253+ # un-yet-typed locals (default `int` from
13254+ # LocalVariableReadNode against an empty scope). Block-
13255+ # param receivers in shapes like
13256+ # `attrs.each { |k, v| row[k.to_sym] = v }` only resolve
13257+ # to "string" through later yield-arg unification, but
13258+ # scan_features runs once on @root_id before that
13259+ # fixpoint completes — and compile_int_method_expr later
13260+ # emits `sp_sym_intern(...)` regardless. Without this
13261+ # flag, the call site references a function whose
13262+ # definition was conditionally skipped, linker error.
1325213263 rt = infer_type(@nd_receiver[nid])
13253- if rt == "string"
13264+ if rt == "string" || rt == "poly" || rt == "int"
1325413265 @needs_sym_intern = 1
1325513266 end
1325613267 end
@@ -17659,7 +17670,22 @@
1765917670 is_super = (@nd_type[sid] == "SuperNode" || @nd_type[sid] == "ForwardingSuperNode")
1766017671 if is_super
1766117672 if @cls_parents[ci] != ""
17662- pi = find_class_idx(@cls_parents[ci])
17673+ # Issue #314 follow-up: when the immediate parent is
17674+ # an "abstract" class (no own `initialize`, e.g.
17675+ # `class ApplicationRecord < ActiveRecord::Base`),
17676+ # the void `_initialize` wrapper is never emitted for
17677+ # it — so a literal `sp_<parent>_initialize(...)` call
17678+ # here would be an undefined reference at link time.
17679+ # Walk up to find_init_class to skip over abstract
17680+ # ancestors and call the nearest emitted wrapper.
17681+ pi_imm = find_class_idx(@cls_parents[ci])
17682+ pi = pi_imm
17683+ if pi_imm >= 0
17684+ pi = find_init_class(pi_imm)
17685+ if pi < 0
17686+ pi = pi_imm
17687+ end
17688+ end
1766317689 if pi >= 0
1766417690 # Look up the parent's init signature so we can cast
1766517691 # each forwarded arg to the parent's declared type
@@ -17697,9 +17723,9 @@
1769717723 end
1769817724 else
1769917725 args_id = @nd_arguments[sid]
17726+ ak = 0
1770017727 if args_id >= 0
1770117728 arg_ids = get_args(args_id)
17702- ak = 0
1770317729 while ak < arg_ids.length
1770417730 if ak > 0
1770517731 super_args = super_args + ", "
@@ -17715,8 +17741,23 @@
1771517741 ak = ak + 1
1771617742 end
1771717743 end
17744+ # Issue #314 follow-up: explicit `super(...)` with
17745+ # fewer args than the parent expects (e.g. `super()`
17746+ # against `def initialize(_attrs = {})`). Ruby fills
17747+ # missing trailing slots from the parent's defaults;
17748+ # the C signature has no defaults, so pad with
17749+ # c_default_val per param type. Without this, gcc
17750+ # errors `too few arguments to function`.
17751+ while ak < p_ptypes.length
17752+ if super_args != ""
17753+ super_args = super_args + ", "
17754+ end
17755+ super_args = super_args + c_default_val(p_ptypes[ak])
17756+ ak = ak + 1
17757+ end
1771817758 end
17719- emit_raw(" sp_" + @cls_parents[ci] + "_initialize((sp_" + @cls_parents[ci] + " *)self" + (super_args != "" ? ", " + super_args : "") + ");")
17759+ target_parent_name = @cls_names[pi]
17760+ emit_raw(" sp_" + target_parent_name + "_initialize((sp_" + target_parent_name + " *)self" + (super_args != "" ? ", " + super_args : "") + ");")
1772017761 end
1772117762 end
1772217763 end