Skip to content

No Data Race Warning with Task capturing isolated method #82841

Open
@benallgeier

Description

@benallgeier

Description

If we have a non-isolated class with a method that creates a task whose closure is isolated to, say, the main actor, capturing a main actor isolated instance method in the capture list does not produce any data race warnings or errors.

Reproduction

class Bar {
  @MainActor func mainActorMethod() {}
}

class Foo {
  let bar = Bar()
  
  init() {
    self.start()
  }
  
  func start() {
    Task { @MainActor [callBar] in
      callBar()
    }
  }
  
  @MainActor func callBar() {
    self.bar.mainActorMethod()
  }
}

Foo()

Expected behavior

I expect a compiler error mentioning risks causing data races.

Environment

Swift 6 and 6.2

Additional information

Additional variations that don't produce any errors are posted in
https://forums.swift.org/t/why-no-data-race-warning-with-task-capturing-isolated-instance-method/80952

under Keith Bauer's comment.

It may be the case that this has been resolved according to some of the comments.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.triage neededThis issue needs more specific labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions