Skip to content

[move-only] Temporarily ban deinits on non-copyable enums. #64692

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
2 changes: 2 additions & 0 deletions include/swift/AST/DiagnosticsParse.def
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,8 @@ ERROR(initializer_result_type,PointsToFirstBadToken,
// Destructor
ERROR(destructor_decl_outside_class_or_noncopyable,none,
"deinitializers may only be declared within a class, actor, or noncopyable type", ())
ERROR(destructor_decl_on_noncopyable_enum,none,
"deinitializers are not yet supported on noncopyable enums", ())
ERROR(destructor_decl_on_objc_enum,none,
"deinitializers cannot be declared on an @objc enum type", ())
ERROR(expected_lbrace_destructor,PointsToFirstBadToken,
Expand Down
1 change: 1 addition & 0 deletions include/swift/Basic/Features.def
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ EXPERIMENTAL_FEATURE(FreestandingMacros, true)
EXPERIMENTAL_FEATURE(MoveOnlyClasses, true)
EXPERIMENTAL_FEATURE(NoImplicitCopy, true)
EXPERIMENTAL_FEATURE(OldOwnershipOperatorSpellings, true)
EXPERIMENTAL_FEATURE(MoveOnlyEnumDeinits, true)

EXPERIMENTAL_FEATURE(OneWayClosureParameters, false)
EXPERIMENTAL_FEATURE(TypeWitnessSystemInference, false)
Expand Down
7 changes: 7 additions & 0 deletions lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3240,6 +3240,13 @@ static bool usesFeatureOldOwnershipOperatorSpellings(Decl *decl) {
return false;
}

static bool usesFeatureMoveOnlyEnumDeinits(Decl *decl) {
if (auto *ei = dyn_cast<EnumDecl>(decl)) {
return usesFeatureMoveOnly(ei) && ei->getValueTypeDestructor();
}
return false;
}

static bool usesFeatureOneWayClosureParameters(Decl *decl) {
return false;
}
Expand Down
8 changes: 8 additions & 0 deletions lib/Sema/TypeCheckDeclPrimary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3609,6 +3609,14 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
DD->diagnose(diag::destructor_decl_outside_class_or_noncopyable);
}

// Temporarily ban deinit on noncopyable enums, unless the experimental
// feature flag is set.
if (!DD->getASTContext().LangOpts.hasFeature(
Feature::MoveOnlyEnumDeinits) &&
nom->isMoveOnly() && isa<EnumDecl>(nom)) {
DD->diagnose(diag::destructor_decl_on_noncopyable_enum);
}

// If we have a noncopyable type, check if we have an @objc enum with a
// deinit and emit a specialized error. We will have technically already
// emitted an error since @objc enum cannot be marked noncopyable, but
Expand Down
2 changes: 1 addition & 1 deletion test/IRGen/moveonly_deinit.sil
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// RUN: %empty-directory(%t)
// RUN: %{python} %utils/chex.py < %s > %t/moveonly_deinit.sil
// RUN: %target-swift-frontend -emit-ir %t/moveonly_deinit.sil | %FileCheck %t/moveonly_deinit.sil --check-prefix=CHECK --check-prefix=CHECK-%target-ptrsize
// RUN: %target-swift-frontend -enable-experimental-feature MoveOnlyEnumDeinits -emit-ir %t/moveonly_deinit.sil | %FileCheck %t/moveonly_deinit.sil --check-prefix=CHECK --check-prefix=CHECK-%target-ptrsize

// UNSUPPORTED: CPU=arm64e

Expand Down
4 changes: 2 additions & 2 deletions test/IRGen/moveonly_deinits.swift
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// TODO: re-enable the simplification passes once rdar://104875010 is fixed
// RUN: %target-swift-emit-ir -Xllvm -sil-disable-pass=simplification %s | %FileCheck -check-prefix=IR %s
// RUN: %target-swift-emit-ir -enable-experimental-feature MoveOnlyEnumDeinits -Xllvm -sil-disable-pass=simplification %s | %FileCheck -check-prefix=IR %s

// Test that makes sure that at IRGen time we properly handle conditional
// releases for trivial and non-trivial move only types. The SIL/SILGen part of
// this test is in test/SILGen/moveonly_deinits. We have a separate test so that
// we can test on other platforms the other behavior.

// REQUIRES: asserts
// REQUIRES: CPU=x86_64
// REQUIRES: CODEGENERATOR=X86

//////////////////////
// Misc Declaration //
Expand Down
2 changes: 1 addition & 1 deletion test/Interpreter/moveonly_forget.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %target-run-simple-swift(-Xfrontend -sil-verify-all) | %FileCheck %s --implicit-check-not closing
// RUN: %target-run-simple-swift(-Xfrontend -enable-experimental-feature -Xfrontend MoveOnlyEnumDeinits -Xfrontend -sil-verify-all) | %FileCheck %s --implicit-check-not closing

// REQUIRES: executable_test

Expand Down
4 changes: 2 additions & 2 deletions test/SILGen/forget.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// RUN: %target-swift-emit-silgen -module-name test %s | %FileCheck %s --enable-var-scope
// RUN: %target-swift-emit-sil -module-name test -sil-verify-all %s | %FileCheck %s --check-prefix CHECK-SIL --enable-var-scope
// RUN: %target-swift-emit-silgen -enable-experimental-feature MoveOnlyEnumDeinits -module-name test %s | %FileCheck %s --enable-var-scope
// RUN: %target-swift-emit-sil -enable-experimental-feature MoveOnlyEnumDeinits -module-name test -sil-verify-all %s | %FileCheck %s --check-prefix CHECK-SIL --enable-var-scope

func invokedDeinit() {}

Expand Down
4 changes: 2 additions & 2 deletions test/SILGen/moveonly_deinits.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// TODO: re-enable the simplification passes once rdar://104875010 is fixed
// RUN: %target-swift-emit-silgen -Xllvm -sil-disable-pass=simplification %s | %FileCheck -check-prefix=SILGEN %s
// RUN: %target-swift-emit-sil -Xllvm -sil-disable-pass=simplification %s | %FileCheck -check-prefix=SIL %s
// RUN: %target-swift-emit-silgen -enable-experimental-feature MoveOnlyEnumDeinits -Xllvm -sil-disable-pass=simplification %s | %FileCheck -check-prefix=SILGEN %s
// RUN: %target-swift-emit-sil -enable-experimental-feature MoveOnlyEnumDeinits -Xllvm -sil-disable-pass=simplification %s | %FileCheck -check-prefix=SIL %s

// Test that makes sure that throughout the pipeline we properly handle
// conditional releases for trivial and non-trivial move only types.
Expand Down
2 changes: 1 addition & 1 deletion test/SILOptimizer/moveonly_deinit_insertion.sil
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %target-sil-opt -module-name main -enable-sil-verify-all -sil-move-only-deinit-insertion -enable-experimental-feature MoveOnlyClasses %s | %FileCheck %s
// RUN: %target-sil-opt -module-name main -enable-sil-verify-all -sil-move-only-deinit-insertion -enable-experimental-feature MoveOnlyClasses -enable-experimental-feature MoveOnlyEnumDeinits %s | %FileCheck %s

sil_stage raw

Expand Down
2 changes: 1 addition & 1 deletion test/SILOptimizer/moveonly_deinits.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %target-swift-frontend -sil-verify-all -verify -emit-sil %s
// RUN: %target-swift-frontend -sil-verify-all -verify -emit-sil -enable-experimental-feature MoveOnlyEnumDeinits %s

class Klass {}

Expand Down
2 changes: 1 addition & 1 deletion test/Sema/forget.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %target-typecheck-verify-swift
// RUN: %target-typecheck-verify-swift -enable-experimental-feature MoveOnlyEnumDeinits

// Typechecking for the forget statement.

Expand Down
10 changes: 10 additions & 0 deletions test/Sema/moveonly_enum.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// RUN: %target-typecheck-verify-swift

@_moveOnly
enum Foo {
deinit {} // expected-error {{deinitializers are not yet supported on noncopyable enums}}
}

@_moveOnly
enum Foo2 {
}
2 changes: 1 addition & 1 deletion test/Sema/moveonly_objc_enum.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %target-typecheck-verify-swift
// RUN: %target-typecheck-verify-swift -enable-experimental-feature MoveOnlyEnumDeinits

// REQUIRES: objc_interop

Expand Down
4 changes: 2 additions & 2 deletions test/Serialization/moveonly_deinit.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// RUN: %empty-directory(%t)
// TODO: re-enable the simplification passes once rdar://104875010 is fixed
// RUN: %target-swift-frontend -Xllvm -sil-disable-pass=simplification -g -emit-module -module-name OtherModule %S/Inputs/moveonly_deinit.swift -emit-module-path %t/OtherModule.swiftmodule
// RUN: %target-swift-frontend -Xllvm -sil-disable-pass=simplification -g -I %t %s -emit-silgen
// RUN: %target-swift-frontend -enable-experimental-feature MoveOnlyEnumDeinits -Xllvm -sil-disable-pass=simplification -g -emit-module -module-name OtherModule %S/Inputs/moveonly_deinit.swift -emit-module-path %t/OtherModule.swiftmodule
// RUN: %target-swift-frontend -enable-experimental-feature MoveOnlyEnumDeinits -Xllvm -sil-disable-pass=simplification -g -I %t %s -emit-silgen

// Make sure we can deserialize deinits of both enums and structs.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// RUN: %empty-directory(%t)
// TODO: re-enable the simplification passes once rdar://104875010 is fixed
// RUN: %target-swift-frontend -Xllvm -sil-disable-pass=simplification -emit-module -o %t/Library.swiftmodule -module-name Library %S/Inputs/moveonly_deinit.swift
// RUN: %target-swift-frontend -Xllvm -sil-disable-pass=simplification -emit-module -o %t/Library.swiftmodule -module-name Library %S/Inputs/moveonly_deinit.swift -enable-experimental-feature MoveOnlyEnumDeinits

// >>> make sure borrow scopes are required when using a move-only type from another module
// RUN: not %target-swift-frontend -DUSE_MOVEONLY_TYPE -typecheck -enable-lexical-borrow-scopes=false -I %t %s 2>&1 | %FileCheck %s --check-prefix CHECK-ERROR
// RUN: not %target-swift-frontend -DUSE_MOVEONLY_TYPE -typecheck -enable-experimental-feature MoveOnlyEnumDeinits -enable-lexical-borrow-scopes=false -I %t %s 2>&1 | %FileCheck %s --check-prefix CHECK-ERROR
// RUN: %target-swift-frontend -DUSE_MOVEONLY_TYPE -typecheck -I %t %s 2>&1 | %FileCheck %s --allow-empty

// >>> try turning off lexical borrow scopes with no move-only types; shouldn't be an error.
// we have to pipe into FileCheck because -verify ignores errors from other files.
// RUN: %target-swift-frontend -enable-lexical-borrow-scopes=false -typecheck -I %t %s 2>&1 | %FileCheck %s --allow-empty
// RUN: %target-swift-frontend -enable-experimental-feature MoveOnlyEnumDeinits -enable-lexical-borrow-scopes=false -typecheck -I %t %s 2>&1 | %FileCheck %s --allow-empty

// This test makes sure that if we import a move only type
// and do not have lexical borrow scopes enabled, we emit an error.
Expand Down