Skip to content

[SR-12093] Checked Casts With Literals Are Deeply Unintuitive #54529

Open
@CodaFi

Description

@CodaFi
Previous ID SR-12093
Radar rdar://problem/58999135
Original Reporter @CodaFi
Type Improvement
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Improvement, StarterProposal
Assignee inatonix (JIRA)
Priority Medium

md5: f23b80191801a31166f6b799915c7a23

cloned from:

  • SR-11421 Checked Cast Diagnostics Should Be More Specific When Literals Are Involved

Issue Description:

Checked casts on literals can have surprising effects at face value

let character = "A" as Character
let bad = "A" as? Character

The former is a character literal and is statically coerced. The latter is a String literal that is being dynamically cast to a Character. This cast will always fail, and we note as much - this also works with as! force casts.

warning: cast from 'String' to unrelated type 'Character' always fails

This behavior is deeply unintuitive and should be revisited by a proposal to unify casting semantics for literals.

Metadata

Metadata

Assignees

No one assigned

    Labels

    castingFeature: explicit casting (is, as, as? and as!)compilerThe Swift compiler itselfimprovementliteralsFeature → expressions: Literals such as an integer or string literalswift evolution proposal neededFlag → feature: A feature that warrants a Swift evolution proposal

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions