Commit ac225100

ac2251005e8fMerged

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

Description

Merge pull request #1506 from ryanseys/rs-lowered-block-given

Fix block_given? in lowered yielding methods

Details

Commit
ac2251005e8f
Parent
538bc0b43846
Merge parent
9319c96911df

src/codegen_call.cmodifiedView file

@@ -4363,10 +4363,22 @@
43634363 }
43644364
43654365 /* block_given? / self.block_given? -> true inside an inlined yielding
4366- method (we only inline when a block is present) */
4366+ method (we only inline when a block is present). In a lowered yielding
4367+ method the block is the `__yblk__` proc parameter, which is non-NULL
4368+ exactly when the caller passed a block, so test it directly. */
43674369 if (!strcmp(name, "block_given?") &&
43684370 (recv < 0 || (nt_type(nt, recv) && !strcmp(nt_type(nt, recv), "SelfNode")))) {
4369- buf_puts(b, g_block_id >= 0 ? "1" : "0");
4371+ /* block_given? asks about the innermost method. An inlined yielding method
4372+ (g_block_id >= 0) statically has a block, so fold to 1 even when the
4373+ enclosing method is lowered; only a genuinely lowered scope inspects its
4374+ runtime __yblk__ parameter. */
4375+ if (g_block_id >= 0) {
4376+ buf_puts(b, "1");
4377+ } else if (g_current_scope_is_lowered) {
4378+ buf_puts(b, "("); emit_yblk_ref(b); buf_puts(b, " != NULL)");
4379+ } else {
4380+ buf_puts(b, "0");
4381+ }
43704382 return;
43714383 }
43724384