Commit 763c8837

763c88376e4eMerged

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

Description

Fix constant namespace collisions and uninitialized constant NameError

  • register_globals_consts: allow value constants even when a module/class has the same name (M::V = "str" is a value constant even if module V exists). Fixes module_body_string_const_interp.
  • emit_puts_one: check class names before type dispatch so puts SomeClass always prints the class name even if a value constant has the same name.
  • emit_expr ConstantReadNode: emit runtime NameError raise for unregistered or TY_UNKNOWN constants instead of failing at compile time. Fixes uninit_const_raises_nameerror.
  • compile_stmt ConstantWriteNode: skip emission when lv->type == TY_UNKNOWN (avoids referencing undeclared C global for self-referential constants).
  • emit_puts_one: handle TY_UNKNOWN ConstantReadNode/ConstantPathNode by emitting the expression (which raises NameError at runtime).

Refs #spinel-c Co-Authored-By: Claude Sonnet 4.6 [email protected]

Details

Commit
763c88376e4e
Parent
2daed1720db9

src/analyze.cmodifiedView file

@@ -2274,8 +2274,10 @@
22742274 LocalVar *cv = comp_const_intern(c, nm);
22752275 cv->type = TY_REGEX;
22762276 }
2277- /* a Struct/Data const names a class, not a value constant */
2278- if (nm && is_c_ident(nm) && comp_class_index(c, nm) < 0 && !is_regex_const) {
2277+ /* a Struct/Data const names a class, not a value constant.
2278+ Do NOT skip when the name collides with a module: M::V = "str" is a
2279+ value constant even though top-level `module V` exists. */
2280+ if (nm && is_c_ident(nm) && !is_regex_const) {
22792281 LocalVar *cv = comp_const_intern(c, nm);
22802282 /* `CONST = SomeClass.new(...)`: reads of CONST during the new()
22812283 (i.e. inside initialize or anything it calls) must raise