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 / .inspect alias .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> / dynamic is_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

@@ -3157,6 +3157,14 @@
31573157 if mname == "inspect"
31583158 return "string"
31593159 end
3160+ # Issue #404 Phase 2: Class#name -- the class's source name as
3161+ # a string. Aliases `.to_s` at the runtime helper level
3162+ # (sp_class_to_s), so the return type is the same.
3163+ if mname == "name"
3164+ if recv >= 0 && infer_type(recv) == "class"
3165+ return "string"
3166+ end
3167+ end
31603168 if mname == "to_i"
31613169 return "int"
31623170 end