[Diagnostics] Coercion warning shouldn't assume that IUO is always optional

In situations like `_ = ... as! Bool!` the `to` type could either
be represented by an optional (default) or unwrapped. Coercion warnings
cannot assume that `to` (even though its IUO) is always represented
as an optional type.

Resolves: rdar://83072606
This commit is contained in:
Pavel Yaskevich
2021-11-02 13:42:11 -07:00
parent 367b4c1355
commit d72871c0e3
2 changed files with 22 additions and 3 deletions

View File

@@ -7677,8 +7677,11 @@ bool CoercibleOptionalCheckedCastFailure::diagnoseForcedCastExpr() const {
bool isBridged = CastKind == CheckedCastKind::BridgingCoercion;
if (isCastTypeIUO()) {
toType = toType->getOptionalObjectType();
extraFromOptionals++;
// IUO type could either be optional or unwrapped.
if (auto objType = toType->getOptionalObjectType()) {
extraFromOptionals++;
toType = objType;
}
}
std::string extraFromOptionalsStr(extraFromOptionals, '!');
@@ -7776,7 +7779,8 @@ bool NoopCheckedCast::diagnoseForcedCastExpr() const {
auto diagLoc = expr->getLoc();
if (isCastTypeIUO()) {
toType = toType->getOptionalObjectType();
if (auto objType = toType->getOptionalObjectType())
toType = objType;
}
if (fromType->isEqual(toType)) {