Skip to content

Commit 918ff24

Browse files
committed
[ConstraintSystem] Repair type mismatches between function types
If it couldn't be determined precisely what is the structural difference between two function types, let's still repair the mismatch by ignoring contextual type.
1 parent ebdb5c7 commit 918ff24

File tree

1 file changed

+28
-12
lines changed

1 file changed

+28
-12
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3975,10 +3975,17 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
39753975

39763976
auto result = matchFunctionTypes(func1, func2, kind, flags, locator);
39773977

3978-
// If this is a type mismatch in assignment, we don't really care
3979-
// (yet) was it argument or result type mismatch, let's produce a
3980-
// diagnostic which means both function types.
39813978
if (shouldAttemptFixes() && result.isFailure()) {
3979+
// If this is a contextual type mismatch failure
3980+
// let's give the solver a chance to "fix" it.
3981+
if (auto last = locator.last()) {
3982+
if (last->is<LocatorPathElt::ContextualType>())
3983+
break;
3984+
}
3985+
3986+
// If this is a type mismatch in assignment, we don't really care
3987+
// (yet) was it argument or result type mismatch, let's produce a
3988+
// diagnostic which mentions both function types.
39823989
auto *anchor = locator.getAnchor();
39833990
if (anchor && isa<AssignExpr>(anchor))
39843991
break;
@@ -8464,15 +8471,24 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyFixConstraint(
84648471
if (recordFix(fix))
84658472
return SolutionKind::Error;
84668473

8467-
// If type produced by expression is a function type
8468-
// with result type matching contextual, it should have
8469-
// been diagnosed as "missing explicit call", let's
8470-
// increase the score to make sure that we don't impede that.
8471-
if (auto *fnType = type1->getAs<FunctionType>()) {
8472-
auto result = matchTypes(fnType->getResult(), type2, matchKind,
8473-
TMF_ApplyingFix, locator);
8474-
if (result == SolutionKind::Solved)
8475-
increaseScore(SK_Fix);
8474+
if (auto *fnType1 = type1->getAs<FunctionType>()) {
8475+
// If this is a contextual mismatch between two
8476+
// function types which we couldn't find a more
8477+
// speficit fix for. Let's assume that such types
8478+
// are competely disjoint and adjust impact of
8479+
// the fix accordingly.
8480+
if (auto *fnType2 = type2->getAs<FunctionType>()) {
8481+
increaseScore(SK_Fix, 10);
8482+
} else {
8483+
// If type produced by expression is a function type
8484+
// with result type matching contextual, it should have
8485+
// been diagnosed as "missing explicit call", let's
8486+
// increase the score to make sure that we don't impede that.
8487+
auto result = matchTypes(fnType1->getResult(), type2, matchKind,
8488+
TMF_ApplyingFix, locator);
8489+
if (result == SolutionKind::Solved)
8490+
increaseScore(SK_Fix);
8491+
}
84768492
}
84778493

84788494
return SolutionKind::Solved;

0 commit comments

Comments
 (0)