Commit 79fb2c44

79fb2c44eabeMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified

Description

fix: improve return type inference and compile_body_return handling

Two changes:

  1. Type inference: replace scan_return_type (first-return-wins) with collect_return_types that gathers ALL return paths and unifies them. T + nil → T? (nullable pointer) instead of poly (boxed tagged union). Also emit return NULL instead of return 0 for nullable pointer methods.

  2. compile_body_return: add puts/print/p/warn/raise/exit and other side-effect methods to the statement-like CallNode list so they get compiled via compile_stmt instead of compile_expr when they appear as the last statement in a method body. Fixes 10 test failures where output was silently dropped.

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

Details

Commit
79fb2c44eabe
Parent
d9b896f606f5

spinel_codegen.rbmodifiedView file

@@ -5080,85 +5080,129 @@
50805080 if stmts.length == 0
50815081 return "void"
50825082 end
5083- # Check for explicit returns
5084- rt = scan_return_type_nid(body_id)
5085- if rt != ""
5086- return rt
5087- end
5088- # Last expression
5089- infer_type(stmts.last)
5090- end
5091-
5092- def scan_return_type_nid(nid)
5083+ # Collect all explicit return types
5084+ types = "".split(",")
5085+ collect_return_types_nid(body_id, types)
5086+ # Add implicit return (last expression)
5087+ last_type = infer_type(stmts.last)
5088+ types.push(last_type)
5089+ # Unify all return path types
5090+ unify_return_type(types)
5091+ end
5092+
5093+ def collect_return_types_nid(nid, types)
50935094 stmts = get_stmts(nid)
5094- i = 0
5095- while i < stmts.length
5096- rt = scan_return_type(stmts[i])
5097- if rt != ""
5098- return rt
5099- end
5100- i = i + 1
5095+ k = 0
5096+ while k < stmts.length
5097+ collect_return_types(stmts[k], types)
5098+ k = k + 1
51015099 end
5102- ""
51035100 end
51045101
5105- def scan_return_type(nid)
5102+ def collect_return_types(nid, types)
51065103 if nid < 0
5107- return ""
5104+ return
51085105 end
51095106 if @nd_type[nid] == "ReturnNode"
51105107 args_id = @nd_arguments[nid]
51115108 if args_id >= 0
51125109 arg_ids = get_args(args_id)
51135110 if arg_ids.length > 0
5114- return infer_type(arg_ids[0])
5111+ types.push(infer_type(arg_ids[0]))
5112+ return
51155113 end
51165114 end
5117- return "void"
5115+ types.push("nil")
5116+ return
5117+ end
5118+ # Don't recurse into nested method definitions
5119+ if @nd_type[nid] == "DefNode"
5120+ return
51185121 end
51195122 if @nd_type[nid] == "IfNode"
5120- r1 = ""
51215123 body = @nd_body[nid]
51225124 if body >= 0
5123- r1 = scan_return_type_nid(body)
5125+ collect_return_types_nid(body, types)
51245126 end
51255127 sub = @nd_subsequent[nid]
51265128 if sub >= 0
5127- r2 = scan_return_type(sub)
5128- if r2 != ""
5129- r1 = r2
5130- end
5129+ collect_return_types(sub, types)
51315130 end
5132- return r1
5131+ return
51335132 end
51345133 if @nd_type[nid] == "ElseNode"
51355134 body = @nd_body[nid]
51365135 if body >= 0
5137- return scan_return_type_nid(body)
5136+ collect_return_types_nid(body, types)
51385137 end
5138+ return
51395139 end
51405140 if @nd_type[nid] == "WhileNode"
51415141 body = @nd_body[nid]
51425142 if body >= 0
5143- return scan_return_type_nid(body)
5143+ collect_return_types_nid(body, types)
51445144 end
5145+ return
51455146 end
51465147 if @nd_type[nid] == "CaseMatchNode"
51475148 conds = parse_id_list(@nd_conditions[nid])
5148- if conds.length > 0
5149- inid = conds.first
5149+ k = 0
5150+ while k < conds.length
5151+ inid = conds[k]
51505152 if @nd_type[inid] == "InNode"
51515153 ibody = @nd_body[inid]
51525154 if ibody >= 0
5153- rt = scan_return_type_nid(ibody)
5154- if rt != ""
5155- return rt
5156- end
5155+ collect_return_types_nid(ibody, types)
51575156 end
51585157 end
5158+ k = k + 1
5159+ end
5160+ return
5161+ end
5162+ end
5163+
5164+ def unify_return_type(types)
5165+ result = ""
5166+ has_nil = 0
5167+ k = 0
5168+ while k < types.length
5169+ t = types[k]
5170+ if t == "nil" || t == "void"
5171+ has_nil = 1
5172+ else
5173+ if result == ""
5174+ result = t
5175+ elsif base_type(result) == base_type(t)
5176+ # Same base type — prefer nullable version
5177+ if is_nullable_type(t) == 1
5178+ result = t
5179+ end
5180+ elsif result == "int"
5181+ # int is default/unresolved — real type takes priority
5182+ result = t
5183+ elsif t == "int"
5184+ # int is default/unresolved — keep existing result
5185+ else
5186+ # Genuinely different types
5187+ return "poly"
5188+ end
51595189 end
5190+ k = k + 1
51605191 end
5161- ""
5192+ if result == ""
5193+ if has_nil == 1
5194+ return "nil"
5195+ end
5196+ return "void"
5197+ end
5198+ if has_nil == 1
5199+ if is_nullable_pointer_type(result) == 1
5200+ if is_nullable_type(result) == 0
5201+ result = result + "?"
5202+ end
5203+ end
5204+ end
5205+ result
51625206 end
51635207
51645208 def fix_lambda_return_types
@@ -13837,9 +13881,17 @@
1383713881 if args_id >= 0
1383813882 arg_ids = get_args(args_id)
1383913883 if arg_ids.length > 0
13884+ rt = infer_type(arg_ids[0])
13885+ # return nil in a nullable pointer method → return NULL
13886+ if rt == "nil" && is_nullable_pointer_type(@current_method_return) == 1
13887+ if @in_gc_scope == 1
13888+ emit(" SP_GC_RESTORE();")
13889+ end
13890+ emit(" return NULL;")
13891+ return
13892+ end
1384013893 if @in_gc_scope == 1
1384113894 # Save return value, restore GC, then return
13842- rt = infer_type(arg_ids[0])
1384313895 tmp = new_temp
1384413896 emit(" " + c_type(rt) + " " + tmp + " = " + compile_expr(arg_ids[0]) + ";")
1384513897 emit(" SP_GC_RESTORE();")
@@ -13850,10 +13902,15 @@
1385013902 return
1385113903 end
1385213904 end
13905+ # bare return — use NULL for nullable pointer types, 0 otherwise
1385313906 if @in_gc_scope == 1
1385413907 emit(" SP_GC_RESTORE();")
1385513908 end
13856- emit(" return 0;")
13909+ if is_nullable_pointer_type(@current_method_return) == 1
13910+ emit(" return NULL;")
13911+ else
13912+ emit(" return 0;")
13913+ end
1385713914 end
1385813915
1385913916
@@ -16980,7 +17037,15 @@
1698017037 lt = @nd_type[last]
1698117038 if lt == "CallNode"
1698217039 lm = @nd_name[last]
16983- if lm == "[]=" || lm == "push" || lm == "pop" || lm == "emit" || lm == "emit_raw"
17040+ if lm == "[]=" || lm == "push" || lm == "pop" || lm == "emit" || lm == "emit_raw" || lm == "puts" || lm == "print" || lm == "p" || lm == "printf" || lm == "warn" || lm == "raise" || lm == "exit" || lm == "delete" || lm == "clear" || lm == "reverse!" || lm == "sort!" || lm == "each" || lm == "times" || lm == "upto" || lm == "downto"
17041+ compile_stmt(last)
17042+ if return_type != "void"
17043+ emit(" return " + c_return_default(return_type) + ";")
17044+ end
17045+ return
17046+ end
17047+ # Receiver-based setter calls (obj.attr = val)
17048+ if lm.end_with?("=") && lm != "==" && lm != "!=" && lm != "<=" && lm != ">="
1698417049 compile_stmt(last)
1698517050 if return_type != "void"
1698617051 emit(" return " + c_return_default(return_type) + ";")