Skip to content

Typed throws that rely on a type's generic placeholder crash the compiler. #74289

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

Closed
JessyCatterwaul opened this issue Jun 11, 2024 · 2 comments · Fixed by #81162
Closed

Typed throws that rely on a type's generic placeholder crash the compiler. #74289

JessyCatterwaul opened this issue Jun 11, 2024 · 2 comments · Fixed by #81162
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. crash Bug: A crash, i.e., an abnormal termination of software typed throws Feature → error handling → throws & rethrows: Typed throws

Comments

@JessyCatterwaul
Copy link

JessyCatterwaul commented Jun 11, 2024

Description

I was trying to add strong error typing (the (UnwrapError) part) to this. It sadly didn't work.

extension Optional {
  /// Represents that an `Optional` was `nil`.
  public struct UnwrapError: Swift.Error & Equatable {
    public init() { }
  }

  @inlinable public var wrappedValue: Wrapped {
     get throws(UnwrapError) {
      switch self {
      case let wrapped?: return wrapped
      case nil: throw UnwrapError()
      }
    }
  }
}

Reproduction

This all compiles fine.

struct GenericError<T>: Error {
  init() { }
}

struct S<T> {
  var property: Void {
    get throws(GenericError<Void>) { }
  }

  func method() throws(GenericError<Void>) { }
}

But change the <Voids> to <T>s, and the compiler crashes.

struct S<T> {
  var property: Void {
    get throws(GenericError<T>) { }
  }

  func method() throws(GenericError<T>) { }
}

Stack dump

Command SwiftCompile failed with a nonzero exit code

Expected behavior

throws() supports all errors that can be thrown.

Environment

swift-driver version: 1.109.2 Apple Swift version 6.0 (swiftlang-6.0.0.3.300 clang-1600.0.20.10)
Target: arm64-apple-macosx14.0

Additional information

No response

@JessyCatterwaul JessyCatterwaul added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. crash Bug: A crash, i.e., an abnormal termination of software triage needed This issue needs more specific labels labels Jun 11, 2024
@hborla hborla added typed throws Feature → error handling → throws & rethrows: Typed throws and removed triage needed This issue needs more specific labels labels Jul 14, 2024
@JessyCatterwaul
Copy link
Author

JessyCatterwaul commented Mar 10, 2025

The compiler has moved on to the point now where the examples above compile. Good work! But having the signatures compile, alone, does not result in usability, as the errors are still not usable. I.e. adding in throw .init() is now what results in the compiler crash.

struct GenericError<T>: Error {
  init() { }
}

struct S<T> {
  var property: Void {
    get throws(GenericError<T>) { throw .init() }
  }

  func method() throws(GenericError<T>) { throw .init() }
}

@slavapestov
Copy link
Contributor

Oops, sorry.

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. crash Bug: A crash, i.e., an abnormal termination of software typed throws Feature → error handling → throws & rethrows: Typed throws
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants