Skip to content

Commit 627bcc5

Browse files
committed
[wip] most of the way there, just need diagnostic I think
1 parent 5e59444 commit 627bcc5

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,9 @@ enum TypeVariableOptions {
333333

334334
/// Whether the type variable can be bound to a pack type or not.
335335
TVO_CanBindToPack = 0x20,
336+
337+
/// Whether the type variable can be bound to a move-only (noncopyable) type.
338+
TVO_CanBindToMoveOnly = 0x40,
336339
};
337340

338341
/// The implementation object for a type variable used within the
@@ -405,6 +408,9 @@ class TypeVariableType::Implementation {
405408
/// Whether this type variable can bind to a PackType.
406409
bool canBindToPack() const { return getRawOptions() & TVO_CanBindToPack; }
407410

411+
/// Whether the type variable can be bound to a move-only (noncopyable) type.
412+
bool canBindToMoveOnly() const { return getRawOptions() & TVO_CanBindToMoveOnly; }
413+
408414
/// Whether this type variable prefers a subtype binding over a supertype
409415
/// binding.
410416
bool prefersSubtypeBinding() const {
@@ -565,6 +571,13 @@ class TypeVariableType::Implementation {
565571
recordBinding(*record);
566572
getTypeVariable()->Bits.TypeVariableType.Options &= ~TVO_CanBindToNoEscape;
567573
}
574+
575+
if (canBindToMoveOnly() && !otherRep->getImpl().canBindToMoveOnly()) {
576+
assert(false && "yay, not dead code!");
577+
if (record)
578+
recordBinding(*record);
579+
getTypeVariable()->Bits.TypeVariableType.Options &= ~TVO_CanBindToMoveOnly;
580+
}
568581
}
569582

570583
/// Retrieve the fixed type that corresponds to this type variable,
@@ -645,6 +658,7 @@ class TypeVariableType::Implementation {
645658
ENTRY(TVO_CanBindToHole, "hole");
646659
ENTRY(TVO_PrefersSubtypeBinding, "");
647660
ENTRY(TVO_CanBindToPack, "pack");
661+
ENTRY(TVO_CanBindToMoveOnly, "moveonly");
648662
}
649663
#undef ENTRY
650664
}

lib/Sema/CSSimplify.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4201,6 +4201,12 @@ ConstraintSystem::matchTypesBindTypeVar(
42014201
}
42024202
}
42034203

4204+
// If the type variable cannot bind to a move-only type, fail.
4205+
if (!typeVar->getImpl().canBindToMoveOnly() && type->isPureMoveOnly()) {
4206+
// TODO: record a fix that just emits an error to reject the substitution.
4207+
return getTypeMatchFailure(locator);
4208+
}
4209+
42044210
// We do not allow keypaths to go through AnyObject. Let's create a fix
42054211
// so this can be diagnosed later.
42064212
if (auto loc = typeVar->getImpl().getLocator()) {

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ void TypeVariableType::Implementation::print(llvm::raw_ostream &OS) {
6767
bindingOptions.push_back(TypeVariableOptions::TVO_CanBindToNoEscape);
6868
if (canBindToHole())
6969
bindingOptions.push_back(TypeVariableOptions::TVO_CanBindToHole);
70+
if (canBindToMoveOnly())
71+
bindingOptions.push_back(TypeVariableOptions::TVO_CanBindToMoveOnly);
7072
if (!bindingOptions.empty()) {
7173
OS << " [allows bindings to: ";
7274
interleave(bindingOptions, OS,

0 commit comments

Comments
 (0)