Skip to content

Commit 12f16fa

Browse files
committed
prevent move-only tuples from matching with Any
`Any`, which is effectively `any _Copyable`, is obviously copyable.
1 parent 271d408 commit 12f16fa

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

include/swift/AST/Types.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -602,9 +602,8 @@ class alignas(1 << TypeAlignInBits) TypeBase
602602

603603
bool isPlaceholder();
604604

605-
/// Returns true if this is a move only type. Returns false if this is a
606-
/// non-move only type or a move only wrapped type.
607-
bool isPureMoveOnly() const;
605+
/// Returns true if this is a move-only type.
606+
bool isPureMoveOnly();
608607

609608
/// Does the type have outer parenthesis?
610609
bool hasParenSugar() const { return getKind() == TypeKind::Paren; }

lib/AST/Type.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,17 @@ bool TypeBase::isAny() {
165165
return constraint->isEqual(getASTContext().TheAnyType);
166166
}
167167

168-
bool TypeBase::isPureMoveOnly() const {
169-
if (auto *nom = getCanonicalType()->getNominalOrBoundGenericNominal())
168+
bool TypeBase::isPureMoveOnly() {
169+
if (auto *nom = getNominalOrBoundGenericNominal())
170170
return nom->isMoveOnly();
171+
172+
// if any components of the tuple are move-only, then the tuple is move-only.
173+
if (auto *tupl = getCanonicalType()->getAs<TupleType>()) {
174+
for (auto eltTy : tupl->getElementTypes())
175+
if (eltTy->isPureMoveOnly())
176+
return true;
177+
}
178+
171179
return false;
172180
}
173181

test/Constraints/moveonly_constraints.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ func testBasic(_ mo: MO) {
9191

9292
let singleton : (MO) = (mo)
9393
takeGeneric(singleton) // expected-error {{move-only type 'MO' cannot be used with generics yet}}
94+
95+
takeAny((mo)) // expected-error {{move-only type 'MO' cannot be used with generics yet}}
96+
takeAny((mo, mo)) // expected-error {{move-only type '(MO, MO)' cannot be used with generics yet}}
9497
}
9598

9699
func checkBasicBoxes() {

0 commit comments

Comments
 (0)