Navigation Menu

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

[SR-15601] Subscripting an actor’s dictionary property that contains tuples leads to compiler error at call site #57904

Open
marcomasser opened this issue Dec 15, 2021 · 0 comments
Labels
compiler The Swift compiler in itself improvement

Comments

@marcomasser
Copy link

Previous ID SR-15601
Radar None
Original Reporter @marcomasser
Type Improvement
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Improvement
Assignee None
Priority Medium

md5: 14b26d0d382ac84d4c70a28291f2627a

Issue Description:

I have the following code and the last statement in main triggers a compiler error:

actor A {
    var dict: [Int: (string: String, int: Int)] = [:]
}

@main struct Main {
    static func main() async {
        let a = A()
        print(await a.dict)
        print(await a.dict[42] as Any)
        print(await a.dict[42] ?? "42")
        print(await a.dict[42]?.string ?? "42") // error: Actor-isolated property 'dict' can not be mutated from the main actor
    }
}

I’m not quite sure what’s going on here, but I’m pretty sure there’s no mutation going on here.

I found two ways to prevent this compiler error (call site stays the same):

1. Make the dict property private(set):

actor A {
    private(set) var dict: [Int: (string: String, int: Int)] = [:]
}

2. Use a struct instead of a tuple like this:

struct S {
    let string: String
}

actor A {
    var dict: [Int: S] = [:]
}

I’m using Xcode 13.2 with Swift 5.5.2.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler The Swift compiler in itself improvement
Projects
None yet
Development

No branches or pull requests

1 participant