Skip to content

Commit 3feb91b

Browse files
committed
[Serialization] Explicitly convert to VarDecl::Specifier when deserializing.
Previously this just relied on serialization::VarDeclSpecifier being identical to swift::VarDecl::Specifier, which is presumably true, but also fragile.
1 parent 12871d7 commit 3feb91b

File tree

1 file changed

+40
-9
lines changed

1 file changed

+40
-9
lines changed

lib/Serialization/Deserialization.cpp

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2078,6 +2078,24 @@ Optional<swift::ResilienceExpansion> getActualResilienceExpansion(uint8_t raw) {
20782078
return None;
20792079
}
20802080

2081+
/// Translate from the serialization VarDeclSpecifier enumerators, which are
2082+
/// guaranteed to be stable, to the AST ones.
2083+
static Optional<swift::VarDecl::Specifier>
2084+
getActualVarDeclSpecifier(serialization::VarDeclSpecifier raw) {
2085+
switch (raw) {
2086+
#define CASE(ID) \
2087+
case serialization::VarDeclSpecifier::ID: \
2088+
return swift::VarDecl::Specifier::ID;
2089+
CASE(Let)
2090+
CASE(Var)
2091+
CASE(InOut)
2092+
CASE(Shared)
2093+
}
2094+
#undef CASE
2095+
return None;
2096+
}
2097+
2098+
20812099
void ModuleFile::configureStorage(AbstractStorageDecl *decl,
20822100
unsigned rawStorageKind,
20832101
serialization::DeclID getter,
@@ -2894,15 +2912,15 @@ ModuleFile::getDeclCheckedImpl(DeclID DID, Optional<DeclContext *> ForcedContext
28942912
DeclContextID contextID;
28952913
bool isImplicit, isObjC, isStatic, hasNonPatternBindingInit;
28962914
bool isGetterMutating, isSetterMutating;
2897-
unsigned specifier;
2915+
unsigned rawSpecifier;
28982916
uint8_t storageKind, rawAccessLevel, rawSetterAccessLevel;
28992917
TypeID interfaceTypeID;
29002918
DeclID getterID, setterID, materializeForSetID, willSetID, didSetID;
29012919
DeclID addressorID, mutableAddressorID, overriddenID;
29022920
ArrayRef<uint64_t> dependencyIDs;
29032921

29042922
decls_block::VarLayout::readRecord(scratch, nameID, contextID,
2905-
isImplicit, isObjC, isStatic, specifier,
2923+
isImplicit, isObjC, isStatic, rawSpecifier,
29062924
hasNonPatternBindingInit,
29072925
isGetterMutating, isSetterMutating,
29082926
storageKind, interfaceTypeID,
@@ -2954,9 +2972,15 @@ ModuleFile::getDeclCheckedImpl(DeclID DID, Optional<DeclContext *> ForcedContext
29542972
if (declOrOffset.isComplete())
29552973
return declOrOffset;
29562974

2957-
auto var = createDecl<VarDecl>(/*IsStatic*/isStatic,
2958-
(VarDecl::Specifier)specifier,
2959-
/*IsCaptureList*/false, SourceLoc(), name,
2975+
auto specifier = getActualVarDeclSpecifier(
2976+
(serialization::VarDeclSpecifier)rawSpecifier);
2977+
if (!specifier) {
2978+
error();
2979+
return nullptr;
2980+
}
2981+
2982+
auto var = createDecl<VarDecl>(/*IsStatic*/ isStatic, *specifier,
2983+
/*IsCaptureList*/ false, SourceLoc(), name,
29602984
Type(), DC);
29612985
var->setHasNonPatternBindingInit(hasNonPatternBindingInit);
29622986
var->setIsGetterMutating(isGetterMutating);
@@ -3003,18 +3027,25 @@ ModuleFile::getDeclCheckedImpl(DeclID DID, Optional<DeclContext *> ForcedContext
30033027
case decls_block::PARAM_DECL: {
30043028
IdentifierID argNameID, paramNameID;
30053029
DeclContextID contextID;
3006-
unsigned specifier;
3030+
unsigned rawSpecifier;
30073031
TypeID interfaceTypeID;
30083032

30093033
decls_block::ParamLayout::readRecord(scratch, argNameID, paramNameID,
3010-
contextID, specifier, interfaceTypeID);
3034+
contextID, rawSpecifier,
3035+
interfaceTypeID);
30113036

30123037
auto DC = ForcedContext ? *ForcedContext : getDeclContext(contextID);
30133038
if (declOrOffset.isComplete())
30143039
return declOrOffset;
30153040

3016-
auto param = createDecl<ParamDecl>((VarDecl::Specifier)specifier,
3017-
SourceLoc(), SourceLoc(),
3041+
auto specifier = getActualVarDeclSpecifier(
3042+
(serialization::VarDeclSpecifier)rawSpecifier);
3043+
if (!specifier) {
3044+
error();
3045+
return nullptr;
3046+
}
3047+
3048+
auto param = createDecl<ParamDecl>(*specifier, SourceLoc(), SourceLoc(),
30183049
getIdentifier(argNameID), SourceLoc(),
30193050
getIdentifier(paramNameID), Type(), DC);
30203051

0 commit comments

Comments
 (0)