Commit 80165494
80165494ac62Merged
Ryan Seys <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified
Description
analyze,codegen: Module#const_defined? folds to TRUE / FALSE statically
A.const_defined?(:X) previously fell through compile_object_method_expr's
recv_type == "class" arm (which only handled to_s / name / inspect
/ superclass / ancestors / nil? / ==/!= / </<=/>/>=) and raised
undefined method 'const_defined?' for class. Even where it
"worked" via a fallback it would have returned 1 / 0 rather
than CRuby's true / false.
Spinel emits one C global per user-class constant named
cst_<Class>_<X>, and the analyzer tracks those names in
@const_names. The static case — literal class receiver
(ConstantReadNode) + literal symbol arg (SymbolNode) —
reduces to a find_const_idx("<Class>_<X>") lookup and folds
to TRUE or FALSE at compile time.
Dynamic receivers, non-literal symbol args, and builtin classes
whose constants live outside @const_names (Math::PI etc.)
fall through and keep raising — the const-table introspection
needed to cover those is its own structural change.
Analyzer side: return-type arm alongside include? / cover? so
puts formats the result as true / false.
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- 80165494ac62
- Parent
- 24ec1cf637a4
spinel_analyze.rbmodifiedView file
if mname == "cover?" return "bool" end if mname == "const_defined?" return "bool" end if mname == "===" return "bool" endspinel_codegen.rbmodifiedView file
Loading diff…
test/module_const_defined.rbaddedView file
Loading diff…
test/module_const_defined.rb.expectedaddedView file
Loading diff…