Skip to content

[5.1 08-28-2019] [Serialization] Teach serialization to get a generic signature from opaque types #27141

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
2 changes: 1 addition & 1 deletion include/swift/Serialization/ModuleFormat.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const uint16_t SWIFTMODULE_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 SWIFTMODULE_VERSION_MINOR = 501; // SIL function availability
const uint16_t SWIFTMODULE_VERSION_MINOR = 502; // generic opaque return type xref

using DeclIDField = BCFixed<31>;

Expand Down
2 changes: 2 additions & 0 deletions lib/Serialization/Deserialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1719,6 +1719,8 @@ ModuleFile::resolveCrossReference(ModuleID MID, uint32_t pathLen) {
currentSig = fn->getGenericSignature();
} else if (auto subscript = dyn_cast<SubscriptDecl>(base)) {
currentSig = subscript->getGenericSignature();
} else if (auto opaque = dyn_cast<OpaqueTypeDecl>(base)) {
currentSig = opaque->getGenericSignature();
}

if (!currentSig) {
Expand Down
4 changes: 2 additions & 2 deletions lib/Serialization/Serialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1947,6 +1947,8 @@ void Serializer::writeCrossReference(const DeclContext *DC, uint32_t pathLen) {
case DeclContextKind::GenericTypeDecl: {
auto generic = cast<GenericTypeDecl>(DC);

writeCrossReference(DC->getParent(), pathLen + 1);

// Opaque return types are unnamed and need a special xref.
if (auto opaque = dyn_cast<OpaqueTypeDecl>(generic)) {
if (!opaque->hasName()) {
Expand All @@ -1960,8 +1962,6 @@ void Serializer::writeCrossReference(const DeclContext *DC, uint32_t pathLen) {
}

assert(generic->hasName());

writeCrossReference(DC->getParent(), pathLen + 1);

abbrCode = DeclTypeAbbrCodes[XRefTypePathPieceLayout::Code];

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
public protocol BestProtocol {}
public protocol GoodProtocol {
associatedtype A
}

public struct BestStruct: BestProtocol {
public init() {}
}

extension BestProtocol {
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
public func _bestValue<X: GoodProtocol>(_ x: X.Type, _ a: X.A) -> some BestProtocol {
return BestStruct()
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"./best-protocol.swift": {
"swiftmodule": "./best-protocol.swiftmodule"
},
"./xref-opaque-generic-type.swift": {
"swiftmodule": "./xref-opaque-generic-type.swiftmodule"
}
}
19 changes: 19 additions & 0 deletions test/Serialization/xref-generic-opaque-type.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// RUN: %empty-directory(%t)
// RUN: cp -r %S/Inputs/xref-opaque-generic-type/* %t/.
// RUN: cp %s %t/xref-generic-opaque-type.swift
// RUN: cd %t
// RUN: %target-swiftc_driver -emit-module -incremental %t/best-protocol.swift %t/xref-generic-opaque-type.swift -module-name A -output-file-map %t/output.json

@usableFromInline
struct GoodStruct: GoodProtocol {
@usableFromInline
typealias A = Int
}

extension BestProtocol {
@inlinable
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
public func bestValue(_ x: Int) -> some BestProtocol {
return _bestValue(GoodStruct.self, x)
}
}