swift icon indicating copy to clipboard operation
swift copied to clipboard

Missing MainActor warning when MainActor is implied through inheritance

Open moreindirection opened this issue 7 months ago • 1 comments

Description

The included code is a reduction of an actual issue I had in a production app while migrating to Swift 6. I've made the example as small as possible.

If you compile the code as is (with Xcode 16 beta 2), it generates the following error on line 5:

Main actor-isolated property 'delegate' can not be mutated from a non-isolated context

This makes sense to me.

However, if I remove the explicit @MainActor annotation on line 5, the code then compiles successfully. But UIView (the parent class of ThumbnailView) is itself @MainActor, so ThumbnailView should automatically be @MainActor, and I should get this warning without having to annotate the class manually.

Reproduction

import UIKit

protocol ThumbnailViewDelegate: AnyObject {}

@MainActor
class ThumbnailView: UIView {
  var delegate: ThumbnailViewDelegate? = nil
}

class Grid: UIViewController {
  private var thumbnailView: ThumbnailView!
  
  init() {
    self.thumbnailView = ThumbnailView(frame: .zero)
    
    super.init(nibName: nil, bundle: nil)
  }
  
  required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }
  
  deinit {
    self.thumbnailView?.delegate = nil
  }
}

Expected behavior

I should get the same warning whether or not I add the redundant @MainActor annotation to ThumbnailView (because it inherits from a MainActor

Environment

swift-driver version: 1.110 Apple Swift version 6.0 (swiftlang-6.0.0.4.52 clang-1600.0.21.1.3)
Target: arm64-apple-macosx14.0

Additional information

Also reported as FB14237108

moreindirection avatar Jul 08 '24 16:07 moreindirection