Commit e1ba19c8

e1ba19c804bbMerged

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

Description

fix(codegen): widen polymorphic ivar slots to poly with boxed writes

Issue #130 variants 1 and 2: an instance variable assigned definite-typed values across multiple methods used to either fail to compile (String+Int → -Wint-conversion), silently coerce (Int+Float → 42 becomes 42.0), or miscompile depending on which type lost inference. The slot was forced to a single concrete type, so the losing assignments produced unsafe casts.

The fix has two coupled parts.

(1) Inference (scan_ivars): when both the existing and new writes are definite-literal AST kinds (IntegerNode, FloatNode, StringNode, SymbolNode, TrueNode, FalseNode) and the inferred types disagree, widen the slot to "poly". The dual definite-literal gate avoids false widening on infer_ivar_init_type's "int" fallback for non-recognized expressions (CallNodes, LocalVariableReadNodes). Without this gate the bootstrap breaks: spinel_codegen's own ivars like @current_method_name = "x" + n.to_s would falsely widen to poly because the CallNode's inferred type defaults to int.

Tracking is via a parallel `@cls_ivar_init_definite` array of "0"/"1"
flags, one per ivar. add_ivar takes an optional definite parameter
and pushes it; struct fields and `class << self`-introduced ivars
default to non-definite (no scanned literal write yet).

(2) Codegen (ivar write emit): every assignment to a poly slot now boxes the RHS via box_expr_to_poly. Three emit paths needed the same treatment — the general InstanceVariableWriteNode statement form (compile_expr at ~19660), the constructor's inline ivar write in emit_constructor (~11600), and the rarer expression form (~13320). Read sites already dispatch through poly-aware emitters (sp_poly_puts and friends) that unbox; no read-side change needed.

Variant 3 of the issue (a single-write slot whose RHS is a ternary with mixed-type branches, segfaulting at runtime) is a different root: the slot widens to "int" because IfNode isn't a definite-literal, and the ternary RHS itself emits mixed types into one assignment. Filing separately so the fix can be designed against that AST shape directly.

Bootstrap (gen2 == gen3) clean. 198 tests + 56 benches pass.

Refs #130

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

Details

Commit
e1ba19c804bb
Parent
c4f1c7755676

spinel_codegen.rbmodifiedView file

@@ -112,6 +112,15 @@
112112 @cls_parents = "".split(",")
113113 @cls_ivar_names = "".split(",")
114114 @cls_ivar_types = "".split(",")
115+ # Issue #130: per-ivar flag — was the ivar's first scanned write a
116+ # definite-literal expression (IntegerNode, FloatNode, StringNode, etc.)?
117+ # `infer_ivar_init_type` falls back to "int" for non-recognized
118+ # CallNodes / LocalVariableReadNodes, so trusting the inferred type
119+ # alone produces false widening when the codegen widens to poly on
120+ # disagreement. The flag distinguishes "concrete literal write" from
121+ # "best-guess inference"; only when both old and new writes are
122+ # definite-literal do we widen to poly on disagreement.
123+ @cls_ivar_init_definite = "".split(",")
115124 @cls_meth_names = "".split(",")
116125 @cls_meth_params = "".split(",")
117126 @cls_meth_ptypes = "".split(",")
@@ -4739,6 +4748,18 @@
47394748 end
47404749 @cls_ivar_names.push(ivar_names)
47414750 @cls_ivar_types.push(ivar_types)
4751+ # Struct fields are added via attr_*-style fallback (no scanned literal
4752+ # write yet). Mark each as non-definite (#130).
4753+ struct_definite = ""
4754+ sk2 = 0
4755+ while sk2 < struct_fields.length
4756+ if sk2 > 0
4757+ struct_definite = struct_definite + ";"
4758+ end
4759+ struct_definite = struct_definite + "0"
4760+ sk2 = sk2 + 1
4761+ end
4762+ @cls_ivar_init_definite.push(struct_definite)
47424763 # Auto-generate initialize method for struct-derived classes
47434764 if struct_fields.length > 0
47444765 init_params = ""
@@ -5277,32 +5298,83 @@
52775298 0
52785299 end
52795300
5280- def add_ivar(ci, iname, itype)
5301+ def add_ivar(ci, iname, itype, definite = 0)
52815302 if @cls_ivar_names[ci] != ""
52825303 @cls_ivar_names[ci] = @cls_ivar_names[ci] + ";" + iname
52835304 @cls_ivar_types[ci] = @cls_ivar_types[ci] + ";" + itype
5305+ @cls_ivar_init_definite[ci] = @cls_ivar_init_definite[ci] + ";" + definite.to_s
52845306 else
52855307 @cls_ivar_names[ci] = iname
52865308 @cls_ivar_types[ci] = itype
5309+ @cls_ivar_init_definite[ci] = definite.to_s
5310+ end
5311+ end
5312+
5313+ # Issue #130: was the AST expression a definite-literal that
5314+ # `infer_ivar_init_type` types unambiguously? Used by scan_ivars to
5315+ # decide when to widen a multi-write ivar slot to poly. Only literal
5316+ # AST kinds count; CallNodes and LocalVariableReadNodes don't, even
5317+ # if the inference happens to return a known concrete type.
5318+ def is_definite_ivar_init(nid)
5319+ if nid < 0
5320+ return 0
5321+ end
5322+ t = @nd_type[nid]
5323+ if t == "IntegerNode" || t == "FloatNode" || t == "StringNode"
5324+ return 1
5325+ end
5326+ if t == "SymbolNode" || t == "TrueNode" || t == "FalseNode"
5327+ return 1
52875328 end
5329+ 0
52885330 end
52895331
5332+ def cls_ivar_definite_flag(ci, iname)
5333+ names = @cls_ivar_names[ci].split(";")
5334+ flags = @cls_ivar_init_definite[ci].split(";")
5335+ k = 0
5336+ while k < names.length
5337+ if names[k] == iname
5338+ if k < flags.length
5339+ return flags[k].to_i
5340+ end
5341+ return 0
5342+ end
5343+ k = k + 1
5344+ end
5345+ 0
5346+ end
5347+
52905348 def scan_ivars(ci, nid)
52915349 if nid < 0
52925350 return
52935351 end
52945352 if @nd_type[nid] == "InstanceVariableWriteNode"
52955353 iname = @nd_name[nid]
5354+ expr_first = @nd_expression[nid]
52965355 if ivar_exists(ci, iname) == 0
5297- vtype = infer_ivar_init_type(@nd_expression[nid])
5298- add_ivar(ci, iname, vtype)
5356+ vtype = infer_ivar_init_type(expr_first)
5357+ add_ivar(ci, iname, vtype, is_definite_ivar_init(expr_first))
52995358 else
5300- # Update type if current type is int (default/nil) and new type is better
5359+ # Issue #130: when the new write is a definite-literal AND the
5360+ # ivar's first scanned write was also a definite-literal AND the
5361+ # types disagree, widen to poly. The dual definite-literal gate
5362+ # avoids false widening on `infer_ivar_init_type`'s "int" fallback
5363+ # for non-recognized expressions (CallNodes, LocalVariableReadNodes).
5364+ # Without the gate, spinel_codegen's own ivars (e.g.,
5365+ # `@current_method_name = "x" + n.to_s`) would falsely widen and
5366+ # break the bootstrap.
53015367 expr = @nd_expression[nid]
53025368 if expr >= 0
53035369 if @nd_type[expr] != "NilNode"
53045370 vtype = infer_ivar_init_type(expr)
5305- if vtype != "int"
5371+ cur = cls_ivar_type(ci, iname)
5372+ new_def = is_definite_ivar_init(expr)
5373+ cur_def = cls_ivar_definite_flag(ci, iname)
5374+ if new_def == 1 && cur_def == 1 && cur != vtype && cur != "poly"
5375+ replace_ivar_type(ci, iname, "poly")
5376+ @needs_rb_value = 1
5377+ elsif vtype != "int"
53065378 update_ivar_type(ci, iname, vtype)
53075379 end
53085380 end
@@ -5713,6 +5785,7 @@
57135785 @cls_parents.push("")
57145786 @cls_ivar_names.push("")
57155787 @cls_ivar_types.push("")
5788+ @cls_ivar_init_definite.push("")
57165789 @cls_meth_names.push("")
57175790 @cls_meth_params.push("")
57185791 @cls_meth_ptypes.push("")
@@ -11526,7 +11599,15 @@
1152611599 @needs_gc = 1
1152711600 emit_raw(" " + self_arrow + ivar + " = " + iv_ctor + ";")
1152811601 else
11529- val = compile_expr(expr_id_iv)
11602+ # Issue #130: same poly-slot boxing as the general
11603+ # InstanceVariableWriteNode emit path (compile_expr branch).
11604+ # Initialize bodies can introduce one of the disagreeing
11605+ # writes to a multi-typed ivar.
11606+ if ivt == "poly"
11607+ val = box_expr_to_poly(expr_id_iv)
11608+ else
11609+ val = compile_expr(expr_id_iv)
11610+ end
1153011611 emit_raw(" " + self_arrow + ivar + " = " + val + ";")
1153111612 end
1153211613 else
@@ -13226,15 +13307,26 @@
1322613307 return self_arrow + sanitize_ivar(@nd_name[nid])
1322713308 end
1322813309 if t == "InstanceVariableWriteNode"
13229- val = compile_expr(@nd_expression[nid])
13310+ # Issue #130: same poly-slot boxing as the statement-form emit.
13311+ # Expression form (`x = (@y = expr)`) is rarer but reaches the
13312+ # same slot through a different compile path.
13313+ iname_w = @nd_name[nid]
13314+ ivt_w = ""
13315+ if @current_class_idx >= 0
13316+ ivt_w = cls_ivar_type(@current_class_idx, iname_w)
13317+ end
13318+ if ivt_w == "poly"
13319+ val = box_expr_to_poly(@nd_expression[nid])
13320+ else
13321+ val = compile_expr(@nd_expression[nid])
13322+ end
1323013323 # Check if in module method
1323113324 mi3 = 0
1323213325 while mi3 < @module_names.length
1323313326 mmod = @module_names[mi3]
1323413327 if mmod != ""
1323513328 if @current_method_name.start_with?(mmod + "_cls_")
13236- iname = @nd_name[nid]
13237- cname3 = mmod + "_" + iname[1, iname.length - 1]
13329+ cname3 = mmod + "_" + iname_w[1, iname_w.length - 1]
1323813330 ci3 = find_const_idx(cname3)
1323913331 if ci3 >= 0
1324013332 return "(cst_" + cname3 + " = " + val + ")"
@@ -13243,7 +13335,7 @@
1324313335 end
1324413336 mi3 = mi3 + 1
1324513337 end
13246- return "(" + self_arrow + sanitize_ivar(@nd_name[nid]) + " = " + val + ")"
13338+ return "(" + self_arrow + sanitize_ivar(iname_w) + " = " + val + ")"
1324713339 end
1324813340 if t == "ConstantReadNode"
1324913341 if @nd_name[nid] == "ARGV"
@@ -19575,7 +19667,16 @@
1957519667 return
1957619668 end
1957719669 end
19578- val = compile_expr(expr_id)
19670+ # Issue #130: poly slot — every concrete-typed RHS must be boxed
19671+ # to sp_RbVal. Without the box, C compiler sees `sp_RbVal = mrb_int`
19672+ # (or const char *, etc.) and either rejects the assignment or
19673+ # silently coerces. Read sites already dispatch through poly-aware
19674+ # emitters (sp_poly_puts, etc.) that unbox.
19675+ if ivt == "poly"
19676+ val = box_expr_to_poly(expr_id)
19677+ else
19678+ val = compile_expr(expr_id)
19679+ end
1957919680 # Check if we're in a module class method
1958019681 mod_ivar = 0
1958119682 mi3 = 0