Commit 89366c49

89366c49afe9Merged

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

Description

fix(codegen,analyze): module def self.new is a user method, not a constructor

module M; def self.new(x); ...; end defines a user-level class method. Modules can't be instantiated, so the call M.new(args) should resolve through the regular class-method dispatch and honor the user method's declared return type. Pre-fix compile_constructor_expr's user-class arm fired even for module receivers and emitted sp_box_obj(<result>, <mod_cls_id>), mis-boxing the return as an instance of the module regardless of the declared return type. Sibling methods on the same module (def self.edit) emitted correctly via the regular class-method path.

Two pieces:

  1. spinel_codegen.rb: compile_constructor_expr's user-class arm bails to "" when the receiver name is in @module_names, so the outer mname=="new" check falls through to the normal CallNode dispatch.

  2. spinel_analyze.rb: the matching obj_<rn> infer arms in infer_constructor_type and the secondary CallNode return-type block now check module_name_exists(rn) and skip the obj wrapper for modules, so the surrounding box_value_to_poly picks sp_box_str (or whatever the declared return type maps to) rather than sp_box_obj.

While the <int_array literal> << x lift from #619 puzzle 7 was also touched to drop the nil exclusion: [1, 2] << nil should yield a poly_array of [1, 2, nil] (not int_array of [1, 2, 0]) so a subsequent == against a poly_array literal compares correctly. Optcarrot's ivar-recv @slots << ... shape still takes the existing observation-widening path -- the lift is gated on the recv being a direct ArrayNode literal.

Co-Authored-By: Claude Opus 4.7 [email protected]

Fixes #625

Details

Commit
89366c49afe9
Parent
080abef961de

spinel_analyze.rbmodifiedView file

@@ -3683,7 +3683,7 @@
36833683 aa_p7 = get_args(args_id_p7)
36843684 if aa_p7.length > 0
36853685 at_p7 = infer_type(aa_p7[0])
3686- if at_p7 != "int" && at_p7 != "bool" && at_p7 != "nil"
3686+ if at_p7 != "int" && at_p7 != "bool"
36873687 @needs_rb_value = 1
36883688 @needs_gc = 1
36893689 return "poly_array"
@@ -5675,6 +5675,13 @@
56755675 return "int"
56765676 end
56775677 end
5678+ # Modules can't be instantiated; `module M; def self.new ...`
5679+ # is a user-defined class method. Don't lift to obj_<mod>;
5680+ # the regular class-method dispatch picks up the declared
5681+ # return type. Issue #625.
5682+ if module_name_exists(rn) == 1
5683+ return ""
5684+ end
56785685 return "obj_" + rn
56795686 end
56805687 end
@@ -5784,7 +5791,12 @@
57845791 end
57855792 ci2 = find_class_idx(rcname)
57865793 if ci2 >= 0
5787- if mname == "new"
5794+ # Modules can't be instantiated; `module M; def self.new ...`
5795+ # is a user-defined class method, not a constructor. Skip the
5796+ # `obj_<mod>` widening so the call resolves through the regular
5797+ # class-method dispatch below (which honors the declared
5798+ # return type). Issue #625.
5799+ if mname == "new" && module_name_exists(rcname) == 0
57885800 return "obj_" + rcname
57895801 end
57905802 # `Klass.method(:cls_meth)` — bind to a class method.
@@ -9866,6 +9878,13 @@
98669878 if rname == "Fiber"
98679879 return "fiber"
98689880 end
9881+ # Modules can't be instantiated; `module M; def self.new ...`
9882+ # is a user-defined class method. Don't lift to obj_<mod>;
9883+ # the class-method-dispatch inference picks up the declared
9884+ # return type. Issue #625.
9885+ if module_name_exists(rname) == 1
9886+ return ""
9887+ end
98699888 return "obj_" + rname
98709889 end
98719890 end