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-13035] False positive warning: cast always fails from 'Result<String, ChildError1>' to 'Result<String, SomeError>' #55481

Closed
keith opened this issue Jun 17, 2020 · 1 comment

Comments

@keith
Copy link
Collaborator

keith commented Jun 17, 2020

Previous ID SR-13035
Radar None
Original Reporter @keith
Type Sub-task
Status Resolved
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s
Labels Sub-task
Assignee None
Priority Medium

md5: 094b37b824821e575c778391062eb070

Parent-Task:

  • SR-13088 False positive warning "cast always fails"

Issue Description:

With this code:

protocol ParentError: Error {}

struct ChildError1: ParentError {}
struct ChildError2: ParentError {}

func foo<SomeError: ParentError>(_ mockResult: Result<String, ChildError1>, _: Result<String, SomeError>) {
    if let castedResult = mockResult as? Result<String, SomeError> {
        print("The warning is wrong: \(castedResult)")
    } else {
        fatalError("If the warning is true it should crash here")
    }
}

foo(.success("mock"), Result<String, ChildError1>.success("ignored"))
// foo(.success("mock"), Result<String, ChildError2>.success("ignored")) // This case does crash

Compiled with Swift 5.2.4 from Xcode 11.5, you get this output:

foo.swift:7:38: warning: cast from 'Result<String, ChildError1>' to unrelated type 'Result<String, SomeError>' always fails
    if let castedResult = mockResult as? Result<String, SomeError> {
                          ~~~~~~~~~~ ^   ~~~~~~~~~~~~~~~~~~~~~~~~~
The warning is wrong: success("mock")

If you uncomment the last line you hit the fatalError branch, so it's can fail, but doesn't always.

Potentially related https://bugs.swift.org/browse/SR-13025 https://bugs.swift.org/browse/SR-12321

@LucianoPAlmeida
Copy link
Collaborator

@keith this is fixed on master, can you please verify on the next available snapshot and close? Thanks 🙂

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

No branches or pull requests

2 participants