Merge pull request #85615 from jamieQ/async-let-it-be

[Sema]: suppress unexpected type warning on some async-let patterns
This commit is contained in:
Hamish Knight
2025-12-01 14:39:08 +00:00
committed by GitHub
2 changed files with 41 additions and 1 deletions

View File

@@ -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();
} else if (auto MTT = diagTy->getAs<AnyMetatypeType>()) {
if (MTT->getInstanceType()->isAnyObject())
shouldRequireType = true;

View File

@@ -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}}
}