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
return n > 0 ? infer_type(c, a[0]) : TY_NIL;}/* Defined in codegen_fold.c (linked in). */int is_descendant(Compiler *c, int k, int anc);/* True when the method body's tail statement unconditionally raises, so the C function never reaches a return (used to widen its void type to the override return type -- the unreachable "return value" can safely take that type). */static int scope_tail_raises(Compiler *c, int s) { const NodeTable *nt = c->nt; int body = c->scopes[s].body; if (body < 0 || nt_kind(nt, body) != NK_StatementsNode) return 0; int bn = 0; const int *bb = nt_arr(nt, body, "body", &bn); if (bn <= 0) return 0; int last = bb[bn - 1]; const char *ty = nt_type(nt, last); return ty && !strcmp(ty, "CallNode") && nt_ref(nt, last, "receiver") < 0 && nt_str(nt, last, "name") && !strcmp(nt_str(nt, last, "name"), "raise");}int infer_return_types(Compiler *c) { const NodeTable *nt = c->nt; int changed = 0; if (pr != TY_UNKNOWN && sc->ret_proc_ret != (int)pr) { sc->ret_proc_ret = (int)pr; changed = 1; } } } /* An abstract base method (`def self.table_name; raise; end`) infers a void return, but a subclass overrides it with a value-returning version. A call bound to the base in value position would then assign void into a temp and fail to compile (#1416). Since the base body always raises, its return is unreachable -- widen its type to the override return(s), so the call is usable. Only raising bases qualify (a genuinely nil-returning void method must stay void). */ for (int s = 1; s < c->nscopes; s++) { Scope *sc = &c->scopes[s]; if (sc->ret != TY_VOID || sc->class_id < 0 || !sc->name) continue; if (sc->ret_specialized || sc->ret_rbs_seeded || sc->cs_synth) continue; if (!scope_tail_raises(c, s)) continue; TyKind unified = TY_VOID; for (int t = 1; t < c->nscopes; t++) { Scope *ot = &c->scopes[t]; if (t == s || !ot->name || strcmp(ot->name, sc->name)) continue; if (ot->is_cmethod != sc->is_cmethod || ot->class_id < 0) continue; if (!is_descendant(c, ot->class_id, sc->class_id)) continue; if (ot->ret == TY_VOID || ot->ret == TY_UNKNOWN) continue; unified = (unified == TY_VOID) ? ot->ret : ty_unify(unified, ot->ret); } if (unified != TY_VOID) { sc->ret = unified; changed = 1; } } free(ret_acc); free(has_ret); return changed;}test/abstract_raise_override_return.rbaddedView file
Loading diff…
test/abstract_raise_override_return.rb.expectedaddedView file
Loading diff…