swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.57k stars 10.36k forks source link

The region based isolation checker does not understand how to check. #75314

Open vookimedlo opened 3 months ago

vookimedlo commented 3 months ago

Description

I got the Pattern that the region based isolation checker does not understand how to check. Please file a bug; this is an error in the Swift 6 language mode.

I stripped down the code from our application to the bare minimum.

The issue is observable on line containing the following code: await image.drawText(text: property.text)

Reproduction

import SwiftUI

@globalActor actor ImageActor: GlobalActor {
    static let shared = ImageActor()
}

@main
class OurApp: App {
    required init() {

    }

    var body: some Scene {
          WindowGroup { [self] in
              EmptyView()
                  .environmentObject(self.textProperties)
          }
    }

    let textProperties = TextProperties()

    @ImageActor
    private func generateImage() async -> ImageGenerator? {
        guard let image = ImageGenerator(size: CGSizeMake(240, 120)) else { return nil }
        for property in await textProperties.properties {
            await image.drawText(text: property.text)
        }
        return image
    }

}

@MainActor
@Observable
final class TextProperty: ObservableObject, Sendable {
    var text: String = "" {
        willSet {
            guard text != newValue else { return }
            // something to do
        }
    }
}

@MainActor
@Observable
class TextProperties: ObservableObject {
    var properties: Array<TextProperty> = [TextProperty()]
}

@ImageActor
final class ImageGenerator {
    public init? (size: CGSize) {
    }

    public func drawText(text: String) {
        guard !text.isEmpty else { return }
    }

}

Expected behavior

The result of the isolation checker shall be provided (compilation without any warnings vs. isolation warnings/errors). Now the isolation check cannot be completed which is wrong.

Environment

Swift toolchain from the XCode 16 beta 3

swift-driver version: 1.111.2 Apple Swift version 6.0 (swiftlang-6.0.0.5.15 clang-1600.0.22.6)
Target: arm64-apple-macosx14.0

Additional information

No response

vookimedlo commented 3 months ago

Find out that the root cause of the issue is hidden in multiple macros:

@MainActor
@Observable
final class TextProperty: ObservableObject, Sendable {

If I remove @Observable the isolation checker provides feedback.

gottesmm commented 3 months ago

@vookimedlo I verified and this is already fixed.

gottesmm commented 3 months ago

(going to try to find a toolchain for you that you can use to validate this)

gottesmm commented 3 months ago

(also, thank you for filing the bug report! They are really helpful!)