Commit 06bf009d

06bf009dd646Merged

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

Description

codegen: uninitialised constant read raises NameError

ConstantReadNode for an unknown name used to emit a stderr warning and return 0, which let `X = X + 1` quietly produce 1 and `puts NONEXISTENT` print 0 instead of failing. Emit a stmt-expression raising NameError so a rescue can catch it and unrescued runs surface the bug.

Fixes #837 Fixes #838

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

Details

Commit
06bf009dd646
Parent
ff20bd874b7f

spinel_codegen.rbmodifiedView file

@@ -13492,8 +13492,14 @@
1349213492 return "sp_ruby_platform_str()"
1349313493 end
1349413494 if is_known_constant_name(rname) == 0
13495- warn_unresolved_const(rname)
13496- return "0"
13495+ # Uninitialised constant — raise NameError at runtime (CRuby
13496+ # behaviour). Stmt-expression so the call site still gets an
13497+ # expression value of the inferred type (the cast is reached
13498+ # only after sp_raise_cls's longjmp, so it's dead code that
13499+ # has to type-check).
13500+ @needs_setjmp = 1
13501+ rt_uc = infer_type(nid)
13502+ return "({ sp_raise_cls(\"NameError\", \"uninitialized constant " + rname + "\"); " + c_default_val(rt_uc) + "; })"
1349713503 end
1349813504 return rname
1349913505 end