Commit 25c6cc35

25c6cc357a4aMerged

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

Description

fix(codegen): pre-scan no-arg-new classes to lift @needs_class_table

A program that declared a namespaced class with no other class- hierarchy usage (no .class, no is_a?, no ancestors) left @needs_class_table at 0, which skipped the #define SP_CLASS_COUNT N emit. The Tier 5 dispatch table (emit_class_new_dispatch) still fired -- it's gated on any user class supporting a no-arg new -- and the emitted sp_class_constructors[SP_CLASS_COUNT] table tripped a C "undeclared identifier" error.

Add a pre-scan alongside the existing @needs_class_parents/@needs_class_ancestors/@needs_class_for_poly lift: walk @cls_names and set @needs_class_table = 1 the moment we find a class that supports no-arg new. SP_CLASS_COUNT then lands ahead of the Tier 5 table that references it.

Issue #524's reproduction (class SomeUnknown::Thing with SomeUnknown undefined) was the visible trigger -- the issue suggested either a clean diagnostic (option 1) or permissive synthesis (option 2). Going with the permissive shape because the real-world case is class Minitest::Test reopens against external rubygem-defined classes that spinel-AOT can't see; emit the table and let unresolved call sites fall through to the existing cannot-resolve warning.

Closes #524

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

Details

Commit
25c6cc357a4a
Parent
d80c0225978e

spinel_codegen.rbmodifiedView file

@@ -6340,6 +6340,26 @@
63406340 if @needs_class_parents == 1 || @needs_class_ancestors == 1 || @needs_class_for_poly == 1
63416341 @needs_class_table = 1
63426342 end
6343+ # Tier 5 (`emit_class_new_dispatch`) emits a
6344+ # `sp_class_constructors[SP_CLASS_COUNT]` table whenever any
6345+ # user class supports a no-arg `new`. That table references
6346+ # `SP_CLASS_COUNT`, which only lands when @needs_class_table
6347+ # is set. Without this pre-scan, a program that only declares
6348+ # a class (no `.class` / `is_a?` / ancestors usage anywhere)
6349+ # left @needs_class_table at 0, skipped the `#define
6350+ # SP_CLASS_COUNT` emit, and the Tier 5 table tripped a C
6351+ # `undeclared identifier` error. Issue #524.
6352+ if @needs_class_table == 0 && @cls_names.length > 0
6353+ ci_scan = 0
6354+ while ci_scan < @cls_names.length
6355+ if cls_supports_noarg_new(ci_scan) == 1
6356+ @needs_class_table = 1
6357+ ci_scan = @cls_names.length
6358+ else
6359+ ci_scan = ci_scan + 1
6360+ end
6361+ end
6362+ end
63436363 emit_sym_runtime
63446364 emit_ffi_externs
63456365 # Emit program-specific regexp patterns