Commit 74c18902

74c1890208a9Merged

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

Description

feat: fix all Phase 1-4 tests (9/9 passing)

  • Fix constructor inheritance for classes without own initialize
  • Fix ivar type inference from constructor call sites
  • Fix method return type inference for CaseNode and bare method calls
  • Fix block parameter type inference for hash.each
  • Fix (range).to_a with parenthesized ranges
  • Add reject/reduce/inject as expression context
  • Add default argument support for top-level method calls
  • Fix incremental return type saving for intra-class method calls

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

Details

Commit
74c1890208a9
Parent
9de90f639f53

spinel_codegen.rbmodifiedView file

@@ -772,6 +772,23 @@
772772 end
773773 return "void"
774774 end
775+ if t == "CaseNode"
776+ # Infer from first when branch
777+ conds = parse_id_list(@nd_conditions[nid])
778+ if conds.length > 0
779+ wid = conds[0]
780+ if @nd_type[wid] == "WhenNode"
781+ wbody = @nd_body[wid]
782+ if wbody >= 0
783+ ws = get_stmts(wbody)
784+ if ws.length > 0
785+ return infer_type(ws[ws.length - 1])
786+ end
787+ end
788+ end
789+ end
790+ return "int"
791+ end
775792 if t == "AndNode"
776793 return "bool"
777794 end
@@ -997,6 +1014,12 @@
9971014 end
9981015 return "int_array"
9991016 end
1017+ if mname == "reject"
1018+ if recv >= 0
1019+ return infer_type(recv)
1020+ end
1021+ return "int_array"
1022+ end
10001023 if mname == "[]"
10011024 if recv >= 0
10021025 rt = infer_type(recv)
@@ -1040,6 +1063,18 @@
10401063 end
10411064 return "string"
10421065 end
1066+ if mname == "to_a"
1067+ if recv >= 0
1068+ rt = infer_type(recv)
1069+ if rt == "range"
1070+ return "int_array"
1071+ end
1072+ if rt == "int_array"
1073+ return "int_array"
1074+ end
1075+ end
1076+ return "int_array"
1077+ end
10431078
10441079 # Method call on object
10451080 if recv >= 0
@@ -1738,7 +1773,209 @@
17381773 end
17391774
17401775 # ---- Return type inference ----
1776+ def infer_constructor_types
1777+ # Scan AST for ClassName.new(args) calls and infer param types
1778+ scan_new_calls(@root_id)
1779+ end
1780+
1781+ def scan_new_calls(nid)
1782+ if nid < 0
1783+ return
1784+ end
1785+ if @nd_type[nid] == "CallNode"
1786+ # Also infer top-level method param types from call sites
1787+ mname = @nd_name[nid]
1788+ if @nd_receiver[nid] < 0
1789+ mi = find_method_idx(mname)
1790+ if mi >= 0
1791+ args_id = @nd_arguments[nid]
1792+ if args_id >= 0
1793+ arg_ids = get_args(args_id)
1794+ ptypes = @meth_param_types[mi].split(",")
1795+ k = 0
1796+ while k < arg_ids.length
1797+ at = infer_type(arg_ids[k])
1798+ if k < ptypes.length
1799+ if ptypes[k] == "int"
1800+ if at != "int"
1801+ ptypes[k] = at
1802+ end
1803+ end
1804+ end
1805+ k = k + 1
1806+ end
1807+ @meth_param_types[mi] = join_sep(ptypes, ",")
1808+ end
1809+ end
1810+ end
1811+ if @nd_name[nid] == "new"
1812+ recv = @nd_receiver[nid]
1813+ if recv >= 0
1814+ if @nd_type[recv] == "ConstantReadNode"
1815+ cname = @nd_name[recv]
1816+ ci = find_class_idx(cname)
1817+ if ci >= 0
1818+ init_ci = find_init_class(ci)
1819+ if init_ci >= 0
1820+ init_idx = cls_find_method_direct(init_ci, "initialize")
1821+ if init_idx >= 0
1822+ args_id = @nd_arguments[nid]
1823+ if args_id >= 0
1824+ arg_ids = get_args(args_id)
1825+ all_ptypes = @cls_meth_ptypes[init_ci].split("|")
1826+ if init_idx < all_ptypes.length
1827+ ptypes = all_ptypes[init_idx].split(",")
1828+ k = 0
1829+ while k < arg_ids.length
1830+ at = infer_type(arg_ids[k])
1831+ if k < ptypes.length
1832+ if ptypes[k] == "int"
1833+ if at != "int"
1834+ ptypes[k] = at
1835+ end
1836+ end
1837+ end
1838+ k = k + 1
1839+ end
1840+ all_ptypes[init_idx] = join_sep(ptypes, ",")
1841+ @cls_meth_ptypes[init_ci] = join_sep(all_ptypes, "|")
1842+ end
1843+ end
1844+ end
1845+ end
1846+ end
1847+ end
1848+ end
1849+ end
1850+ end
1851+ # Recurse into children
1852+ if @nd_body[nid] >= 0
1853+ scan_new_calls(@nd_body[nid])
1854+ end
1855+ stmts = parse_id_list(@nd_stmts[nid])
1856+ k = 0
1857+ while k < stmts.length
1858+ scan_new_calls(stmts[k])
1859+ k = k + 1
1860+ end
1861+ if @nd_receiver[nid] >= 0
1862+ scan_new_calls(@nd_receiver[nid])
1863+ end
1864+ if @nd_arguments[nid] >= 0
1865+ scan_new_calls(@nd_arguments[nid])
1866+ end
1867+ args = parse_id_list(@nd_args[nid])
1868+ k = 0
1869+ while k < args.length
1870+ scan_new_calls(args[k])
1871+ k = k + 1
1872+ end
1873+ if @nd_expression[nid] >= 0
1874+ scan_new_calls(@nd_expression[nid])
1875+ end
1876+ if @nd_predicate[nid] >= 0
1877+ scan_new_calls(@nd_predicate[nid])
1878+ end
1879+ if @nd_subsequent[nid] >= 0
1880+ scan_new_calls(@nd_subsequent[nid])
1881+ end
1882+ if @nd_else_clause[nid] >= 0
1883+ scan_new_calls(@nd_else_clause[nid])
1884+ end
1885+ if @nd_left[nid] >= 0
1886+ scan_new_calls(@nd_left[nid])
1887+ end
1888+ if @nd_right[nid] >= 0
1889+ scan_new_calls(@nd_right[nid])
1890+ end
1891+ if @nd_block[nid] >= 0
1892+ scan_new_calls(@nd_block[nid])
1893+ end
1894+ elems = parse_id_list(@nd_elements[nid])
1895+ k = 0
1896+ while k < elems.length
1897+ scan_new_calls(elems[k])
1898+ k = k + 1
1899+ end
1900+ conds = parse_id_list(@nd_conditions[nid])
1901+ k = 0
1902+ while k < conds.length
1903+ scan_new_calls(conds[k])
1904+ k = k + 1
1905+ end
1906+ end
1907+
1908+ def update_ivar_types_from_params
1909+ # For each class, if initialize assigns @ivar = param, update ivar type from param type
1910+ i = 0
1911+ while i < @cls_names.length
1912+ init_idx = cls_find_method_direct(i, "initialize")
1913+ if init_idx >= 0
1914+ all_params = @cls_meth_params[i].split("|")
1915+ all_ptypes = @cls_meth_ptypes[i].split("|")
1916+ pnames = []
1917+ ptypes = []
1918+ if init_idx < all_params.length
1919+ pnames = all_params[init_idx].split(",")
1920+ end
1921+ if init_idx < all_ptypes.length
1922+ ptypes = all_ptypes[init_idx].split(",")
1923+ end
1924+ bodies = @cls_meth_bodies[i].split(";")
1925+ bid = -1
1926+ if init_idx < bodies.length
1927+ bid = bodies[init_idx].to_i
1928+ end
1929+ if bid >= 0
1930+ stmts = get_stmts(bid)
1931+ k = 0
1932+ while k < stmts.length
1933+ sid = stmts[k]
1934+ if @nd_type[sid] == "InstanceVariableWriteNode"
1935+ expr = @nd_expression[sid]
1936+ if expr >= 0
1937+ if @nd_type[expr] == "LocalVariableReadNode"
1938+ pname = @nd_name[expr]
1939+ # Find param index
1940+ pi = 0
1941+ while pi < pnames.length
1942+ if pnames[pi] == pname
1943+ if pi < ptypes.length
1944+ # Update ivar type
1945+ iname = @nd_name[sid]
1946+ ivar_names = @cls_ivar_names[i].split(";")
1947+ ivar_types = @cls_ivar_types[i].split(";")
1948+ ij = 0
1949+ while ij < ivar_names.length
1950+ if ivar_names[ij] == iname
1951+ if ivar_types[ij] == "int"
1952+ ivar_types[ij] = ptypes[pi]
1953+ end
1954+ end
1955+ ij = ij + 1
1956+ end
1957+ @cls_ivar_types[i] = join_sep(ivar_types, ";")
1958+ end
1959+ end
1960+ pi = pi + 1
1961+ end
1962+ end
1963+ end
1964+ end
1965+ k = k + 1
1966+ end
1967+ end
1968+ end
1969+ i = i + 1
1970+ end
1971+ end
1972+
17411973 def infer_all_returns
1974+ # Pre-pass: scan for .new calls to infer constructor param types
1975+ infer_constructor_types
1976+ # Update ivar types from constructor params
1977+ update_ivar_types_from_params
1978+
17421979 # Top-level methods
17431980 i = 0
17441981 while i < @meth_names.length
@@ -1828,10 +2065,11 @@
18282065 if j < returns.length
18292066 returns[j] = rt
18302067 end
2068+ # Save incrementally so later methods can see updated return types
2069+ @cls_meth_returns[i] = join_sep(returns, ";")
18312070 pop_scope
18322071 j = j + 1
18332072 end
1834- @cls_meth_returns[i] = join_sep(returns, ";")
18352073
18362074 # Class methods
18372075 cmnames = @cls_cmeth_names[i].split(";")
@@ -1883,6 +2121,37 @@
18832121 end
18842122 end
18852123 end
2124+ # Also check super calls
2125+ if @nd_type[sid] == "SuperNode"
2126+ super_args = @nd_arguments[sid]
2127+ if super_args >= 0
2128+ sa_ids = get_args(super_args)
2129+ sk = 0
2130+ while sk < sa_ids.length
2131+ if @nd_type[sa_ids[sk]] == "LocalVariableReadNode"
2132+ if @nd_name[sa_ids[sk]] == pname
2133+ # This param is passed to parent's initialize at position sk
2134+ if @cls_parents[ci] != ""
2135+ parent_ci = find_class_idx(@cls_parents[ci])
2136+ if parent_ci >= 0
2137+ parent_init = cls_find_method_direct(parent_ci, "initialize")
2138+ if parent_init >= 0
2139+ parent_ptypes = @cls_meth_ptypes[parent_ci].split("|")
2140+ if parent_init < parent_ptypes.length
2141+ ppt = parent_ptypes[parent_init].split(",")
2142+ if sk < ppt.length
2143+ return ppt[sk]
2144+ end
2145+ end
2146+ end
2147+ end
2148+ end
2149+ end
2150+ end
2151+ sk = sk + 1
2152+ end
2153+ end
2154+ end
18862155 k = k + 1
18872156 end
18882157 end
@@ -2057,7 +2326,8 @@
20572326 end
20582327 if mname == "to_a"
20592328 if @nd_receiver[nid] >= 0
2060- if @nd_type[@nd_receiver[nid]] == "RangeNode"
2329+ rt = infer_type(@nd_receiver[nid])
2330+ if rt == "range"
20612331 @needs_int_array = 1
20622332 @needs_gc = 1
20632333 end
@@ -3058,9 +3328,36 @@
30583328 lnames = []
30593329 ltypes = []
30603330 scan_locals(bid, lnames, ltypes, params)
3331+ # Declare all locals first so block param inference can see them
30613332 j = 0
30623333 while j < lnames.length
30633334 declare_var(lnames[j], ltypes[j])
3335+ j = j + 1
3336+ end
3337+ # Second pass: re-scan with types now in scope for better block param inference
3338+ lnames2 = []
3339+ ltypes2 = []
3340+ scan_locals(bid, lnames2, ltypes2, params)
3341+ # Update types that may have improved
3342+ j = 0
3343+ while j < lnames2.length
3344+ k = 0
3345+ while k < lnames.length
3346+ if lnames[k] == lnames2[j]
3347+ if ltypes[k] == "int"
3348+ if ltypes2[j] != "int"
3349+ ltypes[k] = ltypes2[j]
3350+ set_var_type(lnames[k], ltypes2[j])
3351+ end
3352+ end
3353+ end
3354+ k = k + 1
3355+ end
3356+ j = j + 1
3357+ end
3358+ # Emit declarations
3359+ j = 0
3360+ while j < lnames.length
30643361 emit(" " + c_type(ltypes[j]) + " lv_" + lnames[j] + " = " + c_default_val(ltypes[j]) + ";")
30653362 j = j + 1
30663363 end
@@ -3121,6 +3418,19 @@
31213418 recv_type = ""
31223419 if @nd_receiver[nid] >= 0
31233420 recv_type = infer_type(@nd_receiver[nid])
3421+ # If type is int and receiver is local var, check names array
3422+ if recv_type == "int"
3423+ if @nd_type[@nd_receiver[nid]] == "LocalVariableReadNode"
3424+ rname = @nd_name[@nd_receiver[nid]]
3425+ ri = 0
3426+ while ri < names.length
3427+ if names[ri] == rname
3428+ recv_type = types[ri]
3429+ end
3430+ ri = ri + 1
3431+ end
3432+ end
3433+ end
31243434 end
31253435 mname = @nd_name[nid]
31263436 if mname == "each"
@@ -3591,7 +3901,7 @@
35913901 if recv < 0
35923902 mi = find_method_idx(mname)
35933903 if mi >= 0
3594- return "sp_" + sanitize_name(mname) + "(" + compile_call_args(nid) + ")"
3904+ return "sp_" + sanitize_name(mname) + "(" + compile_call_args_with_defaults(nid, mi) + ")"
35953905 end
35963906 if @current_class_idx >= 0
35973907 cidx = cls_find_method(@current_class_idx, mname)
@@ -3868,6 +4178,25 @@
38684178 end
38694179 end
38704180
4181+ # reject as expression
4182+ if mname == "reject"
4183+ if @nd_block[nid] >= 0
4184+ return compile_reject_expr(nid)
4185+ end
4186+ end
4187+
4188+ # reduce/inject as expression
4189+ if mname == "reduce"
4190+ if @nd_block[nid] >= 0
4191+ return compile_reduce_expr(nid)
4192+ end
4193+ end
4194+ if mname == "inject"
4195+ if @nd_block[nid] >= 0
4196+ return compile_reduce_expr(nid)
4197+ end
4198+ end
4199+
38714200 # Math
38724201 if @nd_type[recv] == "ConstantReadNode"
38734202 if @nd_name[recv] == "Math"
@@ -3877,12 +4206,27 @@
38774206 end
38784207 end
38794208
3880- # to_a on range
4209+ # to_a on range (including parenthesized range)
38814210 if mname == "to_a"
4211+ range_nid = -1
38824212 if @nd_type[recv] == "RangeNode"
4213+ range_nid = recv
4214+ end
4215+ if @nd_type[recv] == "ParenthesesNode"
4216+ pb = @nd_body[recv]
4217+ if pb >= 0
4218+ pstmts = get_stmts(pb)
4219+ if pstmts.length > 0
4220+ if @nd_type[pstmts[0]] == "RangeNode"
4221+ range_nid = pstmts[0]
4222+ end
4223+ end
4224+ end
4225+ end
4226+ if range_nid >= 0
38834227 @needs_int_array = 1
38844228 @needs_gc = 1
3885- return "sp_IntArray_from_range(" + compile_expr(@nd_left[recv]) + ", " + compile_expr(@nd_right[recv]) + ")"
4229+ return "sp_IntArray_from_range(" + compile_expr(@nd_left[range_nid]) + ", " + compile_expr(@nd_right[range_nid]) + ")"
38864230 end
38874231 end
38884232
@@ -3984,6 +4328,40 @@
39844328 "(" + lc + " " + op + " " + rc + ")"
39854329 end
39864330
4331+ def compile_call_args_with_defaults(nid, mi)
4332+ args_id = @nd_arguments[nid]
4333+ arg_ids = []
4334+ if args_id >= 0
4335+ arg_ids = get_args(args_id)
4336+ end
4337+ pnames = @meth_param_names[mi].split(",")
4338+ defaults = @meth_has_defaults[mi].split(",")
4339+ result = ""
4340+ k = 0
4341+ while k < pnames.length
4342+ if k > 0
4343+ result = result + ", "
4344+ end
4345+ if k < arg_ids.length
4346+ result = result + compile_expr(arg_ids[k])
4347+ else
4348+ # Use default value
4349+ if k < defaults.length
4350+ def_id = defaults[k].to_i
4351+ if def_id >= 0
4352+ result = result + compile_expr(def_id)
4353+ else
4354+ result = result + "0"
4355+ end
4356+ else
4357+ result = result + "0"
4358+ end
4359+ end
4360+ k = k + 1
4361+ end
4362+ result
4363+ end
4364+
39874365 def compile_call_args(nid)
39884366 args_id = @nd_arguments[nid]
39894367 if args_id < 0
@@ -4873,6 +5251,16 @@
48735251 @in_loop = old
48745252 end
48755253
5254+ def compile_reduce_expr(nid)
5255+ # Emit the reduce loop as side effects, return accumulator
5256+ compile_reduce_block(nid)
5257+ bp1 = get_block_param(nid, 0)
5258+ if bp1 == ""
5259+ bp1 = "_acc"
5260+ end
5261+ "lv_" + bp1
5262+ end
5263+
48765264 def compile_reduce_block(nid)
48775265 rc = compile_expr(@nd_receiver[nid])
48785266 init_val = compile_arg0(nid)
@@ -4908,6 +5296,40 @@
49085296 end
49095297 end
49105298
5299+ def compile_reject_expr(nid)
5300+ rc = compile_expr(@nd_receiver[nid])
5301+ bp1 = get_block_param(nid, 0)
5302+ if bp1 == ""
5303+ bp1 = "_x"
5304+ end
5305+ rt = infer_type(@nd_receiver[nid])
5306+ if rt == "int_array"
5307+ @needs_int_array = 1
5308+ tmp_arr = new_temp
5309+ emit(" sp_IntArray *" + tmp_arr + " = sp_IntArray_new();")
5310+ tmp_i = new_temp
5311+ emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_IntArray_length(" + rc + "); " + tmp_i + "++) {")
5312+ emit(" lv_" + bp1 + " = sp_IntArray_get(" + rc + ", " + tmp_i + ");")
5313+ @indent = @indent + 1
5314+ blk = @nd_block[nid]
5315+ if blk >= 0
5316+ body = @nd_body[blk]
5317+ if body >= 0
5318+ stmts = get_stmts(body)
5319+ if stmts.length > 0
5320+ last = stmts[stmts.length - 1]
5321+ cond = compile_expr(last)
5322+ emit(" if (!(" + cond + ")) sp_IntArray_push(" + tmp_arr + ", lv_" + bp1 + ");")
5323+ end
5324+ end
5325+ end
5326+ @indent = @indent - 1
5327+ emit(" }")
5328+ return tmp_arr
5329+ end
5330+ "0"
5331+ end
5332+
49115333 def compile_reject_block(nid)
49125334 rc = compile_expr(@nd_receiver[nid])
49135335 bp1 = get_block_param(nid, 0)