Commit 57515e7b

57515e7b7a61Merged

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

Description

Body-driven String param inference + module method resolution

  • infer_string_params: a still-unknown parameter is inferred as String when the body calls a String-only method (split/strip/gsub/...) on it, so a method with no concrete-typed caller compiles.
  • Modules are now registered in the class table like classes, so Module.method (def self.m / module_function) calls resolve to the method and its return type. respond_to? on a module constant also consults the module's def'd (module_function) instance methods.

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

Details

Commit
57515e7b7a61
Parent
af0646bb4023

src/analyze.cmodifiedView file

@@ -920,7 +920,7 @@
920920 int child = scope_idx;
921921 int child_class = class_id;
922922
923- if (ty && !strcmp(ty, "ClassNode")) {
923+ if (ty && (!strcmp(ty, "ClassNode") || !strcmp(ty, "ModuleNode"))) {
924924 int cp = nt_ref(c->nt, id, "constant_path");
925925 const char *cname = cp >= 0 ? nt_str(c->nt, cp, "name") : NULL;
926926 if (cname && comp_class_index(c, cname) < 0) {
@@ -928,7 +928,7 @@
928928 child_class = c->nclasses - 1;
929929 }
930930 else if (cname) {
931- child_class = comp_class_index(c, cname); /* reopened class */
931+ child_class = comp_class_index(c, cname); /* reopened class/module */
932932 }
933933 }
934934 else if (ty && !strcmp(ty, "DefNode")) {
@@ -1562,6 +1562,42 @@
15621562 return changed;
15631563 }
15641564
1565+/* Methods that only Strings respond to -- definitive evidence that a
1566+ receiver is a String. (length/size/etc are shared with containers and so
1567+ are deliberately excluded to keep the inference conservative.) */
1568+static int is_string_only_method(const char *m) {
1569+ static const char *const set[] = {
1570+ "split", "strip", "lstrip", "rstrip", "chomp", "chop", "upcase",
1571+ "downcase", "capitalize", "swapcase", "gsub", "sub", "tr", "tr_s",
1572+ "squeeze", "scan", "start_with?", "end_with?", "each_char", "chars",
1573+ "center", "ljust", "rjust", "to_str", "encode", "unpack", "match?",
1574+ "partition", "rpartition", "succ", "hex", "oct", "codepoints", "scrub",
1575+ "crypt", "delete_prefix", "delete_suffix", "casecmp", "casecmp?",
1576+ "force_encoding", NULL };
1577+ for (int i = 0; set[i]; i++) if (!strcmp(m, set[i])) return 1;
1578+ return 0;
1579+}
1580+
1581+/* Infer a still-unknown parameter as String when the body calls a
1582+ String-only method on it (a param with no concrete-typed caller). */
1583+static int infer_string_params(Compiler *c) {
1584+ const NodeTable *nt = c->nt;
1585+ int changed = 0;
1586+ for (int id = 0; id < nt->count; id++) {
1587+ const char *ty = nt_type(nt, id);
1588+ if (!ty || strcmp(ty, "CallNode")) continue;
1589+ const char *name = nt_str(nt, id, "name");
1590+ int recv = nt_ref(nt, id, "receiver");
1591+ if (!name || recv < 0 || !is_string_only_method(name)) continue;
1592+ const char *rty = nt_type(nt, recv);
1593+ if (!rty || strcmp(rty, "LocalVariableReadNode")) continue;
1594+ Scope *s = comp_scope_of(c, id);
1595+ LocalVar *lv = scope_local(s, nt_str(nt, recv, "name"));
1596+ if (lv && lv->is_param && lv->type == TY_UNKNOWN) { lv->type = TY_STRING; changed = 1; }
1597+ }
1598+ return changed;
1599+}
1600+
15651601 static int infer_param_types(Compiler *c) {
15661602 const NodeTable *nt = c->nt;
15671603 int changed = 0;
@@ -2174,6 +2210,7 @@
21742210 int ch = 0;
21752211 ch |= infer_write_types(c);
21762212 ch |= infer_param_types(c);
2213+ ch |= infer_string_params(c);
21772214 ch |= infer_default_param_types(c);
21782215 ch |= infer_block_params(c);
21792216 ch |= infer_for_index(c);