Skip to content

[SE-0302] Implement structural conformances to Sendable. #38669

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
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: 7 additions & 0 deletions include/swift/AST/ASTContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ namespace swift {
class InheritedProtocolConformance;
class SelfProtocolConformance;
class SpecializedProtocolConformance;
class BuiltinProtocolConformance;
enum class ProtocolConformanceState;
class Pattern;
enum PointerTypeKind : unsigned;
Expand Down Expand Up @@ -1029,6 +1030,12 @@ class ASTContext final {
SelfProtocolConformance *
getSelfConformance(ProtocolDecl *protocol);

/// Produce the builtin conformance for some structural type to some protocol.
BuiltinProtocolConformance *
getBuiltinConformance(Type type, ProtocolDecl *protocol,
GenericSignature genericSig,
ArrayRef<Requirement> conditionalRequirements);

/// A callback used to produce a diagnostic for an ill-formed protocol
/// conformance that was type-checked before we're actually walking the
/// conformance itself, along with a bit indicating whether this diagnostic
Expand Down
6 changes: 6 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -1923,6 +1923,12 @@ ERROR(type_cannot_conform, none,
NOTE(only_concrete_types_conform_to_protocols,none,
"only concrete types such as structs, enums and classes can conform to protocols",
())
NOTE(nonsendable_function_type,none,
"a function type must be marked '@Sendable' to conform to 'Sendable'", ())
NOTE(nonsendable_tuple_type,none,
"a tuple type must be composed of 'Sendable' elements to conform to "
"'Sendable'", ())

NOTE(required_by_opaque_return,none,
"required by opaque return type of %0 %1", (DescriptiveDeclKind, DeclName))
NOTE(required_by_decl,none,
Expand Down
111 changes: 107 additions & 4 deletions include/swift/AST/ProtocolConformance.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
Expand Down Expand Up @@ -66,7 +66,10 @@ enum class ProtocolConformanceKind {
Specialized,
/// Conformance of a generic class type projected through one of its
/// superclass's conformances.
Inherited
Inherited,
/// Builtin conformances are special conformaces that the runtime handles
/// and isn't implemented directly in Swift.
Builtin
};

/// Describes the state of a protocol conformance, which may be complete,
Expand Down Expand Up @@ -329,7 +332,9 @@ class alignas(1 << DeclAlignInBits) ProtocolConformance {
/// - the type is directly declared to conform to the protocol (a
/// normal conformance) or
/// - the protocol's existential type is known to conform to itself (a
/// self-conformance).
/// self-conformance) or
/// - the type's conformance is declared within the runtime (a builtin
/// conformance).
class RootProtocolConformance : public ProtocolConformance {
protected:
RootProtocolConformance(ProtocolConformanceKind kind, Type conformingType)
Expand Down Expand Up @@ -380,7 +385,8 @@ class RootProtocolConformance : public ProtocolConformance {

static bool classof(const ProtocolConformance *conformance) {
return conformance->getKind() == ProtocolConformanceKind::Normal ||
conformance->getKind() == ProtocolConformanceKind::Self;
conformance->getKind() == ProtocolConformanceKind::Self ||
conformance->getKind() == ProtocolConformanceKind::Builtin;
}
};

Expand Down Expand Up @@ -987,6 +993,103 @@ class InheritedProtocolConformance : public ProtocolConformance,
}
};

/// A builtin conformance appears when a non-nominal type has a
/// conformance that is synthesized by the implementation.
class BuiltinProtocolConformance final : public RootProtocolConformance,
private llvm::TrailingObjects<BuiltinProtocolConformance, Requirement> {
friend ASTContext;
friend TrailingObjects;

ProtocolDecl *protocol;
GenericSignature genericSig;
size_t numConditionalRequirements;

size_t numTrailingObjects(OverloadToken<Requirement>) const {
return numConditionalRequirements;
}

BuiltinProtocolConformance(Type conformingType, ProtocolDecl *protocol,
GenericSignature genericSig,
ArrayRef<Requirement> conditionalRequirements);

public:
/// Get the protocol being conformed to.
ProtocolDecl *getProtocol() const {
return protocol;
}

/// Retrieve the generic signature that describes the type parameters used
/// within the conforming type.
GenericSignature getGenericSignature() const {
return genericSig;
}

/// Get any requirements that must be satisfied for this conformance to apply.
Optional<ArrayRef<Requirement>>
getConditionalRequirementsIfAvailable() const {
return getConditionalRequirements();
}

/// Get any requirements that must be satisfied for this conformance to apply.
ArrayRef<Requirement> getConditionalRequirements() const {
return {getTrailingObjects<Requirement>(), numConditionalRequirements};
}

/// Get the declaration context that contains the nominal type declaration.
DeclContext *getDeclContext() const {
return getProtocol();
}

/// Retrieve the state of this conformance.
ProtocolConformanceState getState() const {
return ProtocolConformanceState::Complete;
}

/// Get the kind of source from which this conformance comes.
ConformanceEntryKind getSourceKind() const {
return ConformanceEntryKind::Synthesized;
}
/// Get the protocol conformance which implied this implied conformance.
NormalProtocolConformance *getImplyingConformance() const {
return nullptr;
}

bool hasTypeWitness(AssociatedTypeDecl *assocType) const {
llvm_unreachable("builtin-conformances never have associated types");
}

/// Retrieve the type witness and type decl (if one exists)
/// for the given associated type.
TypeWitnessAndDecl
getTypeWitnessAndDecl(AssociatedTypeDecl *assocType,
SubstOptions options=None) const {
llvm_unreachable("builtin-conformances never have associated types");
}

/// Given that the requirement signature of the protocol directly states
/// that the given dependent type must conform to the given protocol,
/// return its associated conformance.
ProtocolConformanceRef
getAssociatedConformance(Type assocType, ProtocolDecl *protocol) const {
llvm_unreachable("builtin-conformances never have associated types");
}

/// Retrieve the witness corresponding to the given value requirement.
ConcreteDeclRef getWitnessDeclRef(ValueDecl *requirement) const {
return ConcreteDeclRef(requirement);
}

/// Determine whether the witness for the given requirement
/// is either the default definition or was otherwise deduced.
bool usesDefaultDefinition(AssociatedTypeDecl *requirement) const {
llvm_unreachable("builtin-conformances never have associated types");
}

static bool classof(const ProtocolConformance *conformance) {
return conformance->getKind() == ProtocolConformanceKind::Builtin;
}
};

inline bool ProtocolConformance::isInvalid() const {
return getRootConformance()->isInvalid();
}
Expand Down
28 changes: 28 additions & 0 deletions lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,10 @@ struct ASTContext::Implementation {
/// The set of inherited protocol conformances.
llvm::FoldingSet<InheritedProtocolConformance> InheritedConformances;

/// The set of builtin protocol conformances.
llvm::DenseMap<std::pair<Type, ProtocolDecl *>,
BuiltinProtocolConformance *> BuiltinConformances;

/// The set of substitution maps (uniqued by their storage).
llvm::FoldingSet<SubstitutionMap::Storage> SubstitutionMaps;

Expand Down Expand Up @@ -2243,6 +2247,28 @@ ASTContext::getSelfConformance(ProtocolDecl *protocol) {
return entry;
}

/// Produce the builtin conformance for some non-nominal to some protocol.
BuiltinProtocolConformance *
ASTContext::getBuiltinConformance(
Type type, ProtocolDecl *protocol,
GenericSignature genericSig,
ArrayRef<Requirement> conditionalRequirements
) {
auto key = std::make_pair(type, protocol);
AllocationArena arena = getArena(type->getRecursiveProperties());
auto &builtinConformances = getImpl().getArena(arena).BuiltinConformances;

auto &entry = builtinConformances[key];
if (!entry) {
auto size = BuiltinProtocolConformance::
totalSizeToAlloc<Requirement>(conditionalRequirements.size());
auto mem = this->Allocate(size, alignof(BuiltinProtocolConformance), arena);
entry = new (mem) BuiltinProtocolConformance(
type, protocol, genericSig, conditionalRequirements);
}
return entry;
}

/// If one of the ancestor conformances already has a matching type, use
/// that instead.
static ProtocolConformance *collapseSpecializedConformance(
Expand All @@ -2259,6 +2285,7 @@ static ProtocolConformance *collapseSpecializedConformance(
case ProtocolConformanceKind::Normal:
case ProtocolConformanceKind::Inherited:
case ProtocolConformanceKind::Self:
case ProtocolConformanceKind::Builtin:
// If the conformance matches, return it.
if (conformance->getType()->isEqual(type)) {
for (auto subConformance : substitutions.getConformances())
Expand Down Expand Up @@ -2496,6 +2523,7 @@ size_t ASTContext::Implementation::Arena::getTotalMemory() const {
// NormalConformances ?
// SpecializedConformances ?
// InheritedConformances ?
// BuiltinConformances ?
}

void AbstractFunctionDecl::setForeignErrorConvention(
Expand Down
6 changes: 5 additions & 1 deletion lib/AST/ASTDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
Expand Down Expand Up @@ -3262,6 +3262,10 @@ static void dumpProtocolConformanceRec(
visited);
break;
}

case ProtocolConformanceKind::Builtin: {
printCommon("builtin");
}
}

PrintWithColorRAII(out, ParenthesisColor) << ')';
Expand Down
27 changes: 21 additions & 6 deletions lib/AST/ASTMangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
Expand Down Expand Up @@ -209,9 +209,11 @@ std::string ASTMangler::mangleWitnessTable(const RootProtocolConformance *C) {
if (isa<NormalProtocolConformance>(C)) {
appendProtocolConformance(C);
appendOperator("WP");
} else {
} else if (isa<SelfProtocolConformance>(C)) {
appendProtocolName(cast<SelfProtocolConformance>(C)->getProtocol());
appendOperator("WS");
} else {
llvm_unreachable("mangling unknown conformance kind");
}
return finalize();
}
Expand All @@ -235,7 +237,11 @@ std::string ASTMangler::mangleWitnessThunk(
}

if (Conformance) {
appendOperator(isa<SelfProtocolConformance>(Conformance) ? "TS" : "TW");
if (isa<SelfProtocolConformance>(Conformance)) {
appendOperator("TS");
} else {
appendOperator("TW");
}
}
return finalize();
}
Expand Down Expand Up @@ -1556,7 +1562,8 @@ void ASTMangler::appendBoundGenericArgs(Type type, bool &isFirstArgList) {
static bool conformanceHasIdentity(const RootProtocolConformance *root) {
auto conformance = dyn_cast<NormalProtocolConformance>(root);
if (!conformance) {
assert(isa<SelfProtocolConformance>(root));
assert(isa<SelfProtocolConformance>(root) ||
isa<BuiltinProtocolConformance>(root));
return true;
}

Expand All @@ -1577,8 +1584,9 @@ static bool conformanceHasIdentity(const RootProtocolConformance *root) {
static bool isRetroactiveConformance(const RootProtocolConformance *root) {
auto conformance = dyn_cast<NormalProtocolConformance>(root);
if (!conformance) {
assert(isa<SelfProtocolConformance>(root));
return false; // self-conformances are never retroactive.
assert(isa<SelfProtocolConformance>(root) ||
isa<BuiltinProtocolConformance>(root));
return false; // self-conformances are never retroactive. nor are builtin.
}

return conformance->isRetroactive();
Expand Down Expand Up @@ -3058,6 +3066,10 @@ ASTMangler::appendProtocolConformance(const ProtocolConformance *conformance) {
appendModule(Mod, DC->getAsDecl()->getAlternateModuleName());
}

// If this is a non-nominal type, we're done.
if (!conformingType->getAnyNominal())
return;

contextSig =
conformingType->getAnyNominal()->getGenericSignatureOfContext();

Expand All @@ -3082,6 +3094,9 @@ void ASTMangler::appendProtocolConformanceRef(
assert(DC->getAsDecl());
appendModule(conformance->getDeclContext()->getParentModule(),
DC->getAsDecl()->getAlternateModuleName());
// Builtin conformances are always from the Swift module.
} else if (isa<BuiltinProtocolConformance>(conformance)) {
appendOperator("HP");
} else if (conformance->getDeclContext()->getParentModule() ==
conformance->getType()->getAnyNominal()->getParentModule()) {
appendOperator("HP");
Expand Down
8 changes: 7 additions & 1 deletion lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
Expand Down Expand Up @@ -5699,6 +5699,12 @@ void ProtocolConformance::printName(llvm::raw_ostream &os,
os << ")";
break;
}
case ProtocolConformanceKind::Builtin: {
auto builtin = cast<BuiltinProtocolConformance>(this);
os << builtin->getProtocol()->getName()
<< " type " << builtin->getType();
break;
}
}
}

Expand Down
Loading