Commit 02363619

02363619d542Merged

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

Description

Merge pull request #350 from ryanseys/rs/rescue-modifier-infer-type

fix(codegen): RescueModifierNode infer_type unifies branch types

Details

Commit
02363619d542
Parent
36f7fe41f83a
Merge parent
d3eb7b50799f

spinel_codegen.rbmodifiedView file

@@ -2269,6 +2269,21 @@
22692269 if t == "RangeNode"
22702270 return "range"
22712271 end
2272+ if t == "RescueModifierNode"
2273+ # `expr rescue fallback` — unify the types of the two branches.
2274+ # The fallback always runs on error, so prefer its type when the
2275+ # main branch is a noreturn-shaped expression like a bare `raise`
2276+ # (whose compile_expr returns the int literal `0`).
2277+ t1 = infer_type(@nd_expression[nid])
2278+ t2 = infer_type(@nd_else_clause[nid])
2279+ if t1 == t2
2280+ return t1
2281+ end
2282+ if t2 != "void" && t2 != "nil"
2283+ return t2
2284+ end
2285+ return t1
2286+ end
22722287 if t == "LocalVariableReadNode"
22732288 vt = find_var_type(@nd_name[nid])
22742289 if vt != ""
@@ -20508,7 +20523,7 @@
2050820523 if t == "RescueModifierNode"
2050920524 @needs_setjmp = 1
2051020525 tmp = new_temp
20511- rt = infer_type(@nd_else_clause[nid])
20526+ rt = infer_type(nid)
2051220527 emit(" " + c_type(rt) + " " + tmp + " = " + c_default_val(rt) + ";")
2051320528 emit(" sp_exc_top++;")
2051420529 emit(" if (setjmp(sp_exc_stack[sp_exc_top-1]) == 0) {")