Commit b1b2394f

b1b2394f7c75Merged

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

Description

Add TY_CLASS dynamic dispatch: .new, is_a?, module/class reflection

  • TY_CLASS var.new: emit runtime switch over user classes → TY_POLY
  • TY_OBJECT.is_a?(dynamic_klass): runtime sp_class_le when arg is TY_CLASS
  • TY_CLASS.is_a?/kind_of?/instance_of? for Module/Class/Object targets
  • TY_CLASS.class: returns "Module" or "Class" via sp_class_is_module_val
  • sp_class_to_s: use qualified names (Foo::Bar) via class_ruby_name
  • emit sp_cls_is_module[] table and sp_class_is_module_val() helper

Fixes dynamic_class_new, dynamic_is_a, module_class_reflection.

Co-Authored-By: Claude Sonnet 4.6 [email protected]

Details

Commit
b1b2394f7c75
Parent
77d838128d22

src/analyze.cmodifiedView file

@@ -414,8 +414,12 @@
414414 if (!strcmp(name, "parameters")) return TY_POLY_ARRAY;
415415 }
416416
417- /* TY_CLASS method dispatch -- do NOT intercept .new here; fall through to
418- the AST-based handler that knows which class the constant refers to. */
417+ /* TY_CLASS method dispatch -- .new on a dynamic class variable returns TY_POLY */
418+ if (recv >= 0 && rt == TY_CLASS && !strcmp(name, "new") &&
419+ nt_type(nt, recv) && strcmp(nt_type(nt, recv), "ConstantReadNode") != 0 &&
420+ strcmp(nt_type(nt, recv), "ConstantPathNode") != 0)
421+ return TY_POLY;
422+
419423 if (recv >= 0 && rt == TY_CLASS && strcmp(name, "new") != 0) {
420424 if (argc == 0 && (!strcmp(name, "to_s") || !strcmp(name, "name") || !strcmp(name, "inspect")))
421425 return TY_STRING;
@@ -425,6 +429,8 @@
425429 if (argc == 1 && (!strcmp(name, "==") || !strcmp(name, "eql?") || !strcmp(name, "!="))) return TY_BOOL;
426430 if (argc == 1 && (!strcmp(name, "<") || !strcmp(name, ">") || !strcmp(name, "<=") || !strcmp(name, ">="))) return TY_BOOL;
427431 if (argc == 0 && !strcmp(name, "ancestors")) return TY_POLY_ARRAY;
432+ if (argc == 1 && (!strcmp(name, "is_a?") || !strcmp(name, "kind_of?") || !strcmp(name, "instance_of?"))) return TY_BOOL;
433+ if (argc == 0 && !strcmp(name, "instance_methods")) return TY_POLY_ARRAY;
428434 }
429435
430436 /* __method__ / __callee__ -> the enclosing method's name (a symbol) */