Skip to content

Commit d9e046d

Browse files
authored
Merge pull request #17497 from rudkx/rdar40955755
[TypeChecker] Fix crash with Objective C keypaths.
2 parents 0f25a3a + 846f159 commit d9e046d

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

lib/Sema/CSGen.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -3280,7 +3280,8 @@ namespace {
32803280
if (auto keyPath = dyn_cast<KeyPathExpr>(expr)) {
32813281
if (keyPath->isObjC()) {
32823282
auto &cs = CG.getConstraintSystem();
3283-
(void)cs.getTypeChecker().checkObjCKeyPathExpr(cs.DC, keyPath);
3283+
if (!cs.getTypeChecker().checkObjCKeyPathExpr(cs.DC, keyPath))
3284+
return { false, nullptr };
32843285
}
32853286
}
32863287

lib/Sema/TypeCheckExprObjC.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -317,6 +317,10 @@ Optional<Type> TypeChecker::checkObjCKeyPathExpr(DeclContext *dc,
317317
// Handle property references.
318318
if (auto var = dyn_cast<VarDecl>(found)) {
319319
validateDecl(var);
320+
if (var->isInvalid() || !var->hasInterfaceType() ||
321+
var->getInterfaceType()->hasError()) {
322+
return None;
323+
}
320324

321325
// Resolve this component to the variable we found.
322326
auto varRef = ConcreteDeclRef(var);

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Foundation
44

55
// REQUIRES: objc_interop
66

7-
@objc class A : NSObject {
7+
@objc class A : NSObject { // expected-error {{class 'A' has no initializers}}
88
@objc var propB: B = B()
99
@objc var propString: String = "" // expected-note {{did you mean 'propString'}}
1010
@objc var propArray: [String] = []
@@ -21,6 +21,8 @@ import Foundation
2121
@objc func someMethod() { }
2222

2323
@objc var `repeat`: String?
24+
25+
@objc var noType // expected-error {{type annotation missing in pattern}}
2426
}
2527

2628
@objc class B : NSObject {
@@ -106,6 +108,9 @@ func testKeyPath(a: A, b: B) {
106108
// Property with keyword name.
107109
let _: String = #keyPath(A.repeat)
108110

111+
// Property with no type
112+
let _: String = #keyPath(A.noType)
113+
109114
// Nested type of a bridged type (rdar://problem/28061409).
110115
typealias IntArray = [Int]
111116
let _: String = #keyPath(IntArray.Foo.propString)

0 commit comments

Comments
 (0)