Skip to content

Commit 11443f2

Browse files
committed
[move-only] Avoid loc from func decl.
It's always the first line of the function, so try to do better.
1 parent eaf4560 commit 11443f2

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

lib/SILOptimizer/Mandatory/MoveOnlyDiagnostics.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include "MoveOnlyDiagnostics.h"
1616

17+
#include "swift/AST/Decl.h"
1718
#include "swift/AST/DiagnosticsSIL.h"
1819
#include "swift/AST/Stmt.h"
1920
#include "swift/Basic/Defer.h"
@@ -226,6 +227,12 @@ void DiagnosticEmitter::emitMissingConsumeInDiscardingContext(
226227
return true;
227228

228229
case SILLocation::RegularKind: {
230+
Decl *decl = loc.getAsASTNode<Decl>();
231+
if (decl && isa<AbstractFunctionDecl>(decl)) {
232+
// Having the function itself as a location results in a location at the
233+
// first line of the function. Find another location.
234+
return false;
235+
}
229236
Stmt *stmt = loc.getAsASTNode<Stmt>();
230237
if (!stmt)
231238
return true; // For non-statements, assume it is exiting the func.

test/SILOptimizer/discard_checking.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ struct Basics: ~Copyable {
165165
}
166166
}
167167

168-
consuming func test8_stillMissingAConsume1(_ c: Color) throws { // expected-error {{must consume 'self' before exiting method that discards self}}
168+
consuming func test8_stillMissingAConsume1(_ c: Color) throws {
169169
if case .red = c {
170170
discard self // expected-note {{discarded self here}}
171171
return
@@ -174,7 +174,7 @@ struct Basics: ~Copyable {
174174
_ = consume self
175175
fatalError("hi")
176176
}
177-
}
177+
} // expected-error {{must consume 'self' before exiting method that discards self}}
178178

179179
consuming func test8_stillMissingAConsume2(_ c: Color) throws {
180180
if case .red = c {
@@ -251,7 +251,7 @@ struct Basics: ~Copyable {
251251
}
252252
}
253253

254-
consuming func test11(_ c: Color) { // expected-error {{must consume 'self' before exiting method that discards self}}
254+
consuming func test11(_ c: Color) {
255255
guard case .red = c else {
256256
discard self // expected-note {{discarded self here}}
257257
return
@@ -264,7 +264,7 @@ struct Basics: ~Copyable {
264264
let x = self
265265
self = x
266266
mutator()
267-
}
267+
} // expected-error {{must consume 'self' before exiting method that discards self}}
268268

269269
consuming func test11_fixed(_ c: Color) {
270270
guard case .red = c else {
@@ -328,13 +328,13 @@ struct Basics: ~Copyable {
328328
_ = consume self
329329
}
330330

331-
consuming func test13(_ c: Color) async { // expected-error {{must consume 'self' before exiting method that discards self}}
331+
consuming func test13(_ c: Color) async {
332332
guard case .red = c else {
333333
discard self // expected-note {{discarded self here}}
334334
return
335335
}
336336
await asyncer()
337-
}
337+
} // expected-error {{must consume 'self' before exiting method that discards self}}
338338

339339
consuming func test13_fixed(_ c: Color) async {
340340
guard case .red = c else {
@@ -345,7 +345,7 @@ struct Basics: ~Copyable {
345345
_ = consume self
346346
}
347347

348-
consuming func test14(_ c: Color) async { // expected-error {{must consume 'self' before exiting method that discards self}}
348+
consuming func test14(_ c: Color) async {
349349
guard case .red = c else {
350350
discard self // expected-note {{discarded self here}}
351351
return
@@ -354,7 +354,7 @@ struct Basics: ~Copyable {
354354
cont.resume()
355355
}
356356
print("back!")
357-
}
357+
} // expected-error {{must consume 'self' before exiting method that discards self}}
358358

359359
consuming func test14_fixed(_ c: Color) async {
360360
guard case .red = c else {

0 commit comments

Comments
 (0)