Skip to content

Commit 57a6bcc

Browse files
committed
Sema: New StrictAccessControl upcoming feature
Some access control holes were unconditionally downgraded to warnings, and others were conditional on Swift 6 language mode. Let's hang them off an upcoming feature instead.
1 parent 3e2943f commit 57a6bcc

File tree

5 files changed

+39
-24
lines changed

5 files changed

+39
-24
lines changed

include/swift/Basic/Features.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ MIGRATABLE_UPCOMING_FEATURE(MemberImportVisibility, 444, 7)
302302
MIGRATABLE_UPCOMING_FEATURE(InferIsolatedConformances, 470, 7)
303303
MIGRATABLE_UPCOMING_FEATURE(NonisolatedNonsendingByDefault, 461, 7)
304304
UPCOMING_FEATURE(ImmutableWeakCaptures, 481, 7)
305+
UPCOMING_FEATURE(StrictAccessControl, 0, 7)
305306

306307
// Optional language features / modes
307308

lib/AST/FeatureSet.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,7 @@ static bool usesFeatureTildeSendable(Decl *decl) {
467467
}
468468

469469
UNINTERESTING_FEATURE(AnyAppleOSAvailability)
470+
UNINTERESTING_FEATURE(StrictAccessControl)
470471

471472
// ----------------------------------------------------------------------------
472473
// MARK: - FeatureSet

lib/Sema/ResilienceDiagnostics.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,9 @@ bool TypeChecker::diagnoseInlinableDeclRefAccess(SourceLoc loc,
119119
}
120120

121121
// Swift 5.0 did not check the underlying types of local typealiases.
122-
if (isa<TypeAliasDecl>(DC) && !Context.isLanguageModeAtLeast(6))
122+
if (isa<TypeAliasDecl>(DC) &&
123+
!Context.hasFeature(Feature::StrictAccessControl) &&
124+
!Context.isLanguageModeAtLeast(6))
123125
downgradeToWarning = DowngradeToWarning::Yes;
124126

125127
auto diagID = diag::resilience_decl_unavailable;
@@ -207,7 +209,8 @@ static bool diagnoseTypeAliasDeclRefExportability(SourceLoc loc,
207209
}
208210
D->diagnose(diag::kind_declared_here, DescriptiveDeclKind::Type);
209211

