Skip to content

Commit 293e97a

Browse files
authored
Merge pull request #40022 from xedin/rdar-83072606
[Diagnostics] Coercion warning shouldn't assume that IUO is always optional
2 parents bd11c30 + d72871c commit 293e97a

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7677,8 +7677,11 @@ bool CoercibleOptionalCheckedCastFailure::diagnoseForcedCastExpr() const {
76777677

76787678
bool isBridged = CastKind == CheckedCastKind::BridgingCoercion;
76797679
if (isCastTypeIUO()) {
7680-
toType = toType->getOptionalObjectType();
7681-
extraFromOptionals++;
7680+
// IUO type could either be optional or unwrapped.
7681+
if (auto objType = toType->getOptionalObjectType()) {
7682+
extraFromOptionals++;
7683+
toType = objType;
7684+
}
76827685
}
76837686

76847687
std::string extraFromOptionalsStr(extraFromOptionals, '!');
@@ -7776,7 +7779,8 @@ bool NoopCheckedCast::diagnoseForcedCastExpr() const {
77767779
auto diagLoc = expr->getLoc();
77777780

77787781
if (isCastTypeIUO()) {
7779-
toType = toType->getOptionalObjectType();
7782+
if (auto objType = toType->getOptionalObjectType())
7783+
toType = objType;
77807784
}
77817785

77827786
if (fromType->isEqual(toType)) {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// RUN: %target-typecheck-verify-swift
2+
3+
struct A {
4+
var prop: Bool
5+
6+
func test() {
7+
_ = AssertUnwrap(prop) as! Bool! // expected-warning {{using '!' here is deprecated and will be removed in a future release}}
8+
// expected-warning@-1 {{forced cast from 'Bool?' to 'Bool' only unwraps optionals; did you mean to use '!'?}}
9+
}
10+
11+
@discardableResult
12+
func AssertUnwrap<T>(_ optional: T?) -> T? {
13+
return optional
14+
}
15+
}

0 commit comments

Comments
 (0)