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
compile_stmts_body(@nd_body[sub]) @indent = @indent - 1 else emit(" } else") # Issue #310: wrap elsif in braces. compile_cond_expr for the # nested predicate may emit prologue C (temp decls, tag checks) # before returning the cond expression — e.g. `value.is_a?(B)` # on a poly receiver expands to `sp_RbVal _t = ...; if (_t.tag # == SP_TAG_OBJ) {...}; if (_flag) ...`. Without wrapping, the # `} else <prologue> if (...)` shape is invalid C: the bare- # else's body is the first statement only (the prologue decl), # and the inner if sits in the outer scope with the decl out # of reach. emit(" } else {") @indent = @indent + 1 compile_if_stmt(sub) @indent = @indent - 1 emit(" }") return end end end @indent = @indent - 1 else emit(" } else") # Issue #310: wrap elsif in braces. See compile_if_stmt for # the same fix; predicates that emit prologue C statements # (poly is_a? etc.) leak declarations out of a bare else # because the bare else's body is one statement. emit(" } else {") @indent = @indent + 1 compile_if_return(sub, rt) @indent = @indent - 1 emit(" }") return end elsetest/elsif_isa_chain.rbaddedView file
Loading diff…
test/elsif_isa_chain.rb.expectedaddedView file
Loading diff…