// RUN: %target-typecheck-verify-swift // Verify the use of unbound generic types. They are permitted in // certain places where type inference can fill in the generic // arguments, and banned everywhere else. // -------------------------------------------------- // Places where generic arguments are always required // -------------------------------------------------- struct Foo { // expected-note 3{{generic struct 'Foo' declared here}} struct Wibble { } } class Dict { } // expected-note 3 {{generic class 'Dict' declared here}} // The underlying type of a typealias can only have unbound generic arguments // at the top level. typealias F = Foo // OK typealias FW = Foo.Wibble // expected-error{{reference to generic type 'Foo' requires arguments in <...>}} typealias FFW = () -> Foo // expected-error{{reference to generic type 'Foo' requires arguments in <...>}} typealias OFW = Optional<() -> Foo> // expected-error{{reference to generic type 'Foo' requires arguments in <...>}} // Cannot inherit from a generic type without arguments. class MyDict : Dict { } // expected-error{{reference to generic type 'Dict' requires arguments in <...>}} // Cannot create variables of a generic type without arguments. // FIXME: would allow it for local variables // only var x : Dict // expected-error{{reference to generic type 'Dict' requires arguments in <...>}} // Cannot create parameters of generic type without arguments. func f(x: Dict) {} // expected-error{{reference to generic type 'Dict' requires arguments in <...>}} class GC { init() {} func f() -> GC { let gc = GC() return gc } } extension GC { func g() -> GC { let gc = GC() return gc } } class SomeClassWithInvalidMethod { func method() { // expected-note {{in call to function 'method()'}} // expected-error@-1 {{generic parameter 'T' is not used in function signature}} self.method() // expected-error@-1 {{generic parameter 'T' could not be inferred}} } } // QoI: Cannot invoke with argument list (T), expected an argument list of (T) protocol r20792596P {} func foor20792596(x: T) -> T { // expected-note {{where 'T' = 'T'}} return x } func callfoor20792596(x: T) -> T { return foor20792596(x) // expected-error@-1 {{missing argument label 'x:' in call}} // expected-error@-2 {{global function 'foor20792596(x:)' requires that 'T' conform to 'r20792596P'}} } // parameter "not used in function signature" when part of a superclass constraint struct X1 { func bar() where T: X2 {} } class X2 {} // missing check for unbound parent type struct Outer { struct Inner {} struct Middle { typealias Inner = Outer.Middle } } func makeInner() -> Outer.Middle.Inner { return .init() } var innerProperty: Outer.Middle.Inner = makeInner() // expected-error@-1 {{reference to generic type 'Outer' requires arguments in <...>}} // Some nested generic cases struct OuterStruct { // expected-note 2{{generic struct 'OuterStruct' declared here}} struct InnerStruct {} // expected-note {{generic struct 'InnerStruct' declared here}} } func nested(_: OuterStruct.InnerStruct) {} // expected-error@-1 {{reference to generic type 'OuterStruct' requires arguments in <...>}} func nested(_: OuterStruct.InnerStruct) {} // expected-error@-1 {{reference to generic type 'OuterStruct' requires arguments in <...>}} func nested(_: OuterStruct.InnerStruct) {} // expected-error@-1 {{reference to generic type 'OuterStruct.InnerStruct' requires arguments in <...>}} func assertExactType(of _: T, is _: T.Type) {} // https://github.com/apple/swift/issues/51217 protocol P { associatedtype A associatedtype B } do { struct Concrete: P { typealias A = Int typealias B = Bool } struct Generic: P {} struct BinderGenericParams1 where T1.A == T2.A, T1.B == T2.B { static func bind(_: T1, _: T2) -> T2 {} } struct BinderGenericParams2 { static func bind(_: T1, _: T2) -> T2 where T1.A == T2.A, T1.B == T2.B {} } let x1 = BinderGenericParams1.bind(Concrete(), Generic()) let x2 = BinderGenericParams2.bind(Concrete(), Generic()) assertExactType(of: x1, is: Generic.self) assertExactType(of: x2, is: Generic.self) } // https://github.com/apple/swift/issues/60922 enum E {} // expected-note@-1 2 {{generic enum 'E' declared here}} extension E? {} // expected-error@-1{{reference to generic type 'E' requires arguments in <...>}} extension Optional {} // expected-error@-1{{reference to generic type 'E' requires arguments in <...>}} struct G {} // expected-note@-1{{generic struct 'G' declared here}} extension G? {} // expected-error@-1{{reference to generic type 'G' requires arguments in <...>}}