Skip to content

Commit 2832aed

Browse files
committed
Make the @_predatesConcurrency diagnostic a once-per-file-per-module remark
Rather than tacking the "add `@_predatesConcurrecy` to import" diagnostic on to the prior diagnostic as a note, make it its own remark. Then, ensure that we only emit this remark once per source file per imported module, so we're not overwhelming the user.
1 parent 23cc638 commit 2832aed

File tree

5 files changed

+32
-4
lines changed

5 files changed

+32
-4
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1952,7 +1952,7 @@ NOTE(non_sendable_nominal,none,
19521952
NOTE(add_nominal_sendable_conformance,none,
19531953
"consider making %0 %1 conform to the 'Sendable' protocol",
19541954
(DescriptiveDeclKind, DeclName))
1955-
NOTE(add_predates_concurrency_import,none,
1955+
REMARK(add_predates_concurrency_import,none,
19561956
"add '@_predatesConcurrency' to %select{suppress|treat}0 "
19571957
"'Sendable'-related %select{warnings|errors}0 from module %1"
19581958
"%select{| as warnings}0", (bool, Identifier))

include/swift/AST/SourceFile.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ class SourceFile final : public FileUnit {
8484
/// This is \c None until it is filled in by the import resolution phase.
8585
Optional<ArrayRef<AttributedImport<ImportedModule>>> Imports;
8686

87+
/// Which imports have made use of @_predatesConcurrency.
88+
llvm::SmallDenseSet<AttributedImport<ImportedModule>>
89+
PredatesConcurrencyImportsUsed;
90+
8791
/// A unique identifier representing this file; used to mark private decls
8892
/// within the file to keep them from conflicting with other files in the
8993
/// same module.
@@ -293,6 +297,14 @@ class SourceFile final : public FileUnit {
293297
/// resolution.
294298
void setImports(ArrayRef<AttributedImport<ImportedModule>> imports);
295299

300+
/// Whether the given import has used @_predatesConcurrency.
301+
bool hasImportUsedPredatesConcurrency(
302+
AttributedImport<ImportedModule> import) const;
303+
304+
/// Note that the given import has used @_predatesConcurrency/
305+
void setImportUsedPredatesConcurrency(
306+
AttributedImport<ImportedModule> import);
307+
296308
enum ImportQueryKind {
297309
/// Return the results for testable or private imports.
298310
TestableAndPrivate,

lib/AST/Module.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2240,6 +2240,16 @@ SourceFile::setImports(ArrayRef<AttributedImport<ImportedModule>> imports) {
22402240
Imports = getASTContext().AllocateCopy(imports);
22412241
}
22422242

2243+
bool SourceFile::hasImportUsedPredatesConcurrency(
2244+
AttributedImport<ImportedModule> import) const {
2245+
return PredatesConcurrencyImportsUsed.count(import) != 0;
2246+
}
2247+
2248+
void SourceFile::setImportUsedPredatesConcurrency(
2249+
AttributedImport<ImportedModule> import) {
2250+
PredatesConcurrencyImportsUsed.insert(import);
2251+
}
2252+
22432253
bool HasImplementationOnlyImportsRequest::evaluate(Evaluator &evaluator,
22442254
SourceFile *SF) const {
22452255
return llvm::any_of(SF->getImports(),

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -776,16 +776,22 @@ static bool diagnoseSingleNonSendableType(
776776
nominal->getName());
777777

778778
// If we can find an import in this context that makes this nominal
779-
// type visible, note that it can be `@_predatesConcurrency` import.
779+
// type visible, remark that it can be `@_predatesConcurrency` import.
780+
// Only emit this remark once per source file, because it can happen a
781+
// lot.
782+
SourceFile *sourceFile = fromContext.fromDC->getParentSourceFile();
780783
auto import = findImportFor(nominal, fromContext.fromDC);
781784
if (import && !import->options.contains(ImportFlags::PredatesConcurrency) &&
782-
import->importLoc.isValid()) {
785+
import->importLoc.isValid() && sourceFile &&
786+
!sourceFile->hasImportUsedPredatesConcurrency(*import)) {
783787
SourceLoc importLoc = import->importLoc;
784788
ctx.Diags.diagnose(
785789
importLoc, diag::add_predates_concurrency_import,
786790
ctx.LangOpts.isSwiftVersionAtLeast(6),
787791
nominal->getParentModule()->getName())
788792
.fixItInsert(importLoc, "@_predatesConcurrency ");
793+
794+
sourceFile->setImportUsedPredatesConcurrency(*import);
789795
}
790796
}
791797

test/Concurrency/actor_isolation.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// RUN: %target-typecheck-verify-swift -I %t -disable-availability-checking -warn-concurrency
44
// REQUIRES: concurrency
55

6-
import OtherActors // expected-note 2{{add '@_predatesConcurrency' to suppress 'Sendable'-related warnings from module 'OtherActors'}}{{1-1=@_predatesConcurrency }}
6+
import OtherActors // expected-remark{{add '@_predatesConcurrency' to suppress 'Sendable'-related warnings from module 'OtherActors'}}{{1-1=@_predatesConcurrency }}
77

88
let immutableGlobal: String = "hello"
99
var mutableGlobal: String = "can't touch this" // expected-note 5{{var declared here}}

0 commit comments

Comments
 (0)