Skip to content

Commit 30c728d

Browse files
committed
prevent MO types from matching with existentials in the solver
1 parent 26452ba commit 30c728d

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3823,6 +3823,18 @@ ConstraintSystem::matchExistentialTypes(Type type1, Type type2,
38233823
return getTypeMatchAmbiguous();
38243824
}
38253825

3826+
// move-only types cannot match with any existential types.
3827+
if (type1->isPureMoveOnly()) {
3828+
// tailor error message
3829+
if (shouldAttemptFixes()) {
3830+
auto *fix = MustBeCopyable::create(*this, type1,
3831+
getConstraintLocator(locator));
3832+
if (!recordFix(fix))
3833+
return getTypeMatchSuccess();
3834+
}
3835+
return getTypeMatchFailure(locator);
3836+
}
3837+
38263838
// FIXME: Feels like a hack.
38273839
if (type1->is<InOutType>())
38283840
return getTypeMatchFailure(locator);

test/Constraints/moveonly_constraints.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,24 @@ func checkMethodCalls() {
137137
takeMaybe(true ? .none : .just(MO())) // expected-error 3{{move-only type 'MO' cannot be used with generics yet}}
138138
}
139139

140-
func checkCasting(_ b: any Box) {
140+
func checkCasting(_ b: any Box, _ mo: MO) {
141141
// casting dynamically is allowed, but should always fail since you can't
142142
// construct such a type.
143143
let box = b as! ValBox<MO> // expected-error {{move-only type 'MO' cannot be used with generics yet}}
144144
let dup = box
145145

146146
let _: MO = dup.get()
147147
let _: MO = dup.val
148+
149+
let _: Sendable = (MO(), MO()) // expected-error {{move-only type '(MO, MO)' cannot be used with generics yet}}
150+
let _: Sendable = MO() // expected-error {{move-only type 'MO' cannot be used with generics yet}}
151+
let _: _Copyable = mo // expected-error {{move-only type 'MO' cannot be used with generics yet}}
152+
let _: AnyObject = MO() // expected-error {{move-only type 'MO' cannot be used with generics yet}}
153+
let _: Any = mo // expected-error {{move-only type 'MO' cannot be used with generics yet}}
154+
155+
// FIXME: this shouldn't be allowed
156+
let _: AnyHashable = MO() as! AnyHashable
157+
148158
}
149159

150160
func checkStdlibTypes(_ mo: MO) {

0 commit comments

Comments
 (0)