Skip to content

Commit 1b6e4e0

Browse files
committed
AST: MemberImportVisibility should ignore bridging header modules.
1 parent d438165 commit 1b6e4e0

File tree

3 files changed

+65
-5
lines changed

3 files changed

+65
-5
lines changed

lib/AST/NameLookup.cpp

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2259,6 +2259,48 @@ void NominalTypeDecl::recordObjCMethod(AbstractFunctionDecl *method,
22592259
vec.push_back(method);
22602260
}
22612261

2262+
static bool missingExplicitImportForMemberDecl(const DeclContext *dc,
2263+
ValueDecl *decl) {
2264+
// Only require explicit imports for members when MemberImportVisibility is
2265+
// enabled.
2266+
if (!dc->getASTContext().LangOpts.hasFeature(Feature::MemberImportVisibility))
2267+
return false;
2268+
2269+
// If the decl is in the same module, no import is required.
2270+
auto declModule = decl->getDeclContext()->getParentModule();
2271+
if (declModule == dc->getParentModule())
2272+
return false;
2273+
2274+
// Source files are not expected to contain an import for the clang header
2275+
// module.
2276+
if (auto *loader = dc->getASTContext().getClangModuleLoader()) {
2277+
if (declModule == loader->getImportedHeaderModule())
2278+
return false;
2279+
}
2280+
2281+
// Only require an import in the context of user authored source file.
2282+
auto sf = dc->getParentSourceFile();
2283+
if (!sf)
2284+
return false;
2285+
2286+
switch (sf->Kind) {
2287+
case SourceFileKind::SIL:
2288+
case SourceFileKind::Interface:
2289+
case SourceFileKind::MacroExpansion:
2290+
case SourceFileKind::DefaultArgument:
2291+
return false;
2292+
case SourceFileKind::Library:
2293+
case SourceFileKind::Main:
2294+
break;
2295+
}
2296+
2297+
// If we've found an import, we're done.
2298+
if (decl->findImport(dc))
2299+
return false;
2300+
2301+
return true;
2302+
}
2303+
22622304
/// Determine whether the given declaration is an acceptable lookup
22632305
/// result when searching from the given DeclContext.
22642306
static bool isAcceptableLookupResult(const DeclContext *dc,
@@ -2291,10 +2333,7 @@ static bool isAcceptableLookupResult(const DeclContext *dc,
22912333

22922334
// Check that there is some import in the originating context that
22932335
// makes this decl visible.
2294-
if (decl->getDeclContext()->getParentModule() != dc->getParentModule() &&
2295-
dc->getASTContext().LangOpts.hasFeature(
2296-
Feature::MemberImportVisibility) &&
2297-
!decl->findImport(dc))
2336+
if (missingExplicitImportForMemberDecl(dc, decl))
22982337
return false;
22992338
}
23002339

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
@import Categories_A;
2+
3+
@interface X (BridgingHeader)
4+
- (void)fromBridgingHeader;
5+
@end
6+
7+
struct StructInBridgingHeader {
8+
int member;
9+
};

test/NameLookup/members_transitive_objc.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// RUN: %target-swift-frontend -emit-module -I %t -I %S/Inputs/Categories -o %t %S/Inputs/Categories/Categories_B.swift
44
// RUN: %target-swift-frontend -emit-module -I %t -I %S/Inputs/Categories -o %t %S/Inputs/Categories/Categories_C.swift
55
// RUN: %target-swift-frontend -emit-module -I %t -I %S/Inputs/Categories -o %t %S/Inputs/Categories/Categories_E.swift
6-
// RUN: %target-swift-frontend -typecheck %s -I %t -I %S/Inputs/Categories -verify -enable-experimental-feature MemberImportVisibility
6+
// RUN: %target-swift-frontend -typecheck %s -I %t -I %S/Inputs/Categories -import-objc-header %S/Inputs/Categories/Bridging.h -verify -enable-experimental-feature MemberImportVisibility
77

88
// REQUIRES: objc_interop
99

@@ -20,6 +20,7 @@ func test(x: X) {
2020
x.fromC() // expected-error {{class method 'fromC()' is not available due to missing import of defining module 'Categories_C'}}
2121
x.fromOverlayForC() // expected-error {{instance method 'fromOverlayForC()' is not available due to missing import of defining module 'Categories_C'}}
2222
x.fromSubmoduleOfD() // expected-error {{class method 'fromSubmoduleOfD()' is not available due to missing import of defining module 'Categories_D'}}
23+
x.fromBridgingHeader()
2324
}
2425

2526
func testAnyObject(a: AnyObject) {
@@ -32,4 +33,15 @@ func testAnyObject(a: AnyObject) {
3233
// `MemberImportVisibility` has no effect on these diagnostics.
3334
a.fromC() // expected-error {{value of type 'AnyObject' has no member 'fromC'}}
3435
a.fromOverlayForCObjC() // expected-error {{value of type 'AnyObject' has no member 'fromOverlayForCObjC'}}
36+
a.fromBridgingHeader()
37+
}
38+
39+
extension StructInBridgingHeader {
40+
init(_ x: Int32) {
41+
self.init(member: x)
42+
}
43+
44+
var wrappedMember: Int32 {
45+
return member
46+
}
3547
}

0 commit comments

Comments
 (0)