1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
|
// RUN: %target-typecheck-verify-swift -parse-as-library
// Protocols can be nested inside non-generic types.
protocol Delegate {}
enum Table {
protocol Delegate {}
}
enum Button {
protocol Delegate {}
}
struct PlainDelegateConformer: Delegate {}
struct TableDelegateConformer: Table.Delegate {}
struct ButtonDelegateConformer: Button.Delegate {}
func testDifferent() {
let plain = PlainDelegateConformer()
if plain is Delegate { print("ok (1)") } // expected-warning {{'is' test is always true}}
if plain is Table.Delegate { print("bad (2)") }
if plain is Button.Delegate { print("bad (3)") }
let tableDel = TableDelegateConformer()
if tableDel is Delegate { print("bad (4)") }
if tableDel is Table.Delegate { print("ok (5)") } // expected-warning {{'is' test is always true}}
if tableDel is Button.Delegate { print("bad (6)") }
let buttonDel = ButtonDelegateConformer()
if buttonDel is Delegate { print("bad (7)") }
if buttonDel is Table.Delegate { print("bad (8)") }
if buttonDel is Button.Delegate { print("ok (9)") } // expected-warning {{'is' test is always true}}
}
enum OuterEnum {
protocol C {}
case C(C)
// expected-error@-1{{invalid redeclaration of 'C'}}
// expected-note@-3{{'C' previously declared here}}
}
class OuterClass {
protocol InnerProtocol : OuterClass { }
}
// Name lookup circularity tests.
protocol SomeBaseProtocol {}
struct ConformanceOnDecl: ConformanceOnDecl.P {
protocol P: SomeBaseProtocol {}
}
struct ConformanceOnDecl_2: ConformanceOnDecl_2.P {
protocol P where Self: SomeBaseProtocol {}
}
struct ConformanceOnDecl_3: Self.P {
protocol P: SomeBaseProtocol {}
}
struct ConformanceOnDecl_4: ConformanceOnDecl_4.Q {
protocol P: SomeBaseProtocol {}
protocol Q: P {}
}
struct ConformanceInExtension {
protocol P: SomeBaseProtocol {}
}
extension ConformanceInExtension: ConformanceInExtension.P {}
struct ConformanceInExtension_2 {
protocol P where Self: SomeBaseProtocol {}
}
extension ConformanceInExtension_2: ConformanceInExtension_2.P {}
struct ConformanceInExtension_3 {
protocol P: SomeBaseProtocol {}
}
extension ConformanceInExtension_3: Self.P {}
struct ConformanceInExtension_4 {
protocol P: SomeBaseProtocol {}
protocol Q: P {}
}
extension ConformanceInExtension_4: ConformanceInExtension_4.Q {}
// Protocols can be nested in actors.
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
actor SomeActor {
protocol Proto {}
}
// Deeply nested.
enum Level0 {
struct Level1 {
class Level2 {
enum Level3 {
struct Level4 {
class Level5 {
protocol DeeplyNested {
func someRequirement()
}
func lookupTest(_ input: some DeeplyNested) {}
}
func lookupTest(_ input: some Level5.DeeplyNested) {}
}
func lookupTest(_ input: some Level4.Level5.DeeplyNested) {}
}
}
}
}
func useDeeplyNested(_ input: some Level0.Level1.Level2.Level3.Level4.Level5.DeeplyNested) {
input.someRequirement()
}
// Add a nested protocol in an extension.
extension Level0.Level1.Level2 {
protocol ModeratelyNested {
associatedtype ReturnValue
func anotherRequirement() -> ReturnValue
}
func lookupTest(_: some ModeratelyNested) { }
}
func useModeratelyNested<Input>(
_ input: Input
) -> Input.ReturnValue where Input: Level0.Level1.Level2.ModeratelyNested {
input.anotherRequirement()
}
struct ModeratelyNestedConformer: Level0.Level1.Level2.ModeratelyNested {
func anotherRequirement() -> Int {
99
}
}
let _umn: Int = useModeratelyNested(ModeratelyNestedConformer())
// Extend a nested protocol.
extension Level0.Level1.Level2.ModeratelyNested {
func extensionMethod() -> ReturnValue {
anotherRequirement()
}
}
extension Level0.Level1.Level2.ModeratelyNested where ReturnValue == Int {
func extensionMethod() -> Int {
anotherRequirement() + 1
}
}
// Protocols may be nested inside non-generic functions.
func someFunction_0() {
protocol Yes {}
struct Foo: Yes {}
struct Bar: Yes {}
}
func someFunction_1() {
struct HowAboutThis {
protocol AlsoYes {
associatedtype SomeType
func doSomething() -> SomeType
}
}
struct Conforms0: HowAboutThis.AlsoYes {
typealias SomeType = Int
func doSomething() -> SomeType { -99 }
}
struct Conforms1: HowAboutThis.AlsoYes {
typealias SomeType = String
func doSomething() -> SomeType { "works" }
}
func usesNested<T: HowAboutThis.AlsoYes>(_ input: T) -> T.SomeType {
input.doSomething()
}
let _: Int = usesNested(Conforms0())
let _: String = usesNested(Conforms1())
}
// Unqualified lookup searches parent scopes.
struct Lookup_Parent {
protocol A {}
class SomeClass {}
}
extension Lookup_Parent {
protocol B: A {}
protocol C: SomeClass, B {
func doSomething(_: some A) -> SomeClass
}
}
// Protocols cannot be nested in generic types or other protocols.
struct OuterGeneric<D> {
protocol InnerProtocol { // expected-error{{protocol 'InnerProtocol' cannot be nested in a generic context}}
associatedtype Rooster
func flip(_ r: Rooster)
func flop(_ t: D) // expected-error {{cannot find type 'D' in scope}}
}
}
class OuterGenericClass<T> {
protocol InnerProtocol { // expected-error{{protocol 'InnerProtocol' cannot be nested in a generic context}}
associatedtype Rooster
func flip(_ r: Rooster)
func flop(_ t: T) // expected-error {{cannot find type 'T' in scope}}
}
}
protocol OuterProtocol {
associatedtype Hen
protocol InnerProtocol { // expected-error{{protocol 'InnerProtocol' cannot be nested in protocol 'OuterProtocol'}}
associatedtype Rooster
func flip(_ r: Rooster)
func flop(_ h: Hen) // expected-error {{cannot find type 'Hen' in scope}}
}
}
extension OuterProtocol {
protocol DefinedInExtension {} // expected-error{{protocol 'DefinedInExtension' cannot be nested in protocol 'OuterProtocol'}}
}
struct ConformsToOuterProtocol : OuterProtocol {
typealias Hen = Int
func f() { let _ = InnerProtocol.self } // expected-error {{use of protocol 'InnerProtocol' as a type must be written 'any InnerProtocol'}}
}
extension OuterProtocol {
func f() {
protocol Invalid_0 {} // expected-error{{type 'Invalid_0' cannot be nested in generic function 'f()'}}
struct SomeType { // expected-error{{type 'SomeType' cannot be nested in generic function 'f()'}}
protocol Invalid_1 {} // expected-error{{protocol 'Invalid_1' cannot be nested in a generic context}}
}
}
func g<T>(_: T) {
protocol Invalid_2 {} // expected-error{{type 'Invalid_2' cannot be nested in generic function 'g'}}
}
}
// 'InnerProtocol' does not inherit the generic parameters of
// 'OtherGenericClass', so the occurrence of 'OtherGenericClass'
// in 'InnerProtocol' is not "in context" with implicitly
// inferred generic arguments <T, U>.
class OtherGenericClass<T, U> { // expected-note {{generic type 'OtherGenericClass' declared here}}
protocol InnerProtocol : OtherGenericClass { }
// expected-error@-1 {{protocol 'InnerProtocol' cannot be nested in a generic context}}
// expected-error@-2 {{reference to generic type 'OtherGenericClass' requires arguments in <...>}}
}
protocol InferredGenericTest {
associatedtype A
}
extension OtherGenericClass {
protocol P: InferredGenericTest where Self.A == T {}
// expected-error@-1 {{protocol 'P' cannot be nested in a generic context}}
// expected-error@-2 {{cannot find type 'T' in scope}}
}
// A nested protocol does not satisfy an associated type requirement.
protocol HasAssoc {
associatedtype A // expected-note {{protocol requires nested type 'A'; add nested type 'A' for conformance}}
}
struct ConformsToHasAssoc: HasAssoc { // expected-error {{type 'ConformsToHasAssoc' does not conform to protocol 'HasAssoc'}}
protocol A {}
}
// @usableFromInline.
struct OuterForUFI {
@usableFromInline
protocol Inner {
func req()
}
}
extension OuterForUFI.Inner {
public func extMethod() {} // The 'public' puts this in a special path.
}
func testLookup(_ x: OuterForUFI.Inner) {
x.req()
x.extMethod()
}
func testLookup<T: OuterForUFI.Inner>(_ x: T) {
x.req()
x.extMethod()
}
// Protocols cannot be nested inside of generic functions.
func invalidProtocolInGeneric<T>(_: T) {
protocol Test {} // expected-error{{type 'Test' cannot be nested in generic function 'invalidProtocolInGeneric'}}
}
struct NestedInGenericMethod<T> {
func someMethod() {
protocol AnotherTest {} // expected-error{{type 'AnotherTest' cannot be nested in generic function 'someMethod()'}}
}
}
// Types cannot be nested inside of protocols.
protocol Racoon {
associatedtype Stripes
class Claw<T> { // expected-error{{type 'Claw' cannot be nested in protocol 'Racoon'}}
func mangle(_ s: Stripes) {}
// expected-error@-1 {{cannot find type 'Stripes' in scope}}
}
struct Fang<T> { // expected-error{{type 'Fang' cannot be nested in protocol 'Racoon'}}
func gnaw(_ s: Stripes) {}
// expected-error@-1 {{cannot find type 'Stripes' in scope}}
}
enum Fur { // expected-error{{type 'Fur' cannot be nested in protocol 'Racoon'}}
case Stripes
}
}
enum SillyRawEnum : SillyProtocol.InnerClass {} // expected-error {{an enum with no cases cannot declare a raw type}}
// expected-error@-1 {{reference to generic type 'SillyProtocol.InnerClass' requires arguments in <...>}}
protocol SillyProtocol {
class InnerClass<T> {} // expected-error {{type 'InnerClass' cannot be nested in protocol 'SillyProtocol'}}
// expected-note@-1 {{generic type 'InnerClass' declared here}}
}
protocol SelfDotTest {
func f(_: Self.Class)
class Class {}
// expected-error@-1{{type 'Class' cannot be nested in protocol 'SelfDotTest'}}
}
struct Outer {
typealias E = NestedValidation.T
protocol NestedValidation {
typealias T = A.B
class A { // expected-error {{type 'A' cannot be nested in protocol 'NestedValidation'}}
typealias B = Int
}
}
}
|