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 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842
|
// RUN: %target-typecheck-verify-swift
var global = 42
@dynamicMemberLookup
struct Gettable {
subscript(dynamicMember member: StaticString) -> Int {
return 42
}
}
@dynamicMemberLookup
struct Settable {
subscript(dynamicMember member: StaticString) -> Int {
get {return 42}
set {}
}
}
@dynamicMemberLookup
struct MutGettable {
subscript(dynamicMember member: StaticString) -> Int {
mutating get {
return 42
}
}
}
@dynamicMemberLookup
struct NonMutSettable {
subscript(dynamicMember member: StaticString) -> Int {
get { return 42 }
nonmutating set {}
}
}
func test(a: Gettable, b: Settable, c: MutGettable, d: NonMutSettable) {
global = a.wyverns
a.flavor = global // expected-error {{cannot assign through dynamic lookup property: 'a' is a 'let' constant}}
global = b.flavor
b.universal = global // expected-error {{cannot assign through dynamic lookup property: 'b' is a 'let' constant}}
b.thing += 1 // expected-error {{left side of mutating operator isn't mutable: 'b' is a 'let' constant}}
var bm = b
global = bm.flavor
bm.universal = global
bm.thing += 1
var cm = c
global = c.dragons // expected-error {{cannot use mutating getter on immutable value: 'c' is a 'let' constant}}
global = c[dynamicMember: "dragons"] // expected-error {{cannot use mutating getter on immutable value: 'c' is a 'let' constant}}
global = cm.dragons
c.woof = global // expected-error {{cannot use mutating getter on immutable value: 'c' is a 'let' constant}}
var dm = d
global = d.dragons // ok
global = dm.dragons // ok
d.woof = global // ok
dm.woof = global // ok
}
func testIUO(a: Gettable!, b: Settable!) {
global = a.wyverns
a.flavor = global // expected-error {{cannot assign through dynamic lookup property: 'a' is a 'let' constant}}
global = b.flavor
b.universal = global // expected-error {{cannot assign through dynamic lookup property: 'b' is a 'let' constant}}
var bm: Settable! = b
global = bm.flavor
bm.universal = global
}
//===----------------------------------------------------------------------===//
// Returning a function
//===----------------------------------------------------------------------===//
@dynamicMemberLookup
struct FnTest {
subscript(dynamicMember member: StaticString) -> (Int) -> () {
return { x in () }
}
}
func testFunction(x: FnTest) {
x.phunky(12)
}
func testFunctionIUO(x: FnTest!) {
x.flavor(12)
}
//===----------------------------------------------------------------------===//
// Explicitly declared members take precedence
//===----------------------------------------------------------------------===//
@dynamicMemberLookup
struct Dog {
public var name = "Kaylee"
subscript(dynamicMember member: String) -> String {
return "Zoey"
}
}
func testDog(person: Dog) -> String {
return person.name + person.otherName
}
//===----------------------------------------------------------------------===//
// Returning an IUO
//===----------------------------------------------------------------------===//
@dynamicMemberLookup
struct IUOResult {
subscript(dynamicMember member: StaticString) -> Int! {
get { return 42 }
nonmutating set {}
}
}
func testIUOResult(x: IUOResult) {
x.foo?.negate() // Test mutating writeback.
let _: Int = x.bar // Test implicitly forced optional
let b = x.bar
// expected-note@-1{{short-circuit}}
// expected-note@-2{{coalesce}}
// expected-note@-3{{force-unwrap}}
let _: Int = b // expected-error {{value of optional type 'Int?' must be unwrapped to a value of type 'Int'}}
// expected-note@-1{{coalesce}}
// expected-note@-2{{force-unwrap}}
}
//===----------------------------------------------------------------------===//
// Error cases
//===----------------------------------------------------------------------===//
// Subscript index must be ExpressibleByStringLiteral.
@dynamicMemberLookup
struct Invalid1 {
// expected-error @+1 {{@dynamicMemberLookup attribute requires 'Invalid1' to have a 'subscript(dynamicMember:)' method that accepts either 'ExpressibleByStringLiteral' or a key path}}
subscript(dynamicMember member: Int) -> Int {
return 42
}
}
// Subscript may not be variadic.
@dynamicMemberLookup
struct Invalid2 {
// expected-error @+1 {{@dynamicMemberLookup attribute requires 'Invalid2' to have a 'subscript(dynamicMember:)' method that accepts either 'ExpressibleByStringLiteral' or a key path}}
subscript(dynamicMember member: String...) -> Int {
return 42
}
}
// References to overloads are resolved just like normal subscript lookup:
// they are either contextually disambiguated or are invalid.
@dynamicMemberLookup
struct Ambiguity {
subscript(dynamicMember member: String) -> Int {
return 42
}
subscript(dynamicMember member: String) -> Float {
return 42
}
}
func testAmbiguity(a: Ambiguity) {
let _: Int = a.flexibility
let _: Float = a.dynamism
_ = a.dynamism // expected-error {{ambiguous use of 'subscript(dynamicMember:)'}}
}
// expected-error @+1 {{'@dynamicMemberLookup' attribute cannot be applied to this declaration}}
@dynamicMemberLookup
extension Int {
subscript(dynamicMember member: String) -> Int {
fatalError()
}
}
// expected-error @+1 {{'@dynamicMemberLookup' attribute cannot be applied to this declaration}}
@dynamicMemberLookup
func NotAllowedOnFunc() {}
// @dynamicMemberLookup cannot be declared on a base class and fulfilled with a
// derived class.
// expected-error @+1 {{@dynamicMemberLookup attribute requires 'InvalidBase' to have a 'subscript(dynamicMember:)' method that accepts either 'ExpressibleByStringLiteral' or a key path}}
@dynamicMemberLookup
class InvalidBase {}
class InvalidDerived : InvalidBase { subscript(dynamicMember: String) -> Int { get {}} }
// expected-error @+1 {{value of type 'InvalidDerived' has no member 'dynamicallyLookedUp'}}
_ = InvalidDerived().dynamicallyLookedUp
//===----------------------------------------------------------------------===//
// Existentials
//===----------------------------------------------------------------------===//
@dynamicMemberLookup
protocol DynamicProtocol {
subscript(dynamicMember member: StaticString) -> DynamicProtocol {
get nonmutating set
}
}
struct MyDynamicStruct : DynamicProtocol {
subscript(dynamicMember member: StaticString) -> DynamicProtocol {
get { fatalError() }
nonmutating set {}
}
}
func testMutableExistential(a: DynamicProtocol,
b: MyDynamicStruct) -> DynamicProtocol {
a.x.y = b
b.x.y = b
return a.foo.bar.baz
}
// Verify protocol compositions and protocol refinements work.
protocol SubDynamicProtocol : DynamicProtocol {}
typealias ProtocolComp = AnyObject & DynamicProtocol
func testMutableExistential2(a: AnyObject & DynamicProtocol,
b: SubDynamicProtocol,
c: ProtocolComp & AnyObject) {
a.x.y = b
b.x.y = b
c.x.y = b
}
@dynamicMemberLookup
protocol ProtoExt {}
extension ProtoExt {
subscript(dynamicMember member: String) -> String {
get {}
}
}
extension String: ProtoExt {}
func testProtoExt() -> String {
let str = "test"
return str.sdfsdfsdf
}
//===----------------------------------------------------------------------===//
// JSON example
//===----------------------------------------------------------------------===//
@dynamicMemberLookup
enum JSON {
case IntValue(Int)
case StringValue(String)
case ArrayValue(Array<JSON>)
case DictionaryValue(Dictionary<String, JSON>)
var stringValue: String? {
if case .StringValue(let str) = self {
return str
}
return nil
}
subscript(index: Int) -> JSON? {
if case .ArrayValue(let arr) = self {
return index < arr.count ? arr[index] : nil
}
return nil
}
subscript(key: String) -> JSON? {
if case .DictionaryValue(let dict) = self {
return dict[key]
}
return nil
}
subscript(dynamicMember member: String) -> JSON? {
if case .DictionaryValue(let dict) = self {
return dict[member]
}
return nil
}
}
func testJsonExample(x: JSON) -> String? {
_ = x.name?.first
return x.name?.first?.stringValue
}
//===----------------------------------------------------------------------===//
// Class inheritance tests
//===----------------------------------------------------------------------===//
@dynamicMemberLookup
class BaseClass {
subscript(dynamicMember member: String) -> Int {
return 42
}
}
class DerivedClass : BaseClass {}
func testDerivedClass(x: BaseClass, y: DerivedClass) -> Int {
return x.life - y.the + x.universe - y.and + x.everything
}
// Test that derived classes can add a setter.
class DerivedClassWithSetter : BaseClass {
override subscript(dynamicMember member: String) -> Int {
get { return super[dynamicMember: member] }
set {}
}
}
func testOverrideSubscript(a: BaseClass, b: DerivedClassWithSetter) {
let x = a.frotz + b.garbalaz
b.baranozo = x
a.balboza = 12 // expected-error {{cannot assign through dynamic lookup property: 'a' is a 'let' constant}}
}
//===----------------------------------------------------------------------===//
// Generics
//===----------------------------------------------------------------------===//
@dynamicMemberLookup
struct SettableGeneric1<T> {
subscript(dynamicMember member: StaticString) -> T? {
get {}
nonmutating set {}
}
}
func testGenericType<T>(a: SettableGeneric1<T>, b: T) -> T? {
a.dfasdf = b
return a.dfsdffff
}
func testConcreteGenericType(a: SettableGeneric1<Int>) -> Int? {
a.dfasdf = 42
return a.dfsdffff
}
@dynamicMemberLookup
struct SettableGeneric2<T> {
subscript<U: ExpressibleByStringLiteral>(dynamicMember member: U) -> T {
get {}
nonmutating set {}
}
}
func testGenericType2<T>(a: SettableGeneric2<T>, b: T) -> T? {
a[dynamicMember: "fasdf"] = b
a.dfasdf = b
return a.dfsdffff
}
func testConcreteGenericType2(a: SettableGeneric2<Int>) -> Int? {
a.dfasdf = 42
return a.dfsdffff
}
/// https://github.com/apple/swift/issues/50610
/// `subscript(dynamicMember:)` works as a `@dynamicMemberLookup` protocol
/// requirement
@dynamicMemberLookup
protocol GenericProtocol {
associatedtype S: ExpressibleByStringLiteral
associatedtype T
subscript(dynamicMember member: S) -> T { get }
}
@dynamicMemberLookup
class GenericClass<S: ExpressibleByStringLiteral, T> {
let t: T
init(_ t: T) { self.t = t }
subscript(dynamicMember member: S) -> T { return t }
}
func testGenerics<S, T, P: GenericProtocol>(
a: P,
b: AnyObject & GenericClass<S, T>
) where P.S == S, P.T == T {
let _: T = a.wew
let _: T = b.lad
}
//===----------------------------------------------------------------------===//
// Keypaths
//===----------------------------------------------------------------------===//
@dynamicMemberLookup
class KP {
subscript(dynamicMember member: String) -> Int { return 7 }
}
_ = \KP.[dynamicMember: "hi"]
_ = \KP.testLookup
/* KeyPath based dynamic lookup */
struct Point {
var x: Int
let y: Int // expected-note 2 {{change 'let' to 'var' to make it mutable}}
private let z: Int = 0 // expected-note 10 {{declared here}}
}
struct Rectangle {
var topLeft, bottomRight: Point
}
@dynamicMemberLookup
struct Lens<T> {
var obj: T
init(_ obj: T) {
self.obj = obj
}
subscript<U>(dynamicMember member: KeyPath<T, U>) -> Lens<U> {
get { return Lens<U>(obj[keyPath: member]) }
}
subscript<U>(dynamicMember member: WritableKeyPath<T, U>) -> Lens<U> {
get { return Lens<U>(obj[keyPath: member]) }
set { obj[keyPath: member] = newValue.obj }
}
}
var topLeft = Point(x: 0, y: 0)
var bottomRight = Point(x: 10, y: 10)
var lens = Lens(Rectangle(topLeft: topLeft,
bottomRight: bottomRight))
_ = lens.topLeft
_ = lens.topLeft.x
_ = lens.topLeft.y
_ = lens.topLeft.z // expected-error {{'z' is inaccessible due to 'private' protection level}}
_ = lens.bottomRight
_ = lens.bottomRight.x
_ = lens.bottomRight.y
_ = lens.bottomRight.z // expected-error {{'z' is inaccessible due to 'private' protection level}}
_ = \Lens<Point>.x
_ = \Lens<Point>.y
_ = \Lens<Point>.z // expected-error {{'z' is inaccessible due to 'private' protection level}}
_ = \Lens<Rectangle>.topLeft.x
_ = \Lens<Rectangle>.topLeft.y
_ = \Lens<Rectangle>.topLeft.z // expected-error {{'z' is inaccessible due to 'private' protection level}}
_ = \Lens<[Int]>.count
_ = \Lens<[Int]>.[0]
_ = \Lens<[[Int]]>.[0].count
lens.topLeft = Lens(Point(x: 1, y: 2)) // Ok
lens.bottomRight.x = Lens(11) // Ok
lens.bottomRight.y = Lens(12) // expected-error {{cannot assign to property: 'y' is a 'let' constant}}
lens.bottomRight.z = Lens(13) // expected-error {{'z' is inaccessible due to 'private' protection level}}
func acceptKeyPathDynamicLookup(_: Lens<Int>) {}
acceptKeyPathDynamicLookup(lens.topLeft.x)
acceptKeyPathDynamicLookup(lens.topLeft.y)
acceptKeyPathDynamicLookup(lens.topLeft.z) // expected-error {{'z' is inaccessible due to 'private' protection level}}
var tupleLens = Lens<(String, Int)>(("ultimate question", 42))
_ = tupleLens.0.count
_ = tupleLens.1
var namedTupleLens = Lens<(question: String, answer: Int)>((question: "ultimate question", answer: 42))
_ = namedTupleLens.question.count
_ = namedTupleLens.answer
@dynamicMemberLookup
class A<T> {
var value: T
init(_ v: T) {
self.value = v
}
subscript<U>(dynamicMember member: KeyPath<T, U>) -> U {
get { return value[keyPath: member] }
}
}
// Let's make sure that keypath dynamic member lookup
// works with inheritance
class B<T> : A<T> {}
func bar(_ b: B<Point>) {
let _: Int = b.x
let _ = b.y
let _: Float = b.y // expected-error {{cannot convert value of type 'Int' to specified type 'Float'}}
let _ = b.z // expected-error {{'z' is inaccessible due to 'private' protection level}}
}
// Existentials and IUOs
@dynamicMemberLookup
protocol KeyPathLookup {
associatedtype T
var value: T { get }
subscript(dynamicMember member: KeyPath<T, Int>) -> Int! { get }
}
extension KeyPathLookup {
subscript(dynamicMember member: KeyPath<T, Int>) -> Int! {
get { return value[keyPath: member] }
}
}
class C<T> : KeyPathLookup {
var value: T
init(_ v: T) {
self.value = v
}
}
func baz(_ c: C<Point>) {
let _: Int = c.x
let _ = c.y
let _: Float = c.y // expected-error {{cannot convert value of type 'Int?' to specified type 'Float'}}
let _ = c.z // expected-error {{'z' is inaccessible due to 'private' protection level}}
}
@dynamicMemberLookup
class D<T> {
var value: T
init(_ v: T) {
self.value = v
}
subscript<U: Numeric>(dynamicMember member: KeyPath<T, U>) -> (U) -> U {
get { return { offset in self.value[keyPath: member] + offset } }
}
}
func faz(_ d: D<Point>) {
let _: Int = d.x(42)
let _ = d.y(1 + 0)
let _: Float = d.y(1 + 0) // expected-error {{cannot convert value of type 'Int' to specified type 'Float'}}
let _ = d.z(1 + 0) // expected-error {{'z' is inaccessible due to 'private' protection level}}
}
@dynamicMemberLookup
struct SubscriptLens<T> {
var value: T
var counter: Int = 0
subscript(foo: String) -> Int {
get { return 42 }
}
subscript(offset: Int) -> Int {
get { return counter }
set { counter = counter + newValue }
}
subscript<U>(dynamicMember member: KeyPath<T, U>) -> U! {
get { return value[keyPath: member] }
}
subscript<U>(dynamicMember member: WritableKeyPath<T, U>) -> U {
get { return value[keyPath: member] }
set { value[keyPath: member] = newValue }
}
}
func keypath_with_subscripts(_ arr: SubscriptLens<[Int]>,
_ dict: inout SubscriptLens<[String: Int]>) {
_ = arr[0..<3]
for idx in 0..<arr.count {
let _ = arr[idx]
print(arr[idx])
}
_ = arr["hello"] // Ok
_ = dict["hello"] // Ok
_ = arr["hello"] = 42 // expected-error {{cannot assign through subscript: subscript is get-only}}
_ = dict["hello"] = 0 // Ok
_ = arr[0] = 42 // expected-error {{cannot assign through subscript: 'arr' is a 'let' constant}}
_ = dict[0] = 1 // Ok
if let index = dict.value.firstIndex(where: { $0.value == 42 }) {
let _ = dict[index]
}
dict["ultimate question"] = 42
}
func keypath_with_incorrect_return_type(_ arr: Lens<Array<Int>>) {
for idx in 0..<arr.count {
// expected-error@-1 {{cannot convert value of type 'Lens<Int>' to expected argument type 'Int'}}
let _ = arr[idx]
}
}
struct WithTrailingClosure {
subscript(fn: () -> Int) -> Int {
get { return fn() }
nonmutating set { _ = fn() + newValue }
}
subscript(offset: Int, _ fn: () -> Int) -> Int {
get { return offset + fn() }
}
}
func keypath_with_trailing_closure_subscript(_ ty: inout SubscriptLens<WithTrailingClosure>) {
_ = ty[0] { 42 } // expected-error {{subscript index of type '() -> Int' in a key path must be Hashable}}
_ = ty[0] { 42 } = 0 // expected-error {{cannot assign through subscript: subscript is get-only}}
// expected-error@-1 {{subscript index of type '() -> Int' in a key path must be Hashable}}
_ = ty[] { 42 } // expected-error {{subscript index of type '() -> Int' in a key path must be Hashable}}
_ = ty[] { 42 } = 0 // expected-error {{subscript index of type '() -> Int' in a key path must be Hashable}}
}
func keypath_to_subscript_to_property(_ lens: inout Lens<Array<Rectangle>>) {
_ = lens[0].topLeft.x
_ = lens[0].topLeft.y
_ = lens[0].topLeft.x = Lens(0)
_ = lens[0].topLeft.y = Lens(1)
// expected-error@-1 {{cannot assign to property: 'y' is a 'let' constant}}
}
@dynamicMemberLookup
struct SingleChoiceLens<T> {
var obj: T
init(_ obj: T) {
self.obj = obj
}
subscript<U>(dynamicMember member: WritableKeyPath<T, U>) -> U {
get { return obj[keyPath: member] }
set { obj[keyPath: member] = newValue }
}
}
// Make sure that disjunction filtering optimization doesn't
// impede keypath dynamic member lookup by eagerly trying to
// simplify disjunctions with a single viable choice.
func test_lens_with_a_single_choice(a: inout SingleChoiceLens<[Int]>) {
a[0] = 1 // Ok
}
func test_chain_of_recursive_lookups(_ lens: Lens<Lens<Lens<Point>>>) {
_ = lens.x
_ = lens.y
_ = lens.z // expected-error {{'z' is inaccessible due to 'private' protection level}}
// Make sure that 'obj' field could be retrieved at any level
_ = lens.obj
_ = lens.obj.obj
_ = lens.obj.x
_ = lens.obj.obj.x
_ = \Lens<Lens<Point>>.x
_ = \Lens<Lens<Point>>.obj.x
}
// KeyPath Dynamic Member Lookup can't refer to methods, mutating setters and static members
// because of the KeyPath limitations
func invalid_refs_through_dynamic_lookup() {
struct S {
static var foo: Int = 42
func bar() -> Q { return Q() }
static func baz(_: String) -> Int { return 0 }
}
struct Q {
var faz: Int = 0
}
func test(_ lens: A<S>) {
_ = lens.foo // expected-error {{dynamic key path member lookup cannot refer to static member 'foo'}}
_ = lens.bar() // expected-error {{dynamic key path member lookup cannot refer to instance method 'bar()'}}
_ = lens.bar().faz + 1 // expected-error {{dynamic key path member lookup cannot refer to instance method 'bar()'}}
_ = lens.baz("hello") // expected-error {{dynamic key path member lookup cannot refer to static method 'baz'}}
}
}
// https://github.com/apple/swift/issues/52997
final class C1_52997 {}
@dynamicMemberLookup
struct S1_52997<T> {
var obj: T
init(_ obj: T) { self.obj = obj }
subscript<U>(dynamicMember member: KeyPath<T, U>) -> U {
return obj[keyPath: member]
}
var wooo: C1_52997 { C1_52997() } // expected-note {{declared here}}
}
do {
_ = S1_52997<C1_52997>(C1_52997()).wooooo
// expected-error@-1 {{value of type 'S1_52997<C1_52997>' has no dynamic member 'wooooo' using key path from root type 'C1_52997'; did you mean 'wooo'?}}
_ = S1_52997<C1_52997>(C1_52997()).bla
// expected-error@-1 {{value of type 'S1_52997<C1_52997>' has no dynamic member 'bla' using key path from root type 'C1_52997'}}
}
final class C2_52997 {
var woo: Int? // expected-note 2 {{'woo' declared here}}
}
@dynamicMemberLookup
struct S2_52997<T> {
var obj: T
init(_ obj: T) { self.obj = obj }
subscript<U>(dynamicMember member: KeyPath<T, U>) -> U {
return obj[keyPath: member]
}
}
do {
_ = S2_52997<C2_52997>(C2_52997()).wooo
// expected-error@-1 {{value of type 'S2_52997<C2_52997>' has no dynamic member 'wooo' using key path from root type 'C2_52997'; did you mean 'woo'?}}
_ = S2_52997<C2_52997>(C2_52997()).bla
// expected-error@-1 {{value of type 'S2_52997<C2_52997>' has no dynamic member 'bla' using key path from root type 'C2_52997'}}
}
// https://github.com/apple/swift/issues/52957
@dynamicMemberLookup
struct S1_52957 {
subscript(dynamicMember: String) -> String { // expected-error {{@dynamicMemberLookup attribute requires 'S1_52957' to have a 'subscript(dynamicMember:)' method that accepts either 'ExpressibleByStringLiteral' or a key path}}
// expected-note@-1 {{add an explicit argument label to this subscript to satisfy the @dynamicMemberLookup requirement}}{{13-13=dynamicMember }}
fatalError()
}
}
@dynamicMemberLookup
struct S2_52957 {
subscript(foo bar: String) -> String { // expected-error {{@dynamicMemberLookup attribute requires 'S2_52957' to have a 'subscript(dynamicMember:)' method that accepts either 'ExpressibleByStringLiteral' or a key path}}
fatalError()
}
subscript(foo: String) -> String { // expected-error {{@dynamicMemberLookup attribute requires 'S2_52957' to have a 'subscript(dynamicMember:)' method that accepts either 'ExpressibleByStringLiteral' or a key path}}
// expected-note@-1 {{add an explicit argument label to this subscript to satisfy the @dynamicMemberLookup requirement}} {{13-13=dynamicMember }}
fatalError()
}
}
// https://github.com/apple/swift/issues/54292
@dynamicMemberLookup
struct S_54292 {
subscript(dynamicMember member: Substring) -> Int { 0 }
}
do {
_ = \S_54292.okay
}
func test_infinite_self_recursion() {
@dynamicMemberLookup
struct Recurse<T> {
subscript<U>(dynamicMember member: KeyPath<Recurse<T>, U>) -> Int {
return 1
}
}
_ = Recurse<Int>().foo
// expected-error@-1 {{value of type 'Recurse<Int>' has no dynamic member 'foo' using key path from root type 'Recurse<Int>'}}
}
// rdar://problem/60225883 - crash during solution application (ExprRewriter::buildKeyPathDynamicMemberIndexExpr)
func test_combination_of_keypath_and_string_lookups() {
@dynamicMemberLookup
struct Outer {
subscript(dynamicMember member: String) -> Outer {
Outer()
}
subscript(dynamicMember member: KeyPath<Inner, Inner>) -> Outer {
Outer()
}
}
@dynamicMemberLookup
struct Inner {
subscript(dynamicMember member: String) -> Inner {
Inner()
}
}
func test(outer: Outer) {
_ = outer.hello.world // Ok
}
}
// https://github.com/apple/swift/issues/55070
@dynamicMemberLookup
struct S_55070 {
var i: Int
subscript(dynamicMember member: KeyPath<S_55070, Int>) -> Int {
get { self[keyPath: member] }
set { self[keyPath: member] = newValue } // expected-error {{cannot assign through subscript: 'member' is a read-only key path}}
}
}
// https://github.com/apple/swift/issues/54864
public struct S1_54864 {}
@dynamicMemberLookup
public struct S2_54864 {}
internal var s2_54864_instance: S2_54864 = S2_54864()
public extension S2_54864 {
subscript<T>(dynamicMember member: WritableKeyPath<S1_54864, T>) -> T {
get { s2_54864_instance[keyPath: member] } // expected-error {{key path with root type 'S1_54864' cannot be applied to a base of type 'S2_54864'}}
set { s2_54864_instance[keyPath: member] = newValue } // expected-error {{key path with root type 'S1_54864' cannot be applied to a base of type 'S2_54864'}}
}
}
@dynamicMemberLookup
public struct S3_54864 {}
public extension S3_54864 {
subscript<T>(dynamicMember member: KeyPath<S3_54864, T>) -> T {
get { s2_54864_instance[keyPath: member] } // expected-error {{key path with root type 'S3_54864' cannot be applied to a base of type 'S2_54864'}}
}
}
// https://github.com/swiftlang/swift/issues/75244
struct WithSendable {
subscript(dynamicMember member: KeyPath<String, Int> & Sendable) -> Bool { // Ok
get { false }
}
}
|