swiftlang / swift

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

Swift fails to correctly select between async and regular overload #69489

Open saagarjha opened 1 year ago

saagarjha commented 1 year ago

Description I have two functions; one that takes an async closure and one that doesn't. Then they invoke it, so the first is async itself and the second is not. I would expect that passing in an async closure or not would allow the compiler to select between them, but this does not seem the case.

Steps to reproduce

func foo(_ expression: @autoclosure () -> Int) {
    _ = expression()
}

func foo(_ expression: @autoclosure () async -> Int) async {
    _ = await expression()
}

foo(1)
await foo(await 1)

Expected behavior This should compile without issues

Environment

Additional context

$ swiftc test.swift
test.swift:11:11: error: 'await' in an autoclosure that does not support concurrency
await foo(await 1)
          ^
test.swift:11:1: warning: no 'async' operations occur within 'await' expression
await foo(await 1)
^
error: fatalError
JessyCatterwaul commented 6 months ago

As far as my problems with this bug go, it's as simple as, "synchronous overloads are not available in an async context."

func scope() async {
  func ƒ() async { }
  await ƒ()

  func ƒ() { }
  ƒ() // ❌ Expression is 'async' but is not marked with 'await'
}