fix(cpp): type receivers per function and keep untyped receivers bound in-file

Review follow-ups to the previous commit.

The receiver table is one dict for the whole file, filled first-write-wins,
so `void g() { Response r; }` retyped `r` in `void f(Request& r)` and f's
`r.send()` went looking on Response. Build each function's table on its
own (a local still shadows a parameter there), then drop any name the file
types two different ways, including a field declared with different types
in two classes. An ambiguous receiver gets no type rather than a guess.

Defer a member call only when its receiver is typed. `auto w = make();
w.paint()`, smart pointers and range-for variables have no table entry, and
deferring them dropped the in-file edge they had on v8.

(cherry picked from commit 539e96f821)
This commit is contained in:
nothariharan authored and safishamsi committed 2026-11-02 19:05:45 +00:00
1 parent ffbdae50c2
commit d3b314d92e
2 files changed
+54 -21

No files matched your search

+28 -12
View File
@@ -3114,8 +3114,8 @@ def _cpp_parameter_types(body_node, source: bytes, table: dict[str, str]) -> Non
def _cpp_field_types(root, source: bytes, table: dict[str, str]) -> None:
"""Collect ``field -> ClassName`` from class/struct member declarations
(#3215), so a member-field receiver (``Inner.IsOk()`` inside a method)
can be typed. Runs LAST: an existing local or parameter entry of the
same name is never overwritten, so a shadowing local keeps winning.
can be typed. A field name declared with two different types maps to ""
so the caller can drop it as ambiguous.
"""
stack = [root]
while stack:
@@ -3128,8 +3128,9 @@ def _cpp_field_types(root, source: bytes, table: dict[str, str]) -> None:
type_name = _read_text(type_node, source).split("::")[-1].strip()
d = n.child_by_field_name("declarator")
var = _cpp_declarator_name(d, source) if d is not None else None
if var and type_name and type_name[:1].isupper() and var not in table:
table[var] = type_name
if var and type_name and type_name[:1].isupper():
# Two classes with a same-named field of different types: "".
table[var] = type_name if table.get(var, type_name) == type_name else ""
stack.extend(n.children)
@@ -10102,13 +10103,15 @@ def _extract_generic(
# `Base* b`, `b->draw()` bound to the file's `Derived::draw`.
# Deferring sends it to _resolve_cpp_member_calls, which types x
# from the file's local/parameter/field table and bails when it
# can't. `this->m()` keeps its in-file binding.
# can't. An untyped receiver and `this->m()` keep the in-file
# binding they had.
_cpp_member_defer = (
config.ts_module == "tree_sitter_cpp"
and is_member_call
and not is_scoped_call
and member_receiver is not None
and member_receiver != "this"
and member_receiver in type_table
)
if _python_defer or _java_defer or _csharp_defer or _js_member_defer or _cpp_member_defer or (_builtin_member_call and not php_builtin_self_call) or _lua_member_defer or is_scoped_call or (
is_member_call
@@ -10688,14 +10691,27 @@ def _extract_generic(
# (#1547). File-scoped (not per-body): a later body's `Foo f;` doesn't clobber
# an earlier binding (`var not in table`), keeping resolution conservative.
if config.ts_module == "tree_sitter_cpp":
# Each function's table first: a local shadows a parameter there. The
# file table is shared by every function, so a name typed differently in
# two places (`Request& r` here, `Response r` there) is ambiguous and
# dropped rather than letting whichever came first win (#3215).
conflicting: set[str] = set()
def _fold(table: dict[str, str]) -> None:
for var, type_name in table.items():
if not type_name or type_table.setdefault(var, type_name) != type_name:
conflicting.add(var)
for _caller_nid, body_node in function_bodies:
_cpp_local_var_types(body_node, source, type_table)
# Parameters second and class fields last (#3215): the table is
# first-write-wins, so a local shadows a parameter shadows a field —
# matching C++ name lookup for an unqualified receiver.
for _caller_nid, body_node in function_bodies:
_cpp_parameter_types(body_node, source, type_table)
_cpp_field_types(root, source, type_table)
fn_table: dict[str, str] = {}
_cpp_local_var_types(body_node, source, fn_table)
_cpp_parameter_types(body_node, source, fn_table)
_fold(fn_table)
field_table: dict[str, str] = {}
_cpp_field_types(root, source, field_table)
_fold(field_table)
for var in conflicting:
type_table.pop(var, None)
# Swift: type local `let x = Type()` / `let x = Type.shared` bindings inside
# method bodies so `x.method()` on a later line resolves — class-level
+26 -9
View File
@@ -45,15 +45,6 @@ def test_base_pointer_call_reaches_the_base_method(tmp_path):
for src, tgt in calls)
def test_untyped_receiver_does_not_bind_by_name(tmp_path):
calls = _calls(tmp_path, {"widget.cpp": (
"class Widget { public: void paint(); };\n"
"void Widget::paint() {}\n"
"template <typename T> void show(T& t) { t.paint(); }\n"
)})
assert not any(src.endswith("show()") for src, _ in calls)
def test_typed_local_receiver_still_resolves(tmp_path):
calls = _calls(tmp_path, {"widget.cpp": (
"class Widget { public: void paint(); };\n"
@@ -68,3 +59,29 @@ def test_this_call_keeps_in_file_binding(tmp_path):
"void Widget::run() { this->paint(); }\n"
)})
assert any(src.endswith("run()") and tgt.endswith("paint()") for src, tgt in calls)
def test_a_local_in_one_function_does_not_retype_a_parameter_in_another(tmp_path):
files = {"net.cpp": (
"class Request { public: void send(); };\n"
"class Response { public: void close(); };\n"
"void f(Request& r) { r.send(); }\n"
"void g() { Response r; r.close(); }\n"
)}
calls = _calls(tmp_path, files)
assert ("net.cpp:f()", "net.cpp:.send()") in calls
result = extract([tmp_path / "net.cpp"], root=tmp_path, cache_root=tmp_path / "cache2")
label = {n["id"]: n["label"] for n in result["nodes"]}
assert not any(
label.get(e["source"]) == "f()" and label.get(e["target"]) == "Response"
for e in result["edges"]
)
def test_auto_receiver_keeps_its_in_file_binding(tmp_path):
calls = _calls(tmp_path, {"widget.cpp": (
"class Widget { public: void paint(); };\n"
"Widget make();\n"
"void run() { auto w = make(); w.paint(); }\n"
)})
assert ("widget.cpp:run()", "widget.cpp:.paint()") in calls