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
.callreceiver through fiber_var_ref, so a proc captured into a nested proc/fiber body resolves to its(*_cap->name)reference instead of an undeclaredlv_<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
# the inherited attr_reader chain so a bare `fmt` in a subclass # method body returns the inherited ivar's type rather than the # int default (issue #508). # `proc { }` / `lambda { }` are Kernel builtins, not instance methods. # Resolve them before the bare-call arm below, which would otherwise # return cls_method_return's int default for the (undefined) # `proc`/`lambda` method and mistype `p = proc { }` as int inside a # class body (it is correctly typed `proc` at top level). Skip when # the class actually defines such a method. if recv < 0 && (mname == "proc" || mname == "lambda") && @current_class_idx >= 0 && find_method_owner(@current_class_idx, mname) == "" return "proc" end if recv < 0 && @current_class_idx >= 0 if cls_has_attr_reader(@current_class_idx, mname) == 1 ivt_attr = cls_ivar_type(@current_class_idx, "@" + attr_reader_ivar(@current_class_idx, mname))spinel_codegen.rbmodifiedView file
Loading diff…
test/captured_block_call_value.rbaddedView file
Loading diff…
test/captured_block_call_value.rb.expectedaddedView file
Loading diff…