Skip to content

Commit 8f894b4

Browse files
committed
[MiscDiagnostics] Suppress KVO warning when the property has an explicit setter
1 parent e3403b5 commit 8f894b4

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

lib/Sema/MiscDiagnostics.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4046,7 +4046,10 @@ static void maybeDiagnoseCallToKeyValueObserveMethod(const Expr *E,
40464046
auto property = lastComponent.getDeclRef().getDecl();
40474047
if (!property)
40484048
return;
4049-
if (property->isObjCDynamic())
4049+
auto propertyVar = cast<VarDecl>(property);
4050+
if (propertyVar->isObjCDynamic() ||
4051+
(propertyVar->isObjC() &&
4052+
propertyVar->getParsedAccessor(AccessorKind::Set)))
40504053
return;
40514054
C.Diags
40524055
.diagnose(expr->getLoc(),

test/expr/primary/keypath/keypath-observe-objc.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ class Foo: NSObject {
88
dynamic var number2 = 2
99
@objc var number3 = 3
1010
@objc dynamic var number4 = 4
11+
@objc var number5: Int {
12+
get { return 5 }
13+
set {}
14+
}
1115
}
1216

1317
class Bar: NSObject {
@@ -35,5 +39,9 @@ class Bar: NSObject {
3539
_ = observe(\.foo.number4, options: [.new]) { _, change in // Okay
3640
print("observer4")
3741
}
42+
43+
_ = observe(\.foo.number5, options: [.new]) { _, change in // Okay
44+
print("observer4")
45+
}
3846
}
3947
}

0 commit comments

Comments
 (0)