Skip to content

Commit 92a58e2

Browse files
authored
Merge pull request #64123 from kavon/moveonly-no-failable-init
disallow failable initializers for noncopyable types
2 parents 9b722b9 + 971ce81 commit 92a58e2

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6748,6 +6748,8 @@ ERROR(moveonly_enums_do_not_support_indirect,none,
67486748
"move-only enum %0 cannot be marked indirect or have indirect cases yet", (Identifier))
67496749
ERROR(moveonly_cast,none,
67506750
"move-only types cannot be conditionally cast", ())
6751+
ERROR(moveonly_failable_init,none,
6752+
"move-only types cannot have failable initializers yet", ())
67516753

67526754
//------------------------------------------------------------------------------
67536755
// MARK: Type inference from default expressions

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3603,6 +3603,16 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
36033603
addDelayedFunction(CD);
36043604
}
36053605

3606+
// a move-only / noncopyable type cannot have a failable initializer, since
3607+
// that would require the ability to wrap one inside an optional
3608+
if (CD->isFailable()) {
3609+
if (auto *nom = CD->getDeclContext()->getSelfNominalTypeDecl()) {
3610+
if (nom->isMoveOnly()) {
3611+
CD->diagnose(diag::moveonly_failable_init);
3612+
}
3613+
}
3614+
}
3615+
36063616
checkDefaultArguments(CD->getParameters());
36073617
checkVariadicParameters(CD->getParameters(), CD);
36083618
}

test/Sema/moveonly_restrictions.swift

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
// RUN: %target-typecheck-verify-swift -enable-experimental-move-only -enable-experimental-feature MoveOnlyClasses
22

33
class CopyableKlass {}
4+
45
@_moveOnly
5-
class MoveOnlyKlass {}
6+
class MoveOnlyKlass {
7+
init?() {} // expected-error {{move-only types cannot have failable initializers yet}}
8+
}
9+
610
@_moveOnly
7-
class MoveOnlyStruct {}
11+
class MoveOnlyStruct {
12+
init?(one: Bool) {} // expected-error {{move-only types cannot have failable initializers yet}}
13+
init!(two: Bool) {} // expected-error {{move-only types cannot have failable initializers yet}}
14+
}
815

916
class C {
1017
var copyable: CopyableKlass? = nil
@@ -57,6 +64,16 @@ enum E { // expected-error {{enum 'E' cannot contain a move-only type without al
5764
enum EMoveOnly {
5865
case lhs(CopyableKlass)
5966
case rhs(MoveOnlyKlass)
67+
68+
init?() {} // expected-error {{move-only types cannot have failable initializers yet}}
69+
}
70+
71+
extension EMoveOnly {
72+
init!(three: Bool) {} // expected-error {{move-only types cannot have failable initializers yet}}
73+
}
74+
75+
extension MoveOnlyStruct {
76+
convenience init?(three: Bool) {} // expected-error {{move-only types cannot have failable initializers yet}}
6077
}
6178

6279
func foo() {

0 commit comments

Comments
 (0)