Skip to content

Commit 898737d

Browse files
committed
[BitwiseCopyable] Require Copyable, Escapable.
In order to conform to _BitwiseCopyable, require that a type not be ~Copyable or ~Escapable. Once the stdlib is built with the relevant feature flags, that checking will occur automatically. Until then, have these bespoke diagnostics.
1 parent 287ee75 commit 898737d

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7611,6 +7611,10 @@ ERROR(escapable_requires_feature_flag,none,
76117611
())
76127612
ERROR(non_bitwise_copyable_type_class,none,
76137613
"class cannot conform to 'BitwiseCopyable'", ())
7614+
ERROR(non_bitwise_copyable_type_noncopyable,none,
7615+
"noncopyable type cannot conform to 'BitwiseCopyable'", ())
7616+
ERROR(non_bitwise_copyable_type_nonescapable,none,
7617+
"nonescapable type cannot conform to 'BitwiseCopyable'", ())
76147618
ERROR(non_bitwise_copyable_type_member,none,
76157619
"%select{stored property %2|associated value %2}1 of "
76167620
"'BitwiseCopyable'-conforming %kind3 has non-bitwise-copyable type %0",

lib/Sema/TypeCheckBitwise.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,16 @@ static bool checkBitwiseCopyableInstanceStorage(NominalTypeDecl *nominal,
222222
assert(dc->getParentModule()->getASTContext().getProtocol(
223223
KnownProtocolKind::BitwiseCopyable));
224224

225+
if (dc->mapTypeIntoContext(nominal->getDeclaredInterfaceType())->isNoncopyable()) {
226+
nominal->diagnose(diag::non_bitwise_copyable_type_noncopyable);
227+
return true;
228+
}
229+
230+
if (!dc->mapTypeIntoContext(nominal->getDeclaredInterfaceType())->isEscapable()) {
231+
nominal->diagnose(diag::non_bitwise_copyable_type_nonescapable);
232+
return true;
233+
}
234+
225235
if (isa<ClassDecl>(nominal)) {
226236
if (!isImplicit(check)) {
227237
nominal->diagnose(diag::non_bitwise_copyable_type_class);

test/Sema/bitwise_copyable.swift

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
// RUN: %target-typecheck-verify-swift \
2-
// RUN: -disable-availability-checking \
3-
// RUN: -enable-experimental-feature BitwiseCopyable \
4-
// RUN: -enable-builtin-module \
1+
// RUN: %target-typecheck-verify-swift \
2+
// RUN: -disable-availability-checking \
3+
// RUN: -enable-experimental-feature NonescapableTypes \
4+
// RUN: -enable-experimental-feature NoncopyableGenerics \
5+
// RUN: -enable-experimental-feature BitwiseCopyable \
6+
// RUN: -enable-builtin-module \
57
// RUN: -debug-diagnostic-names
68

79
//==============================================================================
@@ -154,8 +156,8 @@ func passAnyAny(_ a: any Any) { take3(a) } // expected-error {{type_does_not_con
154156
func passString(_ s: String) { take3(s) } // expected-error {{type_does_not_conform_decl_owner}}
155157
// expected-note@-17 {{where_requirement_failure_one_subst}}
156158

157-
extension Optional {
158-
struct Some : _BitwiseCopyable {
159+
extension Optional where Wrapped : Copyable & Escapable {
160+
struct Some : _BitwiseCopyable & Copyable & Escapable {
159161
var wrapped: Wrapped // expected-error {{non_bitwise_copyable_type_member}}
160162
}
161163
}
@@ -184,6 +186,10 @@ struct S_Explicit_With_2_BitwiseCopyable_Generic_Optional<T : _BitwiseCopyable>
184186
var o2: T?
185187
}
186188

189+
struct S_Explicit_Nonescapable : ~Escapable, _BitwiseCopyable {} // expected-error{{non_bitwise_copyable_type_nonescapable}}
190+
191+
struct S_Explicit_Noncopyable : ~Copyable, _BitwiseCopyable {} // expected-error{{non_bitwise_copyable_type_noncopyable}}
192+
187193
//==============================================================================
188194
//==========================STDLIB-DEPENDENCY TESTS=(BEGIN)==================={{
189195
//==============================================================================

0 commit comments

Comments
 (0)