Skip to content

[Diagnostics] Treat type requirement failures associated with Self #26941

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3832,8 +3832,8 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyConformsToConstraint(
ConstraintKind kind,
ConstraintLocatorBuilder locator,
TypeMatchOptions flags) {
auto *typeVar = type->getAs<TypeVariableType>();
if (shouldAttemptFixes()) {
auto *typeVar = type->getAs<TypeVariableType>();
// If type variable, associated with this conformance check,
// has been determined to be a "hole" in constraint system,
// let's consider this check a success without recording
Expand Down Expand Up @@ -3983,6 +3983,23 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyConformsToConstraint(
return SolutionKind::Error;

if (path.back().is<LocatorPathElt::AnyRequirement>()) {
// If this is a requirement associated with `Self` which is bound
// to `Any`, let's consider this "too incorrect" to continue.
//
// 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.
if (typeVar && type->isAny()) {
auto *GP = typeVar->getImpl().getGenericParameter();
if (auto *GPD = GP->getDecl()) {
auto *DC = GPD->getDeclContext();
if (DC->isTypeContext() && DC->getSelfInterfaceType()->isEqual(GP))
return SolutionKind::Error;
}
}

if (auto *fix =
fixRequirementFailure(*this, type, protocolTy, anchor, path)) {
if (!recordFix(fix)) {
Expand Down
3 changes: 1 addition & 2 deletions test/Constraints/closures.swift
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,7 @@ func r22162441(_ lines: [String]) {

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's still not quite right is it? The diagnostic talks about an 'element type' but that's Int here isn't it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not really Int, since there is a literal which could be a Double as well, which would match + inside of the closure body.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see it from the perspective that if there was no a in there, everything would have type-checked correctly as [Double].

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only other possible failure we could produce would say that $0 expected to be Double which might not be more useful since it still comes down to use of a.

}

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