Skip to content

Commit 4e5e6cc

Browse files
committed
[AST/TypeChecker] NFC: Transform findImportFor into ValueDecl::findImport
1 parent db79596 commit 4e5e6cc

File tree

3 files changed

+39
-36
lines changed

3 files changed

+39
-36
lines changed

include/swift/AST/Decl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2734,6 +2734,10 @@ class ValueDecl : public Decl {
27342734
}
27352735

27362736
public:
2737+
/// Find the import that makes the given declaration available.
2738+
llvm::Optional<AttributedImport<ImportedModule>>
2739+
findImport(const DeclContext *fromDC);
2740+
27372741
/// Return true if this protocol member is a protocol requirement.
27382742
///
27392743
/// Asserts if this is not a member of a protocol.

lib/AST/Decl.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "swift/AST/Expr.h"
3131
#include "swift/AST/GenericEnvironment.h"
3232
#include "swift/AST/GenericSignature.h"
33+
#include "swift/AST/ImportCache.h"
3334
#include "swift/AST/Initializer.h"
3435
#include "swift/AST/InverseMarking.h"
3536
#include "swift/AST/LazyResolver.h"
@@ -3789,6 +3790,37 @@ ValueDecl::getSatisfiedProtocolRequirements(bool Sorted) const {
37893790
return NTD->getSatisfiedProtocolRequirementsForMember(this, Sorted);
37903791
}
37913792

3793+
llvm::Optional<AttributedImport<ImportedModule>>
3794+
ValueDecl::findImport(const DeclContext *fromDC) {
3795+
// If the type is from the current module, there's no import.
3796+
auto module = getModuleContext();
3797+
if (module == fromDC->getParentModule())
3798+
return llvm::None;
3799+
3800+
auto fromSourceFile = fromDC->getParentSourceFile();
3801+
if (!fromSourceFile)
3802+
return llvm::None;
3803+
3804+
// Look to see if the owning module was directly imported.
3805+
for (const auto &import : fromSourceFile->getImports()) {
3806+
if (import.module.importedModule == module)
3807+
return import;
3808+
}
3809+
3810+
// Now look for transitive imports.
3811+
auto &importCache = getASTContext().getImportCache();
3812+
for (const auto &import : fromSourceFile->getImports()) {
3813+
auto &importSet = importCache.getImportSet(import.module.importedModule);
3814+
for (const auto &transitive : importSet.getTransitiveImports()) {
3815+
if (transitive.importedModule == module) {
3816+
return import;
3817+
}
3818+
}
3819+
}
3820+
3821+
return llvm::None;
3822+
}
3823+
37923824
bool ValueDecl::isProtocolRequirement() const {
37933825
assert(isa<ProtocolDecl>(getDeclContext()));
37943826

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -810,38 +810,6 @@ static bool hasExplicitSendableConformance(NominalTypeDecl *nominal,
810810
conformance.getConcrete())->isMissing());
811811
}
812812

813-
/// Find the import that makes the given declaration available.
814-
static llvm::Optional<AttributedImport<ImportedModule>>
815-
findImportFor(const DeclContext *dc, const DeclContext *fromDC) {
816-
// If the type is from the current module, there's no import.
817-
auto module = dc->getParentModule();
818-
if (module == fromDC->getParentModule())
819-
return llvm::None;
820-
821-
auto fromSourceFile = fromDC->getParentSourceFile();
822-
if (!fromSourceFile)
823-
return llvm::None;
824-
825-
// Look to see if the owning module was directly imported.
826-
for (const auto &import : fromSourceFile->getImports()) {
827-
if (import.module.importedModule == module)
828-
return import;
829-
}
830-
831-
// Now look for transitive imports.
832-
auto &importCache = dc->getASTContext().getImportCache();
833-
for (const auto &import : fromSourceFile->getImports()) {
834-
auto &importSet = importCache.getImportSet(import.module.importedModule);
835-
for (const auto &transitive : importSet.getTransitiveImports()) {
836-
if (transitive.importedModule == module) {
837-
return import;
838-
}
839-
}
840-
}
841-
842-
return llvm::None;
843-
}
844-
845813
/// Determine the diagnostic behavior for a Sendable reference to the given
846814
/// nominal type.
847815
DiagnosticBehavior SendableCheckContext::diagnosticBehavior(
@@ -870,7 +838,7 @@ SendableCheckContext::preconcurrencyBehavior(Decl *decl) const {
870838
if (auto *nominal = dyn_cast<NominalTypeDecl>(decl)) {
871839
// Determine whether this nominal type is visible via a @preconcurrency
872840
// import.
873-
auto import = findImportFor(nominal, fromDC);
841+
auto import = nominal->findImport(fromDC);
874842
auto sourceFile = fromDC->getParentSourceFile();
875843

876844
if (!import || !import->options.contains(ImportFlags::Preconcurrency))
@@ -937,7 +905,7 @@ bool swift::diagnoseSendabilityErrorBasedOn(
937905
// This type was imported from another module; try to find the
938906
// corresponding import.
939907
llvm::Optional<AttributedImport<swift::ImportedModule>> import =
940-
findImportFor(nominal, fromContext.fromDC);
908+
nominal->findImport(fromContext.fromDC);
941909

942910
// If we found the import that makes this nominal type visible, remark
943911
// that it can be @preconcurrency import.
@@ -3012,8 +2980,7 @@ namespace {
30122980
return false;
30132981
}
30142982

3015-
const auto import =
3016-
findImportFor(var->getDeclContext(), getDeclContext());
2983+
const auto import = var->findImport(getDeclContext());
30172984
const bool isPreconcurrencyImport =
30182985
import && import->options.contains(ImportFlags::Preconcurrency);
30192986
const auto isPreconcurrencyUnspecifiedIsolation =

0 commit comments

Comments
 (0)