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
  1. 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.

  2. 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).

  3. 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

@@ -7608,6 +7608,16 @@
76087608 types[k] = new_type + "?"
76097609 @cls_ivar_types[ci] = types.join(";")
76107610 @cls_ivar_types_version = @cls_ivar_types_version + 1
7611+ # Same base, one nullable: keep / adopt the nullable form
7612+ # rather than collapsing to poly. `@x = "hi"` (string) +
7613+ # `@x = some_string_or_nil` (string?) is one storage shape
7614+ # — `const char *` with NULL as the nil sentinel.
7615+ elsif base_type(old) == base_type(new_type) && is_nullable_pointer_type(old) == 1
7616+ if is_nullable_type(new_type) == 1 && is_nullable_type(old) == 0
7617+ types[k] = new_type
7618+ @cls_ivar_types[ci] = types.join(";")
7619+ @cls_ivar_types_version = @cls_ivar_types_version + 1
7620+ end
76117621 else
76127622 types[k] = "poly"
76137623 @cls_ivar_types[ci] = types.join(";")
@@ -9965,14 +9975,14 @@
99659975 inh_arg_ids = get_args(inh_args_id)
99669976 inh_ptypes = cls_meth_ptypes_get(inh_owner_ci, inh_midx)
99679977 if inh_ptypes.length > 0
9968- inh_k = 0
9969- while inh_k < inh_arg_ids.length
9970- inh_at = infer_type(inh_arg_ids[inh_k])
9971- if inh_k < inh_ptypes.length
9972- inh_ptypes[inh_k] = unify_call_types(inh_ptypes[inh_k], inh_at, inh_arg_ids[inh_k])
9973- end
9974- inh_k = inh_k + 1
9975- end
9978+ # Route through widen_ptypes_from_args so a trailing
9979+ # KeywordHashNode lands on the matching named kwarg slot
9980+ # rather than the next positional. Without this, `head(204,
9981+ # content_type: "x")` from a child class widened
9982+ # `content_type`'s slot with the *whole keyword hash*'s
9983+ # type (sym_str_hash) and left it un-pinned to "string".
9984+ inh_pnames = cls_meth_pnames_get(inh_owner_ci, inh_midx)
9985+ widen_ptypes_from_args(inh_arg_ids, inh_pnames, inh_ptypes)
99769986 cls_meth_ptypes_put(inh_owner_ci, inh_midx, inh_ptypes)
99779987 end
99789988 end
@@ -12588,6 +12598,49 @@
1258812598 # widen the slot to poly. Mirror the update_ivar_type /
1258912599 # unify_call_types normalization.
1259012600 distinct = drop_stale_unqualified_obj_obs(distinct)
12601+ # Collapse nullable-and-non-nullable variants of the same
12602+ # base type to a single (nullable) entry. `@x = "hi"` (string)
12603+ # in initialize + `@x = some_nullable_string_param` in a
12604+ # setter are semantically the same shape — both store a
12605+ # string, the second one optionally nil. Counting them as 2
12606+ # distinct observations widens the ivar to poly even though
12607+ # the slot can stay `const char *` with a NULL sentinel.
12608+ # Without this, callers that read the slot through an attr_
12609+ # reader then forward into another method's typed param see a
12610+ # poly value and degrade every downstream slot.
12611+ collapsed = "".split(",")
12612+ ck = 0
12613+ while ck < distinct.length
12614+ cur = distinct[ck]
12615+ cur_base = base_type(cur)
12616+ cm = 0
12617+ seen = -1
12618+ while cm < collapsed.length
12619+ if base_type(collapsed[cm]) == cur_base
12620+ seen = cm
12621+ cm = collapsed.length
12622+ else
12623+ cm = cm + 1
12624+ end
12625+ end
12626+ if seen < 0
12627+ collapsed.push(cur)
12628+ else
12629+ # Prefer the nullable form so subsequent reads keep the
12630+ # null-check semantics callers may have relied on.
12631+ if is_nullable_type(cur) == 1 && is_nullable_type(collapsed[seen]) == 0
12632+ collapsed[seen] = cur
12633+ end
12634+ end
12635+ ck = ck + 1
12636+ end
12637+ distinct = collapsed
12638+ if distinct.length == 1 && types[ivk] != distinct[0]
12639+ if base_type(types[ivk]) == base_type(distinct[0])
12640+ types[ivk] = distinct[0]
12641+ changed = 1
12642+ end
12643+ end
1259112644 if distinct.length >= 2
1259212645 # When every observed type is itself an array AND the
1259312646 # current type is already `poly_array` (set by an earlier