Commit 564e96fe

564e96fe5d4dMerged

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

Description

analyze,codegen: Kernel#method returns a real Method object

method(:puts) returned the integer placeholder 0, so .class was Integer and .name was unresolved. Top-level method(:sym) now builds a real obj_Method.

  • Method gains a @name ivar (the symbol name), set at every sp_Method_new site. Method#name reads it via explicit dispatch (NOT an attr_reader — registering "name" would make any_user_class_defines_imeth true and mis-route .name on class values and other objects).
  • analyze types top-level method(:sym) as obj_Method (was gated on being inside a class). The m = method(:sym) LV-write keeps the method-ref record (so m.call(x) still rewrites to the direct sp_<sym>(x) with the correct ABI) AND now emits the real obj_Method assignment so .class/.name work; obj_Method.call is skipped for ref-vars so the rewrite wins.
  • Non-literal method(expr) is now a clear compile error (documented limitation — can't resolve to a C symbol at compile time).

Proc fn return casts pointer tails to mrb_int (shared with #709). String-returning m.call remains a pre-existing limitation.

Fixes #960

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

Details

Commit
564e96fe5d4d
Parent
87061dae35a7

spinel_analyze.rbmodifiedView file

@@ -3301,6 +3301,11 @@
33013301 end
33023302 end
33033303 end
3304+ # Method#name returns the bound method's name as a String. Issue
3305+ # #960. (Dispatched explicitly, not via attr_reader.)
3306+ if mname == "name" && recv >= 0 && base_type(infer_type(recv)) == "obj_Method"
3307+ return "string"
3308+ end
33043309 # Fiber[:k] / Fiber.current[:k] / fiber[:k] — per-fiber storage
33053310 # read. Returns poly (sp_RbVal) since storage values are
33063311 # arbitrary Ruby objects, matching MRI's Hash[Symbol, Object]
@@ -3332,7 +3337,10 @@
33323337 # the binding and `m.call(x)` rewrites to a direct `sp_<foo>(x)`
33333338 # call. .
33343339 if mname == "method"
3335- if recv < 0 && @current_class_idx >= 0
3340+ if recv < 0
3341+ # `method(:sym)` — bound Method object, whether inside a class
3342+ # (binds to self) or at top level (a top-level/Kernel method).
3343+ # Issue #960.
33363344 return "obj_Method"
33373345 end
33383346 if recv >= 0
@@ -12881,19 +12889,25 @@
1288112889 @cls_is_sra.push(0)
1288212890 @cls_parents.push("")
1288312891 @cls_includes.push("")
12884- @cls_ivar_names.push("@self_obj;@fn_ptr")
12885- @cls_ivar_types.push("obj_Method;int")
12886- @cls_ivar_init_definite.push("1;1")
12887- @cls_ivar_observed_types.push("obj_Method;int")
12892+ # @name (string) carries the method's symbol name so Method#name
12893+ # works; exposed as an attr_reader. Issue #960.
12894+ @cls_ivar_names.push("@self_obj;@fn_ptr;@name")
12895+ @cls_ivar_types.push("obj_Method;int;string")
12896+ @cls_ivar_init_definite.push("1;1;1")
12897+ @cls_ivar_observed_types.push("obj_Method;int;string")
1288812898 @cls_ivar_nil_checked.push("")
12889- @cls_ivar_rbs_types.push(";")
12899+ @cls_ivar_rbs_types.push(";;")
1289012900 @cls_meth_names.push("initialize")
12891- @cls_meth_params.push("self_obj,fn_ptr")
12892- @cls_meth_ptypes.push("obj_Method,int")
12901+ @cls_meth_params.push("self_obj,fn_ptr,name")
12902+ @cls_meth_ptypes.push("obj_Method,int,string")
1289312903 @cls_meth_returns.push("void")
1289412904 @cls_meth_bodies.push("-2")
12895- @cls_meth_defaults.push("0,0")
12905+ @cls_meth_defaults.push("0,0,0")
1289612906 @cls_meth_ptypes_empty.push("")
12907+ # `name` is dispatched explicitly for obj_Method recv (see codegen)
12908+ # rather than as an attr_reader — registering it would make
12909+ # any_user_class_defines_imeth("name") true and mis-route `.name`
12910+ # on class values / other objects. Issue #960.
1289712911 @cls_attr_readers.push("")
1289812912 @cls_attr_writers.push("")
1289912913 @cls_meth_prep_chain.push("")