@@ -1289,9 +1289,8 @@ static id bridgeAnythingNonVerbatimToObjectiveC(OpaqueValue *src,
12891289 swift_unknownObjectRetain (result);
12901290 return result;
12911291 }
1292-
12931292 // Dig through existential types.
1294- if (auto srcExistentialTy = dyn_cast<ExistentialTypeMetadata>(srcType)) {
1293+ else if (auto srcExistentialTy = dyn_cast<ExistentialTypeMetadata>(srcType)) {
12951294 OpaqueValue *srcInnerValue;
12961295 const Metadata *srcInnerType;
12971296 bool isOutOfLine;
@@ -1320,10 +1319,9 @@ static id bridgeAnythingNonVerbatimToObjectiveC(OpaqueValue *src,
13201319 }
13211320 return result;
13221321 }
1323-
1324- // Handle metatypes.
1325- if (isa<ExistentialMetatypeMetadata>(srcType)
1326- || isa<MetatypeMetadata>(srcType)) {
1322+ // Handle metatypes and existential metatypes
1323+ else if (isa<ExistentialMetatypeMetadata>(srcType)
1324+ || isa<MetatypeMetadata>(srcType)) {
13271325 const Metadata *srcMetatypeValue;
13281326 memcpy (&srcMetatypeValue, src, sizeof (srcMetatypeValue));
13291327
@@ -1342,8 +1340,9 @@ static id bridgeAnythingNonVerbatimToObjectiveC(OpaqueValue *src,
13421340 return objc_retain (protocolObj);
13431341 }
13441342 }
1343+ }
13451344 // Handle bridgeable types.
1346- } else if (auto srcBridgeWitness = findBridgeWitness (srcType)) {
1345+ else if (auto srcBridgeWitness = findBridgeWitness (srcType)) {
13471346 // Bridge the source value to an object.
13481347 auto srcBridgedObject =
13491348 srcBridgeWitness->bridgeToObjectiveC (src, srcType, srcBridgeWitness);
@@ -1353,13 +1352,24 @@ static id bridgeAnythingNonVerbatimToObjectiveC(OpaqueValue *src,
13531352 srcType->vw_destroy (src);
13541353
13551354 return (id)srcBridgedObject;
1355+ }
13561356 // Handle Errors.
1357- } else if (auto srcErrorWitness = findErrorWitness (srcType)) {
1357+ else if (auto srcErrorWitness = findErrorWitness (srcType)) {
13581358 // Bridge the source value to an NSError.
13591359 auto flags = consume ? DynamicCastFlags::TakeOnSuccess
13601360 : DynamicCastFlags::Default;
13611361 return dynamicCastValueToNSError (src, srcType, srcErrorWitness, flags);
13621362 }
1363+ // Handle functions: "Block" types can be bridged literally
1364+ else if (auto fn = dyn_cast<FunctionTypeMetadata>(srcType)) {
1365+ if (fn->getConvention () == FunctionMetadataConvention::Block) {
1366+ id result;
1367+ memcpy (&result, src, sizeof (id));
1368+ if (!consume)
1369+ swift_unknownObjectRetain (result);
1370+ return result;
1371+ }
1372+ }
13631373
13641374 // Fall back to boxing.
13651375 return (id)bridgeAnythingToSwiftValueObject (src, srcType, consume);
0 commit comments