Skip to content

NCGenerics: conformances can depend on Copyable #71794

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 2 commits into from
Feb 22, 2024
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
11 changes: 9 additions & 2 deletions lib/Sema/TypeCheckProtocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2210,6 +2210,13 @@ static bool hasAdditionalSemanticChecks(ProtocolDecl *proto) {
return proto->isSpecificProtocol(KnownProtocolKind::Sendable);
}

/// Determine whether a conformance to this protocol can be determined at
/// runtime for an arbitrary type.
static bool hasRuntimeConformanceInfo(ProtocolDecl *proto) {
return !proto->isMarkerProtocol()
|| proto->isSpecificProtocol(KnownProtocolKind::Copyable);
}

static void ensureRequirementsAreSatisfied(ASTContext &ctx,
NormalProtocolConformance *conformance);

Expand Down Expand Up @@ -2354,11 +2361,11 @@ checkIndividualConformance(NormalProtocolConformance *conformance) {

// If the protocol to which we are conditionally conforming is not a marker
// protocol, the conditional requirements must not involve conformance to a
// marker protocol. We cannot evaluate such a conformance at runtime.
// protocol that cannot be evaluated at runtime, like most marker protocols.
if (!Proto->isMarkerProtocol()) {
for (const auto &req : conditionalReqs) {
if (req.getKind() == RequirementKind::Conformance &&
req.getProtocolDecl()->isMarkerProtocol()) {
!hasRuntimeConformanceInfo(req.getProtocolDecl())) {
Context.Diags.diagnose(
ComplainLoc, diag::marker_protocol_conditional_conformance,
Proto->getName(), req.getFirstType(),
Expand Down
17 changes: 17 additions & 0 deletions test/Generics/inverse_generics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -462,3 +462,20 @@ func checkExistentials() {
let _: any Copyable & ~Copyable = 1 // expected-error {{composition cannot contain '~Copyable' when another member requires 'Copyable'}}
let _: any Escapable & ~Escapable = 1 // expected-error {{composition cannot contain '~Escapable' when another member requires 'Escapable'}}
}

// Conformances can be conditional on whether a generic parameter is Copyable
protocol Arbitrary {}
protocol AnotherOne {}
struct UnethicalPointer<Pointee: ~Copyable> {}
extension UnethicalPointer: Arbitrary {}
extension UnethicalPointer: AnotherOne where Pointee: Copyable {}

struct StillIllegal1<Pointee: ~Escapable> {}
extension StillIllegal1: Arbitrary {}
// expected-error@-1 {{conditional conformance to non-marker protocol 'Arbitrary' cannot depend on conformance of 'Pointee' to marker protocol 'Escapable'}}
extension StillIllegal1: AnotherOne where Pointee: Escapable {}
// expected-error@-1 {{conditional conformance to non-marker protocol 'AnotherOne' cannot depend on conformance of 'Pointee' to marker protocol 'Escapable'}}

struct SillIllegal2<Pointee> {}
extension SillIllegal2: Arbitrary where Pointee: Sendable {}
// expected-error@-1 {{conditional conformance to non-marker protocol 'Arbitrary' cannot depend on conformance of 'Pointee' to marker protocol 'Sendable'}}
2 changes: 1 addition & 1 deletion test/ModuleInterface/noncopyable_generics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// RUN: -enable-experimental-feature NonescapableTypes \
// RUN: -o %t/Swiftskell.swiftmodule \
// RUN: -emit-module-interface-path %t/Swiftskell.swiftinterface \
// RUN: %S/Inputs/Swiftskell.swift
// RUN: %S/../Inputs/Swiftskell.swift

// Check the interfaces

Expand Down