// RUN: %target-typecheck-verify-swift // RUN: not %target-swift-frontend -typecheck %s -debug-generic-signatures 2>&1 | %FileCheck %s func testInvalidConformance() { // expected-error@+1 {{type 'T' constrained to non-protocol, non-class type 'Int'}} func invalidIntConformance(_: T) where T: Int {} // expected-error@+1 {{type 'T' constrained to non-protocol, non-class type 'Int'}} struct InvalidIntConformance {} struct S { // expected-error@+2 {{type 'T' constrained to non-protocol, non-class type 'Int'}} // expected-note@+1 {{use 'T == Int' to require 'T' to be 'Int'}} func method() where T: Int {} } } // Check directly-concrete same-type constraints typealias NotAnInt = Double protocol X {} // expected-error@+1{{generic signature requires types 'NotAnInt' (aka 'Double') and 'Int' to be the same}} extension X where NotAnInt == Int {} protocol EqualComparable { func isEqual(_ other: Self) -> Bool } func badTypeConformance1(_: T) where Int : EqualComparable {} // expected-error{{type 'Int' in conformance requirement does not refer to a generic parameter or associated type}} func badTypeConformance2(_: T) where T.Blarg : EqualComparable { } // expected-error{{'Blarg' is not a member type of type 'T'}} func badTypeConformance3(_: T) where (T) -> () : EqualComparable { } // expected-error@-1{{type '(T) -> ()' in conformance requirement does not refer to a generic parameter or associated type}} func badTypeConformance4(_: T) where (inout T) throws -> () : EqualComparable { } // expected-error@-1{{type '(inout T) throws -> ()' in conformance requirement does not refer to a generic parameter or associated type}} func badTypeConformance5(_: T) where T & Sequence : EqualComparable { } // expected-error@-1 {{non-protocol, non-class type 'T' cannot be used within a protocol-constrained type}} func redundantTypeConformance6(_: T) where [T] : Collection { } func concreteSameTypeRedundancy(_: T) where Int == Int {} func concreteSameTypeRedundancy(_: T) where Array == Array {} // expected-warning@-1{{same-type requirement makes generic parameter 'T' non-generic}} protocol P {} struct S: P {} func concreteConformanceRedundancy(_: T) where S : P {} class C {} func concreteLayoutRedundancy(_: T) where C : AnyObject {} func concreteLayoutConflict(_: T) where Int : AnyObject {} // expected-error@-1{{type 'Int' in conformance requirement does not refer to a generic parameter or associated type}} class C2: C {} func concreteSubclassRedundancy(_: T) where C2 : C {} class D {} func concreteSubclassConflict(_: T) where D : C {} // expected-error@-1{{type 'D' in conformance requirement does not refer to a generic parameter or associated type}} protocol UselessProtocolWhereClause where Int == Int {} protocol InvalidProtocolWhereClause where Self: Int {} // expected-error@-1 {{type 'Self' constrained to non-protocol, non-class type 'Int'}} typealias Alias = T where Int == Int func cascadingConflictingRequirement(_: T) where DoesNotExist : EqualComparable { } // expected-error@-1 {{cannot find type 'DoesNotExist' in scope}} func cascadingInvalidConformance(_: T) where T : DoesNotExist { } // expected-error@-1 {{cannot find type 'DoesNotExist' in scope}} func trivialRedundant1(_: T) where T: P, T: P {} func trivialRedundant2(_: T) where T: AnyObject, T: AnyObject {} func trivialRedundant3(_: T) where T: C, T: C {} func trivialRedundant4(_: T) where T == T {} protocol TrivialRedundantConformance: P, P {} protocol TrivialRedundantLayout: AnyObject, AnyObject {} // expected-error@-1 {{duplicate inheritance from 'AnyObject'}} protocol TrivialRedundantSuperclass: C, C {} // expected-error@-1 {{duplicate inheritance from 'C'}} protocol TrivialRedundantSameType where Self == Self { associatedtype T where T == T } struct G { } protocol Pair { associatedtype A associatedtype B } // CHECK-LABEL: .test1@ // CHECK-NEXT: Generic signature: , T.[Pair]B == Int> func test1(_: T) where T.A == G, T.A == G, T.B == Int { } protocol P1 { func p1() } protocol P2 : P1 { } protocol P3 { associatedtype P3Assoc : P2 } protocol P4 { associatedtype P4Assoc : P1 } // CHECK-LABEL: .inferSameType2@ // CHECK-NEXT: Generic signature: func inferSameType2(_: T, _: U) where U.P4Assoc : P2, T.P3Assoc == U.P4Assoc {} protocol P5 { associatedtype Element } protocol P6 { associatedtype AssocP6 : P5 } protocol P7 : P6 { associatedtype AssocP7: P6 } // CHECK-LABEL: ExtensionDecl line={{.*}} base=P7 // CHECK-NEXT: Generic signature: func sameTypeConcrete2(_: T) where T.B : X3, T.C == T.B, T.C == X3 { } // Test inferred requirements. protocol P11 { associatedtype X associatedtype Y associatedtype Z } // CHECK-LABEL: .inferred1@ // CHECK-NEXT: Generic signature: func inferred1(_: Set) {} // CHECK-LABEL: .inferred2@ // CHECK-NEXT: Generic signature: func inferred2(_: Set) where T: Hashable {} // CHECK-LABEL: .inferred3@ // CHECK-NEXT: Generic signature: > func inferred3(_: T) where T.X : Hashable, T.Z == Set, T.X == T.Y {} // CHECK-LABEL: .inferred4@ // CHECK-NEXT: Generic signature: > func inferred4(_: T) where T.Z == Set, T.X : Hashable, T.X == T.Y {} // CHECK-LABEL: .inferred5@ // CHECK-NEXT: Generic signature: > func inferred5(_: T) where T.Z == Set, T.Y : Hashable, T.X == T.Y {} // CHECK-LABEL: .inferred6@ // CHECK-NEXT: Generic signature: > func inferred6(_: T) where T.Y : Hashable, T.Z == Set, T.X == T.Y {} func typeMatcherSugar(_: T) where Array == Array, Array == Array {} // expected-warning@-1{{same-type requirement makes generic parameter 'T' non-generic}} struct ConcreteSelf: ConcreteProtocol {} protocol ConcreteProtocol where Self == ConcreteSelf {} // expected-error@-1 {{same-type requirement makes generic parameter 'Self' non-generic}} // MARK: - Conflict diagnostics protocol ProtoAlias1 { typealias A1 = Int } protocol ProtoAlias2 { typealias A2 = String } func basicConflict(_:T) where T.A1 == T.A2 {} // expected-error@-1{{no type for 'T.A1' can satisfy both 'T.A1 == String' and 'T.A1 == Int'}} protocol RequiresAnyObject { associatedtype A: AnyObject } protocol RequiresConformance { associatedtype A: P } class Super {} protocol RequiresSuperclass { associatedtype A: Super } func testMissingRequirements() { struct S {} func conflict1(_: T) where T.A == S {} // expected-error@-1{{no type for 'T.A' can satisfy both 'T.A == S' and 'T.A : AnyObject'}} func conflict2(_: T) where T.A == C {} // expected-error@-1{{no type for 'T.A' can satisfy both 'T.A == C' and 'T.A : P'}} class C {} func conflict3(_: T) where T.A == C {} // expected-error@-1{{no type for 'T.A' can satisfy both 'T.A : C' and 'T.A : Super'}} func conflict4(_: T) where T.A: C {} // expected-error@-1{{no type for 'T.A' can satisfy both 'T.A : C' and 'T.A : Super'}} } protocol Fooable { associatedtype Foo var foo: Foo { get } } protocol Barrable { associatedtype Bar: Fooable var bar: Bar { get } } protocol Concrete { associatedtype X where X == Int } func sameTypeConflicts() { struct X {} struct Y: Fooable { typealias Foo = X var foo: X { return X() } } struct Z: Barrable { typealias Bar = Y var bar: Y { return Y() } } // expected-error@+1{{no type for 'T.Foo' can satisfy both 'T.Foo == X' and 'T.Foo == Y'}} func fail1< T: Fooable, U: Fooable >(_ t: T, u: U) -> (X, Y) where T.Foo == X, U.Foo == Y, T.Foo == U.Foo { fatalError() } // expected-error@+1{{no type for 'T.Foo' can satisfy both 'T.Foo == Y' and 'T.Foo == X'}} func fail2< T: Fooable, U: Fooable >(_ t: T, u: U) -> (X, Y) where T.Foo == U.Foo, T.Foo == X, U.Foo == Y { fatalError() } // expected-error@+1{{no type for 'T.Bar' can satisfy both 'T.Bar == X' and 'T.Bar : Fooable'}} func fail3(_ t: T) -> X where T.Bar == X { fatalError() } func fail4(_ t: T) -> (Y, Z) where T.Bar == Y, T.Bar.Foo == Z { // expected-error@-1{{generic signature requires types 'Y.Foo' (aka 'X') and 'Z' to be the same}} fatalError() } func fail5(_ t: T) -> (Y, Z) where T.Bar.Foo == Z, // expected-error@-1{{generic signature requires types 'Y.Foo' (aka 'X') and 'Z' to be the same}} T.Bar == Y { fatalError() } // expected-error@+1{{no type for 'T.X' can satisfy both 'T.X == String' and 'T.X == Int'}} func fail6(_: U, _: T) where T.X == String {} struct G {} // expected-error@+1{{no type for 'T.X' can satisfy both 'T.X == G' and 'T.X == Int'}} func fail7(_: U, _: T) where T.X == G {} // expected-warning@+2{{same-type requirement makes generic parameter 'T' non-generic; this is an error in the Swift 6 language mode}} // expected-error@+1{{no type for 'T' can satisfy both 'T == G' and 'T == Int'}} func fail8(_: U, _: T) where T == G, T == Int {} // expected-error@+1{{no type for 'T' can satisfy both 'T == G' and 'T == Int'}} func fail9(_: U, _: T) where T == Int, T == G {} // expected-warning@-1{{same-type requirement makes generic parameter 'T' non-generic; this is an error in the Swift 6 language mode}} }