Skip to content

Commit e7902db

Browse files
authored
Merge pull request #83865 from tshortli/simplify-require-access-level-tracking
2 parents d1a1f70 + e5adbe9 commit e7902db

File tree

4 files changed

+36
-50
lines changed

4 files changed

+36
-50
lines changed

lib/Sema/ResilienceDiagnostics.cpp

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,7 @@ bool TypeChecker::diagnoseInlinableDeclRefAccess(SourceLoc loc,
7676
}
7777

7878
// Remember that the module defining the decl must be imported publicly.
79-
recordRequiredImportAccessLevelForDecl(
80-
D, DC, AccessLevel::Public,
81-
[&](AttributedImport<ImportedModule> attributedImport) {
82-
ModuleDecl *importedVia = attributedImport.module.importedModule,
83-
*sourceModule = D->getModuleContext();
84-
Context.Diags.diagnose(loc, diag::module_api_import, D, importedVia,
85-
sourceModule, importedVia == sourceModule,
86-
/*isImplicit*/ false);
87-
});
79+
recordRequiredImportAccessLevelForDecl(D, DC, AccessLevel::Public, loc);
8880

8981
// General check on access-level of the decl.
9082
auto declAccessScope =

lib/Sema/TypeCheckAccess.cpp

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
25822582
void 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

26632656
void swift::checkAccessControl(Decl *D) {

lib/Sema/TypeCheckDistributed.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -711,15 +711,8 @@ void TypeChecker::checkDistributedActor(SourceFile *SF, NominalTypeDecl *nominal
711711

712712
auto &C = nominal->getASTContext();
713713
auto loc = nominal->getLoc();
714-
recordRequiredImportAccessLevelForDecl(
715-
C.getDistributedActorDecl(), nominal, nominal->getEffectiveAccess(),
716-
[&](AttributedImport<ImportedModule> attributedImport) {
717-
ModuleDecl *importedVia = attributedImport.module.importedModule,
718-
*sourceModule = nominal->getModuleContext();
719-
C.Diags.diagnose(loc, diag::module_api_import, nominal, importedVia,
720-
sourceModule, importedVia == sourceModule,
721-
/*isImplicit*/ false);
722-
});
714+
recordRequiredImportAccessLevelForDecl(C.getDistributedActorDecl(), nominal,
715+
nominal->getEffectiveAccess(), loc);
723716

724717
// ==== Constructors
725718
// --- Get the default initializer

lib/Sema/TypeChecker.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1509,12 +1509,20 @@ using RequiredImportAccessLevelCallback =
15091509
std::function<void(AttributedImport<ImportedModule>)>;
15101510

15111511
/// Make a note that uses of \p decl in \p dc require that the decl's defining
1512-
/// module be imported with an access level that is at least as permissive as \p
1513-
/// accessLevel.
1512+
/// module be imported with an access level that is at least as permissive as
1513+
/// \p accessLevel.
15141514
void recordRequiredImportAccessLevelForDecl(
15151515
const Decl *decl, const DeclContext *dc, AccessLevel accessLevel,
15161516
RequiredImportAccessLevelCallback remark);
15171517

1518+
/// Make a note that uses of \p decl in \p dc require that the decl's defining
1519+
/// module be imported with an access level that is at least as permissive as
1520+
/// \p accessLevel. If `-Rmodule-api-import` is specified, a remark is emitted.
1521+
void recordRequiredImportAccessLevelForDecl(const ValueDecl *decl,
1522+
const DeclContext *dc,
1523+
AccessLevel accessLevel,
1524+
SourceLoc loc);
1525+
15181526
/// Report imports that are marked public but are not used in API.
15191527
void diagnoseUnnecessaryPublicImports(SourceFile &SF);
15201528

0 commit comments

Comments
 (0)