Skip to content

Commit c1e1cba

Browse files
committed
AST: teach getOpaqueResultTypeDecl() to get result for serialized AST
TBD was missing several opaque type descriptor symbols. The root causes are: (1) the AST API called by TBD doesn't return opaque type decl if the decl is from a serialized AST; and (2) the access level of opaque type decl isn't serialized so TBD considers them as internal. This change fixes both. rdar://61833970
1 parent cf3c74d commit c1e1cba

File tree

7 files changed

+43
-8
lines changed

7 files changed

+43
-8
lines changed

include/swift/AST/ASTMangler.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,8 @@ class ASTMangler : public Mangler {
238238

239239
std::string mangleOpaqueTypeDecl(const OpaqueTypeDecl *decl);
240240

241+
std::string mangleOpaqueTypeDecl(const ValueDecl *decl);
242+
241243
enum SpecialContext {
242244
ObjCContext,
243245
ClangImporterContext,

lib/AST/ASTMangler.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -693,9 +693,13 @@ std::string ASTMangler::mangleLocalTypeDecl(const TypeDecl *type) {
693693
}
694694

695695
std::string ASTMangler::mangleOpaqueTypeDecl(const OpaqueTypeDecl *decl) {
696+
return mangleOpaqueTypeDecl(decl->getNamingDecl());
697+
}
698+
699+
std::string ASTMangler::mangleOpaqueTypeDecl(const ValueDecl *decl) {
696700
DWARFMangling = true;
697701
OptimizeProtocolNames = false;
698-
return mangleDeclAsUSR(decl->getNamingDecl(), MANGLING_PREFIX_STR);
702+
return mangleDeclAsUSR(decl, MANGLING_PREFIX_STR);
699703
}
700704

701705
void ASTMangler::appendSymbolKind(SymbolKind SKind) {

lib/AST/Decl.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "swift/AST/AccessScope.h"
2020
#include "swift/AST/ASTContext.h"
2121
#include "swift/AST/ASTWalker.h"
22+
#include "swift/AST/ASTMangler.h"
2223
#include "swift/AST/DiagnosticEngine.h"
2324
#include "swift/AST/DiagnosticsSema.h"
2425
#include "swift/AST/ExistentialLayout.h"
@@ -2875,8 +2876,18 @@ OpaqueReturnTypeRepr *ValueDecl::getOpaqueResultTypeRepr() const {
28752876
}
28762877

28772878
OpaqueTypeDecl *ValueDecl::getOpaqueResultTypeDecl() const {
2878-
if (getOpaqueResultTypeRepr() == nullptr)
2879+
if (getOpaqueResultTypeRepr() == nullptr) {
2880+
if (isa<ModuleDecl>(this))
2881+
return nullptr;
2882+
auto file = cast<FileUnit>(getDeclContext()->getModuleScopeContext());
2883+
// Don't look up when the decl is from source, otherwise a cycle will happen.
2884+
if (file->getKind() == FileUnitKind::SerializedAST) {
2885+
Mangle::ASTMangler mangler;
2886+
auto name = mangler.mangleOpaqueTypeDecl(this);
2887+
return file->lookupOpaqueResultType(name);
2888+
}
28792889
return nullptr;
2890+
}
28802891

28812892
return evaluateOrDefault(getASTContext().evaluator,
28822893
OpaqueResultTypeRequest{const_cast<ValueDecl *>(this)},

lib/Serialization/Deserialization.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3230,11 +3230,11 @@ class DeclDeserializer {
32303230
TypeID interfaceTypeID;
32313231
GenericSignatureID genericSigID;
32323232
SubstitutionMapID underlyingTypeID;
3233-
3233+
uint8_t rawAccessLevel;
32343234
decls_block::OpaqueTypeLayout::readRecord(scratch, contextID,
32353235
namingDeclID, interfaceSigID,
32363236
interfaceTypeID, genericSigID,
3237-
underlyingTypeID);
3237+
underlyingTypeID, rawAccessLevel);
32383238

32393239
auto declContext = MF.getDeclContext(contextID);
32403240
auto interfaceSig = MF.getGenericSignature(interfaceSigID);
@@ -3254,6 +3254,11 @@ class DeclDeserializer {
32543254
auto namingDecl = cast<ValueDecl>(MF.getDecl(namingDeclID));
32553255
opaqueDecl->setNamingDecl(namingDecl);
32563256

3257+
if (auto accessLevel = getActualAccessLevel(rawAccessLevel))
3258+
opaqueDecl->setAccess(*accessLevel);
3259+
else
3260+
MF.fatal();
3261+
32573262
if (auto genericParams = MF.maybeReadGenericParams(opaqueDecl)) {
32583263
ctx.evaluator.cacheOutput(GenericParamListRequest{opaqueDecl},
32593264
std::move(genericParams));

lib/Serialization/ModuleFormat.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ const uint16_t SWIFTMODULE_VERSION_MAJOR = 0;
5555
/// describe what change you made. The content of this comment isn't important;
5656
/// it just ensures a conflict if two people change the module format.
5757
/// Don't worry about adhering to the 80-column limit for this line.
58-
const uint16_t SWIFTMODULE_VERSION_MINOR = 553; // change to USR mangling
58+
const uint16_t SWIFTMODULE_VERSION_MINOR = 554; // serialize accesslevel for OpaqueTypeDecl
5959

6060
/// A standard hash seed used for all string hashes in a serialized module.
6161
///
@@ -1292,7 +1292,8 @@ namespace decls_block {
12921292
GenericSignatureIDField, // interface generic signature
12931293
TypeIDField, // interface type for opaque type
12941294
GenericSignatureIDField, // generic environment
1295-
SubstitutionMapIDField // optional substitution map for underlying type
1295+
SubstitutionMapIDField, // optional substitution map for underlying type
1296+
AccessLevelField // access level
12961297
// trailed by generic parameters
12971298
>;
12981299

lib/Serialization/Serialization.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3454,12 +3454,13 @@ class Serializer::DeclSerializer : public DeclVisitor<DeclSerializer> {
34543454
SubstitutionMapID underlyingTypeID = 0;
34553455
if (auto underlying = opaqueDecl->getUnderlyingTypeSubstitutions())
34563456
underlyingTypeID = S.addSubstitutionMapRef(*underlying);
3457-
3457+
uint8_t rawAccessLevel =
3458+
getRawStableAccessLevel(opaqueDecl->getFormalAccess());
34583459
unsigned abbrCode = S.DeclTypeAbbrCodes[OpaqueTypeLayout::Code];
34593460
OpaqueTypeLayout::emitRecord(S.Out, S.ScratchRecord, abbrCode,
34603461
contextID.getOpaqueValue(), namingDeclID,
34613462
interfaceSigID, interfaceTypeID, genericSigID,
3462-
underlyingTypeID);
3463+
underlyingTypeID, rawAccessLevel);
34633464
writeGenericParams(opaqueDecl->getGenericParams());
34643465
}
34653466

test/TBD/emit-tbd-from-driver.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// REQUIRES: VENDOR=apple
2+
// RUN: %empty-directory(%t)
3+
4+
// RUN: echo "public func foo() -> some CustomStringConvertible { 32 }" > %t/source1.swift
5+
// RUN: echo "" > %t/source2.swift
6+
7+
// RUN: %target-build-swift -target x86_64-apple-macosx10.15 -module-name multifile %t/source1.swift %t/source2.swift -Xfrontend -validate-tbd-against-ir=all -emit-tbd -emit-tbd-path %t/multifile.tbd -enable-library-evolution -emit-module
8+
9+
// RUN: %FileCheck %s < %t/multifile.tbd
10+
11+
// CHECK: s9multifile3fooQryFQOMQ

0 commit comments

Comments
 (0)