Files
swift-mirror/test/Constraints/rdar39931339.swift
Slava Pestov 54155fe290 Sema: Check generic requirements of parent context when realizing non-generic types
When realizing a type like Foo<A>.Bar, we have to account for the
possibility that Bar is defined in a constrained extension of Foo,
and has generic requirements beyond those that Foo itself places
on 'A'.

Previously we only handled this for types referenced from the
constraint system as part of openUnboundGenericType(), so we were
allowing invalid types through in type context.

Add the right checking to applyGenericArguments() to close the
hole. Note that the old code path still exists in the constraint
solver; it is used for member accesses on metatype bases only.

Fixes <https://bugs.swift.org/browse/SR-10466>.
2019-11-14 18:26:43 -05:00

49 lines
1.3 KiB
Swift

// RUN: %target-typecheck-verify-swift
protocol P {}
struct S<T> {}
class C : P {}
extension S where T : P { // expected-note {{where 'T' = 'T'}}
typealias A = Int
typealias B<U> = S<U>
}
extension S where T == Float { // expected-note {{requirement specified as 'T' == 'Float' [with T = Int]}}
typealias C = Int
}
class A<T, U> {}
extension A where T == [U], U: P {
typealias S1 = Int
}
extension A where T == [U], U == Int {
// expected-note@-1 {{requirement specified as 'T' == '[Int]' [with T = [String]]}}
typealias S2 = Int
}
class B<U> : A<[U], U> {}
_ = B<C>.S1() // Ok
_ = B<Int>.S2() // Ok
_ = B<Float>.S1() // expected-error {{type 'Float' does not conform to protocol 'P'}}
_ = B<String>.S2()
// expected-error@-1 {{'B<String>.S2' (aka 'Int') requires the types '[String]' and '[Int]' be equivalent}}
_ = S<C>.A() // Ok
_ = S<Int>.A() // expected-error {{type 'Int' does not conform to protocol 'P'}}
_ = S<String>.B<Int>() // expected-error {{type 'String' does not conform to protocol 'P'}}
_ = S<Int>.C() // expected-error {{'S<Int>.C' (aka 'Int') requires the types 'Int' and 'Float' be equivalent}}
func foo<T>(_ s: S<T>.Type) {
_ = s.A() // expected-error {{referencing type alias 'A' on 'S' requires that 'T' conform to 'P'}}
}
func bar<T: P>(_ s: S<T>.Type) {
_ = s.A() // Ok
}