[move-only] Avoid loc from func decl.

It's always the first line of the function, so try to do better.
This commit is contained in:
Nate Chandler
2023-06-16 18:36:56 -07:00
parent eaf4560cd7
commit 11443f26ed
2 changed files with 15 additions and 8 deletions

View File

@@ -14,6 +14,7 @@
#include "MoveOnlyDiagnostics.h"
#include "swift/AST/Decl.h"
#include "swift/AST/DiagnosticsSIL.h"
#include "swift/AST/Stmt.h"
#include "swift/Basic/Defer.h"
@@ -226,6 +227,12 @@ void DiagnosticEmitter::emitMissingConsumeInDiscardingContext(
return true;
case SILLocation::RegularKind: {
Decl *decl = loc.getAsASTNode<Decl>();
if (decl && isa<AbstractFunctionDecl>(decl)) {
// Having the function itself as a location results in a location at the
// first line of the function. Find another location.
return false;
}
Stmt *stmt = loc.getAsASTNode<Stmt>();
if (!stmt)
return true; // For non-statements, assume it is exiting the func.

View File

@@ -165,7 +165,7 @@ struct Basics: ~Copyable {
}
}
consuming func test8_stillMissingAConsume1(_ c: Color) throws { // expected-error {{must consume 'self' before exiting method that discards self}}
consuming func test8_stillMissingAConsume1(_ c: Color) throws {
if case .red = c {
discard self // expected-note {{discarded self here}}
return
@@ -174,7 +174,7 @@ struct Basics: ~Copyable {
_ = consume self
fatalError("hi")
}
}
} // expected-error {{must consume 'self' before exiting method that discards self}}
consuming func test8_stillMissingAConsume2(_ c: Color) throws {
if case .red = c {
@@ -251,7 +251,7 @@ struct Basics: ~Copyable {
}
}
consuming func test11(_ c: Color) { // expected-error {{must consume 'self' before exiting method that discards self}}
consuming func test11(_ c: Color) {
guard case .red = c else {
discard self // expected-note {{discarded self here}}
return
@@ -264,7 +264,7 @@ struct Basics: ~Copyable {
let x = self
self = x
mutator()
}
} // expected-error {{must consume 'self' before exiting method that discards self}}
consuming func test11_fixed(_ c: Color) {
guard case .red = c else {
@@ -328,13 +328,13 @@ struct Basics: ~Copyable {
_ = consume self
}
consuming func test13(_ c: Color) async { // expected-error {{must consume 'self' before exiting method that discards self}}
consuming func test13(_ c: Color) async {
guard case .red = c else {
discard self // expected-note {{discarded self here}}
return
}
await asyncer()
}
} // expected-error {{must consume 'self' before exiting method that discards self}}
consuming func test13_fixed(_ c: Color) async {
guard case .red = c else {
@@ -345,7 +345,7 @@ struct Basics: ~Copyable {
_ = consume self
}
consuming func test14(_ c: Color) async { // expected-error {{must consume 'self' before exiting method that discards self}}
consuming func test14(_ c: Color) async {
guard case .red = c else {
discard self // expected-note {{discarded self here}}
return
@@ -354,7 +354,7 @@ struct Basics: ~Copyable {
cont.resume()
}
print("back!")
}
} // expected-error {{must consume 'self' before exiting method that discards self}}
consuming func test14_fixed(_ c: Color) async {
guard case .red = c else {