Files
swift-mirror/test/decl/subscript/static.swift
Brent Royal-Gordon c37fee1719 Add parallel tests for static subscripts
This commit modifies various subscript-related test files to add static subscript equivalents of existing tests.
2019-04-10 23:17:04 -07:00

46 lines
1.3 KiB
Swift

// RUN: %target-typecheck-verify-swift
struct MyStruct {
static subscript(_ i: Int) -> String {
get { return "get \(i)" }
set { print("set \(i) = \(newValue)") }
}
}
func useMyStruct() {
print(MyStruct.self[0])
print(MyStruct[0])
MyStruct.self[1] = "zyzyx"
MyStruct[2] = "asdfg"
MyStruct()[0] // expected-error {{static member 'subscript' cannot be used on instance of type 'MyStruct'}}
MyStruct()[1] = "zyzyx" // expected-error {{static member 'subscript' cannot be used on instance of type 'MyStruct'}}
}
struct BadStruct {
static subscript(_ i: Int) -> String {
nonmutating get { fatalError() } // expected-error{{static functions must not be declared mutating}}
mutating set { fatalError() } // expected-error{{static functions must not be declared mutating}}
}
}
@dynamicMemberLookup
class Dyn {
static subscript(dynamicMember name: String) -> String {
return "Dyn.\(name)"
}
}
func useDyn() {
_ = Dyn.foo
_ = Dyn().bar // expected-error{{static member 'bar' cannot be used on instance of type 'Dyn'}}
}
class BadBase {
static subscript(_ i: Int) -> String { return "Base" } // expected-note{{overridden declaration is here}}
}
class BadDerived: BadBase {
override static subscript(_ i: Int) -> String { return "DerivedGood" } // expected-error{{cannot override static subscript}}
}