Skip to content

Use any Error for caught error type of an exhaustive, non-throwing do..catch #71129

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

Merged

Conversation

DougGregor
Copy link
Member

Prior to the introduction of typed throws, a do..catch always had a caught error type of any Error, even if there were no throwing sites within the do body. With typed throws, such a do..catch would have a caught error type of Never, because it never throws. Unfortunately, this causes code like the following to produce an error, because Never cannot be pattern-matched to HomeworkError.forgot:

func test() {
  do {
    try nonthrowing()
  } catch HomeworkError.forgot {
  } catch {
  }
}

For source-compatibility reasons, adjust the caught error type to any Error in this case, so we continue to accept the code above. Don't do this adjustment under FullTypedThrows, because it's only there for compatibility.

Fixes rdar://121526201.

…do..catch

Prior to the introduction of typed throws, a `do..catch` always had a caught
error type of `any Error`, even if there were no throwing sites within
the `do` body. With typed throws, such a `do..catch` would have a
caught error type of `Never`, because it never throws. Unfortunately,
this causes code like the following to produce an error, because
`Never` cannot be pattern-matched to `HomeworkError.forgot`:

    func test() {
      do {
        try nonthrowing()
      } catch HomeworkError.forgot {
      } catch {
      }
    }

For source-compatibility reasons, adjust the caught error type to
`any Error` in this case, so we continue to accept the code above.
Don't do this adjustment under `FullTypedThrows`, because it's only
there for compatibility.

Fixes rdar://121526201.
@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@DougGregor DougGregor enabled auto-merge January 24, 2024 22:43
@DougGregor
Copy link
Member Author

No idea what this is from the Linux failure:

SwiftPM log: [error]: Invalid manifest (compiled with: ["/home/build-user/swift-nightly-install/usr/bin/swiftc", "-vfsoverlay", "/tmp/TemporaryDirectory.f38VPO/vfs.yaml", "-L", "/home/build-user/swift-nightly-install/usr/lib/swift/pm/ManifestAPI", "-lPackageDescription", "-Xlinker", "-rpath", "-Xlinker", "/home/build-user/swift-nightly-install/usr/lib/swift/pm/ManifestAPI", "-swift-version", "4.2", "-I", "/home/build-user/swift-nightly-install/usr/lib/swift/pm/ManifestAPI", "-package-description-version", "4.2.0", "/tmp/sourcekit-lsp-test-scratch/testUnparsablePackage-69D0139B/pkg/Package.swift", "-o", "/tmp/TemporaryDirectory.59KPXO/pkg-manifest"])
error: fatalError
/tmp/sourcekit-lsp-test-scratch/testUnparsablePackage-69D0139B/pkg/Package.swift:4:5: error: type annotation missing in pattern
let pack
    ^
---

@DougGregor
Copy link
Member Author

@swift-ci please smoke test Linux

1 similar comment
@DougGregor
Copy link
Member Author

@swift-ci please smoke test Linux

@DougGregor DougGregor merged commit 446ef9c into swiftlang:main Jan 25, 2024
@DougGregor DougGregor deleted the nonthrowing-do-catch-adjustment-fix branch January 25, 2024 07:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant