Skip to content

Commit fb26e29

Browse files
Merge pull request #71102 from nate-chandler/bitwise-copyable/not-for-nonescaping-noncopyable
[BitwiseCopyable] Add targeted diagnostics for Copyable and Escapable.
2 parents 443489f + 41f3451 commit fb26e29

File tree

4 files changed

+46
-11
lines changed

4 files changed

+46
-11
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: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,18 @@ void BitwiseCopyableStorageVisitor::emitNonconformingMemberTypeDiagnostic(
219219
static bool checkBitwiseCopyableInstanceStorage(NominalTypeDecl *nominal,
220220
DeclContext *dc,
221221
BitwiseCopyableCheck check) {
222-
// If the BitwiseCopyable protocol doesn't exist, there's nothing to do.
223-
if (!dc->getParentModule()->getASTContext().getProtocol(
224-
KnownProtocolKind::BitwiseCopyable))
225-
return false;
222+
assert(dc->getParentModule()->getASTContext().getProtocol(
223+
KnownProtocolKind::BitwiseCopyable));
224+
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+
}
226234

227235
if (isa<ClassDecl>(nominal)) {
228236
if (!isImplicit(check)) {

test/SILGen/bitwise_copyable.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,20 @@ struct B<T> {
2222
func doit() -> B<Int> {
2323
return .init(t: 0)
2424
}
25+
26+
struct Conditional<T> {
27+
var t: T
28+
}
29+
extension Conditional : _BitwiseCopyable where T : _BitwiseCopyable {}
30+
31+
func doit() -> B<Conditional<Int>> {
32+
.init(t: .init(t: 0))
33+
}
34+
35+
enum Context<T> {
36+
struct Here {
37+
var t: T
38+
}
39+
}
40+
41+
func doit() -> Context<Int>.Here { .init(t: 0) }

test/Sema/bitwise_copyable.swift

Lines changed: 13 additions & 7 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,8 +186,12 @@ 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
//==============================================================================
188-
//==========================STDLIB-DEPENDENCY TESTS=(BEGIN)==================={{
194+
//==========================STDLIB-DEPENDENCY TESTS=(END)=====================}}
189195
//==============================================================================
190196

191197
//==============================================================================

0 commit comments

Comments
 (0)