Skip to content

Commit cbb69ce

Browse files
authored
Merge pull request swiftlang#39751 from xedin/make-diag-ambiguity-deterministic
[ConstraintSystem] Fix non-determinism in `diagnoseAmbiguity`
2 parents e80cbe7 + ffca407 commit cbb69ce

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

lib/Sema/ConstraintSystem.cpp

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4230,19 +4230,34 @@ bool ConstraintSystem::diagnoseAmbiguity(ArrayRef<Solution> solutions) {
42304230

42314231
for (unsigned i = 0, n = diff.overloads.size(); i != n; ++i) {
42324232
auto &overload = diff.overloads[i];
4233+
auto *locator = overload.locator;
4234+
4235+
Expr *anchor = nullptr;
4236+
4237+
// Simplification of member locator would produce a base expression,
4238+
// this is what we want for diagnostics but not for comparisons here
4239+
// because base expression is located at a different depth which would
4240+
// lead to incorrect results if both reference and base expression are
4241+
// ambiguous e.g. `test[x].count` if both `[x]` and `count` are ambiguous
4242+
// than simplification of `count` would produce `[x]` which is incorrect.
4243+
if (locator->isLastElement<LocatorPathElt::Member>() ||
4244+
locator->isLastElement<LocatorPathElt::ConstructorMember>()) {
4245+
anchor = getAsExpr(locator->getAnchor());
4246+
} else {
4247+
anchor = getAsExpr(simplifyLocatorToAnchor(overload.locator));
4248+
}
42334249

42344250
// If we can't resolve the locator to an anchor expression with no path,
42354251
// we can't diagnose this well.
4236-
auto *anchor = getAsExpr(simplifyLocatorToAnchor(overload.locator));
42374252
if (!anchor)
42384253
continue;
42394254

4240-
auto it = indexMap.find(castToExpr(anchor));
4255+
auto it = indexMap.find(anchor);
42414256
if (it == indexMap.end())
42424257
continue;
42434258
unsigned index = it->second;
42444259

4245-
auto optDepth = getExprDepth(castToExpr(anchor));
4260+
auto optDepth = getExprDepth(anchor);
42464261
if (!optDepth)
42474262
continue;
42484263
unsigned depth = *optDepth;

0 commit comments

Comments
 (0)