Repository navigation
Detect an ambiguous method call - #697
Conversation
There was a problem hiding this comment.
🔵 Needs a closer look
It alters core method-overload resolution exercised by every call site, so potential regressions need human review and a full test-suite run that cannot be verified here.
0 open findings
What changed in this PR
This PR fixes a compiler bug where an ambiguous method call (two overloads that both match a call site exactly, differing only by return type) was silently resolved by picking the first exact match instead of being reported as an error. The fix lives in the core overload-resolution helper collectMatchingMethods, so that all equally-exact candidates are collected and the downstream chooseBest can detect and report the ambiguity as COMPILER-63 (SIGNATURE_AMBIGUOUS).
Changes:
- In
collectMatchingMethods, replaced the "return on first exact match" fast path with afFoundExactflag: the first exact match clears prior non-exact candidates, subsequent exact matches are also collected, and non-exact matches found afterward are discarded — allowing genuine ambiguity to surface. - Fixed per-argument exactness so
fExactis accumulated across all arguments (fExact &= …) instead of being overwritten by the last argument's result. - Added a negative test (
errors.x, expectingCOMPILER-63forvar o = f()andf()) and a positive test (misc.x, verifying that return-type context and avoidoverload still disambiguate correctly).
| File | Description |
|---|---|
javatools/src/main/java/org/xvm/compiler/ast/AstNode.java |
Core fix: collect all exact matches via fFoundExact and accumulate fExact across args so ambiguous calls reach chooseBest. |
xdk/src/test/resources/compiler-errors/errors.x |
Negative test asserting COMPILER-63 for unconstrained ambiguous calls. |
manualTests/src/main/x/misc.x |
Positive test confirming return-type/context and a void overload disambiguate without error. |
I traced the change through findMethod → chooseBest and confirmed the collected exact matches correctly produce SIGNATURE_AMBIGUOUS (COMPILER-63) when neither signature is substitutable for the other, while the WithVoid/NonVoid cases remain unambiguous. The fExact &= change is a legitimate correctness fix (previously a later exact argument could wrongly reset an earlier non-exact result). I found no objective defects to flag inline.
One thing I could not verify in this environment (no XDK build / test execution available): because this touches the overload-resolution path exercised by every method call, it could newly surface ambiguity errors in code that previously compiled by arbitrarily picking the first match. That behavioral broadening is the intent, but its repository-wide blast radius warrants running the full manualTests/XDK suite before merge.
🧠 Review effort: Balanced
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
fdc1579 to
fef1bc4
Compare
fef1bc4 to
52ce683
Compare
There was a problem hiding this comment.
Approved!
The updated fix addresses the three-overload issue I reported. Approved, with two follow-ups:
-
I took the liberty of adding a regression/coverage test for
fExact &= .... A two-argument case where one overload widens the first argument and the other widens the last should report ambiguity. That would catch the previous assignment incorrectly treating the first overload as an exact match. -
The generic/concrete example from the earlier review still reports
COMPILER-63:String pick(String)alongside<T> T pick(T)remains ambiguous forString text = pick("x"). If this is intentional, please explicitly record that semantic change in the commit message so it is traceable when investigating compiler/LSP behavior.
Add an expected ambiguity diagnostic when two overloads widen different arguments, plus a manual test asserting that a fully exact overload wins. Verify that replacing fExact &= with assignment removes the expected diagnostic.
|
Regarding point 2) - as Cameron wrote earlier:
Due to the extremely rare nature of the issue I decided not to chase it any further. |
Dima reported the issue on Discord when the compiler fails to report an ambiguous call target.
This PR is a fix for the reported issue