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-13701] Confusing errors (involving inout + closure + rethrows) after applying swiftc fix-it #56099

Open
regexident opened this issue Oct 7, 2020 · 1 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

Comments

@regexident
Copy link
Contributor

Previous ID SR-13701
Radar rdar://problem/70058701
Original Reporter @regexident
Type Bug
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, DiagnosticsQoI, TypeChecker
Assignee None
Priority Medium

md5: cfb42507db3efad1ced5dac773dab076

Issue Description:

The following (incorrect) code

struct Foo {
    mutating func bar<T>(
        closure: inout (Self) throws -> T
    ) rethrows -> T {
        try closure(&self)
    }
}

… produces the following error:

Untitled.swift:13:21: error: '&' used with non-inout argument of type 'Foo'
        try closure(&self)
                    ^~~~~

The error message is correct as is, but could be made more helpful by suggesting to move inout into (Self) (i.e. (inout Self)).

The code is admittedly silly (or at least ambiguous), but swiftc suggested changing my initial

struct Foo {
    mutating func bar<T>(
        closure: inout Self throws -> T
    ) rethrows -> T {
        try closure(self)
    }
}

to above code, which is what brought me there in the first place.

Since I've never seen a closure be passed as inout I'd expect swiftc to suggest (inout Self) …, instead of inout (Self) ….

Especially since without the & of closure(&self) the error becomes even more confusing:

struct Foo {
    mutating func bar<T>(
        closure: inout (Self) throws -> T
    ) rethrows -> T {
        try closure(self)
    }
}
Untitled.swift:13:13: error: call can throw, but the error is not handled; a function declared 'rethrows' may only throw if its parameter does
        try closure(self)
            ^
@typesanitizer
Copy link

@swift-ci create

@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 diagnostics QoI Bug: Diagnostics Quality of Implementation type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

2 participants