Commit 04000f21

04000f21a009Merged

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

Description

fix(codegen): reject unresolved constant references with a clear error

p Foo for an undefined constant leaked the bare Foo identifier into the C output, surfacing as gcc's "undeclared identifier" error several layers downstream. p Foo.bar, p Foo.bar.baz.quux, and p Foo.baz.new.quux were worse: they silently lowered the whole chain to 0 because the receiver fell through every dispatcher and hit the compile_call_expr "0" fallback. p Foo.new.baz mixed both modes by tripping over sp_Foo instead.

compile_expr's ConstantReadNode arm now rejects a name that isn't either:

  • a user-defined class / module / constant (via const_namespace_exists), or
  • a built-in module-like receiver / class name / exception name that the dispatcher handles directly (via the new is_known_constant_name)

When neither matches, the codegen aborts with Error: uninitialized constant <Name> — the same shape Ruby's NameError takes for the same situation. Every method-call shape from issue #75 reaches this arm via the receiver-compile step, so all of Foo, Foo.bar, Foo.bar.baz.quux, Foo.new.baz, and Foo.baz.new.quux now produce one consistent message instead of the four different broken behaviours.

test/known_constants.rb pins the legitimate constant references that look superficially similar (user class, user constant, Math., ARGV) — the new gate must let all of them through.

Closes #75

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

Details

Commit
04000f21a009
Parent
c036aa7ddf59

spinel_codegen.rbmodifiedView file

@@ -843,6 +843,33 @@
843843 0
844844 end
845845
846+ # Constant names the codegen recognises as legitimate even when no
847+ # user-defined class / module / constant of the same name exists.
848+ # These are dispatcher-handled module-like receivers (Math, File,
849+ # ENV, Dir, Time, Process, IO), the global ARGV, the built-in type
850+ # names used in `is_a?` / `case`/`when` arms, and a handful of
851+ # exception classes referenced by `raise` / `rescue` patterns.
852+ def is_known_constant_name(name)
853+ if const_namespace_exists(name) == 1
854+ return 1
855+ end
856+ if name == "ARGV" || name == "ENV" || name == "STDIN" || name == "STDOUT" || name == "STDERR"
857+ return 1
858+ end
859+ if name == "Math" || name == "File" || name == "Dir" || name == "Time" || name == "IO" || name == "Process" || name == "Kernel" || name == "Comparable" || name == "Enumerable"
860+ return 1
861+ end
862+ if name == "Object" || name == "Integer" || name == "String" || name == "Float" || name == "Symbol" || name == "Array" || name == "Hash" || name == "Range" || name == "Numeric" || name == "TrueClass" || name == "FalseClass" || name == "NilClass" || name == "Proc" || name == "Lambda" || name == "Regexp" || name == "MatchData" || name == "StringIO" || name == "Fiber"
863+ return 1
864+ end
865+ # Common exception classes referenced by raise / rescue. We
866+ # don't model the exception hierarchy beyond name-tagging.
867+ if name == "StandardError" || name == "RuntimeError" || name == "ArgumentError" || name == "TypeError" || name == "NameError" || name == "NoMethodError" || name == "IndexError" || name == "KeyError" || name == "ZeroDivisionError" || name == "FloatDomainError" || name == "RangeError" || name == "IOError" || name == "Errno" || name == "NotImplementedError" || name == "StopIteration" || name == "RegexpError" || name == "FrozenError" || name == "LocalJumpError" || name == "Exception"
868+ return 1
869+ end
870+ 0
871+ end
872+
846873 def current_lexical_scope_name
847874 if @current_lexical_scope != ""
848875 return @current_lexical_scope
@@ -12593,6 +12620,17 @@
1259312620 end
1259412621 return "cst_" + rname
1259512622 end
12623+ # Built-in module-like constants (Math, File, ENV, …) and
12624+ # registered classes / modules legitimately reach here as a
12625+ # method-call receiver and don't need their own value at the
12626+ # use site. Any other unresolved constant means the user wrote
12627+ # a name we don't know about — reject it now with a clear
12628+ # NameError-style message instead of emitting a bare C
12629+ # identifier that the C compiler later trips over (issue #75).
12630+ if is_known_constant_name(rname) == 0
12631+ $stderr.puts "Error: uninitialized constant " + rname
12632+ exit(1)
12633+ end
1259612634 return rname
1259712635 end
1259812636 if t == "ConstantPathNode"