Commit 94e1c141

94e1c1419a69Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified

Description

fix(analyze): module reopen replaces same-name cmeth, no duplicate C symbols

Closes #517. Reopening module M; def self.X; ... previously appended a second @meth_* row keyed M_cls_X and the emit walk generated two sp_M_cls_X C functions. When the two definitions had different return types (e.g. first body returned [] -> int_array, second returned [{...}] -> poly_array), the C build failed with "conflicting types for 'sp_M_cls_X'". Sibling to #489 (class reopen): CRuby's last-definition-wins applies to module cmeths too.

Fix in collect_module_with_prefix's DefNode-with-self-receiver arm: probe @meth_names for an existing entry with the prefixed name. If present, overwrite the row in place (params, types, defaults, body_id, etc.). Otherwise push as before. Methods unique to either copy survive; only the same-name duplicate collapses.

The same gap surfaced as a silent inference issue when the second definition was hidden from the analyzer (e.g. under a begin/rescue around require_relative); the in-place replace covers both the explicit-reopen and the visible-pair shapes the issue described.

Local: 485 tests pass.

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

Details

Commit
94e1c1419a69
Parent
3d882457de02

spinel_analyze.rbmodifiedView file

@@ -9027,21 +9027,41 @@
90279027 end
90289028 if is_self_def == 1 || (in_module_function == 1 && @nd_receiver[sid] < 0)
90299029 dmname = @nd_name[sid]
9030+ full_dn = mname + "_cls_" + dmname
9031+ # CRuby's module re-open: last definition wins. If the same
9032+ # `module M; def self.X` is declared a second time (whether
9033+ # in a re-opened block in the same file or via two
9034+ # `require_relative`'d files), spinel previously appended a
9035+ # second @meth_* row and emitted two same-name C functions
9036+ # with possibly-disagreeing return types. Replace the row in
9037+ # place instead. Issue #517.
9038+ existing_dn = find_method_idx(full_dn)
9039+ if existing_dn >= 0
9040+ @meth_param_names[existing_dn] = collect_params_str(sid)
9041+ @meth_param_types[existing_dn] = collect_ptypes_str(sid, -1)
9042+ @meth_param_empty[existing_dn] = ""
9043+ @meth_return_types[existing_dn] = "int"
9044+ @meth_body_ids[existing_dn] = @nd_body[sid]
9045+ @meth_has_yield[existing_dn] = 0
9046+ @meth_has_defaults[existing_dn] = collect_defaults_str(sid)
9047+ @meth_rest_index[existing_dn] = collect_rest_index(sid)
9048+ else
90309049 # Create as top-level method with module prefix for dispatch
9031- @meth_names.push(mname + "_cls_" + dmname)
9032- @meth_param_names.push(collect_params_str(sid))
9033- @meth_param_types.push(collect_ptypes_str(sid, -1))
9034- @meth_param_empty.push("")
9035- @meth_return_types.push("int")
9036- @meth_body_ids.push(@nd_body[sid])
9037- @meth_has_yield.push(0)
9050+ @meth_names.push(full_dn)
9051+ @meth_param_names.push(collect_params_str(sid))
9052+ @meth_param_types.push(collect_ptypes_str(sid, -1))
9053+ @meth_param_empty.push("")
9054+ @meth_return_types.push("int")
9055+ @meth_body_ids.push(@nd_body[sid])
9056+ @meth_has_yield.push(0)
90389057 # Capture default-arg expressions so call sites that
90399058 # omit trailing args get them filled in by
90409059 # compile_call_args_with_defaults — the actual default
90419060 # value is required (not just literal 0) for string
90429061 # default args etc.
9043- @meth_has_defaults.push(collect_defaults_str(sid))
9044- @meth_rest_index.push(collect_rest_index(sid))
9062+ @meth_has_defaults.push(collect_defaults_str(sid))
9063+ @meth_rest_index.push(collect_rest_index(sid))
9064+ end
90459065 end
90469066 end
90479067 # Collect module-level ivar writes as global statics