Skip to content

Commit 1880118

Browse files
committed
Restructure getDisplayDecl() Sendable forcing
Move this into a helper function that lives in Sema, avoiding libswiftAST calling into libswiftSema.
1 parent 4e383e3 commit 1880118

File tree

11 files changed

+43
-33
lines changed

11 files changed

+43
-33
lines changed

include/swift/AST/Module.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,9 @@ class ModuleDecl
758758
/// The order of the results is not guaranteed to be meaningful.
759759
///
760760
/// This can differ from \c getTopLevelDecls, e.g. it returns decls from a
761-
/// shadowed clang module.
761+
/// shadowed clang module. It does not force synthesized top-level decls that
762+
/// should be printed to be added; use \c swift::getTopLevelDeclsForDisplay()
763+
/// for that.
762764
void getDisplayDecls(SmallVectorImpl<Decl*> &results) const;
763765

764766
using LinkLibraryCallback = llvm::function_ref<void(LinkLibrary)>;

include/swift/Sema/IDETypeChecking.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,13 @@ namespace swift {
140140
LookupResult
141141
lookupSemanticMember(DeclContext *DC, Type ty, DeclName name);
142142

143+
/// Get all of the top-level declarations that should be printed as part of
144+
/// this module. This may force synthesis of top-level declarations that
145+
/// \c ModuleDecl::getDisplayDecls() would only return if previous
146+
/// work happened to have synthesized them.
147+
void
148+
getTopLevelDeclsForDisplay(ModuleDecl *M, SmallVectorImpl<Decl*> &Results);
149+
143150
struct ExtensionInfo {
144151
// The extension with the declarations to apply.
145152
ExtensionDecl *Ext;

lib/APIDigester/ModuleAnalyzerNodes.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#include "llvm/ADT/STLExtras.h"
2+
#include "swift/AST/ASTMangler.h"
23
#include "swift/Basic/Defer.h"
4+
#include "swift/Sema/IDETypeChecking.h"
35
#include "swift/SIL/SILDeclRef.h"
4-
#include "swift/AST/ASTMangler.h"
56
#include <swift/APIDigester/ModuleAnalyzerNodes.h>
67
#include <algorithm>
78

@@ -1872,7 +1873,7 @@ void SwiftDeclCollector::printTopLevelNames() {
18721873
void SwiftDeclCollector::lookupVisibleDecls(ArrayRef<ModuleDecl *> Modules) {
18731874
for (auto M: Modules) {
18741875
llvm::SmallVector<Decl*, 512> Decls;
1875-
M->getDisplayDecls(Decls);
1876+
swift::getTopLevelDeclsForDisplay(M, Decls);
18761877
for (auto D : Decls) {
18771878
if (Ctx.shouldIgnore(D))
18781879
continue;

lib/AST/Module.cpp

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -909,28 +909,6 @@ SourceFile::getExternalRawLocsForDecl(const Decl *D) const {
909909
void ModuleDecl::getDisplayDecls(SmallVectorImpl<Decl*> &Results) const {
910910
// FIXME: Should this do extra access control filtering?
911911
FORWARD(getDisplayDecls, (Results));
912-
913-
// Force Sendable on all types, which might synthesize some extensions.
914-
// FIXME: We can remove this if @_nonSendable stops creating extensions.
915-
for (auto result : Results) {
916-
if (auto NTD = dyn_cast<NominalTypeDecl>(result))
917-
(void)swift::isSendableType(const_cast<ModuleDecl *>(this),
918-
NTD->getDeclaredInterfaceType());
919-
}
920-
921-
// Re-add anything from synthesized file units...
922-
auto oldCutoff = Results.size();
923-
for (auto file : getFiles())
924-
if (auto synthFile = dyn_cast<SynthesizedFileUnit>(file))
925-
synthFile->getDisplayDecls(Results);
926-
927-
// And then remove anything that was already in the results.
928-
auto oldResults = makeArrayRef(Results).take_front(oldCutoff);
929-
auto uniqueEnd = std::remove_if(Results.begin() + oldCutoff, Results.end(),
930-
[&](auto result) {
931-
return llvm::is_contained(oldResults, result);
932-
});
933-
Results.erase(uniqueEnd, Results.end());
934912
}
935913

936914
ProtocolConformanceRef

lib/FrontendTool/ImportedModules.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ bool swift::emitImportedModules(ModuleDecl *mainModule,
5959
llvm::SetVector<StringRef> Modules;
6060

6161
// Find the imports in the main Swift code.
62+
// We don't need `getTopLevelDeclsForDisplay()` here because we only care
63+
// about `ImportDecl`s.
6264
llvm::SmallVector<Decl *, 32> Decls;
6365
mainModule->getDisplayDecls(Decls);
6466
for (auto D : Decls) {

lib/IDE/IDETypeChecking.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,25 @@
3333

3434
using namespace swift;
3535

36+
void
37+
swift::getTopLevelDeclsForDisplay(ModuleDecl *M,
38+
SmallVectorImpl<Decl*> &Results) {
39+
auto startingSize = Results.size();
40+
M->getDisplayDecls(Results);
41+
42+
// Force Sendable on all types, which might synthesize some extensions.
43+
// FIXME: We can remove this if @_nonSendable stops creating extensions.
44+
for (auto result : Results) {
45+
if (auto NTD = dyn_cast<NominalTypeDecl>(result))
46+
(void)swift::isSendableType(M, NTD->getDeclaredInterfaceType());
47+
}
48+
49+
// Remove what we fetched and fetch again, possibly now with additional
50+
// extensions.
51+
Results.resize(startingSize);
52+
M->getDisplayDecls(Results);
53+
}
54+
3655
static bool shouldPrintAsFavorable(const Decl *D, const PrintOptions &Options) {
3756
if (!Options.TransformContext ||
3857
!isa<ExtensionDecl>(D->getDeclContext()) ||

lib/IDE/ModuleInterfacePrinting.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -399,12 +399,12 @@ static std::pair<ArrayRef<Decl*>, ArrayRef<Decl*>>
399399
getDeclsFromCrossImportOverlay(ModuleDecl *Overlay, ModuleDecl *Declaring,
400400
SmallVectorImpl<Decl *> &Decls,
401401
AccessLevel AccessFilter) {
402-
Overlay->getDisplayDecls(Decls);
402+
swift::getTopLevelDeclsForDisplay(Overlay, Decls);
403403

404404
// Collect the imports of the underlying module so we can filter them out.
405405
SmallPtrSet<ModuleDecl *, 8> PrevImported;
406406
SmallVector<Decl*, 1> DeclaringDecls;
407-
Declaring->getDisplayDecls(DeclaringDecls);
407+
swift::getTopLevelDeclsForDisplay(Declaring, DeclaringDecls);
408408
for (auto *D: DeclaringDecls) {
409409
if (auto *ID = dyn_cast<ImportDecl>(D))
410410
PrevImported.insert(ID->getModule());
@@ -548,7 +548,7 @@ void swift::ide::printModuleInterface(
548548
adjustPrintOptions(AdjustedOptions);
549549

550550
SmallVector<Decl *, 1> Decls;
551-
TopLevelMod->getDisplayDecls(Decls);
551+
swift::getTopLevelDeclsForDisplay(TopLevelMod, Decls);
552552

553553
SmallVector<ImportDecl *, 1> ImportDecls;
554554
llvm::DenseSet<const clang::Module *> ClangModulesForImports;

lib/SymbolGraphGen/SymbolGraphGen.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "llvm/Support/Path.h"
1515
#include "swift/AST/ASTContext.h"
1616
#include "swift/AST/FileSystem.h"
17+
#include "swift/Sema/IDETypeChecking.h"
1718
#include "swift/SymbolGraphGen/SymbolGraphGen.h"
1819

1920
#include "SymbolGraphASTWalker.h"
@@ -56,7 +57,7 @@ symbolgraphgen::emitSymbolGraphForModule(ModuleDecl *M,
5657
const SymbolGraphOptions &Options) {
5758
SymbolGraphASTWalker Walker(*M, Options);
5859
SmallVector<Decl *, 64> ModuleDecls;
59-
M->getDisplayDecls(ModuleDecls);
60+
swift::getTopLevelDeclsForDisplay(M, ModuleDecls);
6061

6162
if (Options.PrintMessages)
6263
llvm::errs() << ModuleDecls.size()

tools/swift-dependency-tool/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,5 @@ target_link_libraries(swift-dependency-tool
66
PRIVATE
77
swiftAST
88
swiftParse
9-
swiftClangImporter
10-
swiftSema)
9+
swiftClangImporter)
1110

tools/swift-ide-test/ModuleAPIDiff.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "swift/Driver/FrontendUtil.h"
1919
#include "swift/Frontend/Frontend.h"
2020
#include "swift/Frontend/PrintingDiagnosticConsumer.h"
21+
#include "swift/Sema/IDETypeChecking.h"
2122
#include "llvm/ADT/ArrayRef.h"
2223
#include "llvm/ADT/Optional.h"
2324
#include "llvm/Support/YAMLTraits.h"
@@ -877,7 +878,7 @@ class SMAModelGenerator : public DeclVisitor<SMAModelGenerator> {
877878

878879
std::shared_ptr<sma::Module> createSMAModel(ModuleDecl *M) {
879880
SmallVector<Decl *, 1> Decls;
880-
M->getDisplayDecls(Decls);
881+
swift::getTopLevelDeclsForDisplay(M, Decls);
881882

882883
SMAModelGenerator Generator;
883884
for (auto *D : Decls) {

tools/swift-ide-test/swift-ide-test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2793,7 +2793,7 @@ static int doPrintModuleGroups(const CompilerInvocation &InitInvok,
27932793
{
27942794
GroupNamesPrinter Printer(llvm::outs());
27952795
llvm::SmallVector<Decl*, 256> Results;
2796-
M->getDisplayDecls(Results);
2796+
swift::getTopLevelDeclsForDisplay(M, Results);
27972797
for (auto R : Results) {
27982798
Printer.addDecl(R);
27992799
}

0 commit comments

Comments
 (0)