@@ -1243,9 +1243,16 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,
12431243 void visitProtocolType (ProtocolType *PT,
12441244 Optional<OptionalTypeKind> optionalKind,
12451245 bool isMetatype = false ) {
1246+ auto proto = PT->getDecl ();
1247+ if (proto->isSpecificProtocol (KnownProtocolKind::Error)) {
1248+ if (isMetatype) os << " Class" ;
1249+ else os << " NSError *" ;
1250+ printNullability (optionalKind);
1251+ return ;
1252+ }
1253+
12461254 os << (isMetatype ? " Class" : " id" );
12471255
1248- auto proto = PT->getDecl ();
12491256 assert (proto->isObjC ());
12501257 if (auto knownKind = proto->getKnownProtocolKind ()) {
12511258 if (*knownKind == KnownProtocolKind::AnyObject) {
@@ -1266,15 +1273,27 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,
12661273 return visitProtocolType (singleProto, optionalKind, isMetatype);
12671274 PCT = cast<ProtocolCompositionType>(canonicalComposition);
12681275
1269- os << (isMetatype ? " Class " : " id " );
1270-
1276+ // Dig out the protocols. If we see 'Error', record that we saw it.
1277+ bool hasError = false ;
12711278 SmallVector<ProtocolDecl *, 4 > protos;
1272- std::transform (PCT->getProtocols ().begin (), PCT->getProtocols ().end (),
1273- std::back_inserter (protos),
1274- [] (Type ty) -> ProtocolDecl * {
1275- return ty->castTo <ProtocolType>()->getDecl ();
1276- });
1279+ for (auto protoTy : PCT->getProtocols ()) {
1280+ auto proto = protoTy->castTo <ProtocolType>()->getDecl ();
1281+ if (proto->isSpecificProtocol (KnownProtocolKind::Error)) {
1282+ hasError = true ;
1283+ continue ;
1284+ }
1285+
1286+ protos.push_back (proto);
1287+ }
1288+
1289+ os << (isMetatype ? " Class"
1290+ : hasError ? " NSError"
1291+ : " id" );
12771292 printProtocols (protos);
1293+
1294+ if (hasError && !isMetatype)
1295+ os << " *" ;
1296+
12781297 printNullability (optionalKind);
12791298 }
12801299
@@ -1630,7 +1649,8 @@ class ModuleWriter {
16301649
16311650 void forwardDeclare (const ProtocolDecl *PD) {
16321651 assert (PD->isObjC () ||
1633- *PD->getKnownProtocolKind () == KnownProtocolKind::AnyObject);
1652+ *PD->getKnownProtocolKind () == KnownProtocolKind::AnyObject ||
1653+ *PD->getKnownProtocolKind () == KnownProtocolKind::Error);
16341654 forwardDeclare (PD, [&]{
16351655 os << " @protocol " << getNameForObjC (PD) << " ;\n " ;
16361656 });
0 commit comments