Commit bd627123

bd6271237caeMerged

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

Description

fix(analyze): class re-open replaces same-name methods, no duplicate C symbols

Closes #489. Two class Foo definitions in the same translation unit (whether two require_relative'd files or a single file with the class re-opened) emitted duplicate C function symbols for each repeated method (sp_Foo_ declared and defined twice), failing C compile with "redefinition of sp_Foo_".

CRuby's class re-open semantics: methods with the same name are overwritten — last definition wins — and methods unique to either copy survive. Mirror that in append_cls_meth and append_cls_cmeth: when a same-named entry already exists in @cls_meth_* / @cls_cmeth_*, split the parallel arrays, replace the matching row, and rejoin. Otherwise the existing append path is unchanged so non-duplicate adds still grow the table.

Validation: tests 461 pass (added regression: single-file re-open with overlapping initialize + go and unique only_first / only_second, asserting that the second go / initialize win and both unique methods survive), bench 57/57, bootstrap 4-way fixpoint OK. Two-file repro from the issue (toy_ruby_neural_network's tinynn.rb + tinynn_cuda.rb both defining FFNFFICache) also compiles cleanly.

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

Details

Commit
bd6271237cae
Parent
aa2145e9398e

spinel_analyze.rbmodifiedView file

@@ -7404,7 +7404,59 @@
74047404 end
74057405
74067406 def append_cls_meth(ci, name, params, ptypes, ret, body_id, defaults)
7407+ # Class re-open (#489): when `class Foo` is defined in multiple
7408+ # files / multiple top-level blocks, methods with the same name
7409+ # collide and produce duplicate C symbols (sp_Foo_<m> appearing
7410+ # twice in the emitted unit). CRuby's semantics for class
7411+ # re-open are "last definition wins" for methods with matching
7412+ # name; methods unique to either copy keep their entry. Mirror
7413+ # that: if a method named `name` already exists on this class,
7414+ # replace its row in the parallel arrays instead of appending.
74077415 if @cls_meth_names[ci] != ""
7416+ existing_mi = cls_find_method_direct(ci, name)
7417+ if existing_mi >= 0
7418+ cur_names = @cls_meth_names[ci].split(";")
7419+ cur_params = @cls_meth_params[ci].split("|")
7420+ cur_ptypes = @cls_meth_ptypes[ci].split("|")
7421+ cur_returns = @cls_meth_returns[ci].split(";")
7422+ cur_bodies = @cls_meth_bodies[ci].split(";")
7423+ cur_defaults = @cls_meth_defaults[ci].split("|")
7424+ cur_pempty = @cls_meth_ptypes_empty[ci].split("|")
7425+ while cur_pempty.length < cur_names.length
7426+ cur_pempty.push("")
7427+ end
7428+ cur_names[existing_mi] = name
7429+ if existing_mi < cur_params.length
7430+ cur_params[existing_mi] = params
7431+ end
7432+ if existing_mi < cur_ptypes.length
7433+ cur_ptypes[existing_mi] = ptypes
7434+ end
7435+ if existing_mi < cur_returns.length
7436+ cur_returns[existing_mi] = ret
7437+ end
7438+ if existing_mi < cur_bodies.length
7439+ cur_bodies[existing_mi] = body_id.to_s
7440+ end
7441+ if existing_mi < cur_defaults.length
7442+ cur_defaults[existing_mi] = defaults
7443+ end
7444+ if existing_mi < cur_pempty.length
7445+ cur_pempty[existing_mi] = ""
7446+ end
7447+ @cls_meth_names[ci] = cur_names.join(";")
7448+ @cls_meth_params[ci] = cur_params.join("|")
7449+ @cls_meth_params_version = @cls_meth_params_version + 1
7450+ @cls_meth_ptypes[ci] = cur_ptypes.join("|")
7451+ @cls_meth_ptypes_version = @cls_meth_ptypes_version + 1
7452+ @cls_meth_returns[ci] = cur_returns.join(";")
7453+ @cls_meth_bodies[ci] = cur_bodies.join(";")
7454+ @cls_meth_defaults[ci] = cur_defaults.join("|")
7455+ @cls_meth_ptypes_empty[ci] = cur_pempty.join("|")
7456+ @cls_meth_idx_cache = {}
7457+ @cls_meth_return_cache = {}
7458+ return
7459+ end
74087460 @cls_meth_names[ci] = @cls_meth_names[ci] + ";" + name
74097461 @cls_meth_params[ci] = @cls_meth_params[ci] + "|" + params
74107462 @cls_meth_params_version = @cls_meth_params_version + 1
@@ -7431,7 +7483,52 @@
74317483 end
74327484
74337485 def append_cls_cmeth(ci, name, params, ptypes, ret, body_id, defaults = "")
7486+ # Class re-open semantics for `def self.<m>` (#489).
7487+ # Last-definition-wins, same shape as append_cls_meth above.
74347488 if @cls_cmeth_names[ci] != ""
7489+ cm_names_re = @cls_cmeth_names[ci].split(";")
7490+ existing_cmi = 0
7491+ found_cmi = -1
7492+ while existing_cmi < cm_names_re.length
7493+ if cm_names_re[existing_cmi] == name
7494+ found_cmi = existing_cmi
7495+ existing_cmi = cm_names_re.length
7496+ else
7497+ existing_cmi = existing_cmi + 1
7498+ end
7499+ end
7500+ if found_cmi >= 0
7501+ cur_cm_params = @cls_cmeth_params[ci].split("|")
7502+ cur_cm_ptypes = @cls_cmeth_ptypes[ci].split("|")
7503+ cur_cm_returns = @cls_cmeth_returns[ci].split(";")
7504+ cur_cm_bodies = @cls_cmeth_bodies[ci].split(";")
7505+ cur_cm_defaults = @cls_cmeth_defaults[ci].split("|")
7506+ cm_names_re[found_cmi] = name
7507+ if found_cmi < cur_cm_params.length
7508+ cur_cm_params[found_cmi] = params
7509+ end
7510+ if found_cmi < cur_cm_ptypes.length
7511+ cur_cm_ptypes[found_cmi] = ptypes
7512+ end
7513+ if found_cmi < cur_cm_returns.length
7514+ cur_cm_returns[found_cmi] = ret
7515+ end
7516+ if found_cmi < cur_cm_bodies.length
7517+ cur_cm_bodies[found_cmi] = body_id.to_s
7518+ end
7519+ if found_cmi < cur_cm_defaults.length
7520+ cur_cm_defaults[found_cmi] = defaults
7521+ end
7522+ @cls_cmeth_names[ci] = cm_names_re.join(";")
7523+ @cls_cmeth_params[ci] = cur_cm_params.join("|")
7524+ @cls_cmeth_params_version = @cls_cmeth_params_version + 1
7525+ @cls_cmeth_ptypes[ci] = cur_cm_ptypes.join("|")
7526+ @cls_cmeth_ptypes_version = @cls_cmeth_ptypes_version + 1
7527+ @cls_cmeth_returns[ci] = cur_cm_returns.join(";")
7528+ @cls_cmeth_bodies[ci] = cur_cm_bodies.join(";")
7529+ @cls_cmeth_defaults[ci] = cur_cm_defaults.join("|")
7530+ return
7531+ end
74357532 @cls_cmeth_names[ci] = @cls_cmeth_names[ci] + ";" + name
74367533 @cls_cmeth_params[ci] = @cls_cmeth_params[ci] + "|" + params
74377534 @cls_cmeth_params_version = @cls_cmeth_params_version + 1