Commit 6f1a73df

6f1a73df1902Merged

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

Description

fix(analyze,codegen): unless-modifier with non-int branch as last expr (#434)

expr unless cond was always lowered through compile_unless_expr to the ternary (!cond ? expr : 0) and inferred as int. When expr returns a pointer type (push returns the array, errors << "msg" returns the array, etc.) and the modifier was the last statement of a method, the function's return signature was mrb_int while the ternary actually produced a pointer — the C compiler rejected with incompatible-pointer-to-integer-conversion.

Three changes:

  • analyze: UnlessNode infer_type unifies body's last-expr type with the implicit nil from the absent else, mirroring IfNode.
  • codegen: compile_unless_expr derives the else-arm default from then_t (NULL for pointers, 0 for ints, etc.) and applies the same poly-boxing arm as compile_if_expr when the unified type is poly.
  • codegen: compile_body_return routes UnlessNode last expr through the new compile_unless_return (mirroring compile_if_return) so the return is emitted inside the if/else arms with the function's return type, not as a heterogeneous ternary.

Closes #434

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

Details

Commit
6f1a73df1902
Parent
87a2ba791fa0

spinel_analyze.rbmodifiedView file

@@ -2173,6 +2173,31 @@
21732173 types.push(else_type)
21742174 return unify_return_type(types)
21752175 end
2176+ if t == "UnlessNode"
2177+ then_type = "nil"
2178+ body = @nd_body[nid]
2179+ if body >= 0
2180+ stmts = get_stmts(body)
2181+ if stmts.length > 0
2182+ then_type = infer_type(stmts.last)
2183+ end
2184+ end
2185+ else_type = "nil"
2186+ ec = @nd_else_clause[nid]
2187+ if ec >= 0
2188+ ebody = @nd_body[ec]
2189+ if ebody >= 0
2190+ es = get_stmts(ebody)
2191+ if es.length > 0
2192+ else_type = infer_type(es.last)
2193+ end
2194+ end
2195+ end
2196+ types = "".split(",")
2197+ types.push(then_type)
2198+ types.push(else_type)
2199+ return unify_return_type(types)
2200+ end
21762201 if t == "CaseMatchNode"
21772202 types = "".split(",")
21782203 conds = parse_id_list(@nd_conditions[nid])