Skip to content

Commit 9371ef0

Browse files
[tests] Adding regression tests and for unwrapping base on key path application
1 parent 1190505 commit 9371ef0

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

test/Constraints/keypath.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,30 @@ func fSR11184_O(_ c: SR11184!, _ kp: ReferenceWritableKeyPath<SR11184, String?>,
152152
c![keyPath: kp] = str // OK
153153
c?[keyPath: kp] = str // OK
154154
}
155+
156+
class KeyPathBase {}
157+
class KeyPathBaseSubtype: KeyPathBase {}
158+
class AnotherBase {}
159+
class AnotherComposeBase {
160+
var member: KeyPathBase?
161+
}
162+
163+
func key_path_root_mismatch<T>(_ base: KeyPathBase?, subBase: KeyPathBaseSubtype?, _ abase: AnotherComposeBase,
164+
_ kp: KeyPath<KeyPathBase, T>, _ kpa: KeyPath<AnotherBase, T>) {
165+
let _ : T = base[keyPath: kp] // expected-error {{value of optional type 'KeyPathBase?' must be unwrapped to a value of type 'KeyPathBase'}}
166+
// expected-note@-1 {{force-unwrap using '!' to abort execution if the optional value contains 'nil'}} {{19-19=!}}
167+
// expected-note@-2 {{use '?' to access key path subscript only for non-'nil' base values}} {{19-19=?}}
168+
let _ : T = base[keyPath: kpa] // expected-error {{key path with root type 'AnotherBase' cannot be applied to a base of type 'KeyPathBase?'}}
169+
170+
// Chained root mismatch
171+
let _ : T = abase.member[keyPath: kp] // expected-error {{value of optional type 'KeyPathBase?' must be unwrapped to a value of type 'KeyPathBase'}}
172+
// expected-note@-1 {{force-unwrap using '!' to abort execution if the optional value contains 'nil'}} {{27-27=!}}
173+
// expected-note@-2 {{use '?' to access key path subscript only for non-'nil' base values}} {{27-27=?}}
174+
let _ : T = abase.member[keyPath: kpa] // expected-error {{key path with root type 'AnotherBase' cannot be applied to a base of type 'KeyPathBase?'}}
175+
176+
let _ : T = subBase[keyPath: kp] // expected-error {{value of optional type 'KeyPathBaseSubtype?' must be unwrapped to a value of type 'KeyPathBaseSubtype'}}
177+
// expected-note@-1 {{force-unwrap using '!' to abort execution if the optional value contains 'nil'}} {{22-22=!}}
178+
// expected-note@-2 {{use '?' to access key path subscript only for non-'nil' base values}} {{22-22=?}}
179+
let _ : T = subBase[keyPath: kpa] // expected-error {{key path with root type 'AnotherBase' cannot be applied to a base of type 'KeyPathBaseSubtype?'}}
180+
181+
}

test/expr/unary/keypath/keypath.swift

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -452,20 +452,24 @@ func testKeyPathSubscriptExistentialBase(concreteBase: inout B,
452452
_ = concreteBase[keyPath: rkp]
453453
_ = concreteBase[keyPath: pkp]
454454

455-
concreteBase[keyPath: kp] = s // expected-error{{}}
456-
concreteBase[keyPath: wkp] = s // expected-error{{}}
455+
concreteBase[keyPath: kp] = s // expected-error {{cannot assign through subscript: 'kp' is a read-only key path}}
456+
concreteBase[keyPath: wkp] = s // expected-error {{key path with root type 'P' cannot be applied to a base of type 'B'}}
457457
concreteBase[keyPath: rkp] = s
458-
concreteBase[keyPath: pkp] = s // expected-error{{}}
458+
// TODO(diagnostics): Improve this diagnostic message because concreteBase is mutable, the problem is related to assign
459+
// through PartialKeyPath.
460+
concreteBase[keyPath: pkp] = s // expected-error {{cannot assign through subscript: 'concreteBase' is immutable}}
459461

460462
_ = existentialBase[keyPath: kp]
461463
_ = existentialBase[keyPath: wkp]
462464
_ = existentialBase[keyPath: rkp]
463465
_ = existentialBase[keyPath: pkp]
464466

465-
existentialBase[keyPath: kp] = s // expected-error{{}}
467+
existentialBase[keyPath: kp] = s // expected-error {{cannot assign through subscript: 'kp' is a read-only key path}}
466468
existentialBase[keyPath: wkp] = s
467469
existentialBase[keyPath: rkp] = s
468-
existentialBase[keyPath: pkp] = s // expected-error{{}}
470+
// TODO(diagnostics): Improve this diagnostic message because existentialBase is mutable, the problem is related to assign
471+
// through PartialKeyPath.
472+
existentialBase[keyPath: pkp] = s // expected-error {{cannot assign through subscript: 'existentialBase' is immutable}}
469473
}
470474

471475
struct AA {

0 commit comments

Comments
 (0)