Commit 3d2e1935
3d2e19353fe5Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +1 added · ~2 modified
Description
fix: kwarg + inherited dispatch + nullable-ivar collapse for #452
Three intertwined gaps surfaced by the same shape:
class Base
def head(status, content_type: nil)
@content_type = content_type unless content_type.nil?
end
end
class Child < Base
def render_json
head(204, content_type: "application/json")
end
end
-
analyze: the inherited-method widening branch iterated arg_ids positionally, so a trailing KeywordHashNode landed on whatever positional slot came next instead of the matching kwarg slot. Route through widen_ptypes_from_args so each `key: value` lands on the named-param slot.
-
codegen: compile_typed_call_args had the same positional-only shape on the emit side — the kwarg expression disappeared at the call site and the slot was padded with `0`. Extract KeywordHashNode pairs up front, inject them into arg_ids at the matching named slot, and route through the existing per- arg type-coerce loop (a `-1` placeholder marks slots that need to fall through to the default path).
-
analyze (update_ivar_type + finalize_ivar_heterogeneity): same-base nullable variants (`@x = "hi"` + `@x = some_nullable_string_param`) counted as 2 distinct observations and widened the ivar to poly. Both write the same C storage; treat T + T? as one nullable observation and prefer the nullable form.
Without (3), fixes (1) and (2) would have pinned head's content_type to "string?" correctly but the body's ivar write would have re-widened @content_type from string-init to poly via the heterogeneity rule, then the attr_reader chain back into write_response's content_type kwarg would have re-poisoned that slot too — cascading the issue rather than closing it.
Closes #452
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 3d2e19353fe5
- Parent
- 2439f673e5cc
spinel_analyze.rbmodifiedView file
types[k] = new_type + "?" @cls_ivar_types[ci] = types.join(";") @cls_ivar_types_version = @cls_ivar_types_version + 1 # Same base, one nullable: keep / adopt the nullable form # rather than collapsing to poly. `@x = "hi"` (string) + # `@x = some_string_or_nil` (string?) is one storage shape # — `const char *` with NULL as the nil sentinel. elsif base_type(old) == base_type(new_type) && is_nullable_pointer_type(old) == 1 if is_nullable_type(new_type) == 1 && is_nullable_type(old) == 0 types[k] = new_type @cls_ivar_types[ci] = types.join(";") @cls_ivar_types_version = @cls_ivar_types_version + 1 end else types[k] = "poly" @cls_ivar_types[ci] = types.join(";") inh_arg_ids = get_args(inh_args_id) inh_ptypes = cls_meth_ptypes_get(inh_owner_ci, inh_midx) if inh_ptypes.length > 0 inh_k = 0 while inh_k < inh_arg_ids.length inh_at = infer_type(inh_arg_ids[inh_k]) if inh_k < inh_ptypes.length inh_ptypes[inh_k] = unify_call_types(inh_ptypes[inh_k], inh_at, inh_arg_ids[inh_k]) end inh_k = inh_k + 1 end # Route through widen_ptypes_from_args so a trailing # KeywordHashNode lands on the matching named kwarg slot # rather than the next positional. Without this, `head(204, # content_type: "x")` from a child class widened # `content_type`'s slot with the *whole keyword hash*'s # type (sym_str_hash) and left it un-pinned to "string". inh_pnames = cls_meth_pnames_get(inh_owner_ci, inh_midx) widen_ptypes_from_args(inh_arg_ids, inh_pnames, inh_ptypes) cls_meth_ptypes_put(inh_owner_ci, inh_midx, inh_ptypes) end end # widen the slot to poly. Mirror the update_ivar_type / # unify_call_types normalization. distinct = drop_stale_unqualified_obj_obs(distinct) # Collapse nullable-and-non-nullable variants of the same # base type to a single (nullable) entry. `@x = "hi"` (string) # in initialize + `@x = some_nullable_string_param` in a # setter are semantically the same shape — both store a # string, the second one optionally nil. Counting them as 2 # distinct observations widens the ivar to poly even though # the slot can stay `const char *` with a NULL sentinel. # Without this, callers that read the slot through an attr_ # reader then forward into another method's typed param see a # poly value and degrade every downstream slot. collapsed = "".split(",") ck = 0 while ck < distinct.length cur = distinct[ck] cur_base = base_type(cur) cm = 0 seen = -1 while cm < collapsed.length if base_type(collapsed[cm]) == cur_base seen = cm cm = collapsed.length else cm = cm + 1 end end if seen < 0 collapsed.push(cur) else # Prefer the nullable form so subsequent reads keep the # null-check semantics callers may have relied on. if is_nullable_type(cur) == 1 && is_nullable_type(collapsed[seen]) == 0 collapsed[seen] = cur end end ck = ck + 1 end distinct = collapsed if distinct.length == 1 && types[ivk] != distinct[0] if base_type(types[ivk]) == base_type(distinct[0]) types[ivk] = distinct[0] changed = 1 end end if distinct.length >= 2 # When every observed type is itself an array AND the # current type is already `poly_array` (set by an earlierspinel_codegen.rbmodifiedView file
Loading diff…
test/inherited_kwarg_nil_default_string_ivar.rbaddedView file
Loading diff…