Commit effd6761

effd67618b56Merged

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

Description

Widen a raising base method's void return to its overrides' return type

A method whose base implementation always raises (def self.table_name; raise; end) infers a C void return. When a subclass overrides it with a value-returning version, a call statically bound to the base in value position emitted sp_RbVal _t = sp_Base_s_table_name() and failed to compile -- the polymorphic case #1397 didn't cover.

Since the raising base body never reaches a return, its return value is unreachable and can safely take the override's type. After return-type inference, for a base method whose tail unconditionally raises, unify its void return with the return types of same-named overrides in descendant classes (poly when they diverge) and adopt that. A genuinely nil-returning void method (e.g. puts-only) is excluded by the raise-tail gate, so it stays void.

This is what ActiveRecord::Base.table_name (raise NotImplementedError, overridden by every model to return a String) needs.

Fixes #1416

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

Details

Commit
effd67618b56
Parent
972cb88b8079

src/analyze_pass.cmodifiedView file

@@ -2813,6 +2813,24 @@
28132813 return n > 0 ? infer_type(c, a[0]) : TY_NIL;
28142814 }
28152815
2816+/* Defined in codegen_fold.c (linked in). */
2817+int is_descendant(Compiler *c, int k, int anc);
2818+
2819+/* True when the method body's tail statement unconditionally raises, so the C
2820+ function never reaches a return (used to widen its void type to the override
2821+ return type -- the unreachable "return value" can safely take that type). */
2822+static int scope_tail_raises(Compiler *c, int s) {
2823+ const NodeTable *nt = c->nt;
2824+ int body = c->scopes[s].body;
2825+ if (body < 0 || nt_kind(nt, body) != NK_StatementsNode) return 0;
2826+ int bn = 0; const int *bb = nt_arr(nt, body, "body", &bn);
2827+ if (bn <= 0) return 0;
2828+ int last = bb[bn - 1];
2829+ const char *ty = nt_type(nt, last);
2830+ return ty && !strcmp(ty, "CallNode") && nt_ref(nt, last, "receiver") < 0 &&
2831+ nt_str(nt, last, "name") && !strcmp(nt_str(nt, last, "name"), "raise");
2832+}
2833+
28162834 int infer_return_types(Compiler *c) {
28172835 const NodeTable *nt = c->nt;
28182836 int changed = 0;
@@ -2880,6 +2898,31 @@
28802898 if (pr != TY_UNKNOWN && sc->ret_proc_ret != (int)pr) { sc->ret_proc_ret = (int)pr; changed = 1; }
28812899 }
28822900 }
2901+
2902+ /* An abstract base method (`def self.table_name; raise; end`) infers a void
2903+ return, but a subclass overrides it with a value-returning version. A call
2904+ bound to the base in value position would then assign void into a temp and
2905+ fail to compile (#1416). Since the base body always raises, its return is
2906+ unreachable -- widen its type to the override return(s), so the call is
2907+ usable. Only raising bases qualify (a genuinely nil-returning void method
2908+ must stay void). */
2909+ for (int s = 1; s < c->nscopes; s++) {
2910+ Scope *sc = &c->scopes[s];
2911+ if (sc->ret != TY_VOID || sc->class_id < 0 || !sc->name) continue;
2912+ if (sc->ret_specialized || sc->ret_rbs_seeded || sc->cs_synth) continue;
2913+ if (!scope_tail_raises(c, s)) continue;
2914+ TyKind unified = TY_VOID;
2915+ for (int t = 1; t < c->nscopes; t++) {
2916+ Scope *ot = &c->scopes[t];
2917+ if (t == s || !ot->name || strcmp(ot->name, sc->name)) continue;
2918+ if (ot->is_cmethod != sc->is_cmethod || ot->class_id < 0) continue;
2919+ if (!is_descendant(c, ot->class_id, sc->class_id)) continue;
2920+ if (ot->ret == TY_VOID || ot->ret == TY_UNKNOWN) continue;
2921+ unified = (unified == TY_VOID) ? ot->ret : ty_unify(unified, ot->ret);
2922+ }
2923+ if (unified != TY_VOID) { sc->ret = unified; changed = 1; }
2924+ }
2925+
28832926 free(ret_acc); free(has_ret);
28842927 return changed;
28852928 }