Skip to content

Commit ab3b5ad

Browse files
committed
Currently decls defined with package access level have a different
access level for optimization: `public`. It requires an extra check for the actual access level that was declared when determining serialization since the behavior should be different. This PR sets its effective access level to `package` as originally defined, updates call sites to make appropriate acces level comparisons, and removes `package` specific checks.
1 parent b4dbd97 commit ab3b5ad

File tree

10 files changed

+17
-28
lines changed

10 files changed

+17
-28
lines changed

lib/AST/Decl.cpp

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4157,17 +4157,7 @@ AccessLevel ValueDecl::getEffectiveAccess() const {
41574157
// Handle @testable/@_private(sourceFile:)
41584158
switch (effectiveAccess) {
41594159
case AccessLevel::Open:
4160-
break;
41614160
case AccessLevel::Package:
4162-
if (getModuleContext()->isTestingEnabled() ||
4163-
getModuleContext()->arePrivateImportsEnabled()) {
4164-
effectiveAccess = getMaximallyOpenAccessFor(this);
4165-
} else {
4166-
// Package declarations are effectively public within their
4167-
// package unit.
4168-
effectiveAccess = AccessLevel::Public;
4169-
}
4170-
break;
41714161
case AccessLevel::Public:
41724162
case AccessLevel::Internal:
41734163
if (getModuleContext()->isTestingEnabled() ||
@@ -5900,9 +5890,9 @@ bool ClassDecl::hasResilientMetadata() const {
59005890
if (!getModuleContext()->isResilient())
59015891
return false;
59025892

5903-
// If the class is not public, we can't use it outside the module at all.
5893+
// If the class is not public or package, we can't use it outside the module at all.
59045894
// Take enable testing into account.
5905-
if (getEffectiveAccess() < AccessLevel::Public)
5895+
if (getEffectiveAccess() < AccessLevel::Package)
59065896
return false;
59075897

59085898
// Otherwise we access metadata members, such as vtable entries, resiliently.

lib/AST/DeclContext.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ swift::FragileFunctionKindRequest::evaluate(Evaluator &evaluator,
467467
auto effectiveAccess =
468468
VD->getFormalAccessScope(/*useDC=*/nullptr,
469469
/*treatUsableFromInlineAsPublic=*/true);
470-
if (effectiveAccess.isPublic()) {
470+
if (effectiveAccess.isPublic() || effectiveAccess.isPackage()) {
471471
return {FragileFunctionKind::DefaultArgument};
472472
}
473473

lib/IRGen/GenDecl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5955,7 +5955,7 @@ bool IRGenModule::hasResilientMetadata(ClassDecl *D,
59555955
ResilienceExpansion
59565956
IRGenModule::getResilienceExpansionForAccess(NominalTypeDecl *decl) {
59575957
if (decl->getModuleContext() == getSwiftModule() &&
5958-
decl->getEffectiveAccess() < AccessLevel::Public)
5958+
decl->getEffectiveAccess() < AccessLevel::Package)
59595959
return ResilienceExpansion::Maximal;
59605960
return ResilienceExpansion::Minimal;
59615961
}

lib/IRGen/GenMeta.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1883,7 +1883,7 @@ namespace {
18831883
// Emit method dispatch thunk if the class is resilient.
18841884
auto *func = cast<AbstractFunctionDecl>(fn.getDecl());
18851885

1886-
if ((Resilient && func->getEffectiveAccess() >= AccessLevel::Public) ||
1886+
if ((Resilient && func->getEffectiveAccess() >= AccessLevel::Package) ||
18871887
IGM.getOptions().VirtualFunctionElimination) {
18881888
IGM.emitDispatchThunk(fn);
18891889
}
@@ -6922,7 +6922,7 @@ bool irgen::methodRequiresReifiedVTableEntry(IRGenModule &IGM,
69226922
auto originatingClass =
69236923
cast<ClassDecl>(method.getOverriddenVTableEntry().getDecl()->getDeclContext());
69246924

6925-
if (originatingClass->getEffectiveAccess() >= AccessLevel::Public) {
6925+
if (originatingClass->getEffectiveAccess() >= AccessLevel::Package) {
69266926
// If the class is public,
69276927
// and it's either marked fragile or part of a non-resilient module, then
69286928
// other modules will directly address vtable offsets and we can't remove
@@ -6932,7 +6932,7 @@ bool irgen::methodRequiresReifiedVTableEntry(IRGenModule &IGM,
69326932
<< vtable->getClass()->getName()
69336933
<< " for ";
69346934
method.print(llvm::dbgs());
6935-
llvm::dbgs() << " originates from a public fragile class\n");
6935+
llvm::dbgs() << " originates from a public/package fragile class\n");
69366936
return true;
69376937
}
69386938
}

lib/IRGen/TypeLayoutDumper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ static void addYAMLTypeInfoNode(NominalTypeDecl *NTD,
8383
IRGenModule &IGM,
8484
std::vector<YAMLTypeInfoNode> &Result) {
8585
// We only care about public and @usableFromInline declarations.
86-
if (NTD->getEffectiveAccess() < AccessLevel::Public)
86+
if (NTD->getEffectiveAccess() < AccessLevel::Package)
8787
return;
8888

8989
// We don't care about protocols or classes.

lib/SIL/IR/SILWitnessTable.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,7 @@ bool SILWitnessTable::conformanceIsSerialized(
171171
if (normalConformance && normalConformance->isResilient())
172172
return false;
173173

174-
if (conformance->getProtocol()->getEffectiveAccess() < AccessLevel::Public ||
175-
conformance->getProtocol()->hasPackageAccess())
174+
if (conformance->getProtocol()->getEffectiveAccess() < AccessLevel::Package)
176175
return false;
177176

178177
auto *nominal = conformance->getDeclContext()->getSelfNominalTypeDecl();

lib/SIL/Verifier/SILVerifier.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ void verifyKeyPathComponent(SILModule &M,
251251
case KeyPathPatternComponent::Kind::StoredProperty: {
252252
auto property = component.getStoredPropertyDecl();
253253
if (expansion == ResilienceExpansion::Minimal) {
254-
require(property->getEffectiveAccess() >= AccessLevel::Public,
254+
require(property->getEffectiveAccess() >= AccessLevel::Package,
255255
"Key path in serialized function cannot reference non-public "
256256
"property");
257257
}

lib/SILGen/SILGenType.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ class SILGenVTable : public SILVTableVisitor<SILGenVTable> {
307307
IsSerialized_t serialized = IsNotSerialized;
308308
auto classIsPublic = theClass->getEffectiveAccess() >= AccessLevel::Public;
309309
// Only public, fixed-layout classes should have serialized vtables.
310-
if (classIsPublic && !theClass->hasPackageAccess() && !isResilient)
310+
if (classIsPublic && !isResilient)
311311
serialized = IsSerialized;
312312

313313
// Finally, create the vtable.
@@ -1093,7 +1093,7 @@ void SILGenModule::emitNonCopyableTypeDeinitTable(NominalTypeDecl *nom) {
10931093
auto serialized = IsSerialized_t::IsNotSerialized;
10941094
bool nomIsPublic = nom->getEffectiveAccess() >= AccessLevel::Public;
10951095
// We only serialize the deinit if the type is public and not resilient.
1096-
if (nomIsPublic && !nom->hasPackageAccess() && !nom->isResilient())
1096+
if (nomIsPublic && !nom->isResilient())
10971097
serialized = IsSerialized;
10981098
SILMoveOnlyDeinit::create(f->getModule(), nom, serialized, f);
10991099
}

lib/SILOptimizer/IPO/CrossModuleOptimization.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ bool CrossModuleOptimization::canSerializeInstruction(SILInstruction *inst,
328328
// properties, because that would require to make the field decl public -
329329
// which keeps more metadata alive.
330330
return !conservative ||
331-
REAI->getField()->getEffectiveAccess() >= AccessLevel::Public;
331+
REAI->getField()->getEffectiveAccess() >= AccessLevel::Package;
332332
}
333333
return true;
334334
}
@@ -365,7 +365,7 @@ bool CrossModuleOptimization::canSerializeType(SILType type) {
365365
CanType subType = rawSubType->getCanonicalType();
366366
if (NominalTypeDecl *subNT = subType->getNominalOrBoundGenericNominal()) {
367367

368-
if (conservative && subNT->getEffectiveAccess() < AccessLevel::Public) {
368+
if (conservative && subNT->getEffectiveAccess() < AccessLevel::Package) {
369369
return true;
370370
}
371371

@@ -596,7 +596,7 @@ void CrossModuleOptimization::makeFunctionUsableFromInline(SILFunction *function
596596

597597
/// Make a nominal type, including it's context, usable from inline.
598598
void CrossModuleOptimization::makeDeclUsableFromInline(ValueDecl *decl) {
599-
if (decl->getEffectiveAccess() >= AccessLevel::Public)
599+
if (decl->getEffectiveAccess() >= AccessLevel::Package)
600600
return;
601601

602602
// We must not modify decls which are defined in other modules.

lib/Serialization/SerializeSIL.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2880,7 +2880,7 @@ void SILSerializer::writeSILVTable(const SILVTable &vt) {
28802880
// Do not emit vtables for non-public classes unless everything has to be
28812881
// serialized.
28822882
if (!ShouldSerializeAll &&
2883-
vt.getClass()->getEffectiveAccess() < swift::AccessLevel::Public)
2883+
vt.getClass()->getEffectiveAccess() < swift::AccessLevel::Package)
28842884
return;
28852885

28862886
if (vt.isSpecialized())
@@ -2924,7 +2924,7 @@ void SILSerializer::writeSILMoveOnlyDeinit(const SILMoveOnlyDeinit &deinit) {
29242924
// Do not emit deinit for non-public nominal types unless everything has to be
29252925
// serialized.
29262926
if (!ShouldSerializeAll && deinit.getNominalDecl()->getEffectiveAccess() <
2927-
swift::AccessLevel::Public)
2927+
swift::AccessLevel::Package)
29282928
return;
29292929

29302930
SILFunction *impl = deinit.getImplementation();

0 commit comments

Comments
 (0)