Skip to content

Commit a2f29b6

Browse files
committed
Use interface types when checking #keyPath. (#7028)
This avoids a crash when the path refers to a property in another Swift module. https://bugs.swift.org/browse/SR-3714
1 parent 0d136dd commit a2f29b6

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

lib/Sema/TypeCheckExprObjC.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,8 @@ Optional<Type> TypeChecker::checkObjCKeyPathExpr(DeclContext *dc,
289289

290290
// Resolve this component to the variable we found.
291291
expr->resolveComponent(idx, var);
292-
updateState(/*isProperty=*/true, var->getType()->getRValueObjectType());
292+
updateState(/*isProperty=*/true,
293+
var->getInterfaceType()->getRValueObjectType());
293294

294295
// Check that the property is @objc.
295296
if (!var->isObjC()) {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// RUN: rm -rf %t && mkdir %t
2+
// RUN: %target-swift-frontend -emit-module %s -DLIBRARY -o %t/Lib.swiftmodule
3+
// RUN: %target-swift-frontend -typecheck %s -I %t -verify
4+
5+
// REQUIRES: objc_interop
6+
7+
#if LIBRARY
8+
9+
import Foundation
10+
public class Test: NSObject {
11+
@objc public var prop: NSObject?
12+
}
13+
14+
#else
15+
16+
import Lib
17+
18+
func test() {
19+
_ = #keyPath(Test.prop) // okay
20+
_ = #keyPath(Test.nonexistent) // expected-error {{type 'Test' has no member 'nonexistent'}}
21+
}
22+
23+
#endif

0 commit comments

Comments
 (0)