@@ -2340,22 +2340,27 @@ ObjCCategoryNameMap ClassDecl::getObjCCategoryNameMap() {
2340
2340
ObjCCategoryNameMap ());
2341
2341
}
2342
2342
2343
- static bool missingImportForMemberDecl ( const DeclContext *dc, ValueDecl *decl) {
2344
- // Only require explicit imports for members when MemberImportVisibility is
2345
- // enabled.
2346
- auto &ctx = dc ->getASTContext ();
2343
+ // / Determines whether MemberImportVisiblity should be enforced for lookups in
2344
+ // / the given context.
2345
+ static bool shouldRequireImportsInContext ( const DeclContext *lookupContext) {
2346
+ auto &ctx = lookupContext ->getASTContext ();
2347
2347
if (!ctx.LangOpts .hasFeature (Feature::MemberImportVisibility))
2348
2348
return false ;
2349
2349
2350
- return !dc->isDeclImported (decl);
2350
+ // Code outside of the main module (which is often synthesized) isn't subject
2351
+ // to MemberImportVisibility rules.
2352
+ if (lookupContext->getParentModule () != ctx.MainModule )
2353
+ return false ;
2354
+
2355
+ return true ;
2351
2356
}
2352
2357
2353
2358
// / Determine whether the given declaration is an acceptable lookup
2354
2359
// / result when searching from the given DeclContext.
2355
- static bool isAcceptableLookupResult (const DeclContext *dc,
2356
- NLOptions options,
2360
+ static bool isAcceptableLookupResult (const DeclContext *dc, NLOptions options,
2357
2361
ValueDecl *decl,
2358
- bool onlyCompleteObjectInits) {
2362
+ bool onlyCompleteObjectInits,
2363
+ bool requireImport) {
2359
2364
// Filter out designated initializers, if requested.
2360
2365
if (onlyCompleteObjectInits) {
2361
2366
if (auto ctor = dyn_cast<ConstructorDecl>(decl)) {
@@ -2383,10 +2388,9 @@ static bool isAcceptableLookupResult(const DeclContext *dc,
2383
2388
2384
2389
// Check that there is some import in the originating context that makes this
2385
2390
// decl visible.
2386
- if (!(options & NL_IgnoreMissingImports)) {
2387
- if (missingImportForMemberDecl (dc, decl))
2391
+ if (requireImport && !(options & NL_IgnoreMissingImports))
2392
+ if (!dc-> isDeclImported ( decl))
2388
2393
return false ;
2389
- }
2390
2394
2391
2395
// Check that it has the appropriate ABI role.
2392
2396
if (!ABIRoleInfo (decl).matchesOptions (options))
@@ -2594,6 +2598,7 @@ QualifiedLookupRequest::evaluate(Evaluator &eval, const DeclContext *DC,
2594
2598
DeclNameRef member, NLOptions options) const {
2595
2599
using namespace namelookup ;
2596
2600
QualifiedLookupResult decls;
2601
+ auto &ctx = DC->getASTContext ();
2597
2602
2598
2603
// Tracking for the nominal types we'll visit.
2599
2604
SmallVector<NominalTypeDecl *, 4 > stack;
@@ -2620,6 +2625,9 @@ QualifiedLookupRequest::evaluate(Evaluator &eval, const DeclContext *DC,
2620
2625
// Whether we only want to return complete object initializers.
2621
2626
bool onlyCompleteObjectInits = false ;
2622
2627
2628
+ // Whether to enforce MemberImportVisibility import restrictions.
2629
+ bool requireImport = shouldRequireImportsInContext (DC);
2630
+
2623
2631
// Visit all of the nominal types we know about, discovering any others
2624
2632
// we need along the way.
2625
2633
bool wantProtocolMembers = (options & NL_ProtocolMembers);
@@ -2654,7 +2662,8 @@ QualifiedLookupRequest::evaluate(Evaluator &eval, const DeclContext *DC,
2654
2662
if ((options & NL_OnlyMacros) && !isa<MacroDecl>(decl))
2655
2663
continue ;
2656
2664
2657
- if (isAcceptableLookupResult (DC, options, decl, onlyCompleteObjectInits))
2665
+ if (isAcceptableLookupResult (DC, options, decl, onlyCompleteObjectInits,
2666
+ requireImport))
2658
2667
decls.push_back (decl);
2659
2668
}
2660
2669
@@ -2792,6 +2801,9 @@ AnyObjectLookupRequest::evaluate(Evaluator &evaluator, const DeclContext *dc,
2792
2801
member.getFullName (), allDecls);
2793
2802
}
2794
2803
2804
+ // / Whether to enforce MemberImportVisibility import restrictions.
2805
+ bool requireImport = shouldRequireImportsInContext (dc);
2806
+
2795
2807
// For each declaration whose context is not something we've
2796
2808
// already visited above, add it to the list of declarations.
2797
2809
llvm::SmallPtrSet<ValueDecl *, 4 > knownDecls;
@@ -2824,7 +2836,8 @@ AnyObjectLookupRequest::evaluate(Evaluator &evaluator, const DeclContext *dc,
2824
2836
// result, add it to the list.
2825
2837
if (knownDecls.insert (decl).second &&
2826
2838
isAcceptableLookupResult (dc, options, decl,
2827
- /* onlyCompleteObjectInits=*/ false ))
2839
+ /* onlyCompleteObjectInits=*/ false ,
2840
+ requireImport))
2828
2841
decls.push_back (decl);
2829
2842
}
2830
2843
0 commit comments