Commit e07a1805

e07a18051babMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · ~3 modified

Description

Fix two undefined-reference paths: post-backstop rebind and NLR blocks

Two ways the generated C could reference a function that was never emitted:

  1. A method whose params are bound only from reads of an ivar that stayed TY_UNKNOWN through the main fixpoint (e.g. @left = nil in initialize, then written solely through poly-receiver attr writers). The post-fixpoint backstop widens such ivars to poly, but parameters bound from those reads were never re-bound, so the untyped-param backstop dropped the method while its call sites stayed — an undefined reference (bm_rbtree's is_red). Track whether the ivar backstops changed anything and re-run the param/return/write mini fixpoint in that case.

  2. foo { return 1 } — a block ending in a non-local return. The block value type inferred TY_UNKNOWN, so the yield-method call site failed the scalar-return gate, skipped inlining, and emitted a direct call to the never-emitted sp_foo (bm_throw). Type a block-final ReturnNode as its argument's type in yield_value_type / method_call_ret, and have emit_block_invoke append an unreachable default value after a block-final return so the ({...}) statement-expr keeps its type.

bm_throw now passes end-to-end; bm_rbtree compiles and links (its remaining failure is the GC-rooting class shared with bm_binary_trees). make test stays green (845 pass).

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

Details

Commit
e07a18051bab
Parent
99e8dee66559

src/analyze.cmodifiedView file

@@ -1047,10 +1047,11 @@
10471047 /* A read-only ivar (referenced but never assigned a typed value) stays
10481048 TY_UNKNOWN -> it has no C type. Such a slot always reads nil at runtime;
10491049 give it a boxed-nil poly field so `.nil?`/`.inspect` behave (#712). */
1050+ int ivar_backstop_changed = 0;
10501051 for (int ci = 0; ci < c->nclasses; ci++) {
10511052 ClassInfo *cl = &c->classes[ci];
10521053 for (int iv = 0; iv < cl->nivars; iv++)
1053- if (cl->ivar_types[iv] == TY_UNKNOWN) cl->ivar_types[iv] = TY_POLY;
1054+ if (cl->ivar_types[iv] == TY_UNKNOWN) { cl->ivar_types[iv] = TY_POLY; ivar_backstop_changed = 1; }
10541055 }
10551056 /* An attr_reader/attr_accessor ivar typed via a writer call (scalar type),
10561057 but whose class has no initialize that writes it, starts nil on fresh
@@ -1072,6 +1073,7 @@
10721073 if (t != TY_INT && t != TY_FLOAT && t != TY_STRING &&
10731074 t != TY_SYMBOL && t != TY_BOOL) continue;
10741075 cl->ivar_types[iv] = TY_POLY;
1076+ ivar_backstop_changed = 1;
10751077 /* Also patch the node-type cache for all InstanceVariableReadNode and
10761078 InstanceVariableWriteNode nodes that reference this ivar, so codegen
10771079 sees TY_POLY for both the struct field and the node type. */
@@ -1101,8 +1103,13 @@
11011103 /* Post-fixpoint body-usage inference: type any param still TY_UNKNOWN
11021104 from how it is used inside the method body (hash subscript patterns,
11031105 array-specific calls). Runs after the main fixpoint so caller-side
1104- types always win; the mini-loop below propagates the new types. */
1105- if (infer_hash_params(c) | infer_array_params(c) | infer_params_from_ivar_hash_ops(c)) {
1106+ types always win; the mini-loop below propagates the new types.
1107+ Also re-runs after the ivar backstops above widened an UNKNOWN slot
1108+ to poly: reads of such an ivar inferred UNKNOWN during the fixpoint,
1109+ leaving params bound from them untyped (and the method dropped,
1110+ turning its call sites into undefined references). */
1111+ if (infer_hash_params(c) | infer_array_params(c) | infer_params_from_ivar_hash_ops(c) |
1112+ ivar_backstop_changed) {
11061113 for (int iter = 0; iter < 16; iter++) {
11071114 int ch = 0;
11081115 ch |= infer_param_types(c);