Skip to content

Commit 0832afe

Browse files
committed
[MiscDiagnostics] Key path diagnostics should account for upcasts
`DerivedToBaseExpr` is inserted when key path literal is upcast to its superclass, diagnostics related to `@objc dynamic` should account for that.
1 parent e67dfc1 commit 0832afe

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

lib/Sema/MiscDiagnostics.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5379,16 +5379,23 @@ static void maybeDiagnoseCallToKeyValueObserveMethod(const Expr *E,
53795379
return;
53805380
SmallVector<KeyPathExpr *, 1> keyPathArgs;
53815381
auto *args = expr->getArgs();
5382+
5383+
auto isKeyPathLiteral = [&](Expr *argExpr) -> KeyPathExpr * {
5384+
if (auto *DTBE = getAsExpr<DerivedToBaseExpr>(argExpr))
5385+
argExpr = DTBE->getSubExpr();
5386+
return getAsExpr<KeyPathExpr>(argExpr);
5387+
};
5388+
53825389
if (fn->getModuleContext()->getName() == C.Id_Foundation &&
53835390
fn->getName().isCompoundName("observe",
53845391
{"", "options", "changeHandler"})) {
5385-
if (auto keyPathArg = dyn_cast<KeyPathExpr>(args->getExpr(0))) {
5392+
if (auto keyPathArg = isKeyPathLiteral(args->getExpr(0))) {
53865393
keyPathArgs.push_back(keyPathArg);
53875394
}
53885395
} else if (fn->getAttrs()
53895396
.hasSemanticsAttr(semantics::KEYPATH_MUST_BE_VALID_FOR_KVO)) {
5390-
for (const auto& arg: *args) {
5391-
if (auto keyPathArg = dyn_cast<KeyPathExpr>(arg.getExpr())) {
5397+
for (auto *argExpr : args->getArgExprs()) {
5398+
if (auto keyPathArg = isKeyPathLiteral(argExpr)) {
53925399
keyPathArgs.push_back(keyPathArg);
53935400
}
53945401
}

0 commit comments

Comments
 (0)