Skip to content

Commit d90c7cb

Browse files
committed
DiagnosticEngine: Fix attribute insertion location for closures with capture lists
1 parent 10ca7f6 commit d90c7cb

File tree

2 files changed

+37
-16
lines changed

2 files changed

+37
-16
lines changed

lib/AST/DiagnosticEngine.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -393,15 +393,17 @@ InFlightDiagnostic::fixItAddAttribute(const DeclAttribute *Attr,
393393
const ClosureExpr *E) {
394394
ASSERT(!E->isImplicit());
395395

396-
SourceLoc insertionLoc;
397-
398-
if (auto *paramList = E->getParameters()) {
399-
// HACK: Don't set insertion loc to param list start loc if it's equal to
400-
// closure start loc (meaning it's implicit).
401-
// FIXME: Don't set the start loc of an implicit param list, or put an
402-
// isImplicit bit on ParameterList.
403-
if (paramList->getStartLoc() != E->getStartLoc()) {
404-
insertionLoc = paramList->getStartLoc();
396+
SourceLoc insertionLoc = E->getBracketRange().Start;
397+
398+
if (insertionLoc.isInvalid()) {
399+
if (auto *paramList = E->getParameters()) {
400+
// HACK: Don't set insertion loc to param list start loc if it's equal to
401+
// closure start loc (meaning it's implicit).
402+
// FIXME: Don't set the start loc of an implicit param list, or put an
403+
// isImplicit bit on ParameterList.
404+
if (paramList->getStartLoc() != E->getStartLoc()) {
405+
insertionLoc = paramList->getStartLoc();
406+
}
405407
}
406408
}
407409

test/Concurrency/attr_execution/adoption_mode.swift

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -220,45 +220,64 @@ do {
220220
do {
221221
nonisolated
222222
func nonisolatedF() {
223+
let x = 0
224+
223225
let _ = { () -> Void in }
224226

225227
let _ = { @concurrent () async -> Void in }
226228
let _ = { @MainActor () async -> Void in }
227229

228230
// expected-warning@+1:13 {{feature 'NonisolatedNonsendingByDefault' will cause nonisolated async closure to run on the caller's actor; use @concurrent to preserve behavior}}{{15-15=@concurrent }}{{none}}
229231
let _ = { () async -> Void in }
230-
231-
func takesInts(_: Int...) {}
232+
// expected-warning@+1:13 {{feature 'NonisolatedNonsendingByDefault' will cause nonisolated async closure to run on the caller's actor; use @concurrent to preserve behavior}}{{15-15=@concurrent }}{{none}}
233+
let _ = { [x] () async -> Void in _ = x }
232234

233235
// expected-warning@+1:13 {{feature 'NonisolatedNonsendingByDefault' will cause nonisolated async closure to run on the caller's actor; use @concurrent to preserve behavior}}{{14-14= @concurrent in }}{{none}}
234236
let _ = {await globalAsyncF()}
237+
// expected-warning@+1:13 {{feature 'NonisolatedNonsendingByDefault' will cause nonisolated async closure to run on the caller's actor; use @concurrent to preserve behavior}}{{14-14=@concurrent }}{{none}}
238+
let _ = {[x] in await globalAsyncF(); _ = x}
239+
235240
// expected-warning@+1:13 {{feature 'NonisolatedNonsendingByDefault' will cause nonisolated async closure to run on the caller's actor; use @concurrent to preserve behavior}}{{14-14= @concurrent in }}{{none}}
236241
let _ = {
237242
await globalAsyncF()
238243
}
239244
// expected-warning@+1:13 {{feature 'NonisolatedNonsendingByDefault' will cause nonisolated async closure to run on the caller's actor; use @concurrent to preserve behavior}}{{14-14= @concurrent in }}{{none}}
240245
let _ = {
241246
await globalAsyncF()
247+
func takesInts(_: Int...) {}
242248
takesInts($0, $1, $2)
243249
}
250+
244251
// expected-warning@+1:13 {{feature 'NonisolatedNonsendingByDefault' will cause nonisolated async closure to run on the caller's actor; use @concurrent to preserve behavior}}{{25-25=@concurrent }}{{none}}
245252
let _ = { @Sendable in
246253
await globalAsyncF()
247254
}
255+
// expected-warning@+1:13 {{feature 'NonisolatedNonsendingByDefault' will cause nonisolated async closure to run on the caller's actor; use @concurrent to preserve behavior}}{{25-25=@concurrent }}{{none}}
256+
let _ = { @Sendable [x] in
257+
_ = x
258+
await globalAsyncF()
259+
}
260+
248261
// expected-warning@+2:18 {{feature 'NonisolatedNonsendingByDefault' will cause nonisolated async function type to be treated as specified to run on the caller's actor; use @concurrent to preserve behavior}}{{18-18=@concurrent }}{{none}}
249262
// expected-warning@+1:45 {{feature 'NonisolatedNonsendingByDefault' will cause nonisolated async closure to run on the caller's actor; use @concurrent to preserve behavior}}{{47-47=@concurrent }}{{none}}
250263
var closure: (Int, Int) async -> Void = { a, b in
251264
await globalAsyncF()
252265
}
266+
// expected-warning@+1:15 {{feature 'NonisolatedNonsendingByDefault' will cause nonisolated async closure to run on the caller's actor; use @concurrent to preserve behavior}}{{17-17=@concurrent }}{{none}}
267+
closure = { [x] a, b in _ = x }
253268
// expected-warning@+1:15 {{feature 'NonisolatedNonsendingByDefault' will cause nonisolated async closure to run on the caller's actor; use @concurrent to preserve behavior}}{{+1:7-7=@concurrent }}{{none}}
254269
closure = {
255-
a, b async in
256-
await globalAsyncF()
270+
a, b async in ()
257271
}
258-
// expected-warning@+1:15 {{feature 'NonisolatedNonsendingByDefault' will cause nonisolated async closure to run on the caller's actor; use @concurrent to preserve behavior}}{{17-17=@concurrent }}{{none}}
259-
closure = { (a, b) in
260-
await globalAsyncF()
272+
// expected-warning@+1:15 {{feature 'NonisolatedNonsendingByDefault' will cause nonisolated async closure to run on the caller's actor; use @concurrent to preserve behavior}}{{+1:7-7=@concurrent }}{{none}}
273+
closure = {
274+
[x] a, b async in _ = x
261275
}
276+
// expected-warning@+1:15 {{feature 'NonisolatedNonsendingByDefault' will cause nonisolated async closure to run on the caller's actor; use @concurrent to preserve behavior}}{{17-17=@concurrent }}{{none}}
277+
closure = { (a, b) in () }
278+
279+
// expected-warning@+1:15 {{feature 'NonisolatedNonsendingByDefault' will cause nonisolated async closure to run on the caller's actor; use @concurrent to preserve behavior}}{{17-17=@concurrent }}{{none}}
280+
closure = { [x] (a, b) in _ = x }
262281

263282
let _ = closure
264283
}

0 commit comments

Comments
 (0)