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-6965] diagnostics bad when an unnecessary try is used in a non-throwing function #49513

Open
weissi opened this issue Feb 9, 2018 · 2 comments
Assignees
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

@weissi
Copy link
Member

weissi commented Feb 9, 2018

Previous ID SR-6965
Radar rdar://problem/37398260
Original Reporter @weissi
Type Bug

Attachment: Download

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, DiagnosticsQoI, TypeChecker
Assignee @xedin
Priority Medium

md5: 763c1623aca7cdb7cc554e887ab791ac

Issue Description:

I've just done a bit of refactoring and this happened a lot: I frequently had code like function bar

func foo(_ body: () -> ()) {
}

func bar() {
    foo {
        try print("hello world")
    }
}

clearly, I can just remove the unnecessary try in bar and everything's good. However I didn't spot the try straight away and then I get a compilation error that I'm turning a throwing function into a non-throwing function.

test.swift:5:9: error: invalid conversion from throwing function of type '() throws -> ()' to non-throwing function type '() -> ()'
    foo {
        ^
test.swift:6:9: warning: no calls to throwing functions occur within 'try' expression
        try print("hello world")
        ^

It shows both. But if there's errors, Xcode and possibly other IDEs prefer to show you the errors first and you can't spot the warning at all (see attached screenshot).

Given that the compiler already figured out that the try is unnecessary, it shouldn't infer the closure's type to have a throws.

Even if I help the type checker by telling it the full type

func foo(_ body: () -> ()) {
}

func bar() {
    foo { () -> () in
        try print("hello world")
    }
}

it still shows the same error 🙁

test.swift:5:9: error: invalid conversion from throwing function of type '() throws -> ()' to non-throwing function type '() -> ()'
    foo { () -> () in
        ^~~~~~~~~~~~~
test.swift:6:9: warning: no calls to throwing functions occur within 'try' expression
        try print("hello world")
        ^
@belkadan
Copy link
Contributor

belkadan commented Feb 9, 2018

cc @xedin

@xedin
Copy link
Member

xedin commented Feb 9, 2018

@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

3 participants