swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.32k stars 10.34k forks source link

Invalid async call in default initialized parameter #73892

Open mattmassicotte opened 3 months ago

mattmassicotte commented 3 months ago

Description

This code successfully compiles with the 2024-05-14 6.0 development snapshot in both Swift 5 and Swift 6 mode.

I'm fairly sure that Swift 5.10's error is correct and this code should be invalid.

Reproduction

@MainActor
func doThing(_ value: Int) {
}

class SomeThing {
    let task = Task {
        doThing(42)
    }
}

Expected behavior

With swift 5.10:

swift -enable-experimental-feature StrictConcurrency test.swift
test.swift:7:3: error: expression is 'async' but is not marked with 'await'
                doThing(42)
                ^~~~~~~~~~~
                await 
test.swift:7:3: note: calls to global function 'doThing' from outside of its actor context are implicitly asynchronous
                doThing(42)
                ^

Environment

swift-6.0-DEVELOPMENT-SNAPSHOT-2024-05-14-a

Apple Swift version 6.0-dev (LLVM 5b202efbc95a8bf, Swift a17d360d1cc66ab) Target: arm64-apple-macosx14.0

Additional information

No response

mattmassicotte commented 1 month ago

Things look different with swift-6.0-DEVELOPMENT-SNAPSHOT-2024-08-06-a

Apple Swift version 6.0-dev (LLVM 31f0cdc6d5ff8a6, Swift 497b266c2188eae)
Target: arm64-apple-macosx14.0
test.swift:6:6: error: main actor-isolated default value in a nonisolated context
 4 | 
 5 | class SomeThing {
 6 |    let task = Task {
   |      `- error: main actor-isolated default value in a nonisolated context
 7 |        doThing(42)
 8 |    }

If I now add the await, the error does not go away! Instead Instead I get the same error with warning that the await is not necessary...