Skip to content

Improved Cast Diagnostics when Literals are involved #64846

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -1184,9 +1184,9 @@ WARNING(conditional_downcast_coercion,none,
(Type, Type))

WARNING(literal_conditional_downcast_to_coercion,none,
"conditional downcast from literal to %0 always fails; "
"cast from literal of inferred type %0 to unrelated type %1 always fails; "
"consider using 'as' coercion",
(Type))
(Type, Type))

WARNING(forced_downcast_noop,none,
"forced cast of %0 to same type has no effect", (Type))
Expand Down
4 changes: 3 additions & 1 deletion lib/Sema/CSDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8621,6 +8621,7 @@ bool UnsupportedRuntimeCheckedCastFailure::diagnoseAsError() {
}

bool CheckedCastToUnrelatedFailure::diagnoseAsError() {
const auto fromType = getFromType();
const auto toType = getToType();
auto *sub = CastExpr->getSubExpr()->getSemanticsProvidingExpr();
// FIXME(https://github.com/apple/swift/issues/54529): This literal diagnostics needs to be revisited by a proposal to unify casting semantics for literals.
Expand All @@ -8632,7 +8633,8 @@ bool CheckedCastToUnrelatedFailure::diagnoseAsError() {
// be statically coerced to the cast type.
if (protocol && TypeChecker::conformsToProtocol(toType, protocol,
dc->getParentModule())) {
emitDiagnostic(diag::literal_conditional_downcast_to_coercion, toType);
emitDiagnostic(diag::literal_conditional_downcast_to_coercion, fromType,
toType);
return true;
}
}
Expand Down
17 changes: 13 additions & 4 deletions test/expr/cast/literals_downcast.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,32 @@

let ok = "A" as Character // OK
let succeed = "A" as? String // expected-warning {{always succeeds}}
let bad = "A" as? Character // expected-warning {{conditional downcast from literal to 'Character' always fails; consider using 'as' coercion}} {{none}}
let force = "A" as! String // expected-warning {{forced cast of 'String' to same type has no effect}}
let bad = "A" as? Character // expected-warning {{cast from literal of inferred type 'String' to unrelated type 'Character' always fails; consider using 'as' coercion}} {{none}}
let bad2 = "Aa" as? Character // expected-warning {{cast from 'String' to unrelated type 'Character' always fails}}
let badForce = "Aa" as! Character // expected-warning {{cast from 'String' to unrelated type 'Character' always fails}}
let bad1 = 1 as? Character // expected-warning {{cast from 'Int' to unrelated type 'Character' always fails}}
let bad1Force = 1 as! Character // expected-warning {{cast from 'Int' to unrelated type 'Character' always fails}}

let okInt = 1 as Int // OK
let IntForce = 1 as! Int // expected-warning {{forced cast of 'Int' to same type has no effect}}
let badInt = 1 as? Int // expected-warning {{always succeeds}}
let badInt1 = 1.0 as? Int // expected-warning {{cast from 'Double' to unrelated type 'Int' always fails}}
let badInt2 = 1 as? Double // expected-warning {{conditional downcast from literal to 'Double' always fails; consider using 'as' coercion}} {{none}}
let badInt2 = 1 as? Double // expected-warning {{cast from literal of inferred type 'Int' to unrelated type 'Double' always fails; consider using 'as' coercion}} {{none}}
let badInt3 = 1 as! Double // expected-warning {{cast from literal of inferred type 'Int' to unrelated type 'Double' always fails; consider using 'as' coercion}}
let badInt4 = 1.0 as! Int // expected-warning {{cast from 'Double' to unrelated type 'Int' always fails}}

let okUInt = 1 as UInt // OK
let badUInt = 1 as? UInt // expected-warning {{conditional downcast from literal to 'UInt' always fails; consider using 'as' coercion}} {{none}}
let badUInt = 1 as? UInt // expected-warning {{cast from literal of inferred type 'Int' to unrelated type 'UInt' always fails; consider using 'as' coercion}} {{none}}
let badUInt1 = 1.0 as? UInt // expected-warning {{cast from 'Double' to unrelated type 'UInt' always fails}}
let badUInt2 = 1.0 as! UInt // expected-warning {{cast from 'Double' to unrelated type 'UInt' always fails}}
let badUInt3 = 1 as! UInt // expected-warning {{cast from literal of inferred type 'Int' to unrelated type 'UInt' always fails; consider using 'as' coercion}}

// Custom protocol adoption
struct S: ExpressibleByStringLiteral {
typealias StringLiteralType = String
init(stringLiteral value: Self.StringLiteralType) {}
}

let a = "A" as? S // expected-warning {{conditional downcast from literal to 'S' always fails; consider using 'as' coercion}} {{none}}
let a = "A" as? S // expected-warning {{cast from literal of inferred type 'String' to unrelated type 'S' always fails; consider using 'as' coercion}} {{none}}
let a1 = "A" as! S // expected-warning {{cast from literal of inferred type 'String' to unrelated type 'S' always fails; consider using 'as' coercion}}