Commit 25442aaf

25442aaf8464Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · ~3 modified

Description

Reopen constant-aliased classes under their real name (refs #1036)

class CONST where CONST is a constant aliased to an existing class (CONST = SomeClass or CONST = <literal-or-class>.class) was treated as defining a brand-new class named after the alias, so methods landed on obj_CONST and dispatch on the real class (or its instances) missed them. The to_words gem reopens Integer this way (INTEGER_KLASS = 1.class; class INTEGER_KLASS; include ToWords; end).

Add a shared pre-pass (compiler_helpers.rb, run in both analyze and codegen before any class collection) that builds a const -> class map from top-level/nested ConstantWriteNodes with a class-valued RHS, then rewrites each matching ClassNode's constant_path name in place. Doing it at the source means every downstream name derivation sees the resolved name, so methods register on and dispatch from one class index -- no half-applied alias. Scoped to ClassNode constant_paths; value-context reads of the alias constant are untouched.

Covers CONST = SomeUserClass and CONST = <int/str/float/...>.class. This is the first of two parts of #1036; the second (include of a module into a reopened built-in like Integer) follows.

Refs #1036

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

Details

Commit
25442aaf8464
Parent
8f8d8fc987de

compiler_helpers.rbmodifiedView file

@@ -811,4 +811,184 @@
811811 result
812812 end
813813
814+ # Resolve `class CONST` (and `module`) where CONST is a constant
815+ # aliased to an existing class via `CONST = SomeClass` or
816+ # `CONST = <literal-or-class>.class`, so the body reopens that class
817+ # rather than defining a fresh one named after the alias (#1036:
818+ # `INTEGER_KLASS = 1.class; class INTEGER_KLASS; include M; end`).
819+ #
820+ # Runs in BOTH analyze and codegen (each loads its own AST) before any
821+ # class collection, and rewrites the ClassNode's constant_path name in
822+ # place. Doing it at the source means every downstream name derivation
823+ # sees the resolved name -- no half-applied alias where methods land
824+ # on one class index and dispatch resolves another.
825+ def resolve_class_aliases
826+ root = @root_id
827+ if @nd_type[root] != "ProgramNode"
828+ return
829+ end
830+ class_names = "".split(",", -1)
831+ collect_classnode_names(root, class_names)
832+ amap_from = "".split(",", -1)
833+ amap_to = "".split(",", -1)
834+ build_class_alias_map(root, class_names, amap_from, amap_to)
835+ if amap_from.length == 0
836+ return
837+ end
838+ rewrite_class_alias_refs(root, amap_from, amap_to)
839+ end
840+
841+ def collect_classnode_names(nid, acc)
842+ if nid < 0
843+ return
844+ end
845+ if @nd_type[nid] == "ClassNode"
846+ cp = @nd_constant_path[nid]
847+ if cp >= 0 && @nd_type[cp] == "ConstantReadNode"
848+ nm = @nd_name[cp]
849+ if not_in(nm, acc) == 1
850+ acc.push(nm)
851+ end
852+ end
853+ end
854+ kids = []
855+ push_child_ids(nid, kids)
856+ k = 0
857+ while k < kids.length
858+ collect_classnode_names(kids[k], acc)
859+ k = k + 1
860+ end
861+ end
862+
863+ # The class a literal node is an instance of, for `<lit>.class`.
864+ def literal_class_name(nid)
865+ if nid < 0
866+ return ""
867+ end
868+ t = @nd_type[nid]
869+ if t == "IntegerNode"
870+ return "Integer"
871+ end
872+ if t == "FloatNode"
873+ return "Float"
874+ end
875+ if t == "StringNode"
876+ return "String"
877+ end
878+ if t == "SymbolNode"
879+ return "Symbol"
880+ end
881+ if t == "ArrayNode"
882+ return "Array"
883+ end
884+ if t == "HashNode"
885+ return "Hash"
886+ end
887+ if t == "RegularExpressionNode"
888+ return "Regexp"
889+ end
890+ if t == "TrueNode"
891+ return "TrueClass"
892+ end
893+ if t == "FalseNode"
894+ return "FalseClass"
895+ end
896+ if t == "NilNode"
897+ return "NilClass"
898+ end
899+ ""
900+ end
901+
902+ def is_builtin_class_const(name)
903+ builtins = "Integer;Float;String;Symbol;Array;Hash;Object;BasicObject;Numeric;Regexp;Range;Proc;Exception;StandardError;Comparable;Enumerable;Kernel;Module;Class".split(";", -1)
904+ if not_in(name, builtins) == 0
905+ return 1
906+ end
907+ 0
908+ end
909+
910+ # Resolve a constant's RHS value-node to the class name it aliases, or
911+ # "" when it isn't a class alias.
912+ def class_alias_target(rhs, class_names)
913+ if rhs < 0
914+ return ""
915+ end
916+ t = @nd_type[rhs]
917+ if t == "ConstantReadNode"
918+ nm = @nd_name[rhs]
919+ if not_in(nm, class_names) == 0 || is_builtin_class_const(nm) == 1
920+ return nm
921+ end
922+ return ""
923+ end
924+ if t == "CallNode" && @nd_name[rhs] == "class"
925+ recv = @nd_receiver[rhs]
926+ if recv < 0
927+ return ""
928+ end
929+ lc = literal_class_name(recv)
930+ if lc != ""
931+ return lc
932+ end
933+ if @nd_type[recv] == "ConstantReadNode"
934+ rnm = @nd_name[recv]
935+ if not_in(rnm, class_names) == 0 || is_builtin_class_const(rnm) == 1
936+ return rnm
937+ end
938+ end
939+ end
940+ ""
941+ end
942+
943+ def build_class_alias_map(nid, class_names, amap_from, amap_to)
944+ if nid < 0
945+ return
946+ end
947+ if @nd_type[nid] == "ConstantWriteNode"
948+ cname = @nd_name[nid]
949+ tgt = class_alias_target(@nd_expression[nid], class_names)
950+ # Only treat it as a class alias when it resolves to a DIFFERENT
951+ # name (`Foo = Foo` / a class's own const are not aliases) and isn't
952+ # already recorded.
953+ if tgt != "" && tgt != cname && not_in(cname, amap_from) == 1
954+ amap_from.push(cname)
955+ amap_to.push(tgt)
956+ end
957+ end
958+ kids = []
959+ push_child_ids(nid, kids)
960+ k = 0
961+ while k < kids.length
962+ build_class_alias_map(kids[k], class_names, amap_from, amap_to)
963+ k = k + 1
964+ end
965+ end
966+
967+ def rewrite_class_alias_refs(nid, amap_from, amap_to)
968+ if nid < 0
969+ return
970+ end
971+ if @nd_type[nid] == "ClassNode"
972+ cp = @nd_constant_path[nid]
973+ if cp >= 0 && @nd_type[cp] == "ConstantReadNode"
974+ nm = @nd_name[cp]
975+ ai = 0
976+ while ai < amap_from.length
977+ if amap_from[ai] == nm
978+ set_string_field(cp, "name", amap_to[ai])
979+ ai = amap_from.length
980+ end
981+ ai = ai + 1
982+ end
983+ end
984+ end
985+ kids = []
986+ push_child_ids(nid, kids)
987+ k = 0
988+ while k < kids.length
989+ rewrite_class_alias_refs(kids[k], amap_from, amap_to)
990+ k = k + 1
991+ end
992+ end
993+
814994 end