Skip to content

NCGenerics: loosen builtin requirements #71733

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
Feb 20, 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
36 changes: 23 additions & 13 deletions lib/AST/Builtins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -884,23 +884,23 @@ static ValueDecl *getTakeOperation(ASTContext &ctx, Identifier id) {

static ValueDecl *getStoreOperation(ASTContext &ctx, Identifier id) {
return getBuiltinFunction(ctx, id, _thin,
_generics(_unrestricted, _conformsToDefaults(0)),
_generics(_unrestricted),
_parameters(_owned(_typeparam(0)),
_rawPointer),
_void);
}

static ValueDecl *getDestroyOperation(ASTContext &ctx, Identifier id) {
return getBuiltinFunction(ctx, id, _thin,
_generics(_unrestricted, _conformsToDefaults(0)),
_generics(_unrestricted),
_parameters(_metatype(_typeparam(0)),
_rawPointer),
_void);
}

static ValueDecl *getDestroyArrayOperation(ASTContext &ctx, Identifier id) {
return getBuiltinFunction(ctx, id, _thin,
_generics(_unrestricted, _conformsToDefaults(0)),
_generics(_unrestricted),
_parameters(_metatype(_typeparam(0)),
_rawPointer,
_word),
Expand All @@ -920,9 +920,20 @@ static ValueDecl *getAssumeAlignment(ASTContext &ctx, Identifier id) {
_rawPointer);
}

static ValueDecl *getCopyArrayOperation(ASTContext &ctx, Identifier id) {
return getBuiltinFunction(ctx, id, _thin,
_generics(_unrestricted,
_conformsTo(_typeparam(0), _copyable)),
_parameters(_metatype(_typeparam(0)),
_rawPointer,
_rawPointer,
_word),
_void);
}

static ValueDecl *getTransferArrayOperation(ASTContext &ctx, Identifier id) {
return getBuiltinFunction(ctx, id, _thin,
_generics(_unrestricted, _conformsToDefaults(0)),
_generics(_unrestricted),
_parameters(_metatype(_typeparam(0)),
_rawPointer,
_rawPointer,
Expand Down Expand Up @@ -985,9 +996,7 @@ static ValueDecl *getAllocWithTailElemsOperation(ASTContext &Context,
static ValueDecl *getProjectTailElemsOperation(ASTContext &ctx,
Identifier id) {
return getBuiltinFunction(ctx, id, _thin,
_generics(_unrestricted, _unrestricted,
_conformsToDefaults(0),
_conformsToDefaults(1)),
_generics(_unrestricted, _unrestricted),
_parameters(_typeparam(0),
_metatype(_typeparam(1))),
_rawPointer);
Expand All @@ -1007,9 +1016,7 @@ static ValueDecl *getGepOperation(ASTContext &ctx, Identifier id,
static ValueDecl *getGetTailAddrOperation(ASTContext &ctx, Identifier id,
Type argType) {
return getBuiltinFunction(ctx, id, _thin,
_generics(_unrestricted, _unrestricted,
_conformsToDefaults(0),
_conformsToDefaults(1)),
_generics(_unrestricted, _unrestricted),
_parameters(_rawPointer,
argType,
_metatype(_typeparam(0)),
Expand Down Expand Up @@ -2785,12 +2792,15 @@ ValueDecl *swift::getBuiltinValueDecl(ASTContext &Context, Identifier Id) {
return getDestroyArrayOperation(Context, Id);

case BuiltinValueKind::CopyArray:
case BuiltinValueKind::TakeArrayNoAlias:
case BuiltinValueKind::TakeArrayFrontToBack:
case BuiltinValueKind::TakeArrayBackToFront:
case BuiltinValueKind::AssignCopyArrayNoAlias:
case BuiltinValueKind::AssignCopyArrayFrontToBack:
case BuiltinValueKind::AssignCopyArrayBackToFront:
if (!Types.empty()) return nullptr;
return getCopyArrayOperation(Context, Id);

case BuiltinValueKind::TakeArrayNoAlias:
case BuiltinValueKind::TakeArrayFrontToBack:
case BuiltinValueKind::TakeArrayBackToFront:
case BuiltinValueKind::AssignTakeArray:
if (!Types.empty()) return nullptr;
return getTransferArrayOperation(Context, Id);
Expand Down
33 changes: 33 additions & 0 deletions test/SILOptimizer/moveonly_builtins.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// RUN: %target-swift-frontend-typecheck -verify %s -DILLEGAL \
// RUN: -enable-experimental-feature NoncopyableGenerics \
// RUN: -enable-builtin-module \
// RUN: -verify-additional-prefix illegal-

// RUN: %target-swift-frontend -emit-sil -sil-verify-all -verify %s \
// RUN: -enable-experimental-feature NoncopyableGenerics \
// RUN: -enable-builtin-module

import Builtin

struct NC: ~Copyable {}

func checkPointerBuiltins(_ ptr: Builtin.RawPointer, _ value: consuming NC) {
Builtin.initialize(value, ptr)
#if ILLEGAL
Builtin.copy(value) // expected-illegal-error {{noncopyable type 'NC' cannot be substituted for copyable generic parameter 'T' in 'copy'}}
#endif
}

func checkArrayBuiltins(_ dest: Builtin.RawPointer, src: Builtin.RawPointer, count: Builtin.Word) {
Builtin.takeArrayFrontToBack(NC.self, dest, src, count)
Builtin.takeArrayBackToFront(NC.self, dest, src, count)
Builtin.assignTakeArray(NC.self, dest, src, count)
Builtin.destroyArray(NC.self, dest, count)

#if ILLEGAL
Builtin.copyArray(NC.self, dest, src, count) // expected-illegal-error {{noncopyable type 'NC' cannot be substituted for copyable generic parameter 'T' in 'copyArray'}}
Builtin.assignCopyArrayNoAlias(NC.self, dest, src, count) // expected-illegal-error {{noncopyable type 'NC' cannot be substituted for copyable generic parameter 'T' in 'assignCopyArrayNoAlias'}}
Builtin.assignCopyArrayFrontToBack(NC.self, dest, src, count) // expected-illegal-error {{noncopyable type 'NC' cannot be substituted for copyable generic parameter 'T' in 'assignCopyArrayFrontToBack'}}
Builtin.assignCopyArrayBackToFront(NC.self, dest, src, count) // expected-illegal-error {{noncopyable type 'NC' cannot be substituted for copyable generic parameter 'T' in 'assignCopyArrayBackToFront'}}
#endif
}