Commit 538bc0b4
538bc0b43846Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 5 files · +2 added · ~3 modified
Description
Merge pull request #1505 from ryanseys/rs-g3-method-missing
Dispatch unresolved object calls to method_missing
Details
- Commit
- 538bc0b43846
- Parent
- eb58d48f362d
- Merge parent
- 1d52911e69c5
src/analyze.cmodifiedView file
@@ -38,10 +38,14 @@
3838
}3939
}4040
41-
/* Names that may be invoked implicitly (no explicit CallNode): keep live. */41+
/* Names that may be invoked implicitly (no explicit CallNode): keep live.42+
method_missing is reached only through the fallback dispatch the codegen43+
synthesizes, never a direct call, so it must be seeded here or the44+
reachability BFS would prune it. */4245
static const char *const implicit[] = {4346
"to_s", "inspect", "==", "<=>", "eql?", "hash", "each", "coerce",44-
"to_str", "to_ary", "to_a", "to_i", "to_int", "to_h", "to_proc", "call", NULL };47+
"to_str", "to_ary", "to_a", "to_i", "to_int", "to_h", "to_proc", "call",48+
"method_missing", NULL };4549
4650
/* BFS queue (scope indices). */4751
int *queue = malloc((size_t)c->nscopes * sizeof(int));@@ -2013,6 +2017,22 @@
20132017
!strcmp(snm, "ir_emit_sa") ? TY_STR_ARRAY : TY_INT_ARRAY;20142018
sc->ret = TY_STRING; /* each returns the accumulated buf (a string) */20152019
}2020+
/* Seed method_missing's signature. It is reached only through the synthesized2021+
fallback dispatch (no AST call site), so the param backstop below can't bind2022+
it: name is the called method's Symbol and args is the rest poly-array.2023+
Unseeded it keeps TY_UNKNOWN params and gets pruned. */2024+
for (int s = 0; s < c->nscopes; s++) {2025+
Scope *sc = &c->scopes[s];2026+
if (sc->class_id < 0 || !sc->name) continue;2027+
if (!strcmp(sc->name, "method_missing") && sc->nparams >= 1 && sc->pnames) {2028+
LocalVar *pn = scope_local(sc, sc->pnames[0]);2029+
if (pn && pn->type == TY_UNKNOWN) pn->type = TY_SYMBOL;2030+
if (sc->rest_idx >= 0 && sc->rest_idx < sc->nparams) {2031+
LocalVar *pr = scope_local(sc, sc->pnames[sc->rest_idx]);2032+
if (pr && pr->type == TY_UNKNOWN) pr->type = TY_POLY_ARRAY;2033+
}2034+
}2035+
}20162036
for (int it = 0; it < 8; it++) { if (!infer_param_types(c)) break; }20172037
20182038
/* Backstop step 2: a reachable method that STILL has TY_UNKNOWN params wassrc/analyze_infer.cmodifiedView file
Loading diff…
src/codegen_call.cmodifiedView file
Loading diff…
test/method_missing_basic.rbaddedView file
Loading diff…
test/method_missing_basic.rb.expectedaddedView file
Loading diff…