Skip to content

Commit bf760aa

Browse files
authored
Merge pull request #85615 from jamieQ/async-let-it-be
[Sema]: suppress unexpected type warning on some async-let patterns
2 parents 5663051 + 80eccb1 commit bf760aa

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

lib/Sema/TypeCheckPattern.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1230,7 +1230,11 @@ Pattern *TypeChecker::coercePatternToType(
12301230
// If the whole pattern is implicit, the user didn't write it.
12311231
// Assume the compiler knows what it's doing.
12321232
} else if (diagTy->isEqual(Context.TheEmptyTupleType)) {
1233-
shouldRequireType = true;
1233+
// Async-let bindings are commonly used to run a Void-returning
1234+
// synchronous function in an async context. As a policy choice, don't
1235+
// diagnose an inferred Void type (or optional thereof) on such bindings
1236+
// as potentially unexpected.
1237+
shouldRequireType = !var->isAsyncLet();
12341238
} else if (auto MTT = diagTy->getAs<AnyMetatypeType>()) {
12351239
if (MTT->getInstanceType()->isAnyObject())
12361240
shouldRequireType = true;

test/decl/var/async_let.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,39 @@ func testInterpolation() async {
4242
async let y = "\(12345)"
4343
_ = await y
4444
}
45+
46+
// https://forums.swift.org/t/disable-constant-inferred-to-have-type-warning-when-using-async-let/83025
47+
48+
func testVoidResultTypeDiagnostics() async {
49+
async let void = print("hello")
50+
await void
51+
async let void2 = ()
52+
await void2
53+
async let void3 = Void()
54+
await void3
55+
async let void4 = { _ = 42 }()
56+
await void4
57+
58+
@Sendable func syncVoid() {}
59+
async let void5 = syncVoid()
60+
await void5
61+
62+
@Sendable func asyncVoid() async {}
63+
async let void6 = asyncVoid()
64+
await void6
65+
66+
async let maybeVoid = { Bool.random() ? () : nil }()
67+
await maybeVoid
68+
69+
do {
70+
final class C: Sendable { func doit() {} }
71+
let c: C? = nil
72+
async let maybeVoid2 = c?.doit()
73+
let _: ()? = await maybeVoid2
74+
}
75+
76+
// expected-warning @+2 {{constant 'boxOVoid' inferred to have type '[()]', which may be unexpected}}
77+
// expected-note @+1 {{add an explicit type annotation to silence this warning}}
78+
async let boxOVoid = { [(), (), ()] }()
79+
await boxOVoid // expected-warning {{expression of type '[()]' is unused}}
80+
}

0 commit comments

Comments
 (0)