@@ -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
}
@@ -2567,6 +2571,7 @@ QualifiedLookupRequest::evaluate(Evaluator &eval, const DeclContext *DC,
2567
2571
DeclNameRef member, NLOptions options) const {
2568
2572
using namespace namelookup ;
2569
2573
QualifiedLookupResult decls;
2574
+ auto &ctx = DC->getASTContext ();
2570
2575
2571
2576
// Tracking for the nominal types we'll visit.
2572
2577
SmallVector<NominalTypeDecl *, 4 > stack;
@@ -2593,6 +2598,9 @@ QualifiedLookupRequest::evaluate(Evaluator &eval, const DeclContext *DC,
2593
2598
// Whether we only want to return complete object initializers.
2594
2599
bool onlyCompleteObjectInits = false ;
2595
2600
2601
+ // Whether to enforce MemberImportVisibility import restrictions.
2602
+ bool requireImport = shouldRequireImportsInContext (DC);
2603
+
2596
2604
// Visit all of the nominal types we know about, discovering any others
2597
2605
// we need along the way.
2598
2606
bool wantProtocolMembers = (options & NL_ProtocolMembers);
@@ -2625,7 +2633,8 @@ QualifiedLookupRequest::evaluate(Evaluator &eval, const DeclContext *DC,
2625
2633
if ((options & NL_OnlyMacros) && !isa<MacroDecl>(decl))
2626
2634
continue ;
2627
2635
2628
- if (isAcceptableLookupResult (DC, options, decl, onlyCompleteObjectInits))
2636
+ if (isAcceptableLookupResult (DC, options, decl, onlyCompleteObjectInits,
2637
+ requireImport))
2629
2638
decls.push_back (decl);
2630
2639
}
2631
2640
@@ -2762,6 +2771,9 @@ AnyObjectLookupRequest::evaluate(Evaluator &evaluator, const DeclContext *dc,
2762
2771
member.getFullName (), allDecls);
2763
2772
}
2764
2773
2774
+ // / Whether to enforce MemberImportVisibility import restrictions.
2775
+ bool requireImport = shouldRequireImportsInContext (dc);
2776
+
2765
2777
// For each declaration whose context is not something we've
2766
2778
// already visited above, add it to the list of declarations.
2767
2779
llvm::SmallPtrSet<ValueDecl *, 4 > knownDecls;
@@ -2794,7 +2806,8 @@ AnyObjectLookupRequest::evaluate(Evaluator &evaluator, const DeclContext *dc,
2794
2806
// result, add it to the list.
2795
2807
if (knownDecls.insert (decl).second &&
2796
2808
isAcceptableLookupResult (dc, options, decl,
2797
- /* onlyCompleteObjectInits=*/ false ))
2809
+ /* onlyCompleteObjectInits=*/ false ,
2810
+ requireImport))
2798
2811
decls.push_back (decl);
2799
2812
}
2800
2813
0 commit comments