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:
-
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.
-
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 surroundingbox_value_to_polypicks 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
aa_p7 = get_args(args_id_p7) if aa_p7.length > 0 at_p7 = infer_type(aa_p7[0]) if at_p7 != "int" && at_p7 != "bool" && at_p7 != "nil" if at_p7 != "int" && at_p7 != "bool" @needs_rb_value = 1 @needs_gc = 1 return "poly_array" return "int" end end # Modules can't be instantiated; `module M; def self.new ...` # is a user-defined class method. Don't lift to obj_<mod>; # the regular class-method dispatch picks up the declared # return type. Issue #625. if module_name_exists(rn) == 1 return "" end return "obj_" + rn end end end ci2 = find_class_idx(rcname) if ci2 >= 0 if mname == "new" # Modules can't be instantiated; `module M; def self.new ...` # is a user-defined class method, not a constructor. Skip the # `obj_<mod>` widening so the call resolves through the regular # class-method dispatch below (which honors the declared # return type). Issue #625. if mname == "new" && module_name_exists(rcname) == 0 return "obj_" + rcname end # `Klass.method(:cls_meth)` — bind to a class method. if rname == "Fiber" return "fiber" end # Modules can't be instantiated; `module M; def self.new ...` # is a user-defined class method. Don't lift to obj_<mod>; # the class-method-dispatch inference picks up the declared # return type. Issue #625. if module_name_exists(rname) == 1 return "" end return "obj_" + rname end endspinel_codegen.rbmodifiedView file
Loading diff…
test/module_singleton_new_user_method.rbaddedView file
Loading diff…
test/module_singleton_new_user_method.rb.expectedaddedView file
Loading diff…