Commit d3eb7b50
d3eb7b50799fMerged
Ryan Seys <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
fix(codegen): RescueModifierNode infer_type unifies branch types
Previously result = expr rescue fallback left result typed as
the codegen's default (int) because RescueModifierNode had no arm
in infer_type. When the fallback was a different type than the
default, assignments produced C type mismatches.
Adds an arm that unifies the type of the main expression and the rescue branch — preferring the rescue branch's type when the main branch is a noreturn-shaped expression like a bare `raise`. The codegen at compile_expr's RescueModifierNode arm now reads the unified type via infer_type(nid) directly rather than only the fallback's type, so both rvalue and lvalue contexts agree.
New test: test/rescue_modifier_assign.rb covers the same-type baseline, mismatched-types raise+string, and the existing endless method shape that the type-inference change must not regress.
Tests: 347 pass, 0 fail, 0 error. Bootstrap: gen2.c == gen3.c.
Details
- Commit
- d3eb7b50799f
- Parent
- d3b8ea6ed04a
spinel_codegen.rbmodifiedView file
if t == "RangeNode" return "range" end if t == "RescueModifierNode" # `expr rescue fallback` — unify the types of the two branches. # The fallback always runs on error, so prefer its type when the # main branch is a noreturn-shaped expression like a bare `raise` # (whose compile_expr returns the int literal `0`). t1 = infer_type(@nd_expression[nid]) t2 = infer_type(@nd_else_clause[nid]) if t1 == t2 return t1 end if t2 != "void" && t2 != "nil" return t2 end return t1 end if t == "LocalVariableReadNode" vt = find_var_type(@nd_name[nid]) if vt != "" if t == "RescueModifierNode" @needs_setjmp = 1 tmp = new_temp rt = infer_type(@nd_else_clause[nid]) rt = infer_type(nid) emit(" " + c_type(rt) + " " + tmp + " = " + c_default_val(rt) + ";") emit(" sp_exc_top++;") emit(" if (setjmp(sp_exc_stack[sp_exc_top-1]) == 0) {")test/rescue_modifier_assign.rbaddedView file
Loading diff…
test/rescue_modifier_assign.rb.expectedaddedView file
Loading diff…