Commit a9b2d911

a9b2d9118f8bMerged

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

Description

fix(codegen): #310 wrap elsif body in braces so prologue C decls stay in scope

compile_cond_expr for predicates like value.is_a?(Class) on a poly receiver emits prologue C statements (a sp_RbVal temp, a tag check, a flag init) BEFORE returning the cond expression. When this predicate sits in an elsif, compile_if_stmt was emitting } else without braces and recursing — the prologue decls then deposited between the bare else and the inner if, where they (a) sat outside the bare-else's implicit single-statement body and (b) had their scope clipped before the inner if could reference them. The C compiler rejected the result with "expected expression" and "undeclared identifier".

Wrap the elsif's recursive call in { ... }. Same fix in both compile_if_stmt (statement context) and compile_if_return (tail- position context). The cosmetic else if becomes else { if ... } — semantically identical, scope-correct, lets the prologue decls land where the inner if can see them.

Note: this addresses #310's stated bug (C-compile failure on is_a?(Class) elsif chains). The orthogonal observation that poly-receiver is_a?(UserClass) still resolves conservatively to FALSE at runtime is out of scope here — the issue's expected fix explicitly is bracing, and the user's #310-listed workaround (if/return chain) routes around the predicate flow entirely.

Fixes #310

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
a9b2d9118f8b
Parent
9454c0742da0

spinel_codegen.rbmodifiedView file

@@ -27320,8 +27320,20 @@
2732027320 compile_stmts_body(@nd_body[sub])
2732127321 @indent = @indent - 1
2732227322 else
27323- emit(" } else")
27323+ # Issue #310: wrap elsif in braces. compile_cond_expr for the
27324+ # nested predicate may emit prologue C (temp decls, tag checks)
27325+ # before returning the cond expression — e.g. `value.is_a?(B)`
27326+ # on a poly receiver expands to `sp_RbVal _t = ...; if (_t.tag
27327+ # == SP_TAG_OBJ) {...}; if (_flag) ...`. Without wrapping, the
27328+ # `} else <prologue> if (...)` shape is invalid C: the bare-
27329+ # else's body is the first statement only (the prologue decl),
27330+ # and the inner if sits in the outer scope with the decl out
27331+ # of reach.
27332+ emit(" } else {")
27333+ @indent = @indent + 1
2732427334 compile_if_stmt(sub)
27335+ @indent = @indent - 1
27336+ emit(" }")
2732527337 return
2732627338 end
2732727339 end
@@ -34418,8 +34430,15 @@
3441834430 end
3441934431 @indent = @indent - 1
3442034432 else
34421- emit(" } else")
34433+ # Issue #310: wrap elsif in braces. See compile_if_stmt for
34434+ # the same fix; predicates that emit prologue C statements
34435+ # (poly is_a? etc.) leak declarations out of a bare else
34436+ # because the bare else's body is one statement.
34437+ emit(" } else {")
34438+ @indent = @indent + 1
3442234439 compile_if_return(sub, rt)
34440+ @indent = @indent - 1
34441+ emit(" }")
3442334442 return
3442434443 end
3442534444 else