Commit 9c05e832
9c05e832a7c0Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
Merge pull request #462 from ryanseys/rs-if-expr-multi-stmt
fix(codegen): emit side-effect stmts in consumed if-expr branches
Details
- Commit
- 9c05e832a7c0
- Parent
- 91d110ac4c57
- Merge parent
- 7c5c27e035d5
spinel_codegen.rbmodifiedView file
@@ -20307,12 +20307,90 @@
2030720307
"sp_" + owner + "_" + sanitize_name(op) + "(" + cast + recv_c + ", " + rhs_c + ")"2030820308
end2030920309
20310+
def if_branch_has_multi_stmt(body)20311+
if body < 020312+
return 020313+
end20314+
if get_stmts(body).length > 120315+
return 120316+
end20317+
020318+
end20319+
20320+
# Assign the value of an if-arm body to a temp, executing any20321+
# leading side-effect statements before the assignment. Body20322+
# convention: stmts[0..n-2] run for their side-effects;20323+
# stmts[n-1] supplies the arm's value. Empty/missing body → "0".20324+
def compile_if_arm_to_tmp(body, tmp, unified_t)20325+
if body < 020326+
emit(" " + tmp + " = 0;")20327+
return20328+
end20329+
stmts = get_stmts(body)20330+
if stmts.length == 020331+
emit(" " + tmp + " = 0;")20332+
return20333+
end20334+
i = 020335+
while i < stmts.length - 120336+
compile_stmt(stmts[i])20337+
i = i + 120338+
end20339+
last_id = stmts.last20340+
val = compile_expr(last_id)20341+
if unified_t == "poly" && infer_type(last_id) != "poly"20342+
val = box_value_to_poly(infer_type(last_id), val)20343+
end20344+
emit(" " + tmp + " = " + val + ";")20345+
end20346+
2031020347
def compile_if_expr(nid)2031120348
cond = compile_cond_expr(@nd_predicate[nid])2031220349
unified_t = infer_type(nid)20350+
body = @nd_body[nid]20351+
sub = @nd_subsequent[nid]20352+
20353+
# Side-effect-aware path: when any branch holds more than one20354+
# statement, the leading stmts must execute conditionally. The20355+
# plain ternary form drops them. Emit a real if-statement that20356+
# assigns to a temp and return the temp as the expression value.20357+
# Elsif chains recurse — the inner call returns either a temp or20358+
# a ternary string, both valid expressions.20359+
has_then_multi = if_branch_has_multi_stmt(body)20360+
has_else_multi = 020361+
if sub >= 0 && @nd_type[sub] == "ElseNode"20362+
has_else_multi = if_branch_has_multi_stmt(@nd_body[sub])20363+
end20364+
if has_then_multi == 1 || has_else_multi == 120365+
tmp = new_temp20366+
emit(" " + c_type(unified_t) + " " + tmp + ";")20367+
emit(" if (" + cond + ") {")20368+
@indent = @indent + 120369+
compile_if_arm_to_tmp(body, tmp, unified_t)20370+
@indent = @indent - 120371+
emit(" } else {")20372+
@indent = @indent + 120373+
if sub >= 0 && @nd_type[sub] == "ElseNode"20374+
compile_if_arm_to_tmp(@nd_body[sub], tmp, unified_t)20375+
elsif sub >= 020376+
sub_val = compile_if_expr(sub)20377+
sub_t = infer_type(sub)20378+
if unified_t == "poly" && sub_t != "poly"20379+
sub_val = box_value_to_poly(sub_t, sub_val)20380+
end20381+
emit(" " + tmp + " = " + sub_val + ";")20382+
else20383+
emit(" " + tmp + " = 0;")20384+
end20385+
@indent = @indent - 120386+
emit(" }")20387+
return tmp20388+
end20389+
20390+
# Fast path: every branch is at most one statement, so the20391+
# whole if-expression collapses to a C ternary.2031320392
then_val = "0"2031420393
then_t = "nil"20315-
body = @nd_body[nid]2031620394
if body >= 02031720395
stmts = get_stmts(body)2031820396
if stmts.length > 0@@ -20322,7 +20400,6 @@
2032220400
end2032320401
else_val = "0"2032420402
else_t = "nil"20325-
sub = @nd_subsequent[nid]2032620403
if sub >= 02032720404
if @nd_type[sub] == "ElseNode"2032820405
eb = @nd_body[sub]test/if_expr_multi_stmt_body.rbaddedView file
Loading diff…
test/if_expr_multi_stmt_body.rb.expectedaddedView file
Loading diff…