Add initial support into DynamicCasts for handling bridging between error types.

Teach it that casts between error types do not always fail and may succeed, in general.

Swift SVN r27098
This commit is contained in:
Roman Levenstein
2015-04-07 22:53:52 +00:00
parent 0c2fc7c1a4
commit f63cae6f13
2 changed files with 24 additions and 0 deletions

View File

@@ -173,6 +173,22 @@ bool swift::isObjectiveCBridgeable(Module *M, CanType Ty) {
return false;
}
/// Check if a given type conforms to _Error protocol.
bool swift::isErrorType(Module *M, CanType Ty) {
// Retrieve the _ErrorType protocol.
auto errorTypeProto =
M->Ctx.getProtocol(KnownProtocolKind::_ErrorType);
if (errorTypeProto) {
// Find the conformance of the value type to _BridgedToObjectiveC.
// Check whether the type conforms to _BridgedToObjectiveC.
auto conformance = M->lookupConformance(Ty, errorTypeProto, nullptr);
return (conformance.getInt() != ConformanceKind::DoesNotConform);
}
return false;
}
/// Try to classify the dynamic-cast relationship between two types.
DynamicCastFeasibility
swift::classifyDynamicCast(Module *M,
@@ -356,6 +372,12 @@ swift::classifyDynamicCast(Module *M,
return DynamicCastFeasibility::MaySucceed;
}
// Check if it is a cast between bridged error types.
if (isErrorType(M, source) && isErrorType(M, target)) {
// TODO: Cast to NSError succeeds always.
return DynamicCastFeasibility::MaySucceed;
}
return DynamicCastFeasibility::WillFail;
}