Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

“await” is needed to access a variable within an actor when using a Task with optional self #66748

Open
LeeKahSeng opened this issue Jun 19, 2023 · 3 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. concurrency Feature: umbrella label for concurrency language features

Comments

@LeeKahSeng
Copy link

Description

actor MyActor {
    
    private var name = ""
    
    func printName() {
        
        // Task 1
        Task {
            print(name)
        }
        
        // Task 2
        Task { [weak self] in
            // Error: Expression is 'async' but is not marked with 'await'
            print(self?.name ?? "")
        }
    }
}

Referring to the code above, Task 1 compiles successfully, whereas Task 2 encounters a compile error.

Expected behavior
The optional self in Task 2 should not foul the analysis

Environment

  • Xcode 14.3
  • Swift 5.8
@LeeKahSeng LeeKahSeng added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Jun 19, 2023
@grynspan
Copy link
Contributor

This is at least nominally correct. Imagine what would happen if you executed this code, but self became nil before the unstructured task began to execute:

Task { [weak self] in
  print(self?.name ?? "")
}

How can you guarantee isolation to self if self could be nil?

@LeeKahSeng
Copy link
Author

If self has become nil, is isolation still needed?

By the way, I have posted the question on the Swift Forum, feel free to take a look for more context.

@ktoso ktoso added concurrency Feature: umbrella label for concurrency language features and removed triage needed This issue needs more specific labels labels Oct 19, 2023
@wiruzx
Copy link
Contributor

wiruzx commented Nov 16, 2023

Duplicate of #62604?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. concurrency Feature: umbrella label for concurrency language features
Projects
None yet
Development

No branches or pull requests

4 participants