Skip to content

Commit 02255d2

Browse files
committed
Sema: Move diagnoseMissingImportForMember() to Sema.
This will make it possible to use type checking APIs to determine the appropriate access level for suggested imports. NFC.
1 parent fff4beb commit 02255d2

File tree

4 files changed

+63
-64
lines changed

4 files changed

+63
-64
lines changed

include/swift/AST/Module.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,11 +1319,6 @@ inline SourceLoc extractNearestSourceLoc(const ModuleDecl *mod) {
13191319
return extractNearestSourceLoc(static_cast<const Decl *>(mod));
13201320
}
13211321

1322-
/// If the import that would make the given declaration visibile is absent,
1323-
/// emit a diagnostic and a fix-it suggesting adding the missing import.
1324-
bool diagnoseMissingImportForMember(const ValueDecl *decl,
1325-
const DeclContext *dc, SourceLoc loc);
1326-
13271322
} // end namespace swift
13281323

13291324
#endif

lib/AST/Module.cpp

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -4093,62 +4093,3 @@ version::Version ModuleDecl::getLanguageVersionBuiltWith() const {
40934093

40944094
return version::Version();
40954095
}
4096-
4097-
bool swift::diagnoseMissingImportForMember(const ValueDecl *decl,
4098-
const DeclContext *dc,
4099-
SourceLoc loc) {
4100-
if (decl->findImport(dc))
4101-
return false;
4102-
4103-
auto &ctx = dc->getASTContext();
4104-
auto definingModule = decl->getModuleContext();
4105-
ctx.Diags.diagnose(loc, diag::candidate_from_missing_import,
4106-
decl->getDescriptiveKind(), decl->getName(),
4107-
definingModule);
4108-
4109-
SourceLoc bestLoc =
4110-
ctx.Diags.getBestAddImportFixItLoc(decl, dc->getParentSourceFile());
4111-
if (!bestLoc.isValid())
4112-
return false;
4113-
4114-
llvm::SmallString<64> importText;
4115-
4116-
// Check other source files for import flags that should be applied to the
4117-
// fix-it for consistency with the rest of the imports in the module.
4118-
auto parentModule = dc->getParentModule();
4119-
OptionSet<ImportFlags> flags;
4120-
for (auto file : parentModule->getFiles()) {
4121-
if (auto sf = dyn_cast<SourceFile>(file))
4122-
flags |= sf->getImportFlags(definingModule);
4123-
}
4124-
4125-
if (flags.contains(ImportFlags::Exported) ||
4126-
parentModule->isClangOverlayOf(definingModule))
4127-
importText += "@_exported ";
4128-
if (flags.contains(ImportFlags::ImplementationOnly))
4129-
importText += "@_implementationOnly ";
4130-
if (flags.contains(ImportFlags::WeakLinked))
4131-
importText += "@_weakLinked ";
4132-
if (flags.contains(ImportFlags::SPIOnly))
4133-
importText += "@_spiOnly ";
4134-
4135-
// FIXME: Access level should be considered, too.
4136-
4137-
// @_spi imports.
4138-
if (decl->isSPI()) {
4139-
auto spiGroups = decl->getSPIGroups();
4140-
if (!spiGroups.empty()) {
4141-
importText += "@_spi(";
4142-
importText += spiGroups[0].str();
4143-
importText += ") ";
4144-
}
4145-
}
4146-
4147-
importText += "import ";
4148-
importText += definingModule->getName().str();
4149-
importText += "\n";
4150-
ctx.Diags.diagnose(bestLoc, diag::candidate_add_import, definingModule)
4151-
.fixItInsert(bestLoc, importText);
4152-
4153-
return true;
4154-
}

lib/Sema/TypeCheckNameLookup.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -754,3 +754,62 @@ TypoCorrectionResults::claimUniqueCorrection() {
754754

755755
return SyntacticTypoCorrection(WrittenName, Loc, uniqueCorrectedName);
756756
}
757+
758+
bool swift::diagnoseMissingImportForMember(const ValueDecl *decl,
759+
const DeclContext *dc,
760+
SourceLoc loc) {
761+
if (decl->findImport(dc))
762+
return false;
763+
764+
auto &ctx = dc->getASTContext();
765+
auto definingModule = decl->getModuleContext();
766+
ctx.Diags.diagnose(loc, diag::candidate_from_missing_import,
767+
decl->getDescriptiveKind(), decl->getName(),
768+
definingModule);
769+
770+
SourceLoc bestLoc =
771+
ctx.Diags.getBestAddImportFixItLoc(decl, dc->getParentSourceFile());
772+
if (!bestLoc.isValid())
773+
return false;
774+
775+
llvm::SmallString<64> importText;
776+
777+
// Check other source files for import flags that should be applied to the
778+
// fix-it for consistency with the rest of the imports in the module.
779+
auto parentModule = dc->getParentModule();
780+
OptionSet<ImportFlags> flags;
781+
for (auto file : parentModule->getFiles()) {
782+
if (auto sf = dyn_cast<SourceFile>(file))
783+
flags |= sf->getImportFlags(definingModule);
784+
}
785+
786+
if (flags.contains(ImportFlags::Exported) ||
787+
parentModule->isClangOverlayOf(definingModule))
788+
importText += "@_exported ";
789+
if (flags.contains(ImportFlags::ImplementationOnly))
790+
importText += "@_implementationOnly ";
791+
if (flags.contains(ImportFlags::WeakLinked))
792+
importText += "@_weakLinked ";
793+
if (flags.contains(ImportFlags::SPIOnly))
794+
importText += "@_spiOnly ";
795+
796+
// FIXME: Access level should be considered, too.
797+
798+
// @_spi imports.
799+
if (decl->isSPI()) {
800+
auto spiGroups = decl->getSPIGroups();
801+
if (!spiGroups.empty()) {
802+
importText += "@_spi(";
803+
importText += spiGroups[0].str();
804+
importText += ") ";
805+
}
806+
}
807+
808+
importText += "import ";
809+
importText += definingModule->getName().str();
810+
importText += "\n";
811+
ctx.Diags.diagnose(bestLoc, diag::candidate_add_import, definingModule)
812+
.fixItInsert(bestLoc, importText);
813+
814+
return true;
815+
}

lib/Sema/TypeChecker.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1535,6 +1535,10 @@ void recordRequiredImportAccessLevelForDecl(
15351535
/// Report imports that are marked public but are not used in API.
15361536
void diagnoseUnnecessaryPublicImports(SourceFile &SF);
15371537

1538+
/// If the import that would make the given declaration visibile is absent,
1539+
/// emit a diagnostic and a fix-it suggesting adding the missing import.
1540+
bool diagnoseMissingImportForMember(const ValueDecl *decl,
1541+
const DeclContext *dc, SourceLoc loc);
15381542
} // end namespace swift
15391543

15401544
#endif

0 commit comments

Comments
 (0)