210-
if (originKind == DisallowedOriginKind::MissingImport &&
212+
if (!ctx.hasFeature(Feature::StrictAccessControl) &&
213+
originKind == DisallowedOriginKind::MissingImport &&
211214
!ctx.isLanguageModeAtLeast(6))
212215
addMissingImport(loc, D, where);
213216

@@ -460,7 +463,8 @@ TypeChecker::diagnoseConformanceExportability(SourceLoc loc,
460463
originKind == DisallowedOriginKind::MissingImport,
461464
6);
462465

463-
if (originKind == DisallowedOriginKind::MissingImport &&
466+
if (!ctx.hasFeature(Feature::StrictAccessControl) &&
467+
originKind == DisallowedOriginKind::MissingImport &&
464468
!ctx.isLanguageModeAtLeast(6))
465469
addMissingImport(loc, ext, where);
466470

lib/Sema/TypeCheckAccess.cpp

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -550,11 +550,12 @@ void AccessControlCheckerBase::checkGenericParamAccess(
550550
if (minAccessScope.isPublic())
551551
return;
552552

553-
// FIXME: Promote these to an error in the next -swift-version break.
554-
if (isa<SubscriptDecl>(ownerDecl) || isa<TypeAliasDecl>(ownerDecl))
553+
auto &Context = ownerDecl->getASTContext();
554+
555+
if (!Context.hasFeature(Feature::StrictAccessControl) &&
556+
(isa<SubscriptDecl>(ownerDecl) || isa<TypeAliasDecl>(ownerDecl)))
555557
downgradeToWarning = DowngradeToWarning::Yes;
556558

557-
auto &Context = ownerDecl->getASTContext();
558559
if (checkUsableFromInline) {
559560
if (!Context.isLanguageModeAtLeast(5))
560561
downgradeToWarning = DowngradeToWarning::Yes;
@@ -2042,14 +2043,16 @@ swift::getDisallowedOriginKind(const Decl *decl,
20422043
downgradeToWarning = DowngradeToWarning::No;
20432044
ModuleDecl *M = decl->getModuleContext();
20442045
auto *SF = where.getDeclContext()->getParentSourceFile();
2046+
auto &Context = M->getASTContext();
20452047

20462048
RestrictedImportKind howImported = SF->getRestrictedImportKind(M);
20472049
if (howImported != RestrictedImportKind::None) {
20482050
// Temporarily downgrade implementation-only exportability in SPI to
20492051
// a warning.
2050-
if (where.isSPI() &&
2052+
if (!Context.hasFeature(Feature::StrictAccessControl) &&
2053+
where.isSPI() &&
20512054
where.getFragileFunctionKind().kind == FragileFunctionKind::None &&
2052-
!SF->getASTContext().LangOpts.EnableSPIOnlyImports)
2055+
!Context.LangOpts.EnableSPIOnlyImports)
20532056
downgradeToWarning = DowngradeToWarning::Yes;
20542057

20552058
if (where.isSPI() && howImported == RestrictedImportKind::SPIOnly)
@@ -2058,8 +2061,9 @@ swift::getDisallowedOriginKind(const Decl *decl,
20582061
// Before Swift 6, implicit imports were not reported unless an
20592062
// implementation-only import was also present. Downgrade to a warning
20602063
// just in this case.
2061-
if (howImported == RestrictedImportKind::MissingImport &&
2062-
!SF->getASTContext().isLanguageModeAtLeast(6) &&
2064+
if (!Context.hasFeature(Feature::StrictAccessControl) &&
2065+
howImported == RestrictedImportKind::MissingImport &&
2066+
!Context.isLanguageModeAtLeast(6) &&
20632067
!SF->hasImportsWithFlag(ImportFlags::ImplementationOnly)) {
20642068
downgradeToWarning = DowngradeToWarning::Yes;
20652069
}
@@ -2087,7 +2091,7 @@ swift::getDisallowedOriginKind(const Decl *decl,
20872091
if (!owningModule)
20882092
continue;
20892093
auto moduleWrapper =
2090-
decl->getASTContext().getClangModuleLoader()->getWrapperForModule(
2094+
Context.getClangModuleLoader()->getWrapperForModule(
20912095
owningModule);
20922096
auto visibleAccessPath =
20932097
find_if(sfImportedModules, [&moduleWrapper](auto importedModule) {
@@ -2150,7 +2154,7 @@ swift::getDisallowedOriginKind(const Decl *decl,
21502154
}
21512155

21522156
// C++ APIs do not support library evolution.
2153-
if (SF->getASTContext().LangOpts.EnableCXXInterop && where.getDeclContext() &&
2157+
if (Context.LangOpts.EnableCXXInterop && where.getDeclContext() &&
21542158
where.getDeclContext()->getAsDecl() &&
21552159
where.getDeclContext()->getAsDecl()->getModuleContext()->isResilient() &&
21562160
!where.getDeclContext()
@@ -2159,13 +2163,13 @@ swift::getDisallowedOriginKind(const Decl *decl,
21592163
->getUnderlyingModuleIfOverlay() &&
21602164
decl->hasClangNode() && !decl->getModuleContext()->isSwiftShimsModule() &&
21612165
isFragileClangNode(decl->getClangNode()) &&
2162-
!SF->getASTContext().LangOpts.hasFeature(
2166+
!Context.LangOpts.hasFeature(
21632167
Feature::AssumeResilientCxxTypes))
21642168
return DisallowedOriginKind::FragileCxxAPI;
21652169

21662170
// Implementation-only memory layouts for non-library-evolution mode.
21672171
if (isa<NominalTypeDecl>(decl) &&
2168-
decl->getASTContext().LangOpts.hasFeature(
2172+
Context.LangOpts.hasFeature(
21692173
Feature::CheckImplementationOnly) &&
21702174
decl->getAttrs().hasAttribute<ImplementationOnlyAttr>())
21712175
return DisallowedOriginKind::ImplementationOnlyMemoryLayout;

lib/Sema/TypeCheckAvailability.cpp

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,7 +1069,7 @@ static bool diagnosePotentialUnavailability(
10691069
// Don't downgrade
10701070
} else if (behaviorLimit >= DiagnosticBehavior::Warning) {
10711071
err.limitBehavior(behaviorLimit);
1072-
} else {
1072+
} else if (!Context.hasFeature(Feature::StrictAccessControl)) {
10731073
err.warnUntilLanguageMode(6);
10741074
}
10751075

@@ -2161,7 +2161,8 @@ bool diagnoseExplicitUnavailability(
21612161
// obsolete decls still map to valid ObjC runtime names, so behave correctly
21622162
// at runtime, even though their use would produce an error outside of a
21632163
// #keyPath expression.
2164-
auto limit = Flags.contains(DeclAvailabilityFlag::ForObjCKeyPath)
2164+
auto limit = (!ctx.hasFeature(Feature::StrictAccessControl) &&
2165+
Flags.contains(DeclAvailabilityFlag::ForObjCKeyPath))
21652166
? DiagnosticBehavior::Warning
21662167
: DiagnosticBehavior::Unspecified;
21672168

@@ -2950,10 +2951,12 @@ diagnoseDeclAsyncAvailability(const ValueDecl *D, SourceRange R,
29502951
attr->getMessage());
29512952
if (D->preconcurrency()) {
29522953
diag.limitBehavior(DiagnosticBehavior::Warning);
2953-
} else if (shouldWarnUntilFutureVersion()) {
2954-
diag.warnUntilFutureLanguageMode();
2955-
} else {
2956-
diag.warnUntilLanguageMode(6);
2954+
} else if (!ctx.hasFeature(Feature::StrictAccessControl)) {
2955+
if (shouldWarnUntilFutureVersion()) {
2956+
diag.warnUntilFutureLanguageMode();
2957+
} else {
2958+
diag.warnUntilLanguageMode(6);
2959+
}
29572960
}
29582961

29592962
if (!attr->getRename().empty()) {
@@ -2974,10 +2977,12 @@ diagnoseDeclAsyncAvailability(const ValueDecl *D, SourceRange R,
29742977
SourceLoc diagLoc = call ? call->getLoc() : R.Start;
29752978
auto diag = ctx.Diags.diagnose(diagLoc, diag::async_unavailable_decl, D,
29762979
attr->Message);
2977-
if (shouldWarnUntilFutureVersion()) {
2978-
diag.warnUntilFutureLanguageMode();
2979-
} else {
2980-
diag.warnUntilLanguageMode(6);
2980+
if (!ctx.hasFeature(Feature::StrictAccessControl)) {
2981+
if (shouldWarnUntilFutureVersion()) {
2982+
diag.warnUntilFutureLanguageMode();
2983+
} else {
2984+
diag.warnUntilLanguageMode(6);
2985+
}
29812986
}
29822987
}
29832988
D->diagnose(diag::decl_declared_here, D);

0 commit comments

Comments
 (0)