Merge pull request #18552 from jckarter/array-of-class-bridging

Sema: Apply metatype-to-object conversions even without restriction hints.
This commit is contained in:
Joe Groff
2018-08-08 09:43:24 -07:00
committed by GitHub
3 changed files with 197 additions and 0 deletions

View File

@@ -6865,6 +6865,45 @@ Expr *ExprRewriter::coerceToType(Expr *expr, Type toType,
}
}
// Coercions from metadata to objects.
if (auto fromMeta = fromType->getAs<AnyMetatypeType>()) {
if (toType->isAnyObject()) {
assert(cs.getASTContext().LangOpts.EnableObjCInterop
&& "metatype-to-object conversion requires objc interop");
if (fromMeta->is<MetatypeType>()) {
assert(fromMeta->getInstanceType()->mayHaveSuperclass()
&& "metatype-to-object input should be a class metatype");
return cs.cacheType(
new (tc.Context) ClassMetatypeToObjectExpr(expr, toType));
}
if (fromMeta->is<ExistentialMetatypeType>()) {
assert(fromMeta->getInstanceType()->getCanonicalType()
->getExistentialLayout().requiresClass()
&& "metatype-to-object input should be a class metatype");
return cs.cacheType(
new (tc.Context) ExistentialMetatypeToObjectExpr(expr, toType));
}
llvm_unreachable("unhandled metatype kind");
}
if (auto toClass = toType->getClassOrBoundGenericClass()) {
if (toClass->getName() == cs.getASTContext().Id_Protocol
&& toClass->getModuleContext()->getName()
== cs.getASTContext().Id_ObjectiveC) {
assert(cs.getASTContext().LangOpts.EnableObjCInterop
&& "metatype-to-object conversion requires objc interop");
assert(fromMeta->is<MetatypeType>()
&& fromMeta->getInstanceType()->is<ProtocolType>()
&& "protocol-metatype-to-Protocol only works for single "
"protocols");
return cs.cacheType(
new (tc.Context) ProtocolMetatypeToObjectExpr(expr, toType));
}
}
}
// Coercions from a type to an existential type.
if (toType->isAnyExistentialType()) {
return coerceExistential(expr, toType, locator);