Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/Sema/TypeCheckPattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1230,7 +1230,11 @@ Pattern *TypeChecker::coercePatternToType(
// If the whole pattern is implicit, the user didn't write it.
// Assume the compiler knows what it's doing.
} else if (diagTy->isEqual(Context.TheEmptyTupleType)) {
shouldRequireType = true;
// Async-let bindings are commonly used to run a Void-returning
// synchronous function in an async context. As a policy choice, don't
// diagnose an inferred Void type (or optional thereof) on such bindings
// as potentially unexpected.
shouldRequireType = var->isAsyncLet() ? false : true;
} else if (auto MTT = diagTy->getAs<AnyMetatypeType>()) {
if (MTT->getInstanceType()->isAnyObject())
shouldRequireType = true;
Expand Down
36 changes: 36 additions & 0 deletions test/decl/var/async_let.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,39 @@ func testInterpolation() async {
async let y = "\(12345)"
_ = await y
}

// https://forums.swift.org/t/disable-constant-inferred-to-have-type-warning-when-using-async-let/83025

func testVoidResultTypeDiagnostics() async {
async let void = print("hello")
await void
async let void2 = ()
await void2
async let void3 = Void()
await void3
async let void4 = { _ = 42 }()
await void4

@Sendable func syncVoid() {}
async let void5 = syncVoid()
await void5

@Sendable func asyncVoid() async {}
async let void6 = asyncVoid()
await void6

async let maybeVoid = { Bool.random() ? () : nil }()
await maybeVoid

do {
final class C: Sendable { func doit() {} }
let c: C? = nil
async let maybeVoid2 = c?.doit()
let _: ()? = await maybeVoid2
}

// expected-warning @+2 {{constant 'boxOVoid' inferred to have type '[()]', which may be unexpected}}
// expected-note @+1 {{add an explicit type annotation to silence this warning}}
async let boxOVoid = { [(), (), ()] }()
await boxOVoid // expected-warning {{expression of type '[()]' is unused}}
}