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 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
|
// RUN: %target-typecheck-verify-swift -swift-version 5
struct ImplicitMembers: Equatable {
struct Other {
var implicit: ImplicitMembers { ImplicitMembers() }
}
static var other = Other()
static func createOther() -> Other {
Other()
}
var anotherOther: Other { Other() }
func getAnotherOther() -> Other {
Other()
}
static var implicit = ImplicitMembers()
static let implicitLet = ImplicitMembers() // expected-note2 {{change 'let' to 'var' to make it mutable}}
static var implicitImmutable: ImplicitMembers { ImplicitMembers() }
static func createImplicit() -> ImplicitMembers {
ImplicitMembers()
}
static var optional: ImplicitMembers? = ImplicitMembers()
static func createOptional() -> ImplicitMembers? {
ImplicitMembers()
}
static var superOptional: ImplicitMembers??? = ImplicitMembers()
static func createIUOArg(_: Int) -> ImplicitMembers { ImplicitMembers() }
var anotherIUO: ImplicitMembers! { ImplicitMembers() }
func getAnotherIUO() -> ImplicitMembers! { ImplicitMembers() }
var another: ImplicitMembers { ImplicitMembers() }
var anotherMutable: ImplicitMembers {
get { ImplicitMembers() }
set {}
}
func getAnother() -> ImplicitMembers {
ImplicitMembers()
}
func getAnother(arg: Int) -> ImplicitMembers {
ImplicitMembers()
}
var anotherOptional: ImplicitMembers? { ImplicitMembers() }
var anotherOptionalMutable: ImplicitMembers? {
get { ImplicitMembers() }
set {}
}
func getAnotherOptional() -> ImplicitMembers? {
ImplicitMembers()
}
func getAnotherOptional(arg: Int) -> ImplicitMembers? {
ImplicitMembers()
}
static func takesClosure(_: (Int) -> Void) -> ImplicitMembers { ImplicitMembers() }
static func takesArgClosure(_: Int, _: (Int) -> Void) -> ImplicitMembers { ImplicitMembers() }
func methodTakesClosure(_: (Int) -> Void) -> ImplicitMembers { ImplicitMembers() }
func methodTakesArgClosure(_: Int, _: (Int) -> Void) -> ImplicitMembers { ImplicitMembers() }
subscript(arg: Void) -> ImplicitMembers {
get { ImplicitMembers() }
set {}
}
subscript(optional arg: Void) -> ImplicitMembers? {
get { ImplicitMembers() }
set {}
}
subscript(immutable arg: Void) -> ImplicitMembers { ImplicitMembers() }
subscript(func arg: Void) -> (() -> ImplicitMembers) { { ImplicitMembers() } }
subscript(funcOptional arg: Void) -> (() -> ImplicitMembers?) { { ImplicitMembers() } }
subscript(optionalFunc arg: Void) -> (() -> ImplicitMembers)? { { ImplicitMembers() } }
subscript(other arg: Void) -> Other { Other() }
}
let _: ImplicitMembers = .implicit
let _: ImplicitMembers? = .implicit
let _: ImplicitMembers? = .optional
let _: ImplicitMembers = .implicit.another.another
let _: ImplicitMembers = .createImplicit().another.another
let _: ImplicitMembers = .init().another.another
let _: ImplicitMembers = .implicit.getAnother().another
let _: ImplicitMembers = .createImplicit().getAnother().another
let _: ImplicitMembers = .init().getAnother().another
let _: ImplicitMembers = .implicit.getAnother(arg: 0).another
let _: ImplicitMembers = .createImplicit().getAnother(arg: 0).another
let _: ImplicitMembers = .init().getAnother(arg: 0).another
let _: ImplicitMembers = .implicit.another.getAnother()
let _: ImplicitMembers = .createImplicit().another.getAnother()
let _: ImplicitMembers = .init().another.getAnother()
let _: ImplicitMembers = .implicit.another.getAnother(arg: 0)
let _: ImplicitMembers = .createImplicit().another.getAnother(arg: 0)
let _: ImplicitMembers = .init().another.getAnother(arg: 0)
let _: ImplicitMembers = .implicit.getAnother().getAnother(arg: 0)
let _: ImplicitMembers = .createImplicit().getAnother().getAnother(arg: 0)
let _: ImplicitMembers = .init().getAnother().getAnother(arg: 0)
let _: ImplicitMembers = .implicit.getAnother().getAnother(arg: 0).another
let _: ImplicitMembers = .createImplicit().getAnother().getAnother(arg: 0).another
let _: ImplicitMembers = .init().getAnother().getAnother(arg: 0).another
let _: ImplicitMembers = .implicit.another.getAnother().getAnother(arg: 0)
let _: ImplicitMembers = .createImplicit().another.getAnother().getAnother(arg: 0)
let _: ImplicitMembers = .init().another.getAnother().getAnother(arg: 0)
let _: ImplicitMembers = .implicit.another.another.another.another.another
let _: ImplicitMembers = .implicit.getAnother().getAnother().getAnother().getAnother().getAnother()
let _: ImplicitMembers = .implicit.getAnother(arg: 0).getAnother(arg: 0).getAnother(arg: 0).getAnother(arg: 0).getAnother(arg: 0)
let _: ImplicitMembers = .implicit.another.getAnother().getAnother(arg: 0).anotherIUO
let _: ImplicitMembers = .createImplicit().another.getAnother().getAnother(arg: 0).anotherIUO
let _: ImplicitMembers = .init().another.getAnother().getAnother(arg: 0).anotherIUO
let _: ImplicitMembers = .implicit.another.getAnother().getAnother(arg: 0).getAnotherIUO()
let _: ImplicitMembers = .createImplicit().another.getAnother().getAnother(arg: 0).getAnotherIUO()
let _: ImplicitMembers = .init().another.getAnother().getAnother(arg: 0).getAnotherIUO()
let _: ImplicitMembers = .createIUOArg(_:)(0)
let _: ImplicitMembers = .optional!
let _: ImplicitMembers = .optional!.another
let _: ImplicitMembers = .createOptional()!.another
let _: ImplicitMembers = .optional!.anotherOptional!
let _: ImplicitMembers = .createOptional()!.anotherOptional!
let _: ImplicitMembers = .optional!.getAnotherOptional()!
let _: ImplicitMembers = .createOptional()!.getAnotherOptional()!
let _: ImplicitMembers = .implicit.getAnotherIUO()
let _: ImplicitMembers = .createImplicit().anotherIUO
let _: ImplicitMembers = .implicit.anotherIUO
let _: ImplicitMembers = .createImplicit().anotherIUO
let _: ImplicitMembers = .optional // expected-error {{value of optional type 'ImplicitMembers?' must be unwrapped to a value of type 'ImplicitMembers'}} expected-note {{coalesce using '??' to provide a default when the optional value contains 'nil'}} {{35-35= ?? <#default value#>}} expected-note {{force-unwrap using '!' to abort execution if the optional value contains 'nil'}} {{35-35=!}}
let _: ImplicitMembers = .implicit.anotherOptional // expected-error {{value of optional type 'ImplicitMembers?' must be unwrapped to a value of type 'ImplicitMembers'}} expected-note {{coalesce using '??' to provide a default when the optional value contains 'nil'}} {{51-51= ?? <#default value#>}} expected-note {{force-unwrap using '!' to abort execution if the optional value contains 'nil'}} {{51-51=!}}
let _: ImplicitMembers = .createOptional() // expected-error {{value of optional type 'ImplicitMembers?' must be unwrapped to a value of type 'ImplicitMembers'}} expected-note {{coalesce using '??' to provide a default when the optional value contains 'nil'}} {{43-43= ?? <#default value#>}} expected-note {{force-unwrap using '!' to abort execution if the optional value contains 'nil'}} {{43-43=!}}
let _: ImplicitMembers = .implicit.getAnotherOptional() // expected-error {{value of optional type 'ImplicitMembers?' must be unwrapped to a value of type 'ImplicitMembers'}} expected-note {{coalesce using '??' to provide a default when the optional value contains 'nil'}} {{56-56= ?? <#default value#>}} expected-note {{force-unwrap using '!' to abort execution if the optional value contains 'nil'}} {{56-56=!}}
let _: ImplicitMembers = .implicit[optional: ()] // expected-error {{value of optional type 'ImplicitMembers?' must be unwrapped to a value of type 'ImplicitMembers'}} expected-note {{coalesce using '??' to provide a default when the optional value contains 'nil'}} {{49-49= ?? <#default value#>}} expected-note {{force-unwrap using '!' to abort execution if the optional value contains 'nil'}} {{49-49=!}}
let _: ImplicitMembers = .implicit[funcOptional: ()]() // expected-error {{value of optional type 'ImplicitMembers?' must be unwrapped to a value of type 'ImplicitMembers'}} expected-note {{coalesce using '??' to provide a default when the optional value contains 'nil'}} {{55-55= ?? <#default value#>}} expected-note {{force-unwrap using '!' to abort execution if the optional value contains 'nil'}} {{55-55=!}}
let _: ImplicitMembers = .implicit.anotherOptional?.another // expected-error {{value of optional type 'ImplicitMembers?' must be unwrapped to a value of type 'ImplicitMembers'}}
// expected-note@-1 {{coalesce using '??' to provide a default when the optional value contains 'nil'}} {{60-60= ?? <#default value#>}}
// expected-note@-2 {{force-unwrap using '!' to abort execution if the optional value contains 'nil'}} {{26-26=(}} {{60-60=)!}}
let _: ImplicitMembers = .implicit[optionalFunc: ()]?() // expected-error{{value of optional type 'ImplicitMembers?' must be unwrapped to a value of type 'ImplicitMembers'}}
// expected-note@-1 {{coalesce using '??' to provide a default when the optional value contains 'nil'}} {{56-56= ?? <#default value#>}}
// expected-note@-2 {{force-unwrap using '!' to abort execution if the optional value contains 'nil'}} {{26-26=(}} {{56-56=)!}}
let _: ImplicitMembers = .other.implicit
let _: ImplicitMembers = .implicit.anotherOther.implicit
let _: ImplicitMembers = .createOther().implicit
let _: ImplicitMembers = .implicit.getAnotherOther().implicit
let _: ImplicitMembers = .implicit[other: ()].implicit
let _: ImplicitMembers = .other // expected-error {{member 'other' in 'ImplicitMembers' produces result of type 'ImplicitMembers.Other', but context expects 'ImplicitMembers'}}
let _: ImplicitMembers = .implicit.anotherOther // expected-error {{member 'anotherOther' in 'ImplicitMembers' produces result of type 'ImplicitMembers.Other', but context expects 'ImplicitMembers'}}
let _: ImplicitMembers = .implicit.anotherOther.nonDeclaredMember // expected-error {{value of type 'ImplicitMembers.Other' has no member 'nonDeclaredMember'}}
let _: ImplicitMembers = .implicit.anotherOther.nonDeclaredMethod() // expected-error {{value of type 'ImplicitMembers.Other' has no member 'nonDeclaredMethod'}}
let _: ImplicitMembers = .implicit.anotherOther.nonDeclaredMember.another // expected-error {{value of type 'ImplicitMembers.Other' has no member 'nonDeclaredMember'}}
let _: ImplicitMembers = .implicit.anotherOther.nonDeclaredMethod().another // expected-error {{value of type 'ImplicitMembers.Other' has no member 'nonDeclaredMethod'}}
let _: ImplicitMembers = .implicit.getAnotherOther() // expected-error {{member 'getAnotherOther()' in 'ImplicitMembers' produces result of type 'ImplicitMembers.Other', but context expects 'ImplicitMembers'}}
let _: ImplicitMembers = .implicit[other: ()] // expected-error {{member 'subscript(other:)' in 'ImplicitMembers' produces result of type 'ImplicitMembers.Other', but context expects 'ImplicitMembers'}}
let _: ImplicitMembers? = .implicit.another
let _: ImplicitMembers? = .implicit.anotherOptional
let _: ImplicitMembers? = .optional
let _: ImplicitMembers? = .optional?.another
let _: ImplicitMembers? = .optional?.anotherOptional
let _: ImplicitMembers? = .optional?.getAnother()
let _: ImplicitMembers? = .optional?.getAnotherOptional()
let _: ImplicitMembers? = .optional?.anotherOptional?.another
let _: ImplicitMembers? = .optional?.getAnotherOptional()?.another
let _: ImplicitMembers? = .createOptional()
let _: ImplicitMembers? = .createOptional()?.another
let _: ImplicitMembers? = .createOptional()?.anotherOptional
let _: ImplicitMembers? = .createOptional()?.getAnother()
let _: ImplicitMembers? = .createOptional()?.getAnotherOptional()
let _: ImplicitMembers? = .createOptional()?.anotherOptional?.another
let _: ImplicitMembers? = .createOptional()?.getAnotherOptional()?.another
let _: ImplicitMembers? = .createOptional()?.getAnotherOptional()?.anotherIUO
let _: ImplicitMembers? = .createOptional()?.getAnotherOptional()?.getAnotherIUO()
// FIXME: This should be allowed
// let _: ImplicitMembers? = .superOptional???.another
let _: ImplicitMembers = .takesClosure { _ in }
let _: ImplicitMembers = .takesArgClosure(0) { _ in }
let _: ImplicitMembers = .implicit.methodTakesClosure { _ in }
let _: ImplicitMembers = .implicit.methodTakesArgClosure(0) { _ in }
let _: ImplicitMembers? = .optional?.methodTakesClosure { _ in }
let _: ImplicitMembers? = .optional?.methodTakesArgClosure(0) { _ in }
let _: ImplicitMembers = .implicit[()]
let _: ImplicitMembers = .implicit[optional: ()]!
let _: ImplicitMembers? = .implicit[optional: ()]
let _: ImplicitMembers = .implicit[func: ()]()
let _: ImplicitMembers = .implicit[funcOptional: ()]()!
let _: ImplicitMembers? = .implicit[funcOptional: ()]()
let _: ImplicitMembers = .implicit[optionalFunc: ()]!()
let _: ImplicitMembers? = .implicit[optionalFunc: ()]?()
let _: ImplicitMembers = .implicit.another[()]
let _: ImplicitMembers = .implicit.another[optional: ()]!
let _: ImplicitMembers? = .implicit.another[optional: ()]
let _: ImplicitMembers = .implicit.another[func: ()]()
let _: ImplicitMembers = .implicit.another[funcOptional: ()]()!
let _: ImplicitMembers? = .implicit.another[funcOptional: ()]()
let _: ImplicitMembers = .implicit.another[optionalFunc: ()]!()
let _: ImplicitMembers? = .implicit.another[optionalFunc: ()]?()
let _: ImplicitMembers = .implicit[()].another
let _: ImplicitMembers = .implicit[optional: ()]!.another
let _: ImplicitMembers? = .implicit[optional: ()]?.another
let _: ImplicitMembers = .implicit[func: ()]().another
let _: ImplicitMembers = .implicit[funcOptional: ()]()!.another
let _: ImplicitMembers? = .implicit[funcOptional: ()]()?.another
let _: ImplicitMembers = .implicit[optionalFunc: ()]!().another
let _: ImplicitMembers? = .implicit[optionalFunc: ()]?().another
func implicit(_ i: inout ImplicitMembers) {
if i == .implicit {}
if i == .implicit.another {}
if i == .implicit.getAnother() {}
if i == .optional?.another {}
if i == .optional!.another {}
if i == .createOptional()?.another {}
}
func testLValues() {
let local = ImplicitMembers();
.implicit = local;
.implicit.anotherMutable = local;
.implicit.anotherOptionalMutable? = local;
.implicit.anotherOptionalMutable! = local;
.implicit[()] = local;
.implicit[()].anotherMutable = local;
.optional?[optional: ()]?.anotherOptionalMutable! = local;
.implicitLet = local; // expected-error {{cannot assign to property: 'implicitLet' is a 'let' constant}}
.implicitImmutable = local; // expected-error {{cannot assign to property: 'implicitImmutable' is a get-only property}}
.createImplicit() = local; // expected-error {{expression is not assignable: function call returns immutable value}}
.implicit.another = local; // expected-error {{cannot assign to property: 'another' is a get-only property}}
.implicit[immutable: ()] = local; // expected-error {{cannot assign through subscript: subscript is get-only}}
.implicit.getAnother() = local; // expected-error {{expression is not assignable: function call returns immutable value}}
.implicitLet.anotherMutable = local; // expected-error {{cannot assign to property: 'implicitLet' is a 'let' constant}}
.implicitImmutable.anotherMutable = local; // expected-error {{cannot assign to property: 'implicitImmutable' is a get-only property}}
.createImplicit().anotherMutable = local; // expected-error {{cannot assign to property: function call returns immutable value}}
.implicit.another.anotherMutable = local; // expected-error {{cannot assign to property: 'another' is a get-only property}}
.implicit[immutable: ()].anotherMutable = local; // expected-error {{cannot assign to property: subscript is get-only}}
.implicit.getAnother().anotherMutable = local; // expected-error {{cannot assign to property: function call returns immutable value}}
// FIXME: These should probably be allowed
//.implicit.anotherOptionalMutable = local;
//.optional = local;
}
struct ImplicitGeneric<T> { // expected-note4 {{arguments to generic parameter 'T' ('Int' and 'String') are expected to be equal}}
static var implicit: ImplicitGeneric<T> { ImplicitGeneric<T>() }
var another: ImplicitGeneric<T> { ImplicitGeneric<T>() }
func getAnother() -> ImplicitGeneric<T> {
ImplicitGeneric<T>()
}
}
extension ImplicitGeneric where T == Int {
static var implicitInt: ImplicitGeneric<Int> { ImplicitGeneric<Int>() }
static var implicitString: ImplicitGeneric<String> { ImplicitGeneric<String>() }
var anotherInt: ImplicitGeneric<Int> { ImplicitGeneric<Int>() }
var anotherIntString: ImplicitGeneric<String> { ImplicitGeneric<String>() }
func getAnotherInt() -> ImplicitGeneric<Int> {
ImplicitGeneric<Int>()
}
}
extension ImplicitGeneric where T == String {
static var implicitString: ImplicitGeneric<String> { ImplicitGeneric<String>() }
var anotherString: ImplicitGeneric<String> { ImplicitGeneric<String>() }
var anotherStringInt: ImplicitGeneric<Int> { ImplicitGeneric<Int>() }
func getAnotherString() -> ImplicitGeneric<String> {
ImplicitGeneric<String>()
}
func getAnotherStringInt() -> ImplicitGeneric<Int> {
ImplicitGeneric<Int>()
}
}
func implicit<T>(_ arg: ImplicitGeneric<T>) {}
implicit(.implicitInt)
implicit(.implicit.anotherInt)
implicit(.implicit.anotherInt.another)
implicit(.implicit.another.anotherInt)
implicit(.implicit.getAnotherInt())
implicit(.implicit.another.getAnotherInt())
implicit(.implicit.getAnother().anotherInt)
implicit(.implicit.getAnotherInt())
implicit(.implicit.getAnother().getAnotherInt())
implicit(.implicitString.anotherStringInt)
// Member types along the chain can have different generic arguments
implicit(.implicit.anotherIntString.anotherStringInt)
implicit(.implicit.anotherString.anotherStringInt) // expected-error {{member chain produces result of type 'ImplicitGeneric<Int>' but contextual base was inferred as 'ImplicitGeneric<String>'}}
implicit(.implicit.getAnotherString().anotherStringInt) // expected-error {{member chain produces result of type 'ImplicitGeneric<Int>' but contextual base was inferred as 'ImplicitGeneric<String>'}}
implicit(.implicit.anotherString.getAnotherStringInt()) // expected-error {{member chain produces result of type 'ImplicitGeneric<Int>' but contextual base was inferred as 'ImplicitGeneric<String>'}}
implicit(.implicit.getAnotherString().getAnotherStringInt()) // expected-error {{member chain produces result of type 'ImplicitGeneric<Int>' but contextual base was inferred as 'ImplicitGeneric<String>'}}
// Implicit member syntax can be used to apply curried instance methods:
struct Curried {
func method() -> Curried { Curried() }
func method(with arg: Int) -> Curried { Curried() }
func method(with arg1: Int, and arg2: String) -> Curried { Curried() }
func takesClosure(_: (Int) -> Void) -> Curried { Curried() }
func takesArgClosure(_: Int, _: (Int) -> Void) -> Curried { Curried() }
static func curried(_ _self: Curried) -> () -> Curried{ return { _self } }
static func curriedWithArgs(_ _self: Curried) -> (Int, String) -> Curried { return { _, _ in _self } }
}
let _: Curried = .method(Curried())()
let _: Curried = .method(Curried())(with: 0)
let _: Curried = .method(Curried())(with: 0, and: "")
let _: Curried = .takesClosure(Curried())() { _ in }
let _: Curried = .takesArgClosure(Curried())(0) { _ in }
let _: Curried = .curried(Curried())()
let _: Curried = .curriedWithArgs(Curried())(0, "")
struct CurriedGeneric<T> {
func create<U>(_: U.Type) -> CurriedGeneric<U> { return CurriedGeneric<U>() }
}
extension CurriedGeneric where T == Int {
func createInt() -> Self {
return self
}
}
let _: CurriedGeneric = .createInt(CurriedGeneric())()
let _: CurriedGeneric = .create(CurriedGeneric())(Int.self)
// rdar://problem/68094328 - failed to compile unresolved member with implicit optional promotion
func rdar68094328() {
struct S {
init(string: String) {}
var value: S {
get { S(string: "") }
}
func baz(str: String) -> S {
S(string: str)
}
}
class C {
func bar(_: S) {}
}
func foo<T>(_: (C) -> (T) -> Void, _: T?) {}
func test(str: String) {
foo(C.bar, .init(string: str)) // Ok
foo(C.bar, .init(string: str).value) // Ok
foo(C.bar, .init(string: str).baz(str: "")) // Ok
}
}
|