Skip to content

Commit bb134f0

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 acf4e28 commit bb134f0

File tree

10 files changed

+18
-28
lines changed

10 files changed

+18
-28
lines changed

lib/AST/Decl.cpp

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4135,16 +4135,7 @@ AccessLevel ValueDecl::getEffectiveAccess() const {
41354135
// Handle @testable/@_private(sourceFile:)
41364136
switch (effectiveAccess) {
41374137
case AccessLevel::Open:
4138-
break;
4139-
case AccessLevel::Package:
4140-
if (getModuleContext()->isTestingEnabled() ||
4141-
getModuleContext()->arePrivateImportsEnabled()) {
4142-
effectiveAccess = getMaximallyOpenAccessFor(this);
4143-
} else {
4144-
// Package declarations are effectively public within their
4145-
// package unit.
4146-
effectiveAccess = AccessLevel::Public;
4147-
}
4138+
case AccessLevel::Package: // Don't elevate package level with @testable
41484139
break;
41494140
case AccessLevel::Public:
41504141
case AccessLevel::Internal:
@@ -5877,9 +5868,9 @@ bool ClassDecl::hasResilientMetadata() const {
58775868
if (!getModuleContext()->isResilient())
58785869
return false;
58795870

5880-
// If the class is not public, we can't use it outside the module at all.
5871+
// If the class is not public or package, we can't use it outside the module at all.
58815872
// Take enable testing into account.
5882-
if (getEffectiveAccess() < AccessLevel::Public)
5873+
if (getEffectiveAccess() < AccessLevel::Package)
58835874
return false;
58845875

58855876
// 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
@@ -466,7 +466,7 @@ swift::FragileFunctionKindRequest::evaluate(Evaluator &evaluator,
466466
auto effectiveAccess =
467467
VD->getFormalAccessScope(/*useDC=*/nullptr,
468468
/*treatUsableFromInlineAsPublic=*/true);
469-
if (effectiveAccess.isPublic()) {
469+
if (effectiveAccess.isPublic() || effectiveAccess.isPackage()) {
470470
return {FragileFunctionKind::DefaultArgument};
471471
}
472472

lib/IRGen/GenDecl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5943,7 +5943,7 @@ bool IRGenModule::hasResilientMetadata(ClassDecl *D,
59435943
ResilienceExpansion
59445944
IRGenModule::getResilienceExpansionForAccess(NominalTypeDecl *decl) {
59455945
if (decl->getModuleContext() == getSwiftModule() &&
5946-
decl->getEffectiveAccess() < AccessLevel::Public)
5946+
decl->getEffectiveAccess() < AccessLevel::Package)
59475947
return ResilienceExpansion::Maximal;
59485948
return ResilienceExpansion::Minimal;
59495949
}

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.
@@ -1080,7 +1080,7 @@ void SILGenModule::emitNonCopyableTypeDeinitTable(NominalTypeDecl *nom) {
10801080
auto serialized = IsSerialized_t::IsNotSerialized;
10811081
bool nomIsPublic = nom->getEffectiveAccess() >= AccessLevel::Public;
10821082
// We only serialize the deinit if the type is public and not resilient.
1083-
if (nomIsPublic && !nom->hasPackageAccess() && !nom->isResilient())
1083+
if (nomIsPublic && !nom->isResilient())
10841084
serialized = IsSerialized;
10851085
SILMoveOnlyDeinit::create(f->getModule(), nom, serialized, f);
10861086
}

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)