Skip to content

A @cpp method called through ?. picks among its overloads - #602

Merged
ASDAlexander77 merged 1 commit into
mainfrom
cpp-overload-optional-call
Oct 17, 2026
Merged

ASDAlexander77 merged 1 commit into
mainfrom
cpp-overload-optional-call

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

Problem

obj?.mix(1, 2, 3) on a @cpp class with mix(a, b) and mix(a, b, c) called the first overload, mix(a, b), and passed it three arguments. There was no diagnostic. Direct calls (obj.mix(1, 2, 3)) were already correct.

With ?., the member access is built in one ts.If and the call in a later one. The call only saw ts.Value of the optional result, so resolveCppOverload couldn't tell which method it was. The call also lost ts.cpp_call.

Fix

cppMethodAccessAgain (MLIRGenAccessCall.cpp): when the optional callee comes from a @cpp method access, the method is accessed again inside the call's region, from ts.GetThis of the bound function. The object's own class is used, so a derived class's overloads count. After that the normal path runs: overload resolution, ts.cpp_call.

Tests

  • 00cpp_overloads.ts: a new Mixer class whose overloads differ only in arity (2 vs 3 params), called directly and through ?.. It also covers ?. on a derived class (override, own overload, base overload) and ?. on null.
  • Full Release suite: 4171/4171 passed.

Not in this PR

ts.CallIndirect has no too-many-args check (new C().m(1, 2, 3) on a 2-param method compiles). That is why this miscompile was silent. It is being handled separately.

🤖 Generated with Claude Code

`obj?.mix(1, 2, 3)` called the first overload, `mix(a, b)`, with three
arguments: the member access is made in a conditional of its own, and the
call saw only the value of its optional result, so resolveCppOverload could
not tell which method it was, and the call was not a C++ call (ts.cpp_call).
The method is now accessed again from the object the callee is bound to,
inside the call's own region.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit 480e5a0 into main Oct 17, 2026
2 checks passed
@ASDAlexander77
ASDAlexander77 deleted the cpp-overload-optional-call branch October 17, 2026 23:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant