Skip to content

Commit 2c685d6

Browse files
committed
feat(d5): merge same-name overloads in MOS instead of dropping them
The extractor names a call's callee `{Type}.{Method}` with no parameter signature, so two overloads (e.g. C.M(Stream) / C.M(string)) collide on the same MOS key and a forward to that name cannot pick an overload. The bridge previously dropped every non-unique name from the skeleton set, so an overloaded method got NO interprocedural summary at all — a forward to it stayed `unknown` (a missed leak) and an overloaded factory was never seen as fresh (note's open question §10 q2). Merge same-name overloads into ONE conservative summary instead of dropping (`_merge_skeletons`): a lattice join at (name, parameter-index) granularity — a parameter transfers `must` only when EVERY overload carrying that index consumes it (an overload that merely keeps the index contributes a `borrow` path, so the join can never fabricate a `must`), and the return is the shared kind only when all overloads agree (else `unknown`, never a fabricated `fresh`). Sound and precision-safe; strictly better than dropping. Arg-type / per-arity disambiguation would need call-site type info the fact stream does not carry — documented as the residual. Two bridge regression tests: agreeing overloads resolve a forward to consume (caller OWN002, previously missed); disagreeing overloads join to `may` so the caller stays silent (no fabricated must). ownir 196/196, mypy --strict, ruff. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019846YSZ35c7CdkWQ1qX5gm
1 parent 4d39b69 commit 2c685d6

2 files changed

Lines changed: 95 additions & 9 deletions

File tree

‎ownlang/ownir.py‎

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,6 +1296,49 @@ def _forward_path_action(callee: str, arg: int) -> PathAction:
12961296
return PathAction("forward", callee, arg)
12971297

12981298

1299+
def _merge_returns(rets: list[ReturnSkeleton]) -> ReturnSkeleton:
1300+
"""The conservative return of a set of same-name overloads. Only a kind ALL
1301+
overloads agree on survives (so a call to the name returns it regardless of which
1302+
overload bound); a `forward`, an `aliasOf` (whose index is overload-specific), or
1303+
any disagreement degrades to a no-op `unknown` return — never a fabricated `fresh`
1304+
(which would mint a phantom acquire at the caller)."""
1305+
kinds = {r.kind for r in rets}
1306+
if kinds == {"fresh"}:
1307+
return ReturnSkeleton("fresh")
1308+
if kinds == {"none"}:
1309+
return ReturnSkeleton("none")
1310+
if kinds == {"aliased"}:
1311+
return ReturnSkeleton("aliased")
1312+
return ReturnSkeleton("unknown") # mixed / forward / aliasOf -> fails closed
1313+
1314+
1315+
def _merge_skeletons(group: list[MethodSkeleton]) -> MethodSkeleton:
1316+
"""Collapse same-name overloads into ONE conservative summary. The extractor
1317+
names a call's callee `{Type}.{Method}` with no parameter signature (note's open
1318+
question 2), so a forward to an overloaded name cannot pick an overload. Rather
1319+
than drop them all (every such forward then stays `unknown`), join them on the
1320+
lattice at (name, parameter-index) granularity: a parameter transfers `must` only
1321+
when EVERY overload carrying that index consumes it, and an overload that merely
1322+
keeps an index contributes a `borrow` path so the join can never fabricate a
1323+
`must`. Disambiguating by argument type would need call-site type info the fact
1324+
stream does not carry; arity is available but not modelled here (the join is
1325+
already precision-safe, just coarser than a per-arity split would be)."""
1326+
if len(group) == 1:
1327+
return group[0]
1328+
by_index: dict[int, list[PathAction]] = {}
1329+
names: dict[int, str] = {}
1330+
for sk in group:
1331+
for p in sk.params:
1332+
# an overload that does nothing with this index KEEPS it (= `no`); record
1333+
# that as a borrow path so it joins in rather than vanishing from concat.
1334+
by_index.setdefault(p.index, []).extend(p.paths or (PathAction("borrow"),))
1335+
names.setdefault(p.index, p.name)
1336+
params = tuple(
1337+
ParamSkeleton(i, names[i], True, tuple(by_index[i])) for i in sorted(by_index)
1338+
)
1339+
return MethodSkeleton(group[0].key, params, _merge_returns([sk.ret for sk in group]))
1340+
1341+
12991342
def _build_skeletons(raw_fns: list[Any]) -> list[MethodSkeleton]:
13001343
"""Derive a Method Ownership Summary skeleton per first-party function from its
13011344
flow body, for the D5.0 solver (P-005 D5.1). A parameter's path actions mirror
@@ -1320,21 +1363,23 @@ def _build_skeletons(raw_fns: list[Any]) -> list[MethodSkeleton]:
13201363
and returns it is `fresh` (a factory); `_infer_return_skeleton` keeps it
13211364
precision-first (a returned parameter is never `fresh`).
13221365
1323-
Functions whose name is not unique are dropped (overload keys are not yet
1324-
distinguishable — note's open question 2 — so a forward to such a name stays
1325-
`unknown` → silent, the precision-safe choice)."""
1366+
Same-name overloads are MERGED into one conservative summary (`_merge_skeletons`)
1367+
rather than dropped: the call node names its callee without a parameter signature
1368+
(note's open question 2), so a forward to an overloaded name cannot pick an
1369+
overload, but a lattice join over the overloads still resolves it precision-safely
1370+
(and lets a uniformly-`fresh` overloaded factory be seen as fresh)."""
13261371
counts = Counter(str(fn.get("name", "")) for fn in raw_fns if isinstance(fn, dict))
1327-
# every first-party method name (even overloaded/dropped ones): the BCL fresh-factory
1328-
# table must NOT apply to a call whose target we compile from source — Tier A (the
1372+
# every first-party method name (even overloaded ones): the BCL fresh-factory table
1373+
# must NOT apply to a call whose target we compile from source — Tier A (the
13291374
# first-party summary) authoritatively overrides Tier B (the curated BCL table) even
13301375
# when the source method happens to share a BCL factory's name (Codex P2).
13311376
first_party = frozenset(_canonical_callee_name(k) for k in counts if k)
1332-
skels: list[MethodSkeleton] = []
1377+
by_key: dict[str, list[MethodSkeleton]] = {}
13331378
for fn in raw_fns:
13341379
if not isinstance(fn, dict):
13351380
continue
13361381
key = str(fn.get("name", ""))
1337-
if not key or counts[key] != 1:
1382+
if not key:
13381383
continue
13391384
body = fn.get("body", [])
13401385
body = body if isinstance(body, list) else []
@@ -1373,8 +1418,10 @@ def _build_skeletons(raw_fns: list[Any]) -> list[MethodSkeleton]:
13731418
params.append(ParamSkeleton(i, cname, True, paths))
13741419
pnames = {str(p.get("name", "")) for p in raw_params if isinstance(p, dict)}
13751420
ret = _infer_return_skeleton(body, pnames, first_party)
1376-
skels.append(MethodSkeleton(key, tuple(params), ret))
1377-
return skels
1421+
by_key.setdefault(key, []).append(MethodSkeleton(key, tuple(params), ret))
1422+
# one skeleton per name: solve() keys by name (a forward names its callee with no
1423+
# signature), so same-name overloads are joined into a single conservative summary.
1424+
return [_merge_skeletons(group) for group in by_key.values()]
13781425

13791426

13801427
def _infer_param_effect(pname: str, nodes: Any,

‎tests/test_ownir.py‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,6 +1341,45 @@ def _sub(source: str | None) -> list[Finding]:
13411341
if cond:
13421342
gotc = [(x.component, x.code) for x in cond]
13431343
fails.append(f"D5.1 conditional forward must be `may`: caller stays silent, got {gotc}")
1344+
# (§10 q2) same-name OVERLOADS are merged, not dropped: when EVERY overload of a
1345+
# name consumes the forwarded arg, a forward to that name resolves to `must`, so a
1346+
# caller using the local after the handoff is OWN002. Before the merge the name was
1347+
# dropped → the forward stayed unknown → this leak was silently missed.
1348+
checks += 1
1349+
ovc = check_facts({"module": "M", "functions": [
1350+
{"name": "C.M", "file": "F.cs", "params": [{"name": "a", "line": 1}],
1351+
"body": [{"op": "release", "var": "a", "line": 2}]},
1352+
{"name": "C.M", "file": "F.cs", "params": [{"name": "b", "line": 5}],
1353+
"body": [{"op": "release", "var": "b", "line": 6}]},
1354+
{"name": "ov_fwd", "file": "F.cs", "params": [{"name": "s", "line": 10}],
1355+
"body": [{"op": "call", "callee": "C.M", "args": ["s"], "line": 11}]},
1356+
{"name": "ov_use", "file": "F.cs",
1357+
"body": [{"op": "acquire", "var": "s", "line": 20},
1358+
{"op": "call", "callee": "ov_fwd", "args": ["s"], "line": 21},
1359+
{"op": "use", "var": "s", "line": 22}]}]})
1360+
gotov = sorted((x.component, x.code) for x in ovc)
1361+
if gotov != [("ov_use", "OWN002")]:
1362+
fails.append("§10 q2 agreeing overloads should resolve the forward to consume "
1363+
f"(ov_use OWN002), got {gotov}")
1364+
# (§10 q2) when overloads DISAGREE (one consumes, one only borrows) the merge joins
1365+
# to `may`, so the forward stays plain and a caller that releases the local itself is
1366+
# clean — the conservative join never fabricates a `must` from an ambiguous name.
1367+
checks += 1
1368+
ovd = check_facts({"module": "M", "functions": [
1369+
{"name": "C.N", "file": "F.cs", "params": [{"name": "a", "line": 1}],
1370+
"body": [{"op": "release", "var": "a", "line": 2}]},
1371+
{"name": "C.N", "file": "F.cs", "params": [{"name": "b", "line": 5}],
1372+
"body": [{"op": "use", "var": "b", "line": 6}]},
1373+
{"name": "ovn_fwd", "file": "F.cs", "params": [{"name": "s", "line": 10}],
1374+
"body": [{"op": "call", "callee": "C.N", "args": ["s"], "line": 11}]},
1375+
{"name": "ovn_use", "file": "F.cs",
1376+
"body": [{"op": "acquire", "var": "s", "line": 20},
1377+
{"op": "call", "callee": "ovn_fwd", "args": ["s"], "line": 21},
1378+
{"op": "release", "var": "s", "line": 22}]}]})
1379+
if ovd:
1380+
gotn = [(x.component, x.code) for x in ovd]
1381+
fails.append("§10 q2 disagreeing overloads must join to `may` (caller stays "
1382+
f"silent), got {gotn}")
13441383
# --- P-005 D5.1b: the per-call-site ownership-contract channel. The extractor
13451384
# routes a call's per-argument ownership through fixed sink externs
13461385
# ($consume / $borrow / $borrow_mut) the bridge pre-declares, so an effect

0 commit comments

Comments
 (0)