Skip to content

[4.2] [Serialization] Track whether a cross-reference came from Clang #17377

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions include/swift/Serialization/ModuleFormat.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const uint16_t VERSION_MAJOR = 0;
/// describe what change you made. The content of this comment isn't important;
/// it just ensures a conflict if two people change the module format.
/// Don't worry about adhering to the 80-column limit for this line.
const uint16_t VERSION_MINOR = 413; // Last change: Remove discriminator from LocalDeclTableInfo.
const uint16_t VERSION_MINOR = 414; // Last change: track whether xrefs come from Clang

using DeclIDField = BCFixed<31>;

Expand Down Expand Up @@ -1316,21 +1316,24 @@ namespace decls_block {
XREF_TYPE_PATH_PIECE,
IdentifierIDField, // name
IdentifierIDField, // private discriminator
BCFixed<1> // restrict to protocol extension
BCFixed<1>, // restrict to protocol extension
BCFixed<1> // imported from Clang?
>;

using XRefValuePathPieceLayout = BCRecordLayout<
XREF_VALUE_PATH_PIECE,
TypeIDField, // type
IdentifierIDField, // name
BCFixed<1>, // restrict to protocol extension
BCFixed<1>, // imported from Clang?
BCFixed<1> // static?
>;

using XRefInitializerPathPieceLayout = BCRecordLayout<
XREF_INITIALIZER_PATH_PIECE,
TypeIDField, // type
BCFixed<1>, // restrict to protocol extension
BCFixed<1>, // imported from Clang?
CtorInitializerKindField // initializer kind
>;

Expand Down
33 changes: 20 additions & 13 deletions lib/Serialization/Deserialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1176,7 +1176,8 @@ static bool reExportedToSameModule(const ModuleDecl *fromModule,
/// from Clang can also appear in any module.
static void filterValues(Type expectedTy, ModuleDecl *expectedModule,
CanGenericSignature expectedGenericSig, bool isType,
bool inProtocolExt, bool isStatic,
bool inProtocolExt, bool importedFromClang,
bool isStatic,
Optional<swift::CtorInitializerKind> ctorInit,
SmallVectorImpl<ValueDecl *> &values) {
CanType canTy;
Expand All @@ -1198,6 +1199,8 @@ static void filterValues(Type expectedTy, ModuleDecl *expectedModule,
return true;
if (value->isStatic() != isStatic)
return true;
if (value->hasClangNode() != importedFromClang)
return true;

if (value->getAttrs().hasAttribute<ForbidSerializingReferenceAttr>())
return true;
Expand Down Expand Up @@ -1275,13 +1278,14 @@ ModuleFile::resolveCrossReference(ModuleDecl *baseModule, uint32_t pathLen) {
TypeID TID = 0;
bool isType = (recordID == XREF_TYPE_PATH_PIECE);
bool inProtocolExt = false;
bool importedFromClang = false;
bool isStatic = false;
if (isType)
XRefTypePathPieceLayout::readRecord(scratch, IID, privateDiscriminator,
inProtocolExt);
inProtocolExt, importedFromClang);
else
XRefValuePathPieceLayout::readRecord(scratch, TID, IID, inProtocolExt,
isStatic);
importedFromClang, isStatic);

DeclBaseName name = getDeclBaseName(IID);
pathTrace.addValue(name);
Expand Down Expand Up @@ -1309,8 +1313,8 @@ ModuleFile::resolveCrossReference(ModuleDecl *baseModule, uint32_t pathLen) {
NL_QualifiedDefault | NL_KnownNoDependency,
/*typeResolver=*/nullptr, values);
}
filterValues(filterTy, nullptr, nullptr, isType, inProtocolExt, isStatic,
None, values);
filterValues(filterTy, nullptr, nullptr, isType, inProtocolExt,
importedFromClang, isStatic, None, values);
break;
}

Expand Down Expand Up @@ -1364,13 +1368,14 @@ ModuleFile::resolveCrossReference(ModuleDecl *baseModule, uint32_t pathLen) {
switch (recordID) {
case XREF_TYPE_PATH_PIECE: {
IdentifierID IID;
XRefTypePathPieceLayout::readRecord(scratch, IID, None, None);
XRefTypePathPieceLayout::readRecord(scratch, IID, None, None, None);
result = getIdentifier(IID);
break;
}
case XREF_VALUE_PATH_PIECE: {
IdentifierID IID;
XRefValuePathPieceLayout::readRecord(scratch, None, IID, None, None);
XRefValuePathPieceLayout::readRecord(scratch, None, IID, None, None,
None);
result = getIdentifier(IID);
break;
}
Expand Down Expand Up @@ -1423,8 +1428,9 @@ ModuleFile::resolveCrossReference(ModuleDecl *baseModule, uint32_t pathLen) {
IdentifierID IID;
IdentifierID privateDiscriminator;
bool onlyInNominal = false;
bool importedFromClang = false;
XRefTypePathPieceLayout::readRecord(scratch, IID, privateDiscriminator,
onlyInNominal);
onlyInNominal, importedFromClang);
if (privateDiscriminator)
goto giveUpFastPath;

Expand Down Expand Up @@ -1481,12 +1487,13 @@ ModuleFile::resolveCrossReference(ModuleDecl *baseModule, uint32_t pathLen) {
Optional<swift::CtorInitializerKind> ctorInit;
bool isType = false;
bool inProtocolExt = false;
bool importedFromClang = false;
bool isStatic = false;
switch (recordID) {
case XREF_TYPE_PATH_PIECE: {
IdentifierID IID, discriminatorID;
XRefTypePathPieceLayout::readRecord(scratch, IID, discriminatorID,
inProtocolExt);
inProtocolExt, importedFromClang);
memberName = getDeclBaseName(IID);
privateDiscriminator = getIdentifier(discriminatorID);
isType = true;
Expand All @@ -1496,15 +1503,15 @@ ModuleFile::resolveCrossReference(ModuleDecl *baseModule, uint32_t pathLen) {
case XREF_VALUE_PATH_PIECE: {
IdentifierID IID;
XRefValuePathPieceLayout::readRecord(scratch, TID, IID, inProtocolExt,
isStatic);
importedFromClang, isStatic);
memberName = getDeclBaseName(IID);
break;
}

case XREF_INITIALIZER_PATH_PIECE: {
uint8_t kind;
XRefInitializerPathPieceLayout::readRecord(scratch, TID, inProtocolExt,
kind);
importedFromClang, kind);
memberName = DeclBaseName::createConstructor();
ctorInit = getActualCtorInitializerKind(kind);
break;
Expand Down Expand Up @@ -1557,8 +1564,8 @@ ModuleFile::resolveCrossReference(ModuleDecl *baseModule, uint32_t pathLen) {
auto members = nominal->lookupDirect(memberName);
values.append(members.begin(), members.end());
}
filterValues(filterTy, M, genericSig, isType, inProtocolExt, isStatic,
ctorInit, values);
filterValues(filterTy, M, genericSig, isType, inProtocolExt,
importedFromClang, isStatic, ctorInit, values);
break;
}

Expand Down
18 changes: 10 additions & 8 deletions lib/Serialization/Serialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1902,7 +1902,8 @@ void Serializer::writeCrossReference(const DeclContext *DC, uint32_t pathLen) {
XRefTypePathPieceLayout::emitRecord(Out, ScratchRecord, abbrCode,
addDeclBaseNameRef(generic->getName()),
addDeclBaseNameRef(discriminator),
false);
/*inProtocolExtension*/false,
generic->hasClangNode());
break;
}

Expand Down Expand Up @@ -1933,7 +1934,8 @@ void Serializer::writeCrossReference(const DeclContext *DC, uint32_t pathLen) {
bool isProtocolExt = SD->getDeclContext()->getAsProtocolExtensionContext();
XRefValuePathPieceLayout::emitRecord(Out, ScratchRecord, abbrCode,
addTypeRef(ty), SUBSCRIPT_ID,
isProtocolExt, SD->isStatic());
isProtocolExt, SD->hasClangNode(),
SD->isStatic());
break;
}

Expand All @@ -1949,6 +1951,7 @@ void Serializer::writeCrossReference(const DeclContext *DC, uint32_t pathLen) {
XRefValuePathPieceLayout::emitRecord(Out, ScratchRecord, abbrCode,
addTypeRef(ty), nameID,
isProtocolExt,
storage->hasClangNode(),
storage->isStatic());

abbrCode =
Expand All @@ -1973,6 +1976,7 @@ void Serializer::writeCrossReference(const DeclContext *DC, uint32_t pathLen) {
XRefInitializerPathPieceLayout::emitRecord(
Out, ScratchRecord, abbrCode, addTypeRef(ty),
(bool)ctor->getDeclContext()->getAsProtocolExtensionContext(),
ctor->hasClangNode(),
getStableCtorInitializerKind(ctor->getInitKind()));
break;
}
Expand All @@ -1982,7 +1986,7 @@ void Serializer::writeCrossReference(const DeclContext *DC, uint32_t pathLen) {
XRefValuePathPieceLayout::emitRecord(Out, ScratchRecord, abbrCode,
addTypeRef(ty),
addDeclBaseNameRef(fn->getBaseName()),
isProtocolExt,
isProtocolExt, fn->hasClangNode(),
fn->isStatic());

if (fn->isOperator()) {
Expand Down Expand Up @@ -2062,7 +2066,7 @@ void Serializer::writeCrossReference(const Decl *D) {
XRefTypePathPieceLayout::emitRecord(Out, ScratchRecord, abbrCode,
addDeclBaseNameRef(type->getName()),
addDeclBaseNameRef(discriminator),
isProtocolExt);
isProtocolExt, D->hasClangNode());
return;
}

Expand All @@ -2071,10 +2075,8 @@ void Serializer::writeCrossReference(const Decl *D) {
abbrCode = DeclTypeAbbrCodes[XRefValuePathPieceLayout::Code];
IdentifierID iid = addDeclBaseNameRef(val->getBaseName());
XRefValuePathPieceLayout::emitRecord(Out, ScratchRecord, abbrCode,
addTypeRef(ty),
iid,
isProtocolExt,
val->isStatic());
addTypeRef(ty), iid, isProtocolExt,
D->hasClangNode(), val->isStatic());
}

/// Translate from the AST associativity enum to the Serialization enum
Expand Down
18 changes: 18 additions & 0 deletions validation-test/Serialization/rdar40839486.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// RUN: %empty-directory(%t)
// RUN: %target-build-swift -emit-module-path %t/main4.swiftmodule -swift-version 4 %s
// RUN: %target-build-swift -emit-module-path %t/main4_2.swiftmodule -swift-version 4.2 %s

// REQUIRES: OS=macosx || OS=ios

import CloudKit

@available(macOS 10.10, iOS 8, *)
extension CKRecord {
@inlinable public func testMethod() -> Any? {
return self.object(forKey: "abc" as CKRecord.FieldKey)
}

@inlinable public func testSubscript() -> Any? {
return self["abc" as CKRecord.FieldKey]
}
}