@@ -4230,19 +4230,34 @@ bool ConstraintSystem::diagnoseAmbiguity(ArrayRef<Solution> solutions) {
4230
4230
4231
4231
for (unsigned i = 0 , n = diff.overloads .size (); i != n; ++i) {
4232
4232
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
+ }
4233
4249
4234
4250
// If we can't resolve the locator to an anchor expression with no path,
4235
4251
// we can't diagnose this well.
4236
- auto *anchor = getAsExpr (simplifyLocatorToAnchor (overload.locator ));
4237
4252
if (!anchor)
4238
4253
continue ;
4239
4254
4240
- auto it = indexMap.find (castToExpr ( anchor) );
4255
+ auto it = indexMap.find (anchor);
4241
4256
if (it == indexMap.end ())
4242
4257
continue ;
4243
4258
unsigned index = it->second ;
4244
4259
4245
- auto optDepth = getExprDepth (castToExpr ( anchor) );
4260
+ auto optDepth = getExprDepth (anchor);
4246
4261
if (!optDepth)
4247
4262
continue ;
4248
4263
unsigned depth = *optDepth;
0 commit comments