Commit 24a8d574

24a8d574b037Merged

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

Description

Carry a captured &block's value back at the block's real type

A nested proc literal that captures an enclosing method's &block param (an sp_Proc) and calls it through a proc-var indirection (p = proc { blk.call }; p.call) now compiles and returns the value at the block's real type.

Three coupled fixes, shipped together because the capture fix alone would turn a compile error into silent garbage for non-int blocks:

  • Route the named-proc-var .call receiver through fiber_var_ref, so a proc captured into a nested proc/fiber body resolves to its (*_cap->name) reference instead of an undeclared lv_<name>.
  • Type a proc {} / lambda {} literal local as proc inside a class body: infer_type's bare-call arm returned cls_method_return's int default for the (undefined) proc/lambda method and mistyped the local.
  • Trace the value type through the proc-var indirection (p.call -> p = proc { blk.call } -> the block body's type) so a string- returning captured block reads back as a string, per call site.

The carried pointer is GC-safe for immediate use; a result held across an allocation can be collected (a known limitation shared with the lam_box path). Recursive yield-value (countdown(n) { ... }) is a separate, still-open gap: its value is dropped at the method tail (a block-bearing tail call is emitted for side-effect), independent of this typing.

Refs #1362

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

Details

Commit
24a8d574b037
Parent
0b41b35951a2

spinel_analyze.rbmodifiedView file

@@ -3814,6 +3814,15 @@
38143814 # the inherited attr_reader chain so a bare `fmt` in a subclass
38153815 # method body returns the inherited ivar's type rather than the
38163816 # int default (issue #508).
3817+ # `proc { }` / `lambda { }` are Kernel builtins, not instance methods.
3818+ # Resolve them before the bare-call arm below, which would otherwise
3819+ # return cls_method_return's int default for the (undefined)
3820+ # `proc`/`lambda` method and mistype `p = proc { }` as int inside a
3821+ # class body (it is correctly typed `proc` at top level). Skip when
3822+ # the class actually defines such a method.
3823+ if recv < 0 && (mname == "proc" || mname == "lambda") && @current_class_idx >= 0 && find_method_owner(@current_class_idx, mname) == ""
3824+ return "proc"
3825+ end
38173826 if recv < 0 && @current_class_idx >= 0
38183827 if cls_has_attr_reader(@current_class_idx, mname) == 1
38193828 ivt_attr = cls_ivar_type(@current_class_idx, "@" + attr_reader_ivar(@current_class_idx, mname))