Commit 26c49657
26c49657c625Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified
Description
feat(codegen): Class#name / inspect / == / != / eql? (#404 Phase 2)
Phase 1 (#404) wired .to_s on a sp_Class value plus the
class-as-value round-trip through locals / ivars / hash values.
Phase 2 fills in the rest of the "simple" reflection surface
that doesn't need precomputed ancestors:
-
.name/.inspectalias.to_s(sp_class_to_s lookup against the per-program sp_class_names[] table). CRuby's #inspect actually returns the bare class name too -- the<Class:Foo>-ish form is the Object#inspect fallback which Class#inspect overrides. Phase 2 stays at the bare-name shape; the canonical CRuby spelling polish is Phase 3 work. -
.==/.eql?->sp_class_eq(a, b)-- new runtime helper emitted alongside sp_class_to_s in emit_class_runtime. Just a cls_id field compare since sp_Class is a single-int wrapper. -
.!=->!sp_class_eq(...).
Two-side coverage: compile_eq adds an early arm so plain
Foo == Bar (handled at the == operator level, not via
compile_object_method_expr) also routes through sp_class_eq.
Without that, the lowered C ((sp_Class){0LL}) == ((sp_Class){1LL})
fails to compile -- plain == on a struct isn't valid C.
Inference: infer_method_name_type returns "string" for
.name on a class-typed recv. .to_s / .inspect already
returned "string" via the existing universal arms; .== /
.eql? / .!= route through the existing comparison-operator
inference (which already returns "bool").
Out of scope for Phase 2:
.superclass/.ancestors/.</.<=/.>/.>=case <obj> when <Class>/ dynamicis_a?(c)against a var
These need the precomputed ancestor table per docs/CLASS-OBJECT.md (#404 Phase 3 -- shared infrastructure with #422's runtime class_id on instances).
Test class_phase2_name_eq.rb covers .name / .inspect on class literals + through obj.class, the canonical equality combinations (same class, sibling subclass, eql? variant), and class-value inequality. The chained obj.class.name shape exercises both Phase 1's static dispatch (#419) and Phase 2's name lookup.
Refs #404
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 26c49657c625
- Parent
- aadd3c6627ab
spinel_analyze.rbmodifiedView file
if mname == "inspect" return "string" end # Issue #404 Phase 2: Class#name -- the class's source name as # a string. Aliases `.to_s` at the runtime helper level # (sp_class_to_s), so the return type is the same. if mname == "name" if recv >= 0 && infer_type(recv) == "class" return "string" end end if mname == "to_i" return "int" endspinel_codegen.rbmodifiedView file
Loading diff…
test/class_phase2_name_eq.rbaddedView file
Loading diff…
test/class_phase2_name_eq.rb.expectedaddedView file
Loading diff…