@@ -212,19 +212,9 @@ void AccessControlCheckerBase::checkTypeAccessImpl(
212212 // Report where it was imported from.
213213 if (contextAccessScope.isPublicOrPackage ()) {
214214 auto report = [&](const DeclRefTypeRepr *typeRepr, const ValueDecl *VD) {
215- // Remember that the module defining the decl must be imported publicly.
215+ SourceLoc diagLoc = typeRepr ? typeRepr-> getLoc () : SourceLoc ();
216216 recordRequiredImportAccessLevelForDecl (
217- VD, useDC, contextAccessScope.accessLevelForDiagnostics (),
218- [&](AttributedImport<ImportedModule> attributedImport) {
219- SourceLoc diagLoc =
220- typeRepr ? typeRepr->getLoc () : extractNearestSourceLoc (useDC);
221- ModuleDecl *importedVia = attributedImport.module .importedModule ,
222- *sourceModule = VD->getModuleContext ();
223- Context.Diags .diagnose (diagLoc, diag::module_api_import, VD,
224- importedVia, sourceModule,
225- importedVia == sourceModule,
226- /* isImplicit*/ !typeRepr);
227- });
217+ VD, useDC, contextAccessScope.accessLevelForDiagnostics (), diagLoc);
228218 };
229219
230220 if (typeRepr) {
@@ -2445,15 +2435,8 @@ class DeclAvailabilityChecker : public DeclVisitor<DeclAvailabilityChecker> {
24452435
24462436 // Remember that the module defining the extended type must be imported
24472437 // publicly.
2448- recordRequiredImportAccessLevelForDecl (
2449- extendedType, DC, AccessLevel::Public,
2450- [&](AttributedImport<ImportedModule> attributedImport) {
2451- ModuleDecl *importedVia = attributedImport.module .importedModule ,
2452- *sourceModule = ED->getModuleContext ();
2453- ED->diagnose (diag::module_api_import, ED, importedVia, sourceModule,
2454- importedVia == sourceModule,
2455- /* isImplicit*/ false );
2456- });
2438+ recordRequiredImportAccessLevelForDecl (extendedType, DC,
2439+ AccessLevel::Public, ED->getLoc ());
24572440 }
24582441 }
24592442
@@ -2579,6 +2562,23 @@ void swift::recordRequiredImportAccessLevelForDecl(
25792562 }
25802563}
25812564
2565+ void swift::recordRequiredImportAccessLevelForDecl (const ValueDecl *decl,
2566+ const DeclContext *dc,
2567+ AccessLevel accessLevel,
2568+ SourceLoc loc) {
2569+ recordRequiredImportAccessLevelForDecl (
2570+ decl, dc, accessLevel,
2571+ [&](AttributedImport<ImportedModule> attributedImport) {
2572+ SourceLoc diagLoc =
2573+ loc.isValid () ? loc : extractNearestSourceLoc (dc);
2574+ ModuleDecl *importedVia = attributedImport.module .importedModule ,
2575+ *sourceModule = decl->getModuleContext ();
2576+ dc->getASTContext ().Diags .diagnose (
2577+ diagLoc, diag::module_api_import, decl, importedVia, sourceModule,
2578+ importedVia == sourceModule, loc.isInvalid ());
2579+ });
2580+ }
2581+
25822582void swift::diagnoseUnnecessaryPublicImports (SourceFile &SF) {
25832583 ASTContext &ctx = SF.getASTContext ();
25842584 if (ctx.TypeCheckerOpts .SkipFunctionBodies != FunctionBodySkipping::None)
@@ -2649,15 +2649,8 @@ void registerPackageAccessForPackageExtendedType(ExtensionDecl *ED) {
26492649
26502650 // Remember that the module defining the decl must be imported with at least
26512651 // package visibility.
2652- recordRequiredImportAccessLevelForDecl (
2653- extendedType, DC, AccessLevel::Package,
2654- [&](AttributedImport<ImportedModule> attributedImport) {
2655- ModuleDecl *importedVia = attributedImport.module .importedModule ,
2656- *sourceModule = ED->getModuleContext ();
2657- ED->diagnose (diag::module_api_import, ED, importedVia, sourceModule,
2658- importedVia == sourceModule,
2659- /* isImplicit*/ false );
2660- });
2652+ recordRequiredImportAccessLevelForDecl (extendedType, DC, AccessLevel::Package,
2653+ ED->getLoc ());
26612654}
26622655
26632656void swift::checkAccessControl (Decl *D) {
0 commit comments