mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
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:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user