Skip to content

Commit d4a5282

Browse files
Merge pull request #36208 from LucianoPAlmeida/SR-14280-ambiguity-repr
[SR-14280][CSSimplify] Increase the score when matching functions with different representations for contextual type
2 parents 9a883e6 + f5f7204 commit d4a5282

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2013,6 +2013,16 @@ ConstraintSystem::matchFunctionTypes(FunctionType *func1, FunctionType *func2,
20132013
return getTypeMatchFailure(locator);
20142014
}
20152015

2016+
// To contextual type increase the score to avoid ambiguity when solver can
2017+
// find more than one viable binding different only in representation e.g.
2018+
// let _: (@convention(block) () -> Void)? = Bool.random() ? nil : {}
2019+
// so same representation should be always favored.
2020+
auto loc = getConstraintLocator(locator);
2021+
if (loc->findLast<LocatorPathElt::ContextualType>() &&
2022+
func1->getRepresentation() != func2->getRepresentation()) {
2023+
increaseScore(SK_FunctionConversion);
2024+
}
2025+
20162026
if (!matchFunctionRepresentations(func1->getExtInfo(), func2->getExtInfo(),
20172027
kind, Options)) {
20182028
return getTypeMatchFailure(locator);

test/Constraints/closures.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,3 +1068,12 @@ func rdar_74435602(error: Error?) {
10681068
}
10691069
})
10701070
}
1071+
1072+
// SR-14280
1073+
let _: (@convention(block) () -> Void)? = Bool.random() ? nil : {} // OK
1074+
let _: (@convention(thin) () -> Void)? = Bool.random() ? nil : {} // OK
1075+
let _: (@convention(c) () -> Void)? = Bool.random() ? nil : {} // OK on type checking, diagnostics are deffered to SIL
1076+
1077+
let _: (@convention(block) () -> Void)? = Bool.random() ? {} : {} // OK
1078+
let _: (@convention(thin) () -> Void)? = Bool.random() ? {} : {} // OK
1079+
let _: (@convention(c) () -> Void)? = Bool.random() ? {} : {} // OK on type checking, diagnostics are deffered to SIL

0 commit comments

Comments
 (0)