Skip to content

Commit c37fee1

Browse files
committed
Add parallel tests for static subscripts
This commit modifies various subscript-related test files to add static subscript equivalents of existing tests.
1 parent eb0d343 commit c37fee1

File tree

10 files changed

+485
-34
lines changed

10 files changed

+485
-34
lines changed

test/Constraints/subscript.swift

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,39 @@ func existentialSubscript(_ a: IntToStringSubscript) -> String {
3333
return a[17]
3434
}
3535

36+
// Static of above:
37+
38+
// Subscript of archetype.
39+
protocol IntToStringStaticSubscript {
40+
static subscript (i : Int) -> String { get }
41+
}
42+
43+
class LameStaticDictionary {
44+
static subscript (i : Int) -> String {
45+
get {
46+
return String(i)
47+
}
48+
}
49+
}
50+
51+
func archetypeStaticSubscript<
52+
T : IntToStringStaticSubscript, U : LameStaticDictionary
53+
>(_ t: T.Type, u: U.Type) -> String {
54+
// Subscript an archetype.
55+
if false { return t[17] }
56+
57+
// Subscript an archetype for which the subscript operator is in a base class.
58+
return u[17]
59+
}
60+
61+
// Subscript of existential type.
62+
func existentialStaticSubscript(
63+
_ a: IntToStringStaticSubscript.Type
64+
) -> String {
65+
return a[17]
66+
}
67+
68+
3669
class MyDictionary<Key, Value> {
3770
subscript (key : Key) -> Value {
3871
get {}
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
//===--- generic_subscript.swift ------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
// RUN: %target-run-simple-swift
13+
// REQUIRES: executable_test
14+
//
15+
16+
import StdlibUnittest
17+
18+
19+
var GenericSubscriptTestSuite = TestSuite("GenericSubscriptStatic")
20+
21+
var ts: [ObjectIdentifier: Any] = [:]
22+
23+
struct S<T> : P {
24+
typealias Element = T
25+
static var t: T {
26+
get {
27+
return ts[ObjectIdentifier(self)] as! T
28+
}
29+
set {
30+
ts[ObjectIdentifier(self)] = newValue
31+
}
32+
}
33+
34+
static subscript<U>(a: (T) -> U, b: (U) -> T) -> U {
35+
get {
36+
print(T.self)
37+
print(U.self)
38+
39+
return a(t)
40+
}
41+
set {
42+
print(T.self)
43+
print(U.self)
44+
45+
t = b(newValue)
46+
}
47+
}
48+
}
49+
50+
protocol P {
51+
associatedtype Element
52+
static subscript<U>(a: (Element) -> U, b: (U) -> Element) -> U { get set }
53+
}
54+
55+
func increment<T : P>(p: T.Type) where T.Element == String {
56+
p[{Int($0)!}, {String($0)}] += 1
57+
}
58+
59+
GenericSubscriptTestSuite.test("Basic") {
60+
var s = S<String>.self
61+
s.t = "0"
62+
increment(p: s)
63+
expectEqual(s.t, "1")
64+
}
65+
66+
protocol AnySubscript {
67+
static subscript(k: AnyHashable) -> Any? { get set }
68+
}
69+
70+
struct AnyDictionary : AnySubscript {
71+
static var dict: [AnyHashable : Any] = [:]
72+
73+
static subscript(k: AnyHashable) -> Any? {
74+
get {
75+
return dict[k]
76+
}
77+
set {
78+
dict[k] = newValue
79+
}
80+
}
81+
}
82+
83+
extension AnySubscript {
84+
static subscript<K : Hashable, V>(k k: K) -> V? {
85+
get {
86+
return self[k] as! V?
87+
}
88+
set {
89+
self[k] = newValue
90+
}
91+
}
92+
}
93+
94+
GenericSubscriptTestSuite.test("ProtocolExtensionConcrete") {
95+
var dict = AnyDictionary.self
96+
97+
func doIt(dict: AnyDictionary.Type) {
98+
dict["a" ] = 0
99+
dict[k: "a"]! += 1
100+
}
101+
102+
doIt(dict: dict)
103+
104+
expectEqual(dict["a"]! as! Int, 1)
105+
expectEqual(dict[k: "a"]!, 1)
106+
}
107+
108+
GenericSubscriptTestSuite.test("ProtocolExtensionAbstract") {
109+
var dict = AnyDictionary.self
110+
111+
func doIt<T : AnySubscript>(dict: T.Type) {
112+
dict["a" ] = 0
113+
dict[k: "a"]! += 1
114+
}
115+
116+
doIt(dict: dict)
117+
118+
expectEqual(dict["a"]! as! Int, 1)
119+
expectEqual(dict[k: "a"]!, 1)
120+
}
121+
122+
protocol GenericSubscript : AnySubscript {
123+
static subscript<K : Hashable, V>(k k: K) -> V? { get set }
124+
}
125+
126+
extension AnyDictionary : GenericSubscript { }
127+
128+
GenericSubscriptTestSuite.test("ProtocolExtensionWitness") {
129+
var dict = AnyDictionary.self
130+
131+
func doIt<T : GenericSubscript>(dict: T.Type) {
132+
dict["a" ] = 0
133+
dict[k: "a"]! += 1
134+
}
135+
136+
doIt(dict: dict)
137+
138+
expectEqual(dict["a"]! as! Int, 1)
139+
expectEqual(dict[k: "a"]!, 1)
140+
}
141+
142+
class EchoBase<T: SignedNumeric> {
143+
// In EchoDerived, subscript(a:) will be overridden.
144+
class subscript(a a: T) -> String {
145+
get {
146+
return "EchoBase.Type.subscript(a: \(a))"
147+
}
148+
}
149+
150+
// In EchoDerived, subscript(b:) will be overridden with a super call.
151+
class subscript(b b: T) -> String {
152+
get {
153+
return "EchoBase.Type.subscript(b: \(b))"
154+
}
155+
}
156+
157+
// In EchoDerived, subscript(c:) will not be overridden.
158+
class subscript(c c: T) -> String {
159+
get {
160+
return "EchoBase.Type.subscript(c: \(c))"
161+
}
162+
}
163+
}
164+
165+
class EchoDerived<T: SignedNumeric>: EchoBase<T> {
166+
override class subscript(a a: T) -> String {
167+
get {
168+
return "EchoDerived.Type.subscript(a: \(a))"
169+
}
170+
}
171+
172+
override class subscript(b b: T) -> String {
173+
get {
174+
return "\(super[b: -b]) EchoDerived.Type.subscript(b: \(b))"
175+
}
176+
}
177+
178+
class subscript(d d: T) -> String {
179+
return "EchoDerived.Type.subscript(d: \(d))"
180+
}
181+
}
182+
183+
GenericSubscriptTestSuite.test("Overrides") {
184+
let base = EchoBase<Int>.self
185+
let derived = EchoDerived<Int>.self
186+
187+
expectEqual(base[a: 42], "EchoBase.Type.subscript(a: 42)")
188+
expectEqual(base[b: 42], "EchoBase.Type.subscript(b: 42)")
189+
expectEqual(base[c: 42], "EchoBase.Type.subscript(c: 42)")
190+
191+
expectEqual(derived[a: 42], "EchoDerived.Type.subscript(a: 42)")
192+
expectEqual(derived[b: 42], "EchoBase.Type.subscript(b: -42) EchoDerived.Type.subscript(b: 42)")
193+
expectEqual(derived[c: 42], "EchoBase.Type.subscript(c: 42)")
194+
expectEqual(derived[d: 42], "EchoDerived.Type.subscript(d: 42)")
195+
}
196+
197+
runAllTests()

test/Interpreter/subscripting.swift

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,28 @@
44
// Check that subscripts and functions named subscript can exist side-by-side
55
struct Foo {
66
subscript() -> String {
7-
return "subscript"
7+
return "instance subscript"
88
}
99

1010
func `subscript`() -> String {
11-
return "func"
11+
return "instance func"
12+
}
13+
14+
static subscript() -> String {
15+
return "static subscript"
16+
}
17+
18+
static func `subscript`() -> String {
19+
return "static func"
1220
}
1321
}
1422

1523
let f = Foo()
16-
print(f[]) // CHECK: subscript
17-
print(f.subscript()) // CHECK: func
24+
print(f[]) // CHECK: instance subscript
25+
print(f.subscript()) // CHECK: instance func
26+
print(Foo[]) // CHECK: static subscript
27+
print(Foo.subscript()) // CHECK: static func
28+
1829

1930
// SR-7418
2031

@@ -40,3 +51,19 @@ func foo<T : P>(_ t: inout T) {
4051

4152
var q = Q()
4253
foo(&q) // CHECK: I survived
54+
55+
protocol PStatic {
56+
static subscript<T : Y>(_: T) -> Int { get set }
57+
}
58+
59+
struct QStatic : PStatic {
60+
static subscript<T : X>(_ idx: T) -> Int {
61+
get { return 0 } set { idx.foo() }
62+
}
63+
}
64+
func fooStatic<T : PStatic>(_ t: T.Type) {
65+
t[Idx()] += 1
66+
}
67+
68+
fooStatic(QStatic.self) // CHECK: I survived
69+

test/Parse/subscripting.swift

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,32 @@ struct X4 {
5757
}
5858
}
5959

60+
struct X5 {
61+
static var stored: Int = 1
62+
63+
static subscript(i: Int) -> Int {
64+
get {
65+
return stored + i
66+
}
67+
set {
68+
stored = newValue - i
69+
}
70+
}
71+
}
72+
73+
class X6 {
74+
static var stored: Int = 1
75+
76+
class subscript(i: Int) -> Int {
77+
get {
78+
return stored + i
79+
}
80+
set {
81+
stored = newValue - i
82+
}
83+
}
84+
}
85+
6086
struct Y1 {
6187
var stored: Int
6288
subscript(_: i, j: Int) -> Int { // expected-error {{use of undeclared type 'i'}}
@@ -155,20 +181,15 @@ struct A6 {
155181
}
156182

157183
struct A7 {
158-
static subscript(a: Int) -> Int {
184+
class subscript(a: Float) -> Int { // expected-error {{class subscripts are only allowed within classes; use 'static' to declare a static subscript}} {{3-8=static}}
159185
get {
160186
return 42
161187
}
162188
}
163189
}
164190

165-
struct A7b {
166-
class subscript(a: Float) -> Int { // expected-error {{class subscripts are only allowed within classes; use 'static' to declare a static subscript}} {{3-8=static}}
167-
get {
168-
return 42
169-
}
170-
}
171-
static subscript(x a: Float) -> Int {
191+
class A7b {
192+
class static subscript(a: Float) -> Int { // expected-error {{'static' specified twice}} {{9-16=}}
172193
get {
173194
return 42
174195
}

0 commit comments

Comments
 (0)