@@ -2330,22 +2330,27 @@ ObjCCategoryNameMap ClassDecl::getObjCCategoryNameMap() {
2330
2330
ObjCCategoryNameMap ());
2331
2331
}
2332
2332
2333
- static bool missingImportForMemberDecl ( const DeclContext *dc, ValueDecl *decl) {
2334
- // Only require explicit imports for members when MemberImportVisibility is
2335
- // enabled.
2336
- auto &ctx = dc ->getASTContext ();
2333
+ // / Determines whether MemberImportVisiblity should be enforced for lookups in
2334
+ // / the given context.
2335
+ static bool shouldRequireImportsInContext ( const DeclContext *lookupContext) {
2336
+ auto &ctx = lookupContext ->getASTContext ();
2337
2337
if (!ctx.LangOpts .hasFeature (Feature::MemberImportVisibility))
2338
2338
return false ;
2339
2339
2340
- return !dc->isDeclImported (decl);
2340
+ // Code outside of the main module (which is often synthesized) isn't subject
2341
+ // to MemberImportVisibility rules.
2342
+ if (lookupContext->getParentModule () != ctx.MainModule )
2343
+ return false ;
2344
+
2345
+ return true ;
2341
2346
}
2342
2347
2343
2348
// / Determine whether the given declaration is an acceptable lookup
2344
2349
// / result when searching from the given DeclContext.
2345
- static bool isAcceptableLookupResult (const DeclContext *dc,
2346
- NLOptions options,
2350
+ static bool isAcceptableLookupResult (const DeclContext *dc, NLOptions options,
2347
2351
ValueDecl *decl,
2348
- bool onlyCompleteObjectInits) {
2352
+ bool onlyCompleteObjectInits,
2353
+ bool requireImport) {
2349
2354
// Filter out designated initializers, if requested.
2350
2355
if (onlyCompleteObjectInits) {
2351
2356
if (auto ctor = dyn_cast<ConstructorDecl>(decl)) {
@@ -2373,10 +2378,9 @@ static bool isAcceptableLookupResult(const DeclContext *dc,
2373
2378
2374
2379
// Check that there is some import in the originating context that makes this
2375
2380
// decl visible.
2376
- if (!(options & NL_IgnoreMissingImports)) {
2377
- if (missingImportForMemberDecl (dc, decl))
2381
+ if (requireImport && !(options & NL_IgnoreMissingImports))
2382
+ if (!dc-> isDeclImported ( decl))
2378
2383
return false ;
2379
- }
2380
2384
2381
2385
return true ;
2382
2386
}
@@ -2593,6 +2597,9 @@ QualifiedLookupRequest::evaluate(Evaluator &eval, const DeclContext *DC,
2593
2597
// Whether we only want to return complete object initializers.
2594
2598
bool onlyCompleteObjectInits = false ;
2595
2599
2600
+ // Whether to enforce MemberImportVisibility import restrictions.
2601
+ bool requireImport = shouldRequireImportsInContext (DC);
2602
+
2596
2603
// Visit all of the nominal types we know about, discovering any others
2597
2604
// we need along the way.
2598
2605
bool wantProtocolMembers = (options & NL_ProtocolMembers);
@@ -2625,7 +2632,8 @@ QualifiedLookupRequest::evaluate(Evaluator &eval, const DeclContext *DC,
2625
2632
if ((options & NL_OnlyMacros) && !isa<MacroDecl>(decl))
2626
2633
continue ;
2627
2634
2628
- if (isAcceptableLookupResult (DC, options, decl, onlyCompleteObjectInits))
2635
+ if (isAcceptableLookupResult (DC, options, decl, onlyCompleteObjectInits,
2636
+ requireImport))
2629
2637
decls.push_back (decl);
2630
2638
}
2631
2639
@@ -2762,6 +2770,9 @@ AnyObjectLookupRequest::evaluate(Evaluator &evaluator, const DeclContext *dc,
2762
2770
member.getFullName (), allDecls);
2763
2771
}
2764
2772
2773
+ // / Whether to enforce MemberImportVisibility import restrictions.
2774
+ bool requireImport = shouldRequireImportsInContext (dc);
2775
+
2765
2776
// For each declaration whose context is not something we've
2766
2777
// already visited above, add it to the list of declarations.
2767
2778
llvm::SmallPtrSet<ValueDecl *, 4 > knownDecls;
@@ -2794,7 +2805,8 @@ AnyObjectLookupRequest::evaluate(Evaluator &evaluator, const DeclContext *dc,
2794
2805
// result, add it to the list.
2795
2806
if (knownDecls.insert (decl).second &&
2796
2807
isAcceptableLookupResult (dc, options, decl,
2797
- /* onlyCompleteObjectInits=*/ false ))
2808
+ /* onlyCompleteObjectInits=*/ false ,
2809
+ requireImport))
2798
2810
decls.push_back (decl);
2799
2811
}
2800
2812
0 commit comments