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-14037] Conflicting Diagnostics for Covariant Self Appearing in Contravariant Position in Cast of Function Type #56428

Open
kavon opened this issue Jan 12, 2021 · 0 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself type checker Area → compiler: Semantic analysis

Comments

@kavon
Copy link
Contributor

kavon commented Jan 12, 2021

Previous ID SR-14037
Radar rdar://72768421
Original Reporter @kavon
Type Bug
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, TypeChecker
Assignee None
Priority Medium

md5: 503768c75262eb22cbe579ec9a721575

Issue Description:

[Refiled on behalf of some contributor due to JIRA database reset]

This code is always safe:

class B {}
class C: B {
    var callback: ((C) -> Void)!
    
    func setCallback(_ callback: @escaping (Self) -> Void) {
        self.callback = (callback as! (C) -> Void)
    }
}

class D: C {}

C().setCallback { (c: B) -> Void in
    print(c)
}

Because covariant `Self` appears in contravariant position, the as! cast here will always succeed. We note as much if the parentheses are removed:

self.callback = callback as! (C) -> Void // treating a forced downcast to '(C) -> Void' as optional will never produce 'nil'

But we do not allow an unconditional cast

self.callback = callback as (C) -> Void // error: '(Self) -> Void' is not convertible to '(C) -> Void'; did you mean to use 'as!' to force downcast?

We should just allow this unconditionally.

@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
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

1 participant