Skip to content

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

@JessyCatterwaul

Description

@JessyCatterwaul

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.crashBug: A crash, i.e., an abnormal termination of softwaretyped throwsFeature → error handling → throws & rethrows: Typed throws

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions