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_classespass 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
@cvar_init_values = "".split(",") @const_expr_ids = [] @const_scope_names = "".split(",") # Parallel to @const_names: when const `<X>`'s init RHS is the # canonical `<Class>.new(...)` shape, record `<Class>` here so # codegen can emit the per-const in-progress flag + read-side # guard that prevents the segfault from reading the partially- # initialized slot during the same RHS evaluation. Empty string # means "no guard needed" (other init shape — literal, helper- # built, etc.). Issue #646. @const_init_class = "".split(",") # `redo` -- labeled-goto target stack. Each loop emitter pushes # a fresh label name when entering an iteration body and pops on return "" end # Issue #646: scan each const's init RHS for the `<Class>.new(...)` # shape. Codegen consumes @const_init_class[ci] (class name or "") # to decide whether to emit a per-const in-progress flag and a # read-side NameError guard. Only the direct shape is detected # here; transitive (`<X> = build_app()` where build_app calls # App.new internally) would need call-graph analysis and is left # to a follow-up. def detect_const_init_classes @const_init_class = "".split(",") i = 0 while i < @const_expr_ids.length cls_name_cic = "" eid_cic = @const_expr_ids[i] if eid_cic >= 0 && @nd_type[eid_cic] == "CallNode" && @nd_name[eid_cic] == "new" recv_cic = @nd_receiver[eid_cic] if recv_cic >= 0 && @nd_type[recv_cic] == "ConstantReadNode" cls_name_cic = @nd_name[recv_cic] # Only treat as guard target when the class actually exists -- # otherwise the `new` call would have been caught by the # existing unresolved-const path. if find_class_idx(cls_name_cic) < 0 cls_name_cic = "" end end end @const_init_class.push(cls_name_cic) i = i + 1 end end def infer_proc_blk_param_types @meth_blk_param_types = "".split(",") mi = 0 # already ran inside arbitrate_rbs_vs_inferred_methods. arbitrate_rbs_method_params_definite_conflict infer_proc_blk_param_types # Issue #646: classify each top-level const init by whether the # RHS is `<Class>.new(...)`. Codegen guards reads of those # consts during their own RHS evaluation to raise NameError # instead of segfaulting on the partially-init slot. detect_const_init_classes if ENV["SP_POLY_REPORT"] == "1" emit_poly_report end buf = ir_emit_sa(buf, "@const_types", @const_types) buf = ir_emit_ia(buf, "@const_expr_ids", @const_expr_ids) buf = ir_emit_sa(buf, "@const_scope_names", @const_scope_names) buf = ir_emit_sa(buf, "@const_init_class", @const_init_class) buf = ir_emit_sa(buf, "@cvar_names", @cvar_names) buf = ir_emit_sa(buf, "@cvar_types", @cvar_types) buf = ir_emit_sa(buf, "@cvar_init_values", @cvar_init_values)spinel_codegen.rbmodifiedView file
Loading diff…
test/const_self_ref_raises_name_error.rbaddedView file
Loading diff…
test/const_self_ref_raises_name_error.rb.expectedaddedView file
Loading diff…