Commit fbb4b817

fbb4b8174aacMerged

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

Description

fix(analyze): widen return type to poly on int-vs-pointer split

When a method's explicit return X and implicit final-expression return contradict each other on the value-vs-pointer axis (e.g. def f(v); return v if v < 0; {"k"=>"v"}; end -- the early return contributes the param's int and the implicit returns a str_str_hash), the lenient unify_return_type demoted int as a sentinel and picked the pointer-shaped type for the function signature. The early return value then emitted return mrb_int from a pointer-typed function -- fatal under -Werror's -Wint-conversion.

Add widen_returns_for_mixed_paths as a post-fixpoint pass running right after unify_imeth_override_returns. For each top-level / class-instance / class method, set up the method's scope (push_scope + declare params + refine_method_body_locals) and check whether explicit/implicit return paths disagree value-vs-pointer. If yes, widen the stored return type to "poly" so the existing codegen-side boxing path (compile_return_stmt's @current_method_return == "poly" arm) routes both branches through box_expr_to_poly.

Three earlier attempts (strict unify in-fixpoint, narrow real- int probe, post-fixpoint widening without scope) all cascaded into self-host. The cascade wasn't a real heterogeneity in spinel's helpers -- it was that the widen-check called infer_type WITHOUT method-body scope, so ivar / local reads fell back to int default and triggered spurious widening on e.g. return @parse_id_pool[k] (actual type int_array; without scope it reads as int). Setting up scope mirrors infer_all_returns's internal pattern and produces accurate types per return path.

Honor @unified_imeth_returns: imeth-override unification already promotes a base method's return to match descendant overrides (e.g. Base#[] raising NotImplementedError unified to "string" from Article#[]'s override). Without skipping these, the widening check would re-widen the imeth-family base to poly because the raise body's "int" disagrees with the override's pointer return.

Fixes #581.

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

Details

Commit
fbb4b8174aac
Parent
efe8e57e0a17

spinel_analyze.rbmodifiedView file

@@ -16232,6 +16232,199 @@
1623216232 end
1623316233
1623416234 # Sibling of unify_imeth_override_returns for param types.
16235+ # Returns 1 when the method body's return paths contradict the
16236+ # declared return type on the value-vs-pointer axis (a primitive
16237+ # type like int / float / symbol returned from a function whose
16238+ # signature has a pointer-shaped return type like sp_StrStrHash *
16239+ # / sp_PtrArray * / sp_<Class> *, or vice versa). Skips bodies
16240+ # whose declared return is itself a sentinel default (int / poly
16241+ # / void / ""). Used by `widen_returns_for_mixed_paths` to widen
16242+ # the affected method's stored return type to "poly". Caller is
16243+ # expected to have set up method scope (push_scope + declare
16244+ # params + refine_method_body_locals) so the infer_type calls
16245+ # against ivar / local reads see the right context; otherwise
16246+ # they fall back to int default and report spurious mismatches.
16247+ # Issue #581.
16248+ def method_returns_need_poly_widening(body_id, declared_rt)
16249+ if body_id < 0
16250+ return 0
16251+ end
16252+ if declared_rt == "" || declared_rt == "poly" || declared_rt == "void"
16253+ return 0
16254+ end
16255+ declared_is_ptr = return_type_is_pointer(declared_rt)
16256+ stmts = get_stmts(body_id)
16257+ if stmts.length == 0
16258+ return 0
16259+ end
16260+ types = []
16261+ collect_return_types_nid(body_id, types)
16262+ last_id = stmts.last
16263+ last_t = infer_type(last_id)
16264+ types.push(last_t)
16265+ k = 0
16266+ while k < types.length
16267+ t = types[k]
16268+ if t != "" && t != "nil" && t != "void" && t != declared_rt && base_type(t) != base_type(declared_rt)
16269+ t_is_ptr = return_type_is_pointer(t)
16270+ if t_is_ptr != declared_is_ptr
16271+ return 1
16272+ end
16273+ end
16274+ k = k + 1
16275+ end
16276+ 0
16277+ end
16278+
16279+ def return_type_is_pointer(t)
16280+ bt = base_type(t)
16281+ if bt == "int" || bt == "bool" || bt == "float" || bt == "symbol" || bt == "nil" || bt == "void" || bt == "poly" || bt == ""
16282+ return 0
16283+ end
16284+ 1
16285+ end
16286+
16287+ # Run method_returns_need_poly_widening with proper method-body
16288+ # scope set up (params + locals declared) so infer_type calls
16289+ # see the right context. Without this, `infer_type` of ivar /
16290+ # local reads inside the body falls back to int default and the
16291+ # check reports spurious mismatches for methods whose body
16292+ # actually has homogeneous returns.
16293+ def widen_check_with_scope(bid, declared_rt, pnames, ptypes)
16294+ if bid < 0
16295+ return 0
16296+ end
16297+ if declared_rt == "" || declared_rt == "poly" || declared_rt == "void"
16298+ return 0
16299+ end
16300+ push_scope
16301+ k = 0
16302+ while k < pnames.length
16303+ pt = "int"
16304+ if k < ptypes.length
16305+ pt = ptypes[k]
16306+ end
16307+ declare_var(pnames[k], pt)
16308+ k = k + 1
16309+ end
16310+ lnames = "".split(",")
16311+ ltypes = "".split(",")
16312+ refine_method_body_locals(bid, lnames, ltypes, pnames)
16313+ need = method_returns_need_poly_widening(bid, declared_rt)
16314+ pop_scope
16315+ need
16316+ end
16317+
16318+ # Post-fixpoint pass: walk every method (top-level, class-
16319+ # instance, class-method) and widen stored return types to
16320+ # "poly" when the body's return paths contradict the analyzer's
16321+ # unified narrow type. Mirrors `infer_all_returns`'s scope-
16322+ # setup pattern (push_scope + declare params + refine locals)
16323+ # so infer_type returns the right answer for ivar / local reads.
16324+ # Issue #581.
16325+ def widen_returns_for_mixed_paths
16326+ # Top-level methods
16327+ mi = 0
16328+ while mi < @meth_names.length
16329+ bid = @meth_body_ids[mi]
16330+ cur_rt = @meth_return_types[mi]
16331+ pnames = @meth_param_names[mi].split(",")
16332+ ptypes = @meth_param_types[mi].split(",")
16333+ saved_meth = @current_method_name
16334+ @current_method_name = @meth_names[mi]
16335+ if widen_check_with_scope(bid, cur_rt, pnames, ptypes) == 1
16336+ @meth_return_types[mi] = "poly"
16337+ @needs_rb_value = 1
16338+ end
16339+ @current_method_name = saved_meth
16340+ mi = mi + 1
16341+ end
16342+ # Class instance methods
16343+ ci = 0
16344+ while ci < @cls_names.length
16345+ saved_ci = @current_class_idx
16346+ @current_class_idx = ci
16347+ bodies = @cls_meth_bodies[ci].split(";")
16348+ mnames = @cls_meth_names[ci].split(";")
16349+ returns = @cls_meth_returns[ci].split(";")
16350+ changed = 0
16351+ j = 0
16352+ while j < bodies.length
16353+ if j < returns.length
16354+ bid = bodies[j].to_i
16355+ cur_rt = returns[j]
16356+ pnames = cls_meth_pnames_get(ci, j)
16357+ ptypes = cls_meth_ptypes_get(ci, j)
16358+ saved_meth = @current_method_name
16359+ if j < mnames.length
16360+ @current_method_name = mnames[j]
16361+ end
16362+ # Skip slots where `unify_imeth_override_returns` already
16363+ # promoted the return type to match descendant overrides --
16364+ # the base method's body (typically `raise
16365+ # NotImplementedError`) infers as int, which would otherwise
16366+ # always mismatch the override-family's pointer return type
16367+ # and silently re-widen to poly. Same `@unified_imeth_returns`
16368+ # marker the imeth-unify pass uses.
16369+ imeth_tag = ci.to_s + ":" + j.to_s
16370+ if @unified_imeth_returns != nil && @unified_imeth_returns.index(imeth_tag) != nil
16371+ @current_method_name = saved_meth
16372+ j = j + 1
16373+ next
16374+ end
16375+ if widen_check_with_scope(bid, cur_rt, pnames, ptypes) == 1
16376+ returns[j] = "poly"
16377+ changed = 1
16378+ @needs_rb_value = 1
16379+ end
16380+ @current_method_name = saved_meth
16381+ end
16382+ j = j + 1
16383+ end
16384+ if changed == 1
16385+ @cls_meth_returns[ci] = returns.join(";")
16386+ @cls_meth_return_cache = {}
16387+ end
16388+ @current_class_idx = saved_ci
16389+ ci = ci + 1
16390+ end
16391+ # Class methods (def self.X)
16392+ ci = 0
16393+ while ci < @cls_names.length
16394+ saved_ci = @current_class_idx
16395+ @current_class_idx = ci
16396+ cbodies = @cls_cmeth_bodies[ci].split(";")
16397+ cmnames = @cls_cmeth_names[ci].split(";")
16398+ creturns = @cls_cmeth_returns[ci].split(";")
16399+ changed = 0
16400+ j = 0
16401+ while j < cbodies.length
16402+ if j < creturns.length
16403+ bid = cbodies[j].to_i
16404+ cur_rt = creturns[j]
16405+ pnames = cls_cmeth_pnames_get(ci, j)
16406+ ptypes = cls_cmeth_ptypes_get(ci, j)
16407+ saved_meth = @current_method_name
16408+ if j < cmnames.length
16409+ @current_method_name = cmnames[j]
16410+ end
16411+ if widen_check_with_scope(bid, cur_rt, pnames, ptypes) == 1
16412+ creturns[j] = "poly"
16413+ changed = 1
16414+ @needs_rb_value = 1
16415+ end
16416+ @current_method_name = saved_meth
16417+ end
16418+ j = j + 1
16419+ end
16420+ if changed == 1
16421+ @cls_cmeth_returns[ci] = creturns.join(";")
16422+ end
16423+ @current_class_idx = saved_ci
16424+ ci = ci + 1
16425+ end
16426+ end
16427+
1623516428 # When a base imeth and a descendant override disagree on a
1623616429 # param slot because the override widened to "poly" (mixed
1623716430 # call-site arg types), widen every member of the family at
@@ -19296,6 +19489,20 @@
1929619489 unify_imeth_override_returns
1929719490 infer_all_returns
1929819491 unify_imeth_override_returns
19492+ # Widen function return types to poly when explicit `return X`
19493+ # and the implicit final-expression return disagree on the
19494+ # value-vs-pointer axis. Runs post-fixpoint so the iterative
19495+ # `infer_all_returns` calls above don't re-clobber the widening
19496+ # (same shape as `unify_imeth_override_returns`). Without this,
19497+ # `def f(v); return v if v < 0; {"k"=>"v"}; end` declares a
19498+ # sp_StrStrHash * return but the early `return v` (int) emits a
19499+ # mrb_int return → -Wint-conversion fatal under -Werror. The
19500+ # codegen-side boxing path (compile_return_stmt's
19501+ # `@current_method_return == "poly"` arm + compile_body_return's
19502+ # implicit-return arm) already routes through box_expr_to_poly
19503+ # when the declared return is poly, so widening here is enough.
19504+ # Issue #581.
19505+ widen_returns_for_mixed_paths
1929919506 # Fix lambda return types based on call-site usage
1930019507 fix_lambda_return_types
1930119508 # Pre-detect bigint variables before feature detection