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

Incorrectly allowed to destructure () #67631

Open
KeithBauerANZ opened this issue Jul 31, 2023 · 0 comments
Open

Incorrectly allowed to destructure () #67631

KeithBauerANZ opened this issue Jul 31, 2023 · 0 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation type checker Area → compiler: Semantic analysis

Comments

@KeithBauerANZ
Copy link

Description

Came across this one by accident:

func returnTuple() -> (Int, String) {
    (1, "hi")
}

func f() {
    // no error on this line, why not?:
    let (a, b) = {
        print("whatever")
        returnTuple() // oops, forgot the `return` keyword
    }()

    // error here instead:
    print(a)
}
swiftc DestructureVoid.swift
DestructureVoid.swift:13:11: error: cannot reference invalid declaration 'a'
    print(a)
          ^
DestructureVoid.swift:7:10: note: 'a' declared here
    let (a, b) = {
         ^

That error is very unhelpful.

Interestingly, something about the indirection is necessary; simpler examples do work as expected:

func f() {
    let (a, b) = ()
}
DestructureVoidSimple.swift:2:18: error: '()' is not convertible to '(_, _)', tuples have a different number of elements
    let (a, b) = ()
                 ^

and

func f() {
    let (a, b) = {}()
}
DestructureVoidSimple.swift:2:18: error: '()' is not convertible to '(_, _)', tuples have a different number of elements
    let (a, b) = {}()
                 ^

Environment
swift-driver version: 1.75.2 Apple Swift version 5.8.1 (swiftlang-5.8.0.124.5 clang-1403.0.22.11.100)
Target: arm64-apple-macosx13.0

@KeithBauerANZ KeithBauerANZ added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Jul 31, 2023
@hamishknight hamishknight added compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation type checker Area → compiler: Semantic analysis and removed triage needed This issue needs more specific labels labels Aug 9, 2023
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 diagnostics QoI Bug: Diagnostics Quality of Implementation type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

2 participants