Closure with typed throws stored as a SwiftUI View property crashes on iOS 17

Not sure if this is a SwiftUI bug or a Swift bug so I'm posting this also into the Swift forum.

I've encountered an issue where storing a throws(PermissionError) closure as a property inside a SwiftUI View causes a runtime crash with EXC_BAD_ACCESS on iOS 17, while it works correctly on iOS 18.

Here’s an example of the affected code:

enum PermissionError: Error {
  case denied
}

struct PermissionCheckedView<AllowedContent: View, DeniedContent: View>: View {
  var protectedView: () throws(PermissionError) -> AllowedContent
  var deniedView: (PermissionError) -> DeniedContent

  init(
    @ViewBuilder protectedView: @escaping () throws(PermissionError) -> AllowedContent,
    @ViewBuilder deniedView: @escaping (PermissionError) -> DeniedContent
  ) {
    self.protectedView = protectedView
    self.deniedView = deniedView
  }

  public var body: some View {
    switch Result(catching: protectedView) {
    case .success(let content): content
    case .failure(let error): deniedView(error)
    }
  }
}

@main
struct TestApp: App {
  var body: some Scene {
    WindowGroup {
      PermissionCheckedView {
        
      } deniedView: { _ in
        
      }
    }
  }
}

Here's the stack trace (not sure how to get the txt version so posting image).
Since I noticed some func like swift_getTypeByMangledNameInContextImpl in the stack trace I thought that this could be a Swift bug

If I use var protectedView: () throws -> AllowedContent without typed throws it works.

1 Like