Skip to content

Commit 1959858

Browse files
authored
Merge pull request #66847 from kavon/5.9-noncopyable-and-raw-error
[5.9🍒] emit error when a noncopyable enum has a raw type
2 parents e680218 + 6cf7ca8 commit 1959858

File tree

4 files changed

+21
-6
lines changed

4 files changed

+21
-6
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3177,6 +3177,9 @@ ERROR(enum_raw_type_nonconforming_and_nonsynthable,none,
31773177
"and conformance could not be synthesized", (Type, Type))
31783178
NOTE(enum_declares_rawrep_with_raw_type,none,
31793179
"%0 declares raw type %1, which implies RawRepresentable", (Type, Type))
3180+
ERROR(enum_raw_type_nonconforming_and_noncopyable,none,
3181+
"%0 declares raw type %1, but cannot yet conform to RawRepresentable "
3182+
"because it is noncopyable", (Type, Type))
31803183
ERROR(enum_raw_type_access,none,
31813184
"enum %select{must be declared %select{"
31823185
"%select{private|fileprivate|internal|package|%error|%error}1|private or fileprivate}3"

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ static void checkInheritanceClause(
277277
}
278278

279279
// If this is an enum inheritance clause, check for a raw type.
280-
if (isa<EnumDecl>(decl)) {
280+
if (auto enumDecl = dyn_cast<EnumDecl>(decl)) {
281281
// Check if we already had a raw type.
282282
if (superclassTy) {
283283
if (superclassTy->isEqual(inheritedTy)) {
@@ -293,6 +293,18 @@ static void checkInheritanceClause(
293293
}
294294
continue;
295295
}
296+
297+
// Noncopyable types cannot have a raw type until there is support for
298+
// generics, since the raw type here is only useful if we'll generate
299+
// a conformance to RawRepresentable, which is currently disabled.
300+
if (enumDecl->isMoveOnly()) {
301+
// TODO: getRemovalRange is not yet aware of ~Copyable entries so it
302+
// will accidentally delete commas or colons that are needed.
303+
diags.diagnose(inherited.getSourceRange().Start,
304+
diag::enum_raw_type_nonconforming_and_noncopyable,
305+
enumDecl->getDeclaredInterfaceType(), inheritedTy)
306+
.highlight(inherited.getSourceRange());
307+
}
296308

297309
// If this is not the first entry in the inheritance clause, complain.
298310
if (i > 0) {
@@ -303,11 +315,9 @@ static void checkInheritanceClause(
303315
.fixItRemoveChars(removeRange.Start, removeRange.End)
304316
.fixItInsert(inheritedClause[0].getSourceRange().Start,
305317
inheritedTy.getString() + ", ");
306-
307-
// Fall through to record the raw type.
308318
}
309319

310-
// Record the raw type.
320+
// Save the raw type locally.
311321
superclassTy = inheritedTy;
312322
superclassRange = inherited.getSourceRange();
313323
continue;

test/Sema/moveonly_objc_enum.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66

77
@_moveOnly
88
@objc enum Foo : Int { // expected-error {{noncopyable enums cannot be marked '@objc'}}
9+
// expected-error@-1 {{'Foo' declares raw type 'Int', but cannot yet conform to RawRepresentable because it is noncopyable}}
910
case X, Y, Z
1011
deinit {} // expected-error {{deinitializers cannot be declared on an @objc enum type}}
1112
}
1213

1314
@_moveOnly
1415
@objc enum Foo2 : Int { // expected-error {{noncopyable enums cannot be marked '@objc'}}
16+
// expected-error@-1 {{'Foo2' declares raw type 'Int', but cannot yet conform to RawRepresentable because it is noncopyable}}
1517
case X, Y, Z
1618
}
1719

test/Sema/moveonly_restrictions.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,8 @@ enum Color {
216216
}
217217
}
218218

219-
@_moveOnly
220-
enum StrengthLevel: Int { // ensure move-only raw enums do not conform to RawRepresentable
219+
// expected-error@+1:21 {{'StrengthLevel' declares raw type 'Int', but cannot yet conform to RawRepresentable because it is noncopyable}}
220+
enum StrengthLevel: Int, ~Copyable {
221221
case none = 0
222222
case low
223223
case high

0 commit comments

Comments
 (0)