Skip to content

Suppress KVO warning when the property has an explicit setter #30098

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/Sema/MiscDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4337,7 +4337,10 @@ static void maybeDiagnoseCallToKeyValueObserveMethod(const Expr *E,
auto property = lastComponent.getDeclRef().getDecl();
if (!property)
return;
if (property->isObjCDynamic())
auto propertyVar = cast<VarDecl>(property);
if (propertyVar->isObjCDynamic() ||
(propertyVar->isObjC() &&
propertyVar->getParsedAccessor(AccessorKind::Set)))
return;
C.Diags
.diagnose(expr->getLoc(),
Expand Down
8 changes: 8 additions & 0 deletions test/expr/primary/keypath/keypath-observe-objc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ class Foo: NSObject {
dynamic var number2 = 2
@objc var number3 = 3
@objc dynamic var number4 = 4
@objc var number5: Int {
get { return 5 }
set {}
}
}

class Bar: NSObject {
Expand Down Expand Up @@ -35,5 +39,9 @@ class Bar: NSObject {
_ = observe(\.foo.number4, options: [.new]) { _, change in // Okay
print("observer4")
}

_ = observe(\.foo.number5, options: [.new]) { _, change in // Okay
print("observer4")
}
}
}