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

@@ -2221,6 +2221,21 @@
22212221 if t == "RangeNode"
22222222 return "range"
22232223 end
2224+ if t == "RescueModifierNode"
2225+ # `expr rescue fallback` — unify the types of the two branches.
2226+ # The fallback always runs on error, so prefer its type when the
2227+ # main branch is a noreturn-shaped expression like a bare `raise`
2228+ # (whose compile_expr returns the int literal `0`).
2229+ t1 = infer_type(@nd_expression[nid])
2230+ t2 = infer_type(@nd_else_clause[nid])
2231+ if t1 == t2
2232+ return t1
2233+ end
2234+ if t2 != "void" && t2 != "nil"
2235+ return t2
2236+ end
2237+ return t1
2238+ end
22242239 if t == "LocalVariableReadNode"
22252240 vt = find_var_type(@nd_name[nid])
22262241 if vt != ""
@@ -20212,7 +20227,7 @@
2021220227 if t == "RescueModifierNode"
2021320228 @needs_setjmp = 1
2021420229 tmp = new_temp
20215- rt = infer_type(@nd_else_clause[nid])
20230+ rt = infer_type(nid)
2021620231 emit(" " + c_type(rt) + " " + tmp + " = " + c_default_val(rt) + ";")
2021720232 emit(" sp_exc_top++;")
2021820233 emit(" if (setjmp(sp_exc_stack[sp_exc_top-1]) == 0) {")