Skip to content

[NCGenerics] ensure builtins are Escapable #70425

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
Dec 14, 2023
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
19 changes: 13 additions & 6 deletions lib/AST/Module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1828,13 +1828,20 @@ static ProtocolConformanceRef getBuiltinMetaTypeTypeConformance(
/// appropriate.
static ProtocolConformanceRef getBuiltinBuiltinTypeConformance(
Type type, const BuiltinType *builtinType, ProtocolDecl *protocol) {
// All builtin are Sendable and Copyable
if (protocol->isSpecificProtocol(KnownProtocolKind::Sendable) ||
protocol->isSpecificProtocol(KnownProtocolKind::Copyable)) {
ASTContext &ctx = protocol->getASTContext();
return ProtocolConformanceRef(
ctx.getBuiltinConformance(type, protocol,
// All builtin are Sendable, Copyable, and Escapable
if (auto kp = protocol->getKnownProtocolKind()) {
switch (*kp) {
case KnownProtocolKind::Sendable:
case KnownProtocolKind::Copyable:
case KnownProtocolKind::Escapable: {
ASTContext &ctx = protocol->getASTContext();
return ProtocolConformanceRef(
ctx.getBuiltinConformance(type, protocol,
BuiltinConformanceKind::Synthesized));
}
default:
break;
}
}

return ProtocolConformanceRef::forMissingOrInvalid(type, protocol);
Expand Down
7 changes: 5 additions & 2 deletions lib/AST/Requirement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,11 @@ void InverseRequirement::expandDefaults(
for (auto gp : gps) {
auto protos = InverseRequirement::expandDefault(gp);
for (auto ip : protos) {
auto protoTy =
ctx.getProtocol(getKnownProtocolKind(ip))->getDeclaredInterfaceType();
auto proto = ctx.getProtocol(getKnownProtocolKind(ip));
if (!proto)
llvm_unreachable("failed to load Copyable/Escapable/etc from stdlib!");

auto protoTy = proto->getDeclaredInterfaceType();
result.push_back({{RequirementKind::Conformance, gp, protoTy},
SourceLoc(),
/*inferred=*/true,
Expand Down
29 changes: 29 additions & 0 deletions test/Generics/inverse_generics_stdlib.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// RUN: %target-typecheck-verify-swift -parse-stdlib -module-name Swift -enable-experimental-feature BuiltinModule -enable-experimental-feature NoncopyableGenerics -enable-experimental-feature NonEscapableTypes

// REQUIRES: asserts

/// This test specifically covers constructs that are only valid in the stdlib.

import Builtin

@_marker public protocol Copyable: ~Escapable {}
@_marker public protocol Escapable: ~Copyable {}

struct NC: ~Copyable {}

@frozen public struct UnsafePointer<T: ~Copyable>: Copyable {
var value: Builtin.RawPointer
}

@frozen
public enum Optional<T: ~Copyable> {
case some(T)
case none
}

public func wrapping<T: ~Copyable>(_ t: consuming T) -> T? {
return .some(t)
}

// No ownership required.
func checkCopyability(_ t: UnsafePointer<NC>) {}