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

"branches have mismatching types" in recursive computed property when using single-expr implicit return; works fine with explicit return in each branch #79565

Open
lukaskollmer opened this issue Feb 23, 2025 · 0 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels

Comments

@lukaskollmer
Copy link

Description

In the program below, the first computed property (using implicit returns) fails to compile, while the second one (using explicit returns) works fine.

since the two are semantically equivalent, the compiler should be able to compile both.

Reproduction

final class Entry {
    var prev: Entry?
    var next: Entry?
    
    var history: some Sequence<Entry> {
        // error: Branches have mismatching types 'some Sequence<Entry>' and 'UnfoldSequence<Entry, (Entry?, Bool)>'
        if let prev {
            prev.history2
        } else {
            sequence(first: self, next: \.next)
        }
    }
    
    var history2: some Sequence<Entry> {
        // fine
        if let prev {
            return prev.history2
        } else {
            return sequence(first: self, next: \.next)
        }
    }
}

Expected behavior

the compiler should treat both equally: since it apparently is already able to understand that the explicit-return version will have the same return type in each branch (bc one branch returns a concrete type, and the other is recursive and will therefore eventually also return that type), it should do the same for the implicit-return version

Environment

swift-driver version: 1.115.1 Apple Swift version 6.0.3 (swiftlang-6.0.3.1.10 clang-1600.0.30.1)
Target: arm64-apple-macosx15.0

Additional information

No response

@lukaskollmer lukaskollmer added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Feb 23, 2025
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. triage needed This issue needs more specific labels
Projects
None yet
Development

No branches or pull requests

1 participant