Skip to content

Commit c23af5f

Browse files
authored
Merge pull request #83934 from tshortli/key-path-member-import-visibility
Sema: Diagnose key paths when `MemberImportVisibility` is enabled
2 parents 9148485 + ea714d2 commit c23af5f

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

lib/Sema/MiscDiagnostics.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "swift/AST/NameLookup.h"
3131
#include "swift/AST/NameLookupRequests.h"
3232
#include "swift/AST/Pattern.h"
33+
#include "swift/AST/PrettyStackTrace.h"
3334
#include "swift/AST/SemanticAttrs.h"
3435
#include "swift/AST/SourceFile.h"
3536
#include "swift/AST/Stmt.h"
@@ -6378,6 +6379,13 @@ static void diagnoseMissingMemberImports(const Expr *E, const DeclContext *DC) {
63786379
if (auto declRef = E->getReferencedDecl())
63796380
checkDecl(declRef.getDecl(), E->getLoc());
63806381

6382+
if (auto *KPE = dyn_cast<KeyPathExpr>(E)) {
6383+
for (const auto &component : KPE->getComponents()) {
6384+
if (component.hasDeclRef())
6385+
checkDecl(component.getDeclRef().getDecl(), component.getLoc());
6386+
}
6387+
}
6388+
63816389
return Action::Continue(E);
63826390
}
63836391

test/NameLookup/member_import_visibility.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// REQUIRES: swift_feature_MemberImportVisibility
1010

1111
import members_C
12-
// expected-member-visibility-note 18{{add import of module 'members_B'}}{{1-1=internal import members_B\n}}
12+
// expected-member-visibility-note 20{{add import of module 'members_B'}}{{1-1=internal import members_B\n}}
1313

1414

1515
func testExtensionMembers(x: X, y: Y<Z>) {
@@ -43,6 +43,16 @@ func testExtensionMembers(x: X, y: Y<Z>) {
4343
let _: Int = disfavoredResult // expected-member-visibility-error{{cannot convert value of type 'Bool' to specified type 'Int'}}
4444
let _: Bool = x.ambiguousDisfavored()
4545
let _: Int = x.ambiguousDisfavored() // expected-member-visibility-error{{instance method 'ambiguousDisfavored()' is not available due to missing import of defining module 'members_B'}}
46+
47+
func takesKeyPath<T, U>(_ t: T, _ keyPath: KeyPath<T, U>) -> () { }
48+
49+
takesKeyPath(x, \.propXinA)
50+
takesKeyPath(x, \.propXinB) // expected-member-visibility-error{{property 'propXinB' is not available due to missing import of defining module 'members_B'}}
51+
takesKeyPath(x, \.propXinC)
52+
53+
takesKeyPath(x, \.propXinA.description)
54+
takesKeyPath(x, \.propXinB.description) // expected-member-visibility-error{{property 'propXinB' is not available due to missing import of defining module 'members_B'}}
55+
takesKeyPath(x, \.propXinC.description)
4656
}
4757

4858
func testOperatorMembers(x: X, y: Y<Z>) {

0 commit comments

Comments
 (0)