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-14134] Throw analysis goes awry on rethrow vs @autoclosure #56514

Open
lorentey opened this issue Jan 31, 2021 · 2 comments
Open

[SR-14134] Throw analysis goes awry on rethrow vs @autoclosure #56514

lorentey opened this issue Jan 31, 2021 · 2 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

@lorentey
Copy link
Member

Previous ID SR-14134
Radar rdar://problem/73799829
Original Reporter @lorentey
Type Bug
Environment

Tested on 5.4 branch

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

md5: b312c8c55ad5176e465579b9cdf806a1

Issue Description:

In the code below, passing a non-throwing expression to a rethrowing function makes the compiler emit a bogus diagnostic:

struct Fail: Error {}

func catcher(_ body: @autoclosure () throws -> Void) {}
func pitcher() throws {}

func audience(_ body: () throws -> Void) rethrows -> Void {}

func test1() {
  // This call doesn't throw.
  catcher(try pitcher())
}

func test2() {
  audience {
    // This is the same call, but this time it is evidently throwing.
    catcher(try pitcher())
  }
}

$ swift test.swift
test.swift:14:3: error: call can throw, but it is not marked with 'try' and the error is not handled
  audience {
  ^
test.swift:14:12: note: call is to 'rethrows' function, but argument function can throw
  audience {
           ^

To work around this diagnostic, I need to insert a dummy function:

func barrier<T>(_ body: () -> T) { body() }

func test3() {
  audience {
    barrier {
      catcher(try pitcher()) // OK
    }
  }
}
@lorentey
Copy link
Member Author

@swift-cicreate

@lorentey
Copy link
Member Author

Actually, the workaround has the same problem, so this reproduces even without a rethrow. I don't have a good workaround, other than extracting the catcher-pitcher expression into a standalone function.

func audience2(_ body: () -> Void) {}

func test3() {
  audience2 { // error: invalid conversion from throwing function of type '() throws -> Void' to non-throwing function type '() -> Void'
    catcher(try pitcher())
  }
}

@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

1 participant