Skip to content

Commit b07c887

Browse files
committed
[ConstraintSystem] Retrieve contextual from a solution for ambiguities
In ambiguity scenarios solutions are not applied back to the constraint system, so it might not always have contextual type information when it was recorded e.g. for a multi-statement closure. Resolves: rdar://97396399
1 parent dd40e14 commit b07c887

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1489,6 +1489,15 @@ class Solution {
14891489
/// types where possible.
14901490
Type resolveInterfaceType(Type type) const;
14911491

1492+
Type getContextualType(ASTNode anchor) const {
1493+
for (const auto &entry : contextualTypes) {
1494+
if (entry.first == anchor) {
1495+
return simplifyType(entry.second.getType());
1496+
}
1497+
}
1498+
return Type();
1499+
}
1500+
14921501
/// For a given locator describing a function argument conversion, or a
14931502
/// constraint within an argument conversion, returns information about the
14941503
/// application of the argument to its parameter. If the locator is not

lib/Sema/ConstraintSystem.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4257,9 +4257,12 @@ static bool diagnoseAmbiguityWithContextualType(
42574257

42584258
auto anchor = locator->getAnchor();
42594259
auto name = result->choices.front().getName();
4260+
auto contextualTy = solution.getContextualType(anchor);
4261+
4262+
assert(contextualTy);
4263+
42604264
DE.diagnose(getLoc(anchor), diag::no_candidates_match_result_type,
4261-
name.getBaseName().userFacingName(),
4262-
cs.getContextualType(anchor, /*forConstraint=*/false));
4265+
name.getBaseName().userFacingName(), contextualTy);
42634266

42644267
for (const auto &solution : solutions) {
42654268
auto overload = solution.getOverloadChoice(calleeLocator);
@@ -4272,10 +4275,9 @@ static bool diagnoseAmbiguityWithContextualType(
42724275

42734276
if (isExpr<ApplyExpr>(anchor) || isExpr<SubscriptExpr>(anchor)) {
42744277
auto fnType = type->castTo<FunctionType>();
4275-
DE.diagnose(
4276-
loc, diag::cannot_convert_candidate_result_to_contextual_type,
4277-
decl->getName(), fnType->getResult(),
4278-
cs.getContextualType(anchor, /*forConstraint=*/false));
4278+
DE.diagnose(loc,
4279+
diag::cannot_convert_candidate_result_to_contextual_type,
4280+
decl->getName(), fnType->getResult(), contextualTy);
42794281
} else {
42804282
DE.diagnose(loc, diag::found_candidate_type, type);
42814283
}

0 commit comments

Comments
 (0)