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
|
// RUN: %target-typecheck-verify-swift
struct X { } // expected-note {{did you mean}}
// 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 X5 {
static var stored: Int = 1
static subscript(i: Int) -> Int {
get {
return stored + i
}
set {
stored = newValue - i
}
}
}
class X6 {
static var stored: Int = 1
class subscript(i: Int) -> Int {
get {
return stored + i
}
set {
stored = newValue - i
}
}
}
struct Y1 {
var stored: Int
subscript(_: i, j: Int) -> Int { // expected-error {{cannot find type 'i' in scope}}
get {
return stored + j
}
set {
stored = j
}
}
}
// Mutating getters on constants
// https://github.com/apple/swift/issues/43457
struct Y2 {
subscript(_: Int) -> Int {
mutating get { return 0 }
}
}
// FIXME: This test case does not belong in Parse/
let y2 = Y2() // expected-note{{change 'let' to 'var' to make it mutable}}{{1-4=var}}
_ = y2[0] // expected-error{{cannot use mutating getter on immutable value: 'y2' is a 'let' constant}}
// Parsing errors
struct A0 {
subscript // expected-error {{expected '(' for subscript parameters}}
i : Int
-> Int {
get {
return stored
}
set {
stored = value
}
}
subscript -> Int { // expected-error {{expected '(' for subscript parameters}} {{12-12=()}}
return 1
}
}
struct A1 {
subscript (i : Int) // expected-error{{expected '->' for subscript element type}}
Int {
get {
return stored // expected-error{{cannot find 'stored' in scope}}
}
set {
stored = newValue// expected-error{{cannot find 'stored' in scope}}
}
}
}
struct A2 {
subscript (i : Int) -> // expected-error{{expected subscripting element type}}
{
get {
return stored
}
set {
stored = newValue // expected-error{{cannot find 'stored' in scope}}
}
}
}
struct A3 {
subscript(i : Int) // expected-error {{expected '->' for subscript element type}}
// expected-error@-1 {{expected subscripting element type}}
{
get {
return i
}
}
}
struct A4 {
subscript(i : Int) { // expected-error {{expected '->' for subscript element type}}
// expected-error@-1 {{expected subscripting element type}}
get {
return i
}
}
}
struct A5 {
subscript(i : Int) -> Int // expected-error {{expected '{' in subscript to specify getter and setter implementation}}
}
struct A6 {
subscript(i: Int)(j: Int) -> Int { // expected-error {{expected '->' for subscript element type}}
// expected-error@-1 {{function types cannot have argument labels}}
// expected-note@-2 {{did you mean}}
get {
return i + j // expected-error {{cannot find 'j' in scope}}
// expected-error@-1 {{cannot convert return expression of type 'Int' to return type '(Int) -> Int'}}
}
}
}
struct A7 {
class subscript(a: Float) -> Int { // expected-error {{class subscripts are only allowed within classes; use 'static' to declare a static subscript}} {{3-8=static}}
get {
return 42
}
}
}
class A7b {
class static subscript(a: Float) -> Int { // expected-error {{'static' cannot appear after another 'static' or 'class' keyword}} {{9-16=}}
get {
return 42
}
}
}
struct A8 {
subscript(i : Int) -> Int // expected-error{{expected '{' in subscript to specify getter and setter implementation}}
get {
return stored
}
set {
stored = value
}
}
struct A9 {
subscript x() -> Int { // expected-error {{subscripts cannot have a name}} {{13-14=}}
return 0
}
}
struct A10 {
subscript x(i: Int) -> Int { // expected-error {{subscripts cannot have a name}} {{13-14=}}
return 0
}
subscript x<T>(i: T) -> Int { // expected-error {{subscripts cannot have a name}} {{13-14=}}
return 0
}
}
struct A11 {
subscript x y : Int -> Int { // expected-error {{expected '(' for subscript parameters}}
return 0
}
}
} // expected-error{{extraneous '}' at top level}} {{1-3=}}
|