Skip to content

[Serialization] Opaque types: serialize conditionally available under… #42167

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
merged 1 commit into from
Apr 6, 2022
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
4 changes: 4 additions & 0 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -2903,6 +2903,10 @@ class OpaqueTypeDecl final :
UniqueUnderlyingType = subs;
}

bool hasConditionallyAvailableSubstitutions() const {
return ConditionallyAvailableTypes.hasValue();
}

ArrayRef<ConditionallyAvailableSubstitutions *>
getConditionallyAvailableSubstitutions() const {
assert(ConditionallyAvailableTypes);
Expand Down
2 changes: 2 additions & 0 deletions lib/Serialization/DeclTypeRecordNodes.def
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ OTHER(DERIVATIVE_FUNCTION_CONFIGURATION, 154)

OTHER(ERROR_FLAG, 155)

TRAILING_INFO(CONDITIONAL_SUBSTITUTION)

#ifndef DECL_ATTR
#define DECL_ATTR(NAME, CLASS, OPTIONS, CODE) RECORD_VAL(CLASS##_DECL_ATTR, 180+CODE)
#endif
Expand Down
64 changes: 62 additions & 2 deletions lib/Serialization/Deserialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3517,7 +3517,50 @@ class DeclDeserializer {
StringRef blobData) {
return deserializeAnyFunc(scratch, blobData, /*isAccessor*/true);
}


void deserializeConditionalSubstitutions(
SmallVectorImpl<OpaqueTypeDecl::ConditionallyAvailableSubstitutions *>
&limitedAvailability) {
SmallVector<uint64_t, 4> scratch;
StringRef blobData;

while (true) {
llvm::BitstreamEntry entry =
MF.fatalIfUnexpected(MF.DeclTypeCursor.advance(AF_DontPopBlockAtEnd));
if (entry.Kind != llvm::BitstreamEntry::Record)
break;

scratch.clear();
unsigned recordID = MF.fatalIfUnexpected(
MF.DeclTypeCursor.readRecord(entry.ID, scratch, &blobData));
if (recordID != decls_block::CONDITIONAL_SUBSTITUTION)
break;

ArrayRef<uint64_t> rawConditions;
SubstitutionMapID substitutionMapRef;

decls_block::ConditionalSubstitutionLayout::readRecord(
scratch, substitutionMapRef, rawConditions);

SmallVector<VersionRange, 4> conditions;
llvm::transform(rawConditions, std::back_inserter(conditions),
[&](uint64_t id) {
llvm::VersionTuple lowerEndpoint;
if (lowerEndpoint.tryParse(MF.getIdentifier(id).str()))
MF.fatal();
return VersionRange::allGTE(lowerEndpoint);
});

auto subMapOrError = MF.getSubstitutionMapChecked(substitutionMapRef);
if (!subMapOrError)
MF.fatal();

limitedAvailability.push_back(
OpaqueTypeDecl::ConditionallyAvailableSubstitutions::get(
ctx, conditions, subMapOrError.get()));
}
}

Expected<Decl *> deserializeOpaqueType(ArrayRef<uint64_t> scratch,
StringRef blobData) {
DeclID namingDeclID;
Expand Down Expand Up @@ -3568,7 +3611,24 @@ class DeclDeserializer {
auto subMapOrError = MF.getSubstitutionMapChecked(underlyingTypeSubsID);
if (!subMapOrError)
return subMapOrError.takeError();
opaqueDecl->setUniqueUnderlyingTypeSubstitutions(subMapOrError.get());

// Check whether there are any conditionally available substitutions.
// If there are, it means that "unique" we just read is a universally
// available substitution.
SmallVector<OpaqueTypeDecl::ConditionallyAvailableSubstitutions *>
limitedAvailability;

deserializeConditionalSubstitutions(limitedAvailability);

if (limitedAvailability.empty()) {
opaqueDecl->setUniqueUnderlyingTypeSubstitutions(subMapOrError.get());
} else {
limitedAvailability.push_back(
OpaqueTypeDecl::ConditionallyAvailableSubstitutions::get(
ctx, VersionRange::empty(), subMapOrError.get()));

opaqueDecl->setConditionallyAvailableSubstitutions(limitedAvailability);
}
}
return opaqueDecl;
}
Expand Down
11 changes: 9 additions & 2 deletions lib/Serialization/ModuleFormat.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,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 = 683; // Remove associatedtype primary bit
const uint16_t SWIFTMODULE_VERSION_MINOR = 684; // Cond. available underlying types for opaque

/// A standard hash seed used for all string hashes in a serialized module.
///
Expand Down Expand Up @@ -1413,7 +1413,13 @@ namespace decls_block {
// - the foreign error convention, if any
// - inlinable body text, if any
>;


using ConditionalSubstitutionLayout = BCRecordLayout<
CONDITIONAL_SUBSTITUTION,
SubstitutionMapIDField,
BCArray<IdentifierIDField> // N conditions where each is <major>.<minor>.<patch>
>;

using OpaqueTypeLayout = BCRecordLayout<
OPAQUE_TYPE_DECL,
DeclContextIDField, // decl context
Expand All @@ -1424,6 +1430,7 @@ namespace decls_block {
SubstitutionMapIDField, // optional substitution map for underlying type
AccessLevelField // access level
// trailed by generic parameters
// trailed by conditional substitutions
>;

// TODO: remove the unnecessary FuncDecl components here
Expand Down
34 changes: 32 additions & 2 deletions lib/Serialization/Serialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3897,16 +3897,44 @@ class Serializer::DeclSerializer : public DeclVisitor<DeclSerializer> {
auto genericSigID = S.addGenericSignatureRef(opaqueDecl->getGenericSignature());

SubstitutionMapID underlyingSubsID = 0;
if (auto underlying = opaqueDecl->getUniqueUnderlyingTypeSubstitutions())
if (auto underlying = opaqueDecl->getUniqueUnderlyingTypeSubstitutions()) {
underlyingSubsID = S.addSubstitutionMapRef(*underlying);
} else if (opaqueDecl->hasConditionallyAvailableSubstitutions()) {
// Universally available type doesn't have any availability conditions
// so it could be serialized into "unique" slot to safe space.
auto universal =
opaqueDecl->getConditionallyAvailableSubstitutions().back();
underlyingSubsID = S.addSubstitutionMapRef(universal->getSubstitutions());
}
uint8_t rawAccessLevel =
getRawStableAccessLevel(opaqueDecl->getFormalAccess());
getRawStableAccessLevel(opaqueDecl->getFormalAccess());
unsigned abbrCode = S.DeclTypeAbbrCodes[OpaqueTypeLayout::Code];
OpaqueTypeLayout::emitRecord(S.Out, S.ScratchRecord, abbrCode,
contextID.getOpaqueValue(), namingDeclID,
interfaceSigID, interfaceTypeID, genericSigID,
underlyingSubsID, rawAccessLevel);
writeGenericParams(opaqueDecl->getGenericParams());

// Serialize all of the conditionally available substitutions expect the
// last one - universal, it's serialized into "unique" slot.
if (opaqueDecl->hasConditionallyAvailableSubstitutions()) {
unsigned abbrCode =
S.DeclTypeAbbrCodes[ConditionalSubstitutionLayout::Code];
for (const auto *subs :
opaqueDecl->getConditionallyAvailableSubstitutions().drop_back()) {
SmallVector<IdentifierID, 4> conditions;

for (const auto &condition : subs->getAvailability()) {
auto lowerEndpoint = condition.getLowerEndpoint();
conditions.push_back(
S.addUniquedStringRef(lowerEndpoint.getAsString()));
}

ConditionalSubstitutionLayout::emitRecord(
S.Out, S.ScratchRecord, abbrCode,
S.addSubstitutionMapRef(subs->getSubstitutions()), conditions);
}
}
}

void visitAccessorDecl(const AccessorDecl *fn) {
Expand Down Expand Up @@ -5074,6 +5102,8 @@ void Serializer::writeAllDeclsAndTypes() {
registerDeclTypeAbbr<MembersLayout>();
registerDeclTypeAbbr<XRefLayout>();

registerDeclTypeAbbr<ConditionalSubstitutionLayout>();

#define DECL_ATTR(X, NAME, ...) \
registerDeclTypeAbbr<NAME##DeclAttrLayout>();
#include "swift/AST/Attr.def"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
public protocol P {
func hello()
}

public struct Empty : P {
public func hello() { print("Hello from Empty") }
}

public struct Tuple<T>: P {
public init(_ tuple: T) {}

public func hello() { print("Hello from Tuple") }
}

@available(macOS 10.15, *)
struct Named : P {
public func hello() { print("Hello from Named") }
}

@resultBuilder
public struct Example {
public static func buildOptional<T: P>(_ v: T?) -> some P {
if #available(macOS 100.0.1, *) {
let result = v!
result.hello()
return result
} else {
let result = Empty()
result.hello()
return result
}
}

public static func buildLimitedAvailability<T: P>(_ component: T) -> some P {
component
}

public static func buildBlock<T: P>(_ components: T) -> T {
return components
}

public static func buildBlock<T1: P, T2: P>(_ v1: T1, _ v2: T2) -> Tuple<(T1, T2)> {
return Tuple((v1, v2))
}
}

public func test() -> some P {
if #available(macOS 100.0.1, *) {
return Tuple<(Int, Int)>((0, 0))
}

return Empty()
}

public func test_return_from_conditional() -> some P {
if #available(macOS 10.15, *) {
return Named()
}

return Tuple<(String, Int)>(("", 0))
}
47 changes: 47 additions & 0 deletions test/Serialization/opaque_with_availability_cross_module.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// RUN: %empty-directory(%t)
// RUN: %target-build-swift -target %target-cpu-apple-macosx10.15 -parse-as-library -emit-library -emit-module-path %t/LimitedAvailOpaque.swiftmodule -module-name LimitedAvailOpaque %S/Inputs/opaque_with_limited_availability.swift -o %t/%target-library-name(LimitedAvailOpaque)
// RUN: %target-build-swift -target %target-cpu-apple-macosx10.15 -lLimitedAvailOpaque -module-name main -I %t -L %t %s -o %t/a.out
// RUN: %target-run %t/a.out | %FileCheck %s --color

// REQUIRES: OS=macosx && (CPU=x86_64 || CPU=arm64)
// REQUIRES: executable_test

import LimitedAvailOpaque

struct S: P {
func hello() { print("Hello from S") }
}

@available(macOS 100.0.1, *)
struct NewS: P {
func hello() { print("Hello from NewS") }
}

public struct Test {
@Example
var body: some P {
// TODO(diagnostics): This is incorrect warning due to `some P` return of `buildWithLimitedAvailability`
// expected-warning@+1 {{result builder 'Example' does not implement 'buildLimitedAvailability'; this code may crash on earlier versions of the OS}}
if #available(macOS 100.0.1, *) {
NewS()
}

S()
}

func sayHello() {
body.hello()
}
}

let result = LimitedAvailOpaque.test()
result.hello()
// CHECK: Hello from Empty

Test().sayHello()
// CHECK: Hello from Empty
// CHECK: Hello from Tuple

let conditionalR = LimitedAvailOpaque.test_return_from_conditional()
conditionalR.hello()
// CHECK: Hello from Named