Skip to content

Commit

Permalink
[ConstraintSystem] Ignore attempt to bind type var to dependent membe…
Browse files Browse the repository at this point in the history
…r with incorrect base

Just like in cases where both sides are dependent member types
with resolved base that can't be simplified to a concrete type
let's ignore this mismatch and mark affected type variable as a hole
because something else has to be fixed already for this to happen.
  • Loading branch information
xedin committed Feb 18, 2020
1 parent 79b757d commit df21cbf
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 7 deletions.
5 changes: 5 additions & 0 deletions lib/Sema/CSDiagnostics.cpp
Expand Up @@ -242,6 +242,11 @@ ValueDecl *RequirementFailure::getDeclRef() const {
// associated with it directly or rather with
// one of its parents.
if (auto *decl = overload->choice.getDeclOrNull()) {
// If declaration is an operator let's always use
// it to produce `in reference to` diagnostics.
if (decl->isOperator())
return decl;

auto *DC = decl->getDeclContext();

do {
Expand Down
9 changes: 9 additions & 0 deletions lib/Sema/CSSimplify.cpp
Expand Up @@ -2329,6 +2329,15 @@ ConstraintSystem::matchTypesBindTypeVar(
// instead of re-trying it and failing later.
if (typeVar->getImpl().canBindToHole() && !type->hasTypeVariable())
return getTypeMatchSuccess();

// Just like in cases where both sides are dependent member types
// with resolved base that can't be simplified to a concrete type
// let's ignore this mismatch and mark affected type variable as a hole
// because something else has to be fixed already for this to happen.
if (type->is<DependentMemberType>() && !type->hasTypeVariable()) {
recordPotentialHole(typeVar);
return getTypeMatchSuccess();
}
}

return formUnsolvedResult();
Expand Down
7 changes: 5 additions & 2 deletions test/Constraints/rdar44770297.swift
Expand Up @@ -4,8 +4,11 @@ protocol P {
associatedtype A
}

func foo<T: P>(_: () throws -> T) -> T.A? { // expected-note {{where 'T' = 'Never'}}
func foo<T: P>(_: () throws -> T) -> T.A? {
fatalError()
}

let _ = foo() {fatalError()} & nil // expected-error {{global function 'foo' requires that 'Never' conform to 'P'}}
// TODO(diagnostics): This expression is truly ambiguous because there is no conformance between `Never` and `P`
// which means no associated type `A` and `nil` can't be an argument to any overload of `&` so we end
// up generating at least 3 fixes per overload of `&`. But we could at least point to where the problems are.
let _ = foo() {fatalError()} & nil // expected-error {{type of expression is ambiguous without more context}}
2 changes: 1 addition & 1 deletion test/Constraints/tuple.swift
Expand Up @@ -181,7 +181,7 @@ variadicWithTrailingClosure(fn: +)
func gcd_23700031<T>(_ a: T, b: T) {
var a = a
var b = b
(a, b) = (b, a % b) // expected-error {{protocol 'BinaryInteger' requires that 'T' conform to 'BinaryInteger'}}
(a, b) = (b, a % b) // expected-error {{referencing operator function '%' on 'BinaryInteger' requires that 'T' conform to 'BinaryInteger'}}
}

// <rdar://problem/24210190>
Expand Down
2 changes: 1 addition & 1 deletion test/Generics/deduction.swift
Expand Up @@ -247,7 +247,7 @@ protocol Addable { // expected-note {{where 'Self' = 'U'}}
static func +(x: Self, y: Self) -> Self
}
func addAddables<T : Addable, U>(_ x: T, y: T, u: U) -> T {
u + u // expected-error{{protocol 'Addable' requires that 'U' conform to 'Addable'}}
u + u // expected-error{{referencing operator function '+' on 'Addable' requires that 'U' conform to 'Addable'}}
return x+y
}

Expand Down
Expand Up @@ -17,7 +17,7 @@ struct M<L : P, R> {
}
}

protocol P {
protocol P { // expected-note {{where 'Self' = 'M<WritableKeyPath<X, Int>, R>'}}
associatedtype A
associatedtype B

Expand All @@ -42,5 +42,6 @@ extension WritableKeyPath : P {
struct X { var y: Int = 0 }
var x = X()
x ~> \X.y ≈> { a in a += 1; return 3 }
// expected-error@-1 {{unable to infer complex closure return type; add explicit type to disambiguate}}
// expected-error@-2 {{cannot convert value of type 'M<WritableKeyPath<X, Int>, R>' to expected argument type 'WritableKeyPath<_, _>'}}
// expected-error@-1 {{referencing operator function '~>' on 'P' requires that 'M<WritableKeyPath<X, Int>, R>' conform to 'P'}}
// expected-error@-2 {{unable to infer complex closure return type; add explicit type to disambiguate}}
// expected-error@-3 {{cannot convert value of type 'X' to expected argument type 'M<WritableKeyPath<X, Int>, R>.A'}}

0 comments on commit df21cbf

Please sign in to comment.