[SR-14943] Fix-it for changing unsafeBitcast to unsafeDowncast should be more narrow or removed #57285
Labels
bug
A deviation from expected or documented behavior. Also: expected but undesirable behavior.
compiler
The Swift compiler in itself
diagnostics QoI
Diagnostics Quality of Implementation
good first issue
Good for newcomers
Additional Detail from JIRA
md5: 3d91228cea2823e2a2396d782c8b9edb
Issue Description:
According to the documentation, unsafeBitcast expects that the target type is layout-compatible with the type of the value passed in.
OTOH, unsafeDowncast expects that the type of the value is the target type (there is a
_debugPrecondition(x is T)
).Currently, we have a fix-it where we recommend replacing
unsafeBitcast
withunsafeDowncast
. However, strengthening a precondition like that in a fix-it is generally not OK unless we have some reason to believe thatx is T
will be true.Here is a code example that can be tested in a playground:
If one applies the fix-it, this code crashes due to the precondition failing. We should either remove the fix-it altogether or only apply it narrowly in the cases where we know for sure that
x is T
will be true.The text was updated successfully, but these errors were encountered: