@@ -392,17 +392,19 @@ func testGenerics<S, T, P: GenericProtocol>(
392
392
//===----------------------------------------------------------------------===//
393
393
394
394
@dynamicMemberLookup
395
- class C {
395
+ class KP {
396
396
subscript( dynamicMember member: String ) -> Int { return 7 }
397
397
}
398
- _ = \C . [ dynamicMember: " hi " ]
399
- _ = \C . testLookup
398
+ _ = \KP . [ dynamicMember: " hi " ]
399
+ _ = \KP . testLookup
400
400
401
401
/* KeyPath based dynamic lookup */
402
402
403
403
struct Point {
404
404
var x : Int
405
405
let y : Int
406
+
407
+ private let z : Int = 0 // expected-note 7 {{declared here}}
406
408
}
407
409
408
410
struct Rectangle {
@@ -433,6 +435,99 @@ var bottomRight = Point(x: 10, y: 10)
433
435
var lens = Lens ( Rectangle ( topLeft: topLeft,
434
436
bottomRight: bottomRight) )
435
437
438
+ _ = lens. topLeft
439
+ _ = lens. topLeft. x
440
+ _ = lens. topLeft. y
441
+ _ = lens. topLeft. z // expected-error {{'z' is inaccessible due to 'private' protection level}}
442
+
443
+ _ = lens. bottomRight
444
+ _ = lens. bottomRight. x
445
+ _ = lens. bottomRight. y
446
+ _ = lens. bottomRight. z // expected-error {{'z' is inaccessible due to 'private' protection level}}
447
+
436
448
lens. topLeft = Lens ( Point ( x: 1 , y: 2 ) ) // Ok
437
449
lens. bottomRight. x = Lens ( 11 ) // Ok
438
450
lens. bottomRight. y = Lens ( 12 ) // expected-error {{cannot assign through dynamic lookup property: 'lens' is immutable}}
451
+ lens. bottomRight. z = Lens ( 13 ) // expected-error {{'z' is inaccessible due to 'private' protection level}}
452
+
453
+ func acceptKeyPathDynamicLookup( _: Lens < Int > ) { }
454
+
455
+ acceptKeyPathDynamicLookup ( lens. topLeft. x)
456
+ acceptKeyPathDynamicLookup ( lens. topLeft. y)
457
+ acceptKeyPathDynamicLookup ( lens. topLeft. z) // expected-error {{'z' is inaccessible due to 'private' protection level}}
458
+
459
+ @dynamicMemberLookup
460
+ class A < T> {
461
+ var value : T
462
+
463
+ init ( _ v: T ) {
464
+ self . value = v
465
+ }
466
+
467
+ subscript< U> ( dynamicMember member: KeyPath < T , U > ) -> U {
468
+ get { return value [ keyPath: member] }
469
+ }
470
+ }
471
+
472
+ // Let's make sure that keypath dynamic member lookup
473
+ // works with inheritance
474
+
475
+ class B < T> : A < T > { }
476
+
477
+ func bar( _ b: B < Point > ) {
478
+ let _: Int = b. x
479
+ let _ = b. y
480
+ let _: Float = b. y // expected-error {{cannot convert value of type 'Int' to specified type 'Float'}}
481
+ let _ = b. z // expected-error {{'z' is inaccessible due to 'private' protection level}}
482
+ }
483
+
484
+ // Existentials and IUOs
485
+
486
+ @dynamicMemberLookup
487
+ protocol KeyPathLookup {
488
+ associatedtype T
489
+
490
+ var value : T { get }
491
+
492
+ subscript( dynamicMember member: KeyPath < T , Int > ) -> Int ! { get }
493
+ }
494
+
495
+ extension KeyPathLookup {
496
+ subscript( dynamicMember member: KeyPath < T , Int > ) -> Int ! {
497
+ get { return value [ keyPath: member] }
498
+ }
499
+ }
500
+
501
+ class C < T> : KeyPathLookup {
502
+ var value : T
503
+ init ( _ v: T ) {
504
+ self . value = v
505
+ }
506
+ }
507
+
508
+ func baz( _ c: C < Point > ) {
509
+ let _: Int = c. x
510
+ let _ = c. y
511
+ let _: Float = c. y // expected-error {{cannot convert value of type 'Int?' to specified type 'Float'}}
512
+ let _ = c. z // expected-error {{'z' is inaccessible due to 'private' protection level}}
513
+ }
514
+
515
+ @dynamicMemberLookup
516
+ class D < T> {
517
+ var value : T
518
+
519
+ init ( _ v: T ) {
520
+ self . value = v
521
+ }
522
+
523
+ subscript< U: Numeric > ( dynamicMember member: KeyPath < T , U > ) -> ( U ) -> U {
524
+ get { return { offset in self . value [ keyPath: member] + offset } }
525
+ }
526
+ }
527
+
528
+ func faz( _ d: D < Point > ) {
529
+ let _: Int = d. x ( 42 )
530
+ let _ = d. y ( 1 + 0 )
531
+ let _: Float = d. y ( 1 + 0 ) // expected-error {{cannot convert value of type 'Int' to specified type 'Float'}}
532
+ let _ = d. z ( 1 + 0 ) // expected-error {{'z' is inaccessible due to 'private' protection level}}
533
+ }
0 commit comments