@@ -6992,6 +6992,45 @@ Expr *ExprRewriter::coerceToType(Expr *expr, Type toType,
6992
6992
}
6993
6993
}
6994
6994
6995
+ // Coercions from metadata to objects.
6996
+ if (auto fromMeta = fromType->getAs <AnyMetatypeType>()) {
6997
+ if (toType->isAnyObject ()) {
6998
+ assert (cs.getASTContext ().LangOpts .EnableObjCInterop
6999
+ && " metatype-to-object conversion requires objc interop" );
7000
+ if (fromMeta->is <MetatypeType>()) {
7001
+ assert (fromMeta->getInstanceType ()->mayHaveSuperclass ()
7002
+ && " metatype-to-object input should be a class metatype" );
7003
+ return cs.cacheType (
7004
+ new (tc.Context ) ClassMetatypeToObjectExpr (expr, toType));
7005
+ }
7006
+
7007
+ if (fromMeta->is <ExistentialMetatypeType>()) {
7008
+ assert (fromMeta->getInstanceType ()->getCanonicalType ()
7009
+ ->getExistentialLayout ().requiresClass ()
7010
+ && " metatype-to-object input should be a class metatype" );
7011
+ return cs.cacheType (
7012
+ new (tc.Context ) ExistentialMetatypeToObjectExpr (expr, toType));
7013
+ }
7014
+
7015
+ llvm_unreachable (" unhandled metatype kind" );
7016
+ }
7017
+
7018
+ if (auto toClass = toType->getClassOrBoundGenericClass ()) {
7019
+ if (toClass->getName () == cs.getASTContext ().Id_Protocol
7020
+ && toClass->getModuleContext ()->getName ()
7021
+ == cs.getASTContext ().Id_ObjectiveC ) {
7022
+ assert (cs.getASTContext ().LangOpts .EnableObjCInterop
7023
+ && " metatype-to-object conversion requires objc interop" );
7024
+ assert (fromMeta->is <MetatypeType>()
7025
+ && fromMeta->getInstanceType ()->is <ProtocolType>()
7026
+ && " protocol-metatype-to-Protocol only works for single "
7027
+ " protocols" );
7028
+ return cs.cacheType (
7029
+ new (tc.Context ) ProtocolMetatypeToObjectExpr (expr, toType));
7030
+ }
7031
+ }
7032
+ }
7033
+
6995
7034
// Coercions from a type to an existential type.
6996
7035
if (toType->isAnyExistentialType ()) {
6997
7036
return coerceExistential (expr, toType, locator);
0 commit comments