feat(csharp): bind extension calls on chained receivers (#4828)
Closes the last shape in the report: `v.ToString().Mask()`. The receiver of the
outer call is the result of an inner call, so it carries no type at all, and
typing it would need BCL return types the corpus does not know.
The untyped rung therefore applies the same two name proofs the out-of-corpus
rung uses — the name is not a BCL member, and no regular method in the corpus
spells it — with no type-compatibility filter, and requires a unique surviving
declaration. A capitalized untyped receiver is a type named in source
(`Convert.ToInt32(x)`), never a value an extension applies to, so it is skipped.
This is the weakest rung and it is scored accordingly: INFERRED 0.70. The cost is
pinned by a fixture where a regular method elsewhere shares the name: the call
stays unbound rather than risking a false edge.
With all three rungs the reporter's corpus now matches the expected output:
Connections (3):
<-- StringExtensions [method] [EXTRACTED] Ext.cs:L3
<-- .A() [calls] [INFERRED] Svc.cs:L3
<-- .B() [calls] [INFERRED] Svc.cs:L4
Refs #4828
This commit is contained in:
1 parent
f80468a01b
commit
99d2834910
3 files changed
+85
-25
No files matched your search
+43
-24
@@ -6249,6 +6249,14 @@ def _resolve_csharp_extension_calls(
|
||||
is not one a BCL type owns (``ToString``, ``GetValue``, …), and no regular
|
||||
method anywhere in the corpus has that name. Such an edge is INFERRED 0.70,
|
||||
one tier below a receiver whose declaration table typed it.
|
||||
|
||||
A chained receiver (``v.ToString().Mask()``) has no type at all: typing it
|
||||
needs the inner call's return type, which for a BCL call the corpus does not
|
||||
know. The same two proofs apply, with no type-compatibility filter — only a
|
||||
name that is unique enough to survive them binds (``v.ToString().Mask()``
|
||||
does; anything named like an ordinary member does not). A capitalized
|
||||
untyped receiver is a type name (``Convert.ToInt32(x)``), never a value an
|
||||
extension can apply to, so it is skipped.
|
||||
"""
|
||||
def _key(label: str) -> str:
|
||||
# Case and `_` are part of a C# name (#4415); drop only the `.M()` decoration.
|
||||
@@ -6395,18 +6403,24 @@ def _resolve_csharp_extension_calls(
|
||||
owner = method_owner.get(caller)
|
||||
owner_node = node_by_id.get(owner) if owner else None
|
||||
recv_name = (owner_node or {}).get("label")
|
||||
if not recv_name:
|
||||
continue
|
||||
|
||||
recv_nid, decisive = _resolve_type_nid(recv_name, caller_node, src_file)
|
||||
if recv_nid is None and decisive:
|
||||
continue # the receiver's own type is ambiguous here
|
||||
roc = recv_nid is None # receiver type declared nowhere in the corpus
|
||||
|
||||
candidates = ext_by_name.get(_key(callee))
|
||||
if not candidates:
|
||||
continue
|
||||
|
||||
closure: set[str] = set()
|
||||
if not recv_name:
|
||||
# A capitalized untyped receiver is a type named in source
|
||||
# (`Convert.ToInt32(x)`), not a value an extension applies to.
|
||||
if receiver and not rc.get("receiver_type") and str(receiver)[:1].isupper():
|
||||
continue
|
||||
roc = True
|
||||
else:
|
||||
recv_nid, decisive = _resolve_type_nid(recv_name, caller_node, src_file)
|
||||
if recv_nid is None and decisive:
|
||||
continue # the receiver's own type is ambiguous here
|
||||
roc = recv_nid is None # receiver type declared nowhere in the corpus
|
||||
|
||||
if not roc:
|
||||
closure, unresolved = _closure(recv_nid)
|
||||
if unresolved:
|
||||
@@ -6432,25 +6446,30 @@ def _resolve_csharp_extension_calls(
|
||||
else:
|
||||
continue
|
||||
|
||||
exact = [
|
||||
d for d in candidates
|
||||
if not d["is_type_param"] and _key(d["extended"]) == _key(recv_name)
|
||||
]
|
||||
inherited = []
|
||||
if not roc:
|
||||
inherited = [
|
||||
if not recv_name:
|
||||
# No type to be compatible with: the guarded name rules above are
|
||||
# the whole proof, and a tie stays unbound.
|
||||
applicable = candidates
|
||||
else:
|
||||
exact = [
|
||||
d for d in candidates
|
||||
if not d["is_type_param"]
|
||||
and _key(d["extended"]) in {
|
||||
_key((node_by_id.get(c) or {}).get("label", "")) for c in closure
|
||||
}
|
||||
]
|
||||
applicable = exact or inherited
|
||||
if not applicable:
|
||||
applicable = [
|
||||
d for d in candidates
|
||||
if d["is_type_param"] or d["extended"].lower() in ("object", "dynamic")
|
||||
if not d["is_type_param"] and _key(d["extended"]) == _key(recv_name)
|
||||
]
|
||||
inherited = []
|
||||
if not roc:
|
||||
inherited = [
|
||||
d for d in candidates
|
||||
if not d["is_type_param"]
|
||||
and _key(d["extended"]) in {
|
||||
_key((node_by_id.get(c) or {}).get("label", "")) for c in closure
|
||||
}
|
||||
]
|
||||
applicable = exact or inherited
|
||||
if not applicable:
|
||||
applicable = [
|
||||
d for d in candidates
|
||||
if d["is_type_param"] or d["extended"].lower() in ("object", "dynamic")
|
||||
]
|
||||
if not applicable:
|
||||
continue
|
||||
|
||||
|
||||
@@ -428,6 +428,26 @@ FIXTURES: dict[str, Fixture] = {
|
||||
forbid_pairs={"// B1": ("E.cs", "// D2")},
|
||||
note="B1 is a correct baseline static call; it must never be re-bound to the extension",
|
||||
),
|
||||
# A chained receiver with no type: the untyped rung's name proofs are the
|
||||
# whole evidence. Here a regular method elsewhere spells the name, so the
|
||||
# conservative rule refuses, and the call stays unbound (documented miss).
|
||||
"untyped_regular_name": Fixture(
|
||||
files={
|
||||
"Cache.cs": "public class Cache {\n public void Flush() { } // D1\n}\n",
|
||||
"E.cs": (
|
||||
"public static class Ext {\n"
|
||||
" public static void Flush(this string s) { } // D2\n"
|
||||
"}\n"
|
||||
),
|
||||
"U.cs": (
|
||||
"public class U {\n"
|
||||
" public void A(long v) { v.ToString().Flush(); } // F1\n"
|
||||
"}\n"
|
||||
),
|
||||
},
|
||||
forbid=["// F1"],
|
||||
note="the regular-name guard refuses this binding on purpose",
|
||||
),
|
||||
# The #1609 regression the fail-closed guard prevents: a member call on a type
|
||||
# that declares no such member must not bind to an unrelated same-named method.
|
||||
"god_node_control": Fixture(
|
||||
|
||||
@@ -20,7 +20,7 @@ FIXTURE_IDS = list(FIXTURES)
|
||||
|
||||
# Stage 1: in-corpus receivers, scope, assignability, poison, arity.
|
||||
# Stage 2: out-of-corpus receivers guarded by the BCL/regular-name rules.
|
||||
# Stage 3 adds: issue_repro (untyped/chained receivers).
|
||||
# Stage 3: untyped/chained receivers.
|
||||
STAGE_FIXTURES = {
|
||||
"domain_cross_file",
|
||||
"same_file",
|
||||
@@ -32,6 +32,8 @@ STAGE_FIXTURES = {
|
||||
# stage 2: out-of-corpus receivers (primitives, third-party types, `this T`)
|
||||
"primitive_and_thirdparty",
|
||||
"generic_wildcard",
|
||||
# stage 3: untyped/chained receivers
|
||||
"issue_repro",
|
||||
}
|
||||
|
||||
|
||||
@@ -140,3 +142,22 @@ def test_open_generic_extension_applies_only_as_a_wildcard():
|
||||
concrete = graph_bindings("type_param_vs_class")
|
||||
assert ("// E1", ("T.cs", "// D1")) in concrete["bindings"]
|
||||
assert ("// E1", ("T.cs", "// D2")) not in concrete["bindings"]
|
||||
|
||||
|
||||
def test_chained_receiver_binds_when_the_name_is_unique():
|
||||
"""The issue's `v.ToString().Mask()` line: no receiver type to check, so the
|
||||
guarded untyped rung is the only possible proof."""
|
||||
result = graph_bindings("issue_repro")
|
||||
assert ("// E1", ("Ext.cs", "// D1")) in result["bindings"]
|
||||
assert ("// E2", ("Ext.cs", "// D1")) in result["bindings"]
|
||||
|
||||
|
||||
def test_untyped_rung_refuses_a_name_a_regular_method_uses():
|
||||
result = graph_bindings("untyped_regular_name")
|
||||
assert not any(marker == "// F1" for marker, _target in result["bindings"])
|
||||
|
||||
|
||||
def test_untyped_rung_refuses_a_capitalized_type_receiver():
|
||||
"""`Conv.ToInt32(x)` names a type; the extension must not be considered."""
|
||||
result = graph_bindings("static_class_receiver")
|
||||
assert ("// B1", ("E.cs", "// D2")) not in result["bindings"]
|
||||
Reference in new issue
Block a user