Skip to content

Commit ef5903f

Browse files
authored
Merge pull request #63443 from gottesmm/pr-1c9625a7ee5b17755b835a7ff4aa7d4d7f6a04d6
[move-only] For now ban non-final classes from containing move only stored fields.
2 parents dc1b73f + 3c976e9 commit ef5903f

File tree

7 files changed

+32
-25
lines changed

7 files changed

+32
-25
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6938,6 +6938,8 @@ ERROR(moveonly_cannot_conform_to_protocol, none,
69386938
ERROR(moveonly_cannot_conform_to_protocol_with_name, none,
69396939
"move-only %0 %1 cannot conform to protocol %2",
69406940
(DescriptiveDeclKind, DeclName, DeclName))
6941+
ERROR(moveonly_non_final_class_cannot_contain_moveonly_field, none,
6942+
"non-final classes containing move only fields is not yet supported", ())
69416943

69426944
//------------------------------------------------------------------------------
69436945
// MARK: Runtime discoverable attributes (@runtimeMetadata)

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2926,6 +2926,16 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
29262926
maybeDiagnoseClassWithoutInitializers(CD);
29272927

29282928
diagnoseMoveOnlyNominalDeclDoesntConformToProtocols(CD);
2929+
2930+
// Ban non-final classes from having move only fields.
2931+
if (!CD->isFinal()) {
2932+
for (auto *field : CD->getStoredProperties()) {
2933+
if (field->getType()->isPureMoveOnly()) {
2934+
field->diagnose(
2935+
diag::moveonly_non_final_class_cannot_contain_moveonly_field);
2936+
}
2937+
}
2938+
}
29292939
}
29302940

29312941
void visitProtocolDecl(ProtocolDecl *PD) {

test/SILGen/moveonly.swift

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
// Declarations //
55
//////////////////
66

7-
public class CopyableKlass {
7+
public final class CopyableKlass {
88
var k = Klass()
99
}
1010

1111
@_moveOnly
1212
public class Klass2 {}
1313

1414
@_moveOnly
15-
public class Klass {
15+
public final class Klass {
1616
var int: Int
1717
var moveOnlyKlass: Klass2
1818
var copyableKlass: CopyableKlass
@@ -333,8 +333,9 @@ func klassBorrowAddressFunctionCall2() {
333333
// CHECK: [[LOAD:%.*]] = load [copy] [[ACCESS]]
334334
// CHECK: end_access [[ACCESS]]
335335
// CHECK: [[BORROW_LOAD:%.*]] = begin_borrow [[LOAD]]
336-
// CHECK: [[COPY_CLASS:%.*]] = apply {{%.*}}([[BORROW_LOAD]]
337-
// CHECK: [[BORROWED_COPY_CLASS:%.*]] = begin_borrow [[COPY_CLASS]]
336+
// CHECK: [[ADDR:%.*]] = ref_element_addr [[BORROW_LOAD]]
337+
// CHECK: [[ACCESS_ADDR:%.*]] = begin_access [read] [dynamic] [[ADDR]]
338+
// CHECK: [[BORROWED_COPY_CLASS:%.*]] = load_borrow [[ACCESS_ADDR]]
338339
// CHECK: apply {{%.*}}([[BORROWED_COPY_CLASS]])
339340
// CHECK: } // end sil function '$s8moveonly38klassBorrowCopyableAddressFunctionCallyyF'
340341
func klassBorrowCopyableAddressFunctionCall() {
@@ -383,15 +384,13 @@ func moveOnlyStructMoveOnlyKlassNonConsumingUse() {
383384
// CHECK: [[STRUCT_EXT_COPY:%.*]] = load [copy] [[STRUCT_EXT]]
384385
// CHECK: end_access [[ACCESS]]
385386
// CHECK: [[STRUCT_EXT_COPY_BORROW:%.*]] = begin_borrow [[STRUCT_EXT_COPY]]
386-
// CHECK: [[METHOD:%.*]] = class_method [[STRUCT_EXT_COPY_BORROW]]
387-
// CHECK: [[KLS:%.*]] = apply [[METHOD]]([[STRUCT_EXT_COPY_BORROW]])
388-
// CHECK: end_borrow [[STRUCT_EXT_COPY_BORROW]]
389-
// CHECK: [[BORROWED_KLS:%.*]] = begin_borrow [[KLS]]
387+
// CHECK: [[ELT_ADDR:%.*]] = ref_element_addr [[STRUCT_EXT_COPY_BORROW]]
388+
// CHECK: [[ACCESS_ELT_ADDR:%.*]] = begin_access [read] [dynamic] [[ELT_ADDR]]
389+
// CHECK: [[KLS:%.*]] = load_borrow [[ACCESS_ELT_ADDR]]
390390
// CHECK: [[FN:%.*]] = function_ref @$s8moveonly21nonConsumingUseKlass2yyAA0E0CF
391-
// CHECK: apply [[FN]]([[BORROWED_KLS]])
392-
// CHECK: end_borrow [[BORROWED_KLS]]
391+
// CHECK: apply [[FN]]([[KLS]])
392+
// CHECK: end_borrow [[KLS]]
393393
// CHECK: destroy_value [[STRUCT_EXT_COPY]]
394-
// CHECK: destroy_value [[KLS]]
395394
// CHECK: } // end sil function '$s8moveonly018moveOnlyStructMovec5KlassecF15NonConsumingUseyyF'
396395
func moveOnlyStructMoveOnlyKlassMoveOnlyKlassNonConsumingUse() {
397396
var k = NonTrivialStruct()
@@ -408,15 +407,13 @@ func moveOnlyStructMoveOnlyKlassMoveOnlyKlassNonConsumingUse() {
408407
// CHECK: [[STRUCT_EXT_COPY:%.*]] = load [copy] [[STRUCT_EXT]]
409408
// CHECK: end_access [[ACCESS]]
410409
// CHECK: [[STRUCT_EXT_COPY_BORROW:%.*]] = begin_borrow [[STRUCT_EXT_COPY]]
411-
// CHECK: [[METHOD:%.*]] = class_method [[STRUCT_EXT_COPY_BORROW]]
412-
// CHECK: [[KLS:%.*]] = apply [[METHOD]]([[STRUCT_EXT_COPY_BORROW]])
413-
// CHECK: end_borrow [[STRUCT_EXT_COPY_BORROW]]
414-
// CHECK: [[BORROWED_KLS:%.*]] = begin_borrow [[KLS]]
410+
// CHECK: [[FIELD:%.*]] = ref_element_addr [[STRUCT_EXT_COPY_BORROW]]
411+
// CHECK: [[ACCESS:%.*]] = begin_access [read] [dynamic] [[FIELD]]
412+
// CHECK: [[BORROWED_KLS:%.*]] = load_borrow [[ACCESS]]
415413
// CHECK: [[FN:%.*]] = function_ref @$s8moveonly28nonConsumingUseCopyableKlassyyAA0eF0CF
416414
// CHECK: apply [[FN]]([[BORROWED_KLS]])
417415
// CHECK: end_borrow [[BORROWED_KLS]]
418416
// CHECK: destroy_value [[STRUCT_EXT_COPY]]
419-
// CHECK: destroy_value [[KLS]]
420417
// CHECK: } // end sil function '$s8moveonly018moveOnlyStructMovec13KlassCopyableF15NonConsumingUseyyF'
421418
func moveOnlyStructMoveOnlyKlassCopyableKlassNonConsumingUse() {
422419
var k = NonTrivialStruct()
@@ -569,15 +566,13 @@ func moveOnlyStructCopyableStructCopyableStructCopyableKlassNonConsumingUse() {
569566
// CHECK: [[COPYABLE_KLASS:%.*]] = load [copy] [[GEP3]]
570567
// CHECK: end_access [[ACCESS]]
571568
// CHECK: [[BORROWED_COPYABLE_KLASS:%.*]] = begin_borrow [[COPYABLE_KLASS]]
572-
// CHECK: [[FN:%.*]] = class_method [[BORROWED_COPYABLE_KLASS]] : $CopyableKlass, #CopyableKlass.k!getter :
573-
// CHECK: [[MOVEONLY_KLASS:%.*]] = apply [[FN]]([[BORROWED_COPYABLE_KLASS]])
574-
// CHECK: end_borrow [[BORROWED_COPYABLE_KLASS]]
575-
// CHECK: [[BORROWED_MOVEONLY_KLASS:%.*]] = begin_borrow [[MOVEONLY_KLASS]]
569+
// CHECK: [[FIELD:%.*]] = ref_element_addr [[BORROWED_COPYABLE_KLASS]]
570+
// CHECK: [[ACCESS:%.*]] = begin_access [read] [dynamic] [[FIELD]]
571+
// CHECK: [[BORROWED_MOVEONLY_KLASS:%.*]] = load_borrow [[ACCESS]]
576572
// CHECK: [[FN:%.*]] = function_ref @$s8moveonly20nonConsumingUseKlassyyAA0E0CF :
577573
// CHECK: apply [[FN]]([[BORROWED_MOVEONLY_KLASS]])
578574
// CHECK: end_borrow [[BORROWED_MOVEONLY_KLASS]]
579575
// CHECK: destroy_value [[COPYABLE_KLASS]]
580-
// CHECK: destroy_value [[MOVEONLY_KLASS]]
581576
// CHECK: } // end sil function '$s8moveonly022moveOnlyStructCopyabledede9KlassMovecF15NonConsumingUseyyF'
582577
func moveOnlyStructCopyableStructCopyableStructCopyableKlassMoveOnlyKlassNonConsumingUse() {
583578
var k = NonTrivialStruct()

test/SILOptimizer/moveonly_addresschecker.sil

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import Swift
77
public class CopyableKlass {}
88

99
@_moveOnly
10-
public class Klass {
10+
public final class Klass {
1111
var intField: Int
1212
var k: Klass
1313
init()

test/SILOptimizer/moveonly_addresschecker_diagnostics.sil

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import Builtin
1818
public class CopyableKlass {}
1919

2020
@_moveOnly
21-
public class Klass {
21+
public final class Klass {
2222
var intField: Int
2323
var k: Klass
2424
init()

test/SILOptimizer/moveonly_addresschecker_diagnostics.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public func consumeVal<T>(_ x: __owned AggGenericStruct<T>) {}
2828
public func consumeVal(_ x: __owned EnumTy) {}
2929

3030
@_moveOnly
31-
public class Klass {
31+
public final class Klass {
3232
var intField: Int
3333
var k: Klass
3434
init() {

test/SILOptimizer/moveonly_objectchecker_diagnostics.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
//////////////////
66

77
@_moveOnly
8-
public class Klass {
8+
public final class Klass {
99
var intField: Int
1010
var k: Klass
1111
init() {

0 commit comments

Comments
 (0)