Swift compiler error when using DeclaredAgeRange with swift 6

I added a dummy view - copy paste directly from the Apple Developer Documentation, in a swift package using swift 6 and got concurency error. The fix is to switch to swift 5

Answered by DTS Engineer in 889871022

It’s hard to investigate Swift concurrency problems without knowing more about your build environment, so I tried reproducing this here in my office:

  1. Using Xcode 26.5, I created a new project from the iOS > App template.
  2. I changed it to use the Swift 6 language mode.
  3. I changed ContentView.swift to this:
import SwiftUI
/* @preconcurrency */ import DeclaredAgeRange

struct ContentView: View {
    @Environment(\.requestAgeRange) var requestAgeRange

    var body: some View {
        VStack {
            Button("Test") {
                Task {
                    do {
                        let response = try await requestAgeRange(ageGates: 13, 15, 18)
//                                                              ^ 
// Sending value of non-Sendable type 'DeclaredAgeRangeAction' risks 
// causing data races
//
// Sending main actor-isolated value of non-Sendable type 
// 'DeclaredAgeRangeAction' to @concurrent instance method 
// 'callAsFunction(ageGates:_:_:)' risks causing races in between 
// main actor-isolated and @concurrent uses
                        print(response)
                    } catch {
                    }
                }
            }
        }
        .padding()
    }
}

#Preview {
    ContentView()
}

I think this accurately reflects the error you’re seeing, but lemme know otherwise.

As to the root cause of this error:

  • requestAgeRange, of type DeclaredAgeRangeAction, is bound to the main actor because it’s part of the view.
  • You’re calling it as a function, using the machinery introduced in SE-0253 Callable values of user-defined nominal types.
  • That effectively passes the self value to that function.
  • But the function is not declared to be main actor isolated, so now you’re passing a value between two isolation domains.

IMO this is a bug in the declaration of DeclaredAgeRangeAction. I’m not sure what the right fix is, but from your perspective the path forward is clear:

  1. Suppress the error temporarily by using @preconcurrency to import the Declared Age Range framework.
  2. File a bug against DeclaredAgeRangeAction requesting a proper fix for this.

Please post your bug number, just for the record.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Attaching the image, it got removed from the original post

It’s hard to investigate Swift concurrency problems without knowing more about your build environment, so I tried reproducing this here in my office:

  1. Using Xcode 26.5, I created a new project from the iOS > App template.
  2. I changed it to use the Swift 6 language mode.
  3. I changed ContentView.swift to this:
import SwiftUI
/* @preconcurrency */ import DeclaredAgeRange

struct ContentView: View {
    @Environment(\.requestAgeRange) var requestAgeRange

    var body: some View {
        VStack {
            Button("Test") {
                Task {
                    do {
                        let response = try await requestAgeRange(ageGates: 13, 15, 18)
//                                                              ^ 
// Sending value of non-Sendable type 'DeclaredAgeRangeAction' risks 
// causing data races
//
// Sending main actor-isolated value of non-Sendable type 
// 'DeclaredAgeRangeAction' to @concurrent instance method 
// 'callAsFunction(ageGates:_:_:)' risks causing races in between 
// main actor-isolated and @concurrent uses
                        print(response)
                    } catch {
                    }
                }
            }
        }
        .padding()
    }
}

#Preview {
    ContentView()
}

I think this accurately reflects the error you’re seeing, but lemme know otherwise.

As to the root cause of this error:

  • requestAgeRange, of type DeclaredAgeRangeAction, is bound to the main actor because it’s part of the view.
  • You’re calling it as a function, using the machinery introduced in SE-0253 Callable values of user-defined nominal types.
  • That effectively passes the self value to that function.
  • But the function is not declared to be main actor isolated, so now you’re passing a value between two isolation domains.

IMO this is a bug in the declaration of DeclaredAgeRangeAction. I’m not sure what the right fix is, but from your perspective the path forward is clear:

  1. Suppress the error temporarily by using @preconcurrency to import the Declared Age Range framework.
  2. File a bug against DeclaredAgeRangeAction requesting a proper fix for this.

Please post your bug number, just for the record.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Swift compiler error when using DeclaredAgeRange with swift 6
 
 
Q