Skip to content

Commit 924064e

Browse files
committed
move noncopyable types out from behind the feature flag
Since these types depend on lexical borrow scopes (a SIL option), we're now making that the requirement whenever such types appear.
1 parent ec35838 commit 924064e

File tree

6 files changed

+47
-31
lines changed

6 files changed

+47
-31
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6780,11 +6780,6 @@ ERROR(experimental_moveonly_feature_can_only_be_used_when_enabled,
67806780
none, "Can not use feature when experimental move only is disabled! Pass"
67816781
" the frontend flag -enable-experimental-move-only to swift to enable "
67826782
"the usage of this language feature", ())
6783-
ERROR(experimental_moveonly_feature_can_only_be_imported_when_enabled,
6784-
none, "Can not import module %0 that uses move only features when "
6785-
"experimental move only is disabled! Pass the frontend flag "
6786-
"-enable-experimental-move-only to swift to enable the usage of this "
6787-
"language feature", (Identifier))
67886783
ERROR(noimplicitcopy_attr_valid_only_on_local_let_params,
67896784
none, "'@_noImplicitCopy' attribute can only be applied to local lets and params", ())
67906785
ERROR(noimplicitcopy_attr_invalid_in_generic_context,
@@ -6867,6 +6862,10 @@ ERROR(move_expression_not_passed_lvalue,none,
68676862
ERROR(borrow_expression_not_passed_lvalue,none,
68686863
"'borrow' can only be applied to lvalues", ())
68696864

6865+
ERROR(moveOnly_requires_lexical_lifetimes,none,
6866+
"noncopyable types require lexical borrow scopes "
6867+
"(add -enable-lexical-borrow-scopes=true)", ())
6868+
68706869
//------------------------------------------------------------------------------
68716870
// MARK: #_hasSymbol
68726871
//------------------------------------------------------------------------------

lib/Sema/TypeCheckAttr.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2157,12 +2157,17 @@ void AttributeChecker::visitFinalAttr(FinalAttr *attr) {
21572157
}
21582158

21592159
void AttributeChecker::visitMoveOnlyAttr(MoveOnlyAttr *attr) {
2160-
if (!D->getASTContext().LangOpts.hasFeature(Feature::MoveOnly)) {
2161-
auto error =
2162-
diag::experimental_moveonly_feature_can_only_be_used_when_enabled;
2163-
diagnoseAndRemoveAttr(attr, error);
2164-
return;
2165-
}
2160+
#ifndef NDEBUG
2161+
// Try to catch uses of @_moveOnly when we don't have the Copyable protocol.
2162+
// This can only happen in rare circumstances with test cases where you're
2163+
// building with -parse-stdlib. You need to also specify that you're building
2164+
// the Swift module (`-module-name Swift`) and then explicitly define Copyable
2165+
if (!D->getASTContext().getProtocol(KnownProtocolKind::Copyable))
2166+
llvm_unreachable("must define Copyable protocol when using -parse-stdlib");
2167+
#endif
2168+
2169+
if (!D->getASTContext().supportsMoveOnlyTypes())
2170+
D->diagnose(diag::moveOnly_requires_lexical_lifetimes);
21662171

21672172
if (isa<StructDecl>(D) || isa<EnumDecl>(D))
21682173
return;

lib/Sema/TypeCheckDecl.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -918,8 +918,14 @@ IsFinalRequest::evaluate(Evaluator &evaluator, ValueDecl *decl) const {
918918

919919
bool IsMoveOnlyRequest::evaluate(Evaluator &evaluator, ValueDecl *decl) const {
920920
// For now only do this for nominal type decls.
921-
if (isa<NominalTypeDecl>(decl))
922-
return decl->getAttrs().hasAttribute<MoveOnlyAttr>();
921+
if (isa<NominalTypeDecl>(decl)) {
922+
if (decl->getAttrs().hasAttribute<MoveOnlyAttr>()) {
923+
if (!decl->getASTContext().supportsMoveOnlyTypes())
924+
decl->diagnose(diag::moveOnly_requires_lexical_lifetimes);
925+
926+
return true;
927+
}
928+
}
923929
return false;
924930
}
925931

lib/Serialization/Deserialization.cpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5484,17 +5484,6 @@ llvm::Error DeclDeserializer::deserializeDeclCommon() {
54845484
MF.fatal(llvm::make_error<InvalidRecordKindError>(recordID));
54855485
}
54865486

5487-
// Do a quick check to see if this attribute is a move only attribute. If
5488-
// so, emit a nice error if we don't have experimental move only enabled.
5489-
if (Attr && Attr->getKind() == DeclAttrKind::DAK_MoveOnly &&
5490-
!MF.getContext().LangOpts.Features.contains(Feature::MoveOnly)) {
5491-
MF.getContext().Diags.diagnose(
5492-
SourceLoc(),
5493-
diag::
5494-
experimental_moveonly_feature_can_only_be_imported_when_enabled,
5495-
MF.getAssociatedModule()->getName());
5496-
}
5497-
54985487
if (!skipAttr) {
54995488
if (!Attr)
55005489
return llvm::Error::success();
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// RUN: %target-swift-frontend -typecheck %s
2+
// RUN: %target-typecheck-verify-swift -enable-lexical-borrow-scopes=false
3+
4+
@_moveOnly
5+
public struct MO { // expected-error 2{{noncopyable types require lexical borrow scopes (add -enable-lexical-borrow-scopes=true)}}
6+
let x = 0
7+
}
8+
9+
public func whatever(_ mo: borrowing MO) {}
Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
11
// RUN: %empty-directory(%t)
22
// TODO: re-enable the simplification passes once rdar://104875010 is fixed
3-
// 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-move-only
4-
// RUN: not %target-swift-frontend -DUSE_MOVEONLY_TYPE -I %t %s -emit-sil -o /dev/null 2>&1 | %FileCheck %s
5-
// RUN: %target-swift-frontend -I %t %s -emit-sil -verify -o /dev/null
3+
// RUN: %target-swift-frontend -Xllvm -sil-disable-pass=simplification -emit-module -o %t/Library.swiftmodule -module-name Library %S/Inputs/moveonly_deinit.swift
64

7-
// This test makes sure that if we import a move only type and do not set the
8-
// experimental move only flag, we get a nice error.
5+
// >>> make sure borrow scopes are required when using a move-only type from another module
6+
// 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
7+
// RUN: %target-swift-frontend -DUSE_MOVEONLY_TYPE -typecheck -I %t %s 2>&1 | %FileCheck %s --allow-empty
8+
9+
// >>> try turning off lexical borrow scopes with no move-only types; shouldn't be an error.
10+
// we have to pipe into FileCheck because -verify ignores errors from other files.
11+
// RUN: %target-swift-frontend -enable-lexical-borrow-scopes=false -typecheck -I %t %s 2>&1 | %FileCheck %s --allow-empty
12+
13+
// This test makes sure that if we import a move only type
14+
// and do not have lexical borrow scopes enabled, we emit an error.
915

1016
import Library
1117

12-
// CHECK: error: Can not import module 'Library' that uses move only features when experimental move only is disabled! Pass the frontend flag -enable-experimental-move-only to swift to enable the usage of this language feature
18+
// CHECK-ERROR: Library.MoveOnlyStruct{{.*}} error: noncopyable types require lexical borrow scopes (add -enable-lexical-borrow-scopes=true)
19+
20+
// CHECK-NOT: error
1321

1422
#if USE_MOVEONLY_TYPE
15-
func f(_ k: MoveOnlyStruct) {}
23+
func f(_ k: borrowing MoveOnlyStruct) {}
1624
#endif
1725

1826
func g(_ k: NormalStruct) {}

0 commit comments

Comments
 (0)