Skip to content

Commit a91bb49

Browse files
committed
[Serialization] Tweaks to deserialization safety writer logic
Apply reviewers comments and fix test.
1 parent 77d8c46 commit a91bb49

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

lib/Serialization/Serialization.cpp

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3099,22 +3099,20 @@ class Serializer::DeclSerializer : public DeclVisitor<DeclSerializer> {
30993099
/// XRef errors and such.
31003100
///
31013101
/// \p decl should be either an \c ExtensionDecl or a \c ValueDecl.
3102-
static bool declIsDeserializationSafe(const Decl *decl) {
3102+
static bool isDeserializationSafe(const Decl *decl) {
31033103
if (auto ext = dyn_cast<ExtensionDecl>(decl)) {
31043104
// Consider extensions as safe as their extended type.
31053105
auto nominalType = ext->getExtendedNominal();
31063106
if (!nominalType ||
3107-
!declIsDeserializationSafe(nominalType))
3107+
!isDeserializationSafe(nominalType))
31083108
return false;
31093109

31103110
// We can mark the extension unsafe only if it has no public members.
31113111
auto members = ext->getMembers();
3112-
int membersCount = 0;
31133112
auto hasSafeMembers = std::any_of(members.begin(), members.end(),
3114-
[&membersCount](const Decl *D) -> bool {
3115-
membersCount ++;
3113+
[](const Decl *D) -> bool {
31163114
if (auto VD = dyn_cast<ValueDecl>(D))
3117-
return declIsDeserializationSafe(VD);
3115+
return isDeserializationSafe(VD);
31183116
return true;
31193117
});
31203118
if (hasSafeMembers)
@@ -3126,12 +3124,12 @@ class Serializer::DeclSerializer : public DeclVisitor<DeclSerializer> {
31263124
ConformanceLookupKind::OnlyExplicit);
31273125
bool hasSafeConformances = std::any_of(protocols.begin(),
31283126
protocols.end(),
3129-
declIsDeserializationSafe);
3127+
isDeserializationSafe);
31303128
if (hasSafeConformances)
31313129
return true;
31323130

31333131
// Truly empty extensions are safe, it may happen in swiftinterfaces.
3134-
if (membersCount == 0 && protocols.size() == 0)
3132+
if (members.empty() && protocols.size() == 0)
31353133
return true;
31363134

31373135
return false;
@@ -3152,7 +3150,7 @@ class Serializer::DeclSerializer : public DeclVisitor<DeclSerializer> {
31523150

31533151
if (auto accessor = dyn_cast<AccessorDecl>(value))
31543152
// Accessors are as safe as their storage.
3155-
if (declIsDeserializationSafe(accessor->getStorage()))
3153+
if (isDeserializationSafe(accessor->getStorage()))
31563154
return true;
31573155

31583156
// Frozen fields are always safe.
@@ -3168,7 +3166,7 @@ class Serializer::DeclSerializer : public DeclVisitor<DeclSerializer> {
31683166

31693167
// Property wrappers storage is as safe as the wrapped property.
31703168
if (VarDecl *wrapped = var->getOriginalWrappedProperty())
3171-
if (declIsDeserializationSafe(wrapped))
3169+
if (isDeserializationSafe(wrapped))
31723170
return true;
31733171
}
31743172

@@ -3178,7 +3176,7 @@ class Serializer::DeclSerializer : public DeclVisitor<DeclSerializer> {
31783176
/// Write a \c DeserializationSafetyLayout record only when \p decl is unsafe
31793177
/// to deserialize.
31803178
///
3181-
/// \sa declIsDeserializationSafe
3179+
/// \sa isDeserializationSafe
31823180
void writeDeserializationSafety(const Decl *decl) {
31833181
using namespace decls_block;
31843182

@@ -3217,7 +3215,7 @@ class Serializer::DeclSerializer : public DeclVisitor<DeclSerializer> {
32173215
// Don't look at decls inside functions and
32183216
// check the ValueDecls themselves.
32193217
auto declIsSafe = DC->isLocalContext() ||
3220-
declIsDeserializationSafe(decl);
3218+
isDeserializationSafe(decl);
32213219
#ifdef NDEBUG
32223220
// In release builds, bail right away if the decl is safe.
32233221
// In debug builds, wait to bail after the debug prints and asserts.
@@ -3246,6 +3244,7 @@ class Serializer::DeclSerializer : public DeclVisitor<DeclSerializer> {
32463244
);
32473245

32483246
#ifndef NDEBUG
3247+
// Bail out here in debug builds, release builds would bailed out earlier.
32493248
if (declIsSafe)
32503249
return;
32513250
#endif

test/Serialization/Safety/unsafe-decls.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public struct PublicStruct {
7272
// SAFETY-PRIVATE: Serialization safety, safe: 'inlinableFunc()'
7373
public func publicFunc() {}
7474
// SAFETY-PRIVATE: Serialization safety, safe: 'publicFunc()'
75+
@available(SwiftStdlib 5.1, *) // for the `some` keyword.
7576
public func opaqueTypeFunc() -> some PublicProto {
7677
return InternalStruct()
7778
}

0 commit comments

Comments
 (0)