Commit cffd0daf

cffd0dafed3aMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 4 files · +2 added · ~2 modified

Description

Treat module/class constants as first-class Module objects

Reflection methods on a module or class constant misbehaved because the sp_Class value wasn't carried as a proper Module/Class object:

  • Module#name / #to_s / #inspect on a nested constant rendered the C-mangled Foo_Bar instead of Foo::Bar. The sp_class_names[] table now stores the Ruby-visible name via cls_name_to_ruby, which also learns to unmangle class-nested (not just module-nested) prefixes, so a nested exception class like AeNetworkConnectionException::ConnectionNotEstablished reports its :: name too.

  • .class on a module/class constant errored or returned Integer. It now returns Module or Class, chosen at runtime by sp_class_is_module (a threshold compare on the unified cls_id space), so dynamic Class-typed values work as well as static constants. The analyzer types <class-value>.class as class, and a nested ConstantPathNode module reference (Outer::Inner) now types as class instead of falling through to int.

  • is_a?/kind_of?/instance_of?(Module|Class|Object|...) returned false for a module/class value. They now answer correctly: is_a?(Module) is true for any class value, is_a?(Class) only for classes, instance_of? distinguishes the two. A non-literal class argument still falls through rather than collapsing to false.

Fixes #1044 Fixes #1047 Fixes #1049

Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]

Details

Commit
cffd0dafed3a
Parent
46bf64108329

spinel_analyze.rbmodifiedView file

@@ -2459,6 +2459,15 @@
24592459 # class constant in value position.
24602460 return "class"
24612461 end
2462+ # nested module constant in value position (`Outer::Inner`
2463+ # where Inner is a module) — same sp_Class representation as
2464+ # the ConstantReadNode case above.
2465+ if module_name_exists(cpname) == 1
2466+ return "class"
2467+ end
2468+ if is_builtin_class_const_name(cpname) == 1
2469+ return "class"
2470+ end
24622471 end
24632472 parent = @nd_receiver[nid]
24642473 if parent >= 0
@@ -4434,6 +4443,11 @@
44344443 if bt_pc == "poly" || bt_pc == "poly?"
44354444 return "class"
44364445 end
4446+ # `.class` on a class/module value -> Class or Module, itself a
4447+ # class value.
4448+ if bt_pc == "class"
4449+ return "class"
4450+ end
44374451 end
44384452 end
44394453 # `<obj>.class.<cmeth>` chained dispatch. When the