Skip to content

[SR-15745] Actor methods able to be called from escaping closure without await #58022

Closed
@CharlesJS

Description

@CharlesJS
Previous ID SR-15745
Radar rdar://90354322
Original Reporter @CharlesJS
Type Bug
Environment
$ swiftc --version
swift-driver version: 1.26.21 Apple Swift version 5.5.2 (swiftlang-1300.0.47.5 clang-1300.0.29.30)
Target: x86_64-apple-macosx12.0
Additional Detail from JIRA
Votes 3
Component/s swift
Labels Bug
Assignee None
Priority Medium

md5: 581829c2432961ebf61e130142bf32e5

Issue Description:

This compiles, and I'm pretty sure it shouldn't:

import Dispatch

func someFunctionThatDoesWhoKnowsWhatWithThis(closure: @escaping () -> ()) {
  DispatchQueue.main.async { closure() }
}

class SomeClass {}

actor Foo {
  var bar = SomeClass()

  func setBar(_ bar: SomeClass) { self.bar = bar }
 
  func baz() {
    someFunctionThatDoesWhoKnowsWhatWithThis {
      self.setBar(.init()) // shouldn't this require an `await`?
    }
  }
}

The code inside the `@escaping` closure is no longer running inside the actor's context, so it seems like it shouldn't be able to call the actor's methods without an `await`, and that this is therefore a bug.

Forum thread with related discussion: https://forums.swift.org/t/calling-escaping-closure-from-actor-context-why-dont-i-need-await-here/51029

Metadata

Metadata

Assignees

No one assigned

    Labels

    async & awaitFeature → concurrency: asynchronous function aka the async/await patternbugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfconcurrencyFeature: umbrella label for concurrency language featuresdiagnostics QoIBug: Diagnostics Quality of Implementationnot a bugResolution → not a bug: Reported as a bug but turned out to be expected behavior or programmer error

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions