Skip to content

Commit 1a3b42c

Browse files
authored
Revert "Prevent noncopyable metatypes from being converted to Any"
1 parent 3bad906 commit 1a3b42c

File tree

6 files changed

+15
-41
lines changed

6 files changed

+15
-41
lines changed

lib/AST/Module.cpp

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1768,18 +1768,10 @@ static ProtocolConformanceRef getBuiltinFunctionTypeConformance(
17681768
/// appropriate.
17691769
static ProtocolConformanceRef getBuiltinMetaTypeTypeConformance(
17701770
Type type, const AnyMetatypeType *metatypeType, ProtocolDecl *protocol) {
1771-
ASTContext &ctx = protocol->getASTContext();
1772-
1773-
// Only metatypes of Copyable types are Copyable.
1774-
if (protocol->isSpecificProtocol(KnownProtocolKind::Copyable) &&
1775-
!metatypeType->getInstanceType()->isPureMoveOnly()) {
1776-
return ProtocolConformanceRef(
1777-
ctx.getBuiltinConformance(type, protocol, GenericSignature(), { },
1778-
BuiltinConformanceKind::Synthesized));
1779-
}
1780-
1781-
// All metatypes are Sendable
1782-
if (protocol->isSpecificProtocol(KnownProtocolKind::Sendable)) {
1771+
// All metatypes are Sendable and Copyable
1772+
if (protocol->isSpecificProtocol(KnownProtocolKind::Sendable) ||
1773+
protocol->isSpecificProtocol(KnownProtocolKind::Copyable)) {
1774+
ASTContext &ctx = protocol->getASTContext();
17831775
return ProtocolConformanceRef(
17841776
ctx.getBuiltinConformance(type, protocol, GenericSignature(), { },
17851777
BuiltinConformanceKind::Synthesized));

lib/Sema/CSSimplify.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3834,8 +3834,8 @@ ConstraintSystem::matchExistentialTypes(Type type1, Type type2,
38343834
return getTypeMatchAmbiguous();
38353835
}
38363836

3837-
// move-only types (and their metatypes) cannot match with existential types.
3838-
if (type1->getMetatypeInstanceType()->isPureMoveOnly()) {
3837+
// move-only types cannot match with any existential types.
3838+
if (type1->isPureMoveOnly()) {
38393839
// tailor error message
38403840
if (shouldAttemptFixes()) {
38413841
auto *fix = MustBeCopyable::create(*this, type1,

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1642,12 +1642,12 @@ TypeChecker::typeCheckCheckedCast(Type fromType, Type toType,
16421642
}
16431643

16441644
// Since move-only types currently cannot conform to protocols, nor be a class
1645-
// type, the subtyping hierarchy looks a bit like this:
1645+
// type, the subtyping hierarchy is a bit bizarre as of now:
16461646
//
1647-
// ~Copyable
1648-
// / \
1649-
// / \
1650-
// +--------- Any noncopyable structs/enums
1647+
// noncopyable
1648+
// structs and enums
1649+
// |
1650+
// +--------- Any
16511651
// | |
16521652
// AnyObject protocol
16531653
// | existentials
@@ -1659,9 +1659,7 @@ TypeChecker::typeCheckCheckedCast(Type fromType, Type toType,
16591659
//
16601660
//
16611661
// Thus, right now, a move-only type is only a subtype of itself.
1662-
// We also want to prevent conversions of a move-only type's metatype.
1663-
if (fromType->getMetatypeInstanceType()->isPureMoveOnly()
1664-
|| toType->getMetatypeInstanceType()->isPureMoveOnly())
1662+
if (fromType->isPureMoveOnly() || toType->isPureMoveOnly())
16651663
return CheckedCastKind::Unresolved;
16661664

16671665
// Check for a bridging conversion.

stdlib/public/Concurrency/PartialAsyncTask.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,9 @@ public struct ExecutorJob: Sendable {
221221
/// and it appearing as 0 for _different_ jobs may lead to misunderstanding it as
222222
/// being "the same 0 id job", we specifically print 0 (id not set) as nil.
223223
if (id > 0) {
224-
return "ExecutorJob(id: \(id))"
224+
return "\(Self.self)(id: \(id))"
225225
} else {
226-
return "ExecutorJob(id: nil)"
226+
return "\(Self.self)(id: nil)"
227227
}
228228
}
229229
}

stdlib/public/Distributed/DistributedDefaultExecutor.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ internal final class DistributedRemoteActorReferenceExecutor: SerialExecutor {
2626
@inlinable
2727
public func enqueue(_ job: __owned ExecutorJob) {
2828
let jobDescription = job.description
29-
fatalError("Attempted to enqueue ExecutorJob (\(jobDescription)) on executor of remote distributed actor reference!")
29+
fatalError("Attempted to enqueue \(ExecutorJob.self) (\(jobDescription)) on executor of remote distributed actor reference!")
3030
}
3131

3232
public func asUnownedSerialExecutor() -> UnownedSerialExecutor {

test/Constraints/moveonly_constraints.swift

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,6 @@ func checkCasting(_ b: any Box, _ mo: borrowing MO, _ a: Any) {
148148
let _: MO = dup.get()
149149
let _: MO = dup.val
150150

151-
let _: Any = MO.self // expected-error {{move-only type 'MO.Type' cannot be used with generics yet}}
152-
let _: AnyObject = MO.self // expected-error {{move-only type 'MO.Type' cannot be used with generics yet}}
153-
let _ = MO.self as Any // expected-error {{move-only type 'MO.Type' cannot be used with generics yet}}
154-
let _ = MO.self is Any // expected-warning {{cast from 'MO.Type' to unrelated type 'Any' always fails}}
155-
156151
let _: Sendable = (MO(), MO()) // expected-error {{move-only type '(MO, MO)' cannot be used with generics yet}}
157152
let _: Sendable = MO() // expected-error {{move-only type 'MO' cannot be used with generics yet}}
158153
let _: _Copyable = mo // expected-error {{'_Copyable' is unavailable}}
@@ -271,14 +266,3 @@ struct GenerousGuy: Gives { // expected-error {{type 'GenerousGuy' does not conf
271266
typealias Ty = MO // expected-note {{possibly intended match 'GenerousGuy.Ty' (aka 'MO') does not conform to '_Copyable'}}
272267
func give() -> Ty {}
273268
}
274-
275-
func doBadMetatypeStuff<T>(_ t: T) {
276-
let y = t as! Any.Type
277-
if let MO_MetaType = y as? MO.Type { // expected-warning {{cast from 'any Any.Type' to unrelated type 'MO.Type' always fails}}
278-
let x = MO_MetaType.init()
279-
let _ = x
280-
}
281-
}
282-
func tryToDoBadMetatypeStuff() {
283-
doBadMetatypeStuff(MO.self) // expected-error {{move-only type 'MO.Type' cannot be used with generics yet}}
284-
}

0 commit comments

Comments
 (0)