Skip to content

[6.0] Enable failable initializers of noncopyable types #72779

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -7727,8 +7727,6 @@ ERROR(noncopyable_enums_do_not_support_indirect,none,
"noncopyable enum %0 cannot be marked indirect or have indirect cases yet", (Identifier))
ERROR(noncopyable_cast,none,
"noncopyable types cannot be conditionally cast", ())
ERROR(noncopyable_failable_init,none,
"noncopyable types cannot have failable initializers yet", ())
ERROR(noncopyable_objc_enum, none,
"noncopyable enums cannot be marked '@objc'", ())
ERROR(moveOnly_not_allowed_here,none,
Expand Down
148 changes: 97 additions & 51 deletions lib/IRGen/GenEnum.cpp

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions lib/IRGen/GenEnum.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,17 @@ class EnumImplStrategy {
TypeInfoKind TIK;
IsFixedSize_t AlwaysFixedSize;
IsABIAccessible_t ElementsAreABIAccessible;
IsTriviallyDestroyable_t TriviallyDestroyable;
IsCopyable_t Copyable;
IsBitwiseTakable_t BitwiseTakable;
unsigned NumElements;

EnumImplStrategy(IRGenModule &IGM,
TypeInfoKind tik,
IsFixedSize_t alwaysFixedSize,
IsTriviallyDestroyable_t triviallyDestroyable,
IsCopyable_t copyable,
IsBitwiseTakable_t bitwiseTakable,
unsigned NumElements,
std::vector<Element> &&ElementsWithPayload,
std::vector<Element> &&ElementsWithNoPayload);
Expand Down
8 changes: 4 additions & 4 deletions lib/IRGen/StructLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,10 @@ StructLayout::StructLayout(IRGenModule &IGM, std::optional<CanType> type,
SpareBits.clear();
IsFixedLayout = true;
IsLoadable = true;
IsKnownTriviallyDestroyable = deinit;
IsKnownBitwiseTakable = IsBitwiseTakable;
IsKnownAlwaysFixedSize = IsFixedSize;
IsKnownCopyable = copyable;
IsKnownTriviallyDestroyable = deinit & builder.isTriviallyDestroyable();
IsKnownBitwiseTakable = builder.isBitwiseTakable();
IsKnownAlwaysFixedSize = builder.isAlwaysFixedSize();
IsKnownCopyable = copyable & builder.isCopyable();
Ty = (typeToFill ? typeToFill : IGM.OpaqueTy);
} else {
MinimumAlign = builder.getAlignment();
Expand Down
10 changes: 0 additions & 10 deletions lib/Sema/TypeCheckDeclPrimary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4035,16 +4035,6 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
addDelayedFunction(CD);
}

// a move-only / noncopyable type cannot have a failable initializer, since
// that would require the ability to wrap one inside an optional
if (CD->isFailable()) {
if (auto *nom = CD->getDeclContext()->getSelfNominalTypeDecl()) {
if (!nom->canBeCopyable()) {
CD->diagnose(diag::noncopyable_failable_init);
}
}
}

checkDefaultArguments(CD->getParameters());
checkVariadicParameters(CD->getParameters(), CD);

Expand Down
2 changes: 1 addition & 1 deletion test/Generics/inverse_generics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ struct NeverCopyableDeinit<T: ~Copyable>: ~Copyable {
}

protocol Test: ~Copyable {
init?() // expected-error {{noncopyable types cannot have failable initializers yet}}
init?()
}

struct NoncopyableAndSendable: ~Copyable, Sendable {}
Expand Down
49 changes: 49 additions & 0 deletions test/IRGen/moveonly_deinits.swift
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,52 @@ public func testKlassEnumPairWithDeinit() {
consumeKlassEnumPairWithDeinit(f)
}
}

struct EmptyMoveOnlyWithDeinit: ~Copyable {
deinit {}
}

struct EnclosesEmptyMoveOnlyWithDeinit: ~Copyable {
var stored: EmptyMoveOnlyWithDeinit
}

// IR-LABEL: define {{.*}}swiftcc void @"$s16moveonly_deinits35testEnclosesEmptyMoveOnlyWithDeinityyF"()
func testEnclosesEmptyMoveOnlyWithDeinit() {
// CHECK-NOT: ret
// CHECK: call swiftcc void @"$s16moveonly_deinits23EmptyMoveOnlyWithDeinitVfD"()
_ = EnclosesEmptyMoveOnlyWithDeinit(stored: EmptyMoveOnlyWithDeinit())
}

enum ESingle: ~Copyable {
case a(EmptyMoveOnlyWithDeinit)
}

struct OtherEmptyMoveOnlyWithDeinit: ~Copyable {
deinit {}
}

enum EMulti: ~Copyable {
case a(EmptyMoveOnlyWithDeinit)
case b(OtherEmptyMoveOnlyWithDeinit)
}


// IR-LABEL: define {{.*}} swiftcc void @"$s16moveonly_deinits14testSingleEnumyyF"()
func testSingleEnum() {
// IR: call swiftcc void @"$s16moveonly_deinits23EmptyMoveOnlyWithDeinitVfD"()
_ = ESingle.a(EmptyMoveOnlyWithDeinit())
}


// IR-LABEL: define {{.*}}swiftcc void @"$s16moveonly_deinits13testMultiEnumyyF"()
func testMultiEnum() {
// IR: call void @"$s16moveonly_deinits6EMultiOWOe"(i1 true)
_ = EMulti.b(OtherEmptyMoveOnlyWithDeinit())
}

// IR-LABEL: define {{.*}}void @"$s16moveonly_deinits6EMultiOWOe"
// IR: br i1
// IR: 1:
// IR: call swiftcc void @"$s16moveonly_deinits23EmptyMoveOnlyWithDeinitVfD"()
// IR: 2:
// IR: call swiftcc void @"$s16moveonly_deinits28OtherEmptyMoveOnlyWithDeinitVfD"()
29 changes: 29 additions & 0 deletions test/SILGen/moveonly_failable_init.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// RUN: %target-swift-emit-sil -module-name test %s | %FileCheck %s --enable-var-scope

struct MoveWithDeinit: ~Copyable {
deinit { }
}

struct MyType: ~Copyable {
var handle: MoveWithDeinit

// CHECK-LABEL: sil hidden @$s4test6MyTypeV6handleACSgAA14MoveWithDeinitVSg_tcfC : $@convention(method) (@owned Optional<MoveWithDeinit>, @thin MyType.Type) -> @owned Optional<MyType>
// CHECK: bb0(%0 : $Optional<MoveWithDeinit>, %1 : $@thin MyType.Type):
init?(handle: consuming MoveWithDeinit?) {
// CHECK: switch_enum [[SUBJECT:%.*]] : $Optional<MoveWithDeinit>, case #Optional.some!enumelt: bb2, case #Optional.none!enumelt: bb1
guard let handle = consume handle else {
// CHECK: bb1:
// CHECK: [[NONE:%.*]] = enum $Optional<MyType>, #Optional.none!enumelt
// CHECK: br bb3([[NONE]] : $Optional<MyType>)
return nil
}

// CHECK: bb2([[WRAPPED:%.*]] : $MoveWithDeinit):
// CHECK: br bb3
self.handle = handle
}
}

func test() -> MyType? {
return MyType(handle: MoveWithDeinit())
}
14 changes: 7 additions & 7 deletions test/Sema/moveonly_restrictions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ class CopyableKlass {}

@_moveOnly
class MoveOnlyKlass { // expected-note 6{{class 'MoveOnlyKlass' has '~Copyable' constraint preventing 'Copyable' conformance}}
init?() {} // expected-error {{noncopyable types cannot have failable initializers yet}}
init?() {}
}

@_moveOnly
struct MoveOnlyStruct { // expected-note {{struct 'MoveOnlyStruct' has '~Copyable' constraint preventing 'Copyable' conformance}}
init?(one: Bool) {} // expected-error {{noncopyable types cannot have failable initializers yet}}
init!(two: Bool) {} // expected-error {{noncopyable types cannot have failable initializers yet}}
init?(one: Bool) {}
init!(two: Bool) {}
}

class C {
Expand Down Expand Up @@ -78,19 +78,19 @@ enum EMoveOnly {
case lhs(CopyableKlass)
case rhs(MoveOnlyKlass)

init?() {} // expected-error {{noncopyable types cannot have failable initializers yet}}
init?() {}
}

extension EMoveOnly {
init!(three: Bool) {} // expected-error {{noncopyable types cannot have failable initializers yet}}
init!(three: Bool) {}
}

extension MoveOnlyKlass {
convenience init?(three: Bool) {} // expected-error {{noncopyable types cannot have failable initializers yet}}
convenience init?(three: Bool) {}
}

extension MoveOnlyStruct {
init?(three: Bool) {} // expected-error {{noncopyable types cannot have failable initializers yet}}
init?(three: Bool) {}
}

func foo() {
Expand Down