Skip to content
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

[SR-2915] Add warning for tuple casts that will always fail #45509

Open
swift-ci opened this issue Oct 11, 2016 · 0 comments
Open

[SR-2915] Add warning for tuple casts that will always fail #45509

swift-ci opened this issue Oct 11, 2016 · 0 comments
Assignees
Labels
compiler The Swift compiler in itself improvement

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-2915
Radar None
Original Reporter hagi (JIRA User)
Type Improvement
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Improvement
Assignee @slavapestov
Priority Medium

md5: ec7c68ee3a0e3d19091e8a48bb0ee42d

Issue Description:

Reproduce:

This snippet reproduces the issue:

protocol MyProto {
   var title: String? { get }
}

let tuple = (x: 100, y: 100)

// Results in a warning as expected: Cast always fails
let castedTupleToInt = tuple as? Int

// No warning, although cast will always fail
let castedTuple = tuple as? MyProto

The way I see it, it should be pretty easy for the compiler to detect that casting a tuple to a protocol always fails, since there’s currently no way for a tuple to conform to a protocol.

Motivation:

We came across the bug when we refactored a method to return a tuple instead of a protocol type, and thought the compiler would make us aware of all the call sites that need to adopt the the refactored return type. However, wherever we casted the return type, no warning was raised and the code just silently returned early because the cast always fails at runtime (as it should, of course).

Previous Discussion:

There was a brief discussion on the swift-users mailing list: "Casting tuples to protocols always fails (and the compiler should know)"

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler The Swift compiler in itself improvement
Projects
None yet
Development

No branches or pull requests

1 participant