Commit 0881702f

0881702f7a17Merged

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

Description

fix(analyze,codegen): const self-ref read raises NameError instead of segfault

Implements the Phase 1 plan from docs/issue-646-plan.md.

A top-level CONST = Class.new(...) assignment whose initialize body (or any method transitively called from it) reads CONST used to deref the NULL slot (cst_CONST is zero-init during the RHS evaluation) and segfault. Now matches MRI by raising NameError at the read site.

  • analyze: new detect_const_init_classes pass populates @const_init_class[i] with the class name when the const's init RHS matches the canonical <Class>.new(...) shape; exported through IR.
  • codegen: for each guarded const, emit static int sp_init_in_progress_<X> = 0; alongside the cst_ decl.
  • codegen emit_main: set the in-progress flag before the assignment and clear it after, so any read of during the call chain sees flag=1.
  • codegen compile_expr's ConstantReadNode arm: when the const has a guard, wrap the read with (flag ? (sp_raise_cls ("NameError", "uninitialized constant X"), <dead>) : cst_X). Uses a compound literal of the const's type as the dead branch so both struct and pointer slots type-check.

Covers both the direct shape (Class#initialize directly reads CONST) and the transitive shape (initialize calls a helper that reads CONST) because the runtime flag is set for the entire call chain.

Default raise mode: 625 / 0 / 0 (one new regression test). Optcarrot checksum 59662 unchanged.

Closes #646.

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
0881702f7a17
Parent
55e53fd1ff7b

spinel_analyze.rbmodifiedView file

@@ -305,6 +305,14 @@
305305 @cvar_init_values = "".split(",")
306306 @const_expr_ids = []
307307 @const_scope_names = "".split(",")
308+ # Parallel to @const_names: when const `<X>`'s init RHS is the
309+ # canonical `<Class>.new(...)` shape, record `<Class>` here so
310+ # codegen can emit the per-const in-progress flag + read-side
311+ # guard that prevents the segfault from reading the partially-
312+ # initialized slot during the same RHS evaluation. Empty string
313+ # means "no guard needed" (other init shape — literal, helper-
314+ # built, etc.). Issue #646.
315+ @const_init_class = "".split(",")
308316
309317 # `redo` -- labeled-goto target stack. Each loop emitter pushes
310318 # a fresh label name when entering an iteration body and pops on
@@ -14489,6 +14497,36 @@
1448914497 return ""
1449014498 end
1449114499
14500+ # Issue #646: scan each const's init RHS for the `<Class>.new(...)`
14501+ # shape. Codegen consumes @const_init_class[ci] (class name or "")
14502+ # to decide whether to emit a per-const in-progress flag and a
14503+ # read-side NameError guard. Only the direct shape is detected
14504+ # here; transitive (`<X> = build_app()` where build_app calls
14505+ # App.new internally) would need call-graph analysis and is left
14506+ # to a follow-up.
14507+ def detect_const_init_classes
14508+ @const_init_class = "".split(",")
14509+ i = 0
14510+ while i < @const_expr_ids.length
14511+ cls_name_cic = ""
14512+ eid_cic = @const_expr_ids[i]
14513+ if eid_cic >= 0 && @nd_type[eid_cic] == "CallNode" && @nd_name[eid_cic] == "new"
14514+ recv_cic = @nd_receiver[eid_cic]
14515+ if recv_cic >= 0 && @nd_type[recv_cic] == "ConstantReadNode"
14516+ cls_name_cic = @nd_name[recv_cic]
14517+ # Only treat as guard target when the class actually exists --
14518+ # otherwise the `new` call would have been caught by the
14519+ # existing unresolved-const path.
14520+ if find_class_idx(cls_name_cic) < 0
14521+ cls_name_cic = ""
14522+ end
14523+ end
14524+ end
14525+ @const_init_class.push(cls_name_cic)
14526+ i = i + 1
14527+ end
14528+ end
14529+
1449214530 def infer_proc_blk_param_types
1449314531 @meth_blk_param_types = "".split(",")
1449414532 mi = 0
@@ -21629,6 +21667,11 @@
2162921667 # already ran inside arbitrate_rbs_vs_inferred_methods.
2163021668 arbitrate_rbs_method_params_definite_conflict
2163121669 infer_proc_blk_param_types
21670+ # Issue #646: classify each top-level const init by whether the
21671+ # RHS is `<Class>.new(...)`. Codegen guards reads of those
21672+ # consts during their own RHS evaluation to raise NameError
21673+ # instead of segfaulting on the partially-init slot.
21674+ detect_const_init_classes
2163221675 if ENV["SP_POLY_REPORT"] == "1"
2163321676 emit_poly_report
2163421677 end
@@ -26911,6 +26954,7 @@
2691126954 buf = ir_emit_sa(buf, "@const_types", @const_types)
2691226955 buf = ir_emit_ia(buf, "@const_expr_ids", @const_expr_ids)
2691326956 buf = ir_emit_sa(buf, "@const_scope_names", @const_scope_names)
26957+ buf = ir_emit_sa(buf, "@const_init_class", @const_init_class)
2691426958 buf = ir_emit_sa(buf, "@cvar_names", @cvar_names)
2691526959 buf = ir_emit_sa(buf, "@cvar_types", @cvar_types)
2691626960 buf = ir_emit_sa(buf, "@cvar_init_values", @cvar_init_values)