Skip to content

Commit 7d69a5c

Browse files
committed
Improved Cast Diagnostics when Literals are involved
Fixes Issue #53822
1 parent 8ac71b1 commit 7d69a5c

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,9 +1184,9 @@ WARNING(conditional_downcast_coercion,none,
11841184
(Type, Type))
11851185

11861186
WARNING(literal_conditional_downcast_to_coercion,none,
1187-
"conditional downcast from literal to %0 always fails; "
1187+
"conditional downcast from literal of inferred type %0 to unrelated type %1 always fails; "
11881188
"consider using 'as' coercion",
1189-
(Type))
1189+
(Type, Type))
11901190

11911191
WARNING(forced_downcast_noop,none,
11921192
"forced cast of %0 to same type has no effect", (Type))

lib/Sema/CSDiagnostics.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8621,6 +8621,7 @@ bool UnsupportedRuntimeCheckedCastFailure::diagnoseAsError() {
86218621
}
86228622

86238623
bool CheckedCastToUnrelatedFailure::diagnoseAsError() {
8624+
const auto fromType = getFromType();
86248625
const auto toType = getToType();
86258626
auto *sub = CastExpr->getSubExpr()->getSemanticsProvidingExpr();
86268627
// FIXME(https://github.com/apple/swift/issues/54529): This literal diagnostics needs to be revisited by a proposal to unify casting semantics for literals.
@@ -8632,7 +8633,8 @@ bool CheckedCastToUnrelatedFailure::diagnoseAsError() {
86328633
// be statically coerced to the cast type.
86338634
if (protocol && TypeChecker::conformsToProtocol(toType, protocol,
86348635
dc->getParentModule())) {
8635-
emitDiagnostic(diag::literal_conditional_downcast_to_coercion, toType);
8636+
emitDiagnostic(diag::literal_conditional_downcast_to_coercion, fromType,
8637+
toType);
86368638
return true;
86378639
}
86388640
}

0 commit comments

Comments
 (0)