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
0 end # Constant names the codegen recognises as legitimate even when no # user-defined class / module / constant of the same name exists. # These are dispatcher-handled module-like receivers (Math, File, # ENV, Dir, Time, Process, IO), the global ARGV, the built-in type # names used in `is_a?` / `case`/`when` arms, and a handful of # exception classes referenced by `raise` / `rescue` patterns. def is_known_constant_name(name) if const_namespace_exists(name) == 1 return 1 end if name == "ARGV" || name == "ENV" || name == "STDIN" || name == "STDOUT" || name == "STDERR" return 1 end if name == "Math" || name == "File" || name == "Dir" || name == "Time" || name == "IO" || name == "Process" || name == "Kernel" || name == "Comparable" || name == "Enumerable" return 1 end 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" return 1 end # Common exception classes referenced by raise / rescue. We # don't model the exception hierarchy beyond name-tagging. 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" return 1 end 0 end def current_lexical_scope_name if @current_lexical_scope != "" return @current_lexical_scope end return "cst_" + rname end # Built-in module-like constants (Math, File, ENV, …) and # registered classes / modules legitimately reach here as a # method-call receiver and don't need their own value at the # use site. Any other unresolved constant means the user wrote # a name we don't know about — reject it now with a clear # NameError-style message instead of emitting a bare C # identifier that the C compiler later trips over (issue #75). if is_known_constant_name(rname) == 0 $stderr.puts "Error: uninitialized constant " + rname exit(1) end return rname end if t == "ConstantPathNode"test/known_constants.rbaddedView file
Loading diff…