Commit 61d1cdfe

61d1cdfec6f7Merged

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

Description

Type an unresolved stdlib constructor as int, not an undeclared struct

Storing a stdlib class with no Spinel struct (Mutex, Pathname, OpenStruct, IPAddr, ...) in an instance variable or local made analyze type the slot obj_<Class>, so codegen emitted an sp_<Class> * field it never defined and the C build failed with unknown type name 'sp_<Class>'. This was the largest spinelgems codegen cluster (21 gems), each blocked on a different stdlib type.

The two .new fallbacks (infer_type and infer_ivar_init_type) returned obj_<name> for any constructor not otherwise special-cased. Guard them with constructable_class_struct?, which is true only for user-defined classes and the builtin Object (the one struct-backed builtin that reaches this fallback). An unconstructable class now types as int — codegen already lowers the unresolved .new to integer 0 — so the local/ivar compiles. The stdlib object stays inert (its methods remain unresolved), which is the boxed-fallback behavior the issue allows.

Fixes #1305

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

Details

Commit
61d1cdfec6f7
Parent
12f205197ae4

spinel_analyze.rbmodifiedView file

@@ -1736,6 +1736,17 @@
17361736 -1
17371737 end
17381738
1739+ # True when codegen can emit a real `sp_<name> *` for `name.new`: a
1740+ # user-defined class, or the builtin `Object` (the only struct-backed
1741+ # builtin that reaches the generic `.new` fallback — Array/Hash/
1742+ # String/Random/StringIO/StringScanner/Fiber are special-cased
1743+ # before it). Unimplemented stdlib classes (Mutex, Pathname,
1744+ # OpenStruct, ...) have no struct, so their `.new` falls back to the
1745+ # int slot rather than an undeclared `sp_<Class> *`. Issue #1305.
1746+ def constructable_class_struct?(name)
1747+ find_class_idx(name) >= 0 || name == "Object"
1748+ end
1749+
17391750 # Walk @cls_parents starting from `child_idx` and return 1 if we
17401751 # ever land on `ancestor_idx`. Used by `is_a?(<Klass>)` on poly
17411752 # receivers to enumerate descendant cls_ids — `recv.is_a?(C)` is
@@ -7100,7 +7111,16 @@
71007111 if is_exception_class_name(rn) == 1
71017112 return "exception"
71027113 end
7103- return "obj_" + rn
7114+ # A constructor whose target class has no Spinel struct (an
7115+ # unimplemented stdlib class such as Mutex/Pathname/OpenStruct)
7116+ # can't be pointed at by a typed `sp_<Class> *` slot, which codegen
7117+ # would emit but never define. Codegen already lowers the unresolved
7118+ # `.new` to the integer `0`, so type the slot as int — the value's
7119+ # natural C type — and the local/ivar compiles.
7120+ if constructable_class_struct?(rn) == true
7121+ return "obj_" + rn
7122+ end
7123+ return "int"
71047124 end
71057125 end
71067126 end
@@ -13461,7 +13481,14 @@
1346113481 if module_name_exists(rname) == 1
1346213482 return ""
1346313483 end
13464- return "obj_" + rname
13484+ # An unresolved stdlib class (no Spinel struct) can't back an
13485+ # `sp_<Class> *` ivar field. Codegen lowers the unresolved `.new`
13486+ # to integer 0, so type the slot as int; mirrors the infer_type
13487+ # `.new` fallback. Issue #1305.
13488+ if constructable_class_struct?(rname) == true
13489+ return "obj_" + rname
13490+ end
13491+ return "int"
1346513492 end
1346613493 end
1346713494 end