Skip to content

Commit c16b888

Browse files
authored
Merge pull request swiftlang#26941 from xedin/req-failures-with-any
[Diagnostics] Treat type requirement failures associated with `Self` …
2 parents 962c9d2 + ecf8146 commit c16b888

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3836,8 +3836,8 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyConformsToConstraint(
38363836
ConstraintKind kind,
38373837
ConstraintLocatorBuilder locator,
38383838
TypeMatchOptions flags) {
3839+
auto *typeVar = type->getAs<TypeVariableType>();
38393840
if (shouldAttemptFixes()) {
3840-
auto *typeVar = type->getAs<TypeVariableType>();
38413841
// If type variable, associated with this conformance check,
38423842
// has been determined to be a "hole" in constraint system,
38433843
// let's consider this check a success without recording
@@ -3987,6 +3987,23 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyConformsToConstraint(
39873987
return SolutionKind::Error;
39883988

39893989
if (path.back().is<LocatorPathElt::AnyRequirement>()) {
3990+
// If this is a requirement associated with `Self` which is bound
3991+
// to `Any`, let's consider this "too incorrect" to continue.
3992+
//
3993+
// This helps us to filter out cases like operator overloads where
3994+
// `Self` type comes from e.g. default for collection element -
3995+
// `[1, "hello"].map { $0 + 1 }`. Main problem here is that
3996+
// collection type couldn't be determined without unification to
3997+
// `Any` and `+` failing for all numeric overloads is just a consequence.
3998+
if (typeVar && type->isAny()) {
3999+
auto *GP = typeVar->getImpl().getGenericParameter();
4000+
if (auto *GPD = GP->getDecl()) {
4001+
auto *DC = GPD->getDeclContext();
4002+
if (DC->isTypeContext() && DC->getSelfInterfaceType()->isEqual(GP))
4003+
return SolutionKind::Error;
4004+
}
4005+
}
4006+
39904007
if (auto *fix =
39914008
fixRequirementFailure(*this, type, protocolTy, anchor, path)) {
39924009
if (!recordFix(fix)) {

test/Constraints/closures.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,7 @@ func r22162441(_ lines: [String]) {
182182

183183
func testMap() {
184184
let a = 42
185-
[1,a].map { $0 + 1.0 } // expected-error {{binary operator '+' cannot be applied to operands of type 'Any' and 'Double'}}
186-
// expected-note @-1 {{expected an argument list of type '(Double, Double)'}}
185+
[1,a].map { $0 + 1.0 } // expected-error {{cannot convert value of type 'Int' to expected element type 'Double'}}
187186
}
188187

189188
// <rdar://problem/22414757> "UnresolvedDot" "in wrong phase" assertion from verifier

0 commit comments

Comments
 (0)