Skip to content

Commit ecf8146

Browse files
committed
[Diagnostics] Treat type requirement failures associated with Self = Any as unrelated
This helps us to filter out cases like operator overloads where `Self` type comes from e.g. default for collection element - `[1, "hello"].map { $0 + 1 }`. Main problem here is that collection type couldn't be determined without unification to `Any` and `+` failing for all numeric overloads is just a consequence.
1 parent facd27f commit ecf8146

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
@@ -3832,8 +3832,8 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyConformsToConstraint(
38323832
ConstraintKind kind,
38333833
ConstraintLocatorBuilder locator,
38343834
TypeMatchOptions flags) {
3835+
auto *typeVar = type->getAs<TypeVariableType>();
38353836
if (shouldAttemptFixes()) {
3836-
auto *typeVar = type->getAs<TypeVariableType>();
38373837
// If type variable, associated with this conformance check,
38383838
// has been determined to be a "hole" in constraint system,
38393839
// let's consider this check a success without recording
@@ -3983,6 +3983,23 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyConformsToConstraint(
39833983
return SolutionKind::Error;
39843984

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