Commit c38448ce
c38448ced0e7Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
fix(codegen): cross-type case-when emits literal 0 not strcmp/== mismatch
case <string> when :sym_literal lowered to
strcmp(str, SPS_<sym>) == 0 — passing an sp_sym (long long)
to a const char * parameter, C type error on every such arm.
The symmetric case <symbol> when "string_literal" had the same
shape with mrb_int == const char *.
CRuby semantics: :sym === "str" and "str" === :sym are both
false (Symbol#=== / String#=== only match same-type receivers),
so the correct lowering for these cross-type when arms is a
literal 0 — the C compiles, the runtime correctly falls
through to the next arm or to the else clause.
Fix in compile_when_conds (used by both compile_case_stmt and
compile_case_return): special-case the (string-pred, SymbolNode-
arm) and (symbol-pred, StringNode-arm) shapes to emit 0
directly rather than dispatching through compile_expr(cid).
Fixes #378
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- c38448ced0e7
- Parent
- c4fcd37e5799
spinel_codegen.rbmodifiedView file
end else if pred_type == "string" result = result + "strcmp(" + tmp + ", " + compile_expr(cid) + ") == 0" # `case <string> when :sym_literal` — CRuby's # `:sym === "str"` is always false (Symbol#=== returns # false for non-Symbol receivers), so emit a literal # 0 for this arm. Without the special-case # compile_expr(cid) returns `SPS_<sym>` (an sp_sym aka # long long) and strcmp would reject it. if @nd_type[cid] == "SymbolNode" result = result + "0" else result = result + "strcmp(" + tmp + ", " + compile_expr(cid) + ") == 0" end else result = result + tmp + " == " + compile_expr(cid) # Symmetric case: `case <sym> when "literal_str"` is # also always false in CRuby (String#=== returns false # for a Symbol). Emit 0 to avoid a `mrb_int == const # char *` type mismatch in the generated C. if pred_type == "symbol" && @nd_type[cid] == "StringNode" result = result + "0" else result = result + tmp + " == " + compile_expr(cid) end end end k = k + 1test/case_string_when_sym_no_match.rbaddedView file
Loading diff…
test/case_string_when_sym_no_match.rb.expectedaddedView file
Loading diff…