Commit 2cfa705c

2cfa705c16c1Merged

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

Description

feat(codegen): warn on self-referential top-level const read in class init

Issue #646: CONST = Class.new(...) whose initialize body reads CONST used to silently emit 0 (compile-time "cannot resolve" warning) — or worse, when the dispatch chain happened to resolve statically through to a real ivar access, the partially-init pointer got dereferenced and the program segfaulted.

Detect the canonical self-ref shape at compile time and emit a clear warning that names the const, the class, and the conflicting <const> = <Class>.new assignment. Pattern: while compiling any method in class <C>, a ConstantReadNode of <X> fires the warning when @const_expr_ids[<X>] is a CallNode new whose recv is the ConstantReadNode <C>. Dedupes via the shared @unresolved_call_warnings stash so the same site fires once.

Does NOT yet plug the runtime segfault for the transitive dispatch case (Tep::APP.sched_current chain from the tep PG repro) — that needs per-const lifecycle tracking on the runtime side and is a bigger change. The warning surfaces the bug class at compile time, which closes the silent-failure mode the tep#13 reporter hit.

Refs #646.

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

Details

Commit
2cfa705c16c1
Parent
1aeb8559dc2f

spinel_codegen.rbmodifiedView file

@@ -4601,6 +4601,54 @@
46014601 $stderr.puts "warning: in " + ctx + ": uninitialized constant '" + rname + "' (emitting 0)"
46024602 end
46034603
4604+ # Issue #646: warn when a class's body reads a top-level const that
4605+ # the program assigns via `<const> = <ThisClass>.new(...)` — i.e.
4606+ # the read happens during the const's own RHS evaluation. The slot
4607+ # is NULL during init; any `.method` / `->iv_` deref will segfault.
4608+ # Pattern-match the const-init RHS for `<ThisClass>.new` (the
4609+ # canonical shape that triggers the bug). Dedupes through the same
4610+ # warning stash as warn_unresolved_call / warn_unresolved_const.
4611+ def warn_self_ref_const_init(rname)
4612+ if @current_class_idx < 0
4613+ return
4614+ end
4615+ cur_cname = @cls_names[@current_class_idx]
4616+ ci_self = find_const_idx(rname)
4617+ if ci_self < 0 || ci_self >= @const_expr_ids.length
4618+ return
4619+ end
4620+ eid_self = @const_expr_ids[ci_self]
4621+ if eid_self < 0
4622+ return
4623+ end
4624+ # RHS must be a `<Class>.new(...)` shape: CallNode with mname
4625+ # "new" whose recv is a ConstantReadNode naming the current
4626+ # class.
4627+ if @nd_type[eid_self] != "CallNode"
4628+ return
4629+ end
4630+ if @nd_name[eid_self] != "new"
4631+ return
4632+ end
4633+ recv_self = @nd_receiver[eid_self]
4634+ if recv_self < 0 || @nd_type[recv_self] != "ConstantReadNode"
4635+ return
4636+ end
4637+ if @nd_name[recv_self] != cur_cname
4638+ return
4639+ end
4640+ key_self = "_self_ref_:" + rname + ":" + cur_cname
4641+ j_self = 0
4642+ while j_self < @unresolved_call_warnings.length
4643+ if @unresolved_call_warnings[j_self] == key_self
4644+ return
4645+ end
4646+ j_self = j_self + 1
4647+ end
4648+ @unresolved_call_warnings.push(key_self)
4649+ $stderr.puts "warning: in " + cur_cname + "#" + @current_method_name + ": reading constant '" + rname + "' from within '" + rname + " = " + cur_cname + ".new' — slot is NULL during init; MRI raises NameError"
4650+ end
4651+
46044652 # Walk every class's parent chain. A cycle anywhere on the chain is
46054653 # a fatal program error: bail with a clear message instead of letting
46064654 # the recursive helpers loop forever. Self-inheritance (`class A < A`)
@@ -11829,6 +11877,15 @@
1182911877 if lv != ""
1183011878 return lv
1183111879 end
11880+ # Issue #646: detect the self-referential init shape — a class
11881+ # whose `initialize` reads a top-level const that's being
11882+ # initialized via `<const> = <ThisClass>.new(...)`. The slot
11883+ # is NULL during init; the read returns NULL and any subsequent
11884+ # `.method` / `->iv_` dereference segfaults. Warn at compile
11885+ # time so the user catches this before runtime. (MRI raises
11886+ # NameError; closing the runtime gap would need lifecycle
11887+ # tracking — see #646 discussion.)
11888+ warn_self_ref_const_init(rname)
1183211889 return "cst_" + rname
1183311890 end
1183411891 # a class constant in value position