forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubscripting.swift
167 lines (146 loc) · 3 KB
/
subscripting.swift
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
// RUN: %target-parse-verify-swift
struct X { }
// Simple examples
struct X1 {
var stored: Int
subscript(i: Int) -> Int {
get {
return stored
}
mutating
set {
stored = newValue
}
}
}
struct X2 {
var stored: Int
subscript(i: Int) -> Int {
get {
return stored + i
}
set(v) {
stored = v - i
}
}
}
struct X3 {
var stored: Int
subscript(_: Int) -> Int {
get {
return stored
}
set(v) {
stored = v
}
}
}
struct X4 {
var stored: Int
subscript(i: Int, j: Int) -> Int {
get {
return stored + i + j
}
mutating
set(v) {
stored = v + i - j
}
}
}
struct Y1 {
var stored: Int
// FIXME: diagnostic spew is horrible here
subscript(_: i, j: Int) -> Int { // expected-error 3{{use of undeclared type 'i'}}
get {
return stored + j
}
set {
stored = j
}
}
}
// Parsing errors
// FIXME: Recovery here is horrible
struct A0 {
subscript // expected-error{{expected '(' for subscript parameters}}
i : Int // expected-error{{expected declaration}}
-> Int {
get {
return stored
}
set {
stored = value
}
}
}
struct A1 {
subscript (i : Int) // expected-error{{expected '->' for subscript element type}}
Int { // expected-error{{expected declaration}}
get {
return stored
}
set {
stored = value
}
}
}
struct A2 {
subscript (i : Int) -> // expected-error{{expected subscripting element type}}
{ // expected-error{{expected declaration}}
get {
return stored
}
set {
stored = value
}
}
}
struct A3 {
subscript(i : Int) // expected-error {{expected '->' for subscript element type}}
{ // expected-error {{expected declaration}}
get {
return i
}
}
}
struct A4 {
subscript(i : Int) { // expected-error {{expected '->' for subscript element type}} expected-error {{consecutive declarations on a line must be separated by ';'}} {{21-21=;}} expected-error {{expected declaration}}
get {
return i
}
}
}
struct A5 {
subscript(i : Int) -> Int // expected-error {{expected '{' for subscripting}}
}
struct A6 {
subscript(i: Int)(j: Int) -> Int { // expected-error {{expected '->' for subscript element type}} expected-error {{consecutive declarations on a line must be separated by ';'}} {{20-20=;}} expected-error {{expected declaration}}
get {
return i + j
}
}
}
struct A7 {
static subscript(a: Int) -> Int { // expected-error {{subscript cannot be marked 'static'}} {{3-10=}}
get {
return 42
}
}
}
struct A7b {
class subscript(a: Float) -> Int { // expected-error {{subscript cannot be marked 'class'}} {{3-9=}}
get {
return 42
}
}
}
struct A8 {
subscript(i : Int) -> Int // expected-error{{expected '{' for subscripting}}
get { // expected-error{{expected declaration}}
return stored
}
set {
stored = value
}
}
} // expected-error{{extraneous '}' at top level}} {{1-3=}}