@@ -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))
@@ -2620,6 +2624,9 @@ QualifiedLookupRequest::evaluate(Evaluator &eval, const DeclContext *DC,
2620
2624
// Whether we only want to return complete object initializers.
2621
2625
bool onlyCompleteObjectInits = false ;
2622
2626
2627
+ // Whether to enforce MemberImportVisibility import restrictions.
2628
+ bool requireImport = shouldRequireImportsInContext (DC);
2629
+
2623
2630
// Visit all of the nominal types we know about, discovering any others
2624
2631
// we need along the way.
2625
2632
bool wantProtocolMembers = (options & NL_ProtocolMembers);
@@ -2654,7 +2661,8 @@ QualifiedLookupRequest::evaluate(Evaluator &eval, const DeclContext *DC,
2654
2661
if ((options & NL_OnlyMacros) && !isa<MacroDecl>(decl))
2655
2662
continue ;
2656
2663
2657
- if (isAcceptableLookupResult (DC, options, decl, onlyCompleteObjectInits))
2664
+ if (isAcceptableLookupResult (DC, options, decl, onlyCompleteObjectInits,
2665
+ requireImport))
2658
2666
decls.push_back (decl);
2659
2667
}
2660
2668
@@ -2792,6 +2800,9 @@ AnyObjectLookupRequest::evaluate(Evaluator &evaluator, const DeclContext *dc,
2792
2800
member.getFullName (), allDecls);
2793
2801
}
2794
2802
2803
+ // / Whether to enforce MemberImportVisibility import restrictions.
2804
+ bool requireImport = shouldRequireImportsInContext (dc);
2805
+
2795
2806
// For each declaration whose context is not something we've
2796
2807
// already visited above, add it to the list of declarations.
2797
2808
llvm::SmallPtrSet<ValueDecl *, 4 > knownDecls;
@@ -2824,7 +2835,8 @@ AnyObjectLookupRequest::evaluate(Evaluator &evaluator, const DeclContext *dc,
2824
2835
// result, add it to the list.
2825
2836
if (knownDecls.insert (decl).second &&
2826
2837
isAcceptableLookupResult (dc, options, decl,
2827
- /* onlyCompleteObjectInits=*/ false ))
2838
+ /* onlyCompleteObjectInits=*/ false ,
2839
+ requireImport))
2828
2840
decls.push_back (decl);
2829
2841
}
2830
2842
0 commit comments