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

@@ -4311,6 +4311,9 @@
43114311 if mname == "cover?"
43124312 return "bool"
43134313 end
4314+ if mname == "const_defined?"
4315+ return "bool"
4316+ end
43144317 if mname == "==="
43154318 return "bool"
43164319 end