Skip to content

Commit 5121349

Browse files
committed
NFC: Move implementation-only import checking to ImportResolution
1 parent 51be974 commit 5121349

File tree

2 files changed

+78
-78
lines changed

2 files changed

+78
-78
lines changed

lib/Sema/ImportResolution.cpp

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,84 @@ void UnboundImport::diagnoseInvalidAttr(DeclAttrKind attrKind,
674674
attr->setInvalid();
675675
}
676676

677+
void swift::checkInconsistentImplementationOnlyImports(ModuleDecl *MainModule) {
678+
bool hasAnyImplementationOnlyImports =
679+
llvm::any_of(MainModule->getFiles(), [](const FileUnit *F) -> bool {
680+
auto *SF = dyn_cast<SourceFile>(F);
681+
return SF && SF->hasImplementationOnlyImports();
682+
});
683+
if (!hasAnyImplementationOnlyImports)
684+
return;
685+
686+
auto diagnose = [MainModule](const ImportDecl *normalImport,
687+
const ImportDecl *implementationOnlyImport) {
688+
auto &diags = MainModule->getDiags();
689+
{
690+
InFlightDiagnostic warning =
691+
diags.diagnose(normalImport, diag::warn_implementation_only_conflict,
692+
normalImport->getModule()->getName());
693+
if (normalImport->getAttrs().isEmpty()) {
694+
// Only try to add a fix-it if there's no other annotations on the
695+
// import to avoid creating things like
696+
// `@_implementationOnly @_exported import Foo`. The developer can
697+
// resolve those manually.
698+
warning.fixItInsert(normalImport->getStartLoc(),
699+
"@_implementationOnly ");
700+
}
701+
}
702+
diags.diagnose(implementationOnlyImport,
703+
diag::implementation_only_conflict_here);
704+
};
705+
706+
llvm::DenseMap<ModuleDecl *, std::vector<const ImportDecl *>> normalImports;
707+
llvm::DenseMap<ModuleDecl *, const ImportDecl *> implementationOnlyImports;
708+
709+
for (const FileUnit *file : MainModule->getFiles()) {
710+
auto *SF = dyn_cast<SourceFile>(file);
711+
if (!SF)
712+
continue;
713+
714+
for (auto *topLevelDecl : SF->getTopLevelDecls()) {
715+
auto *nextImport = dyn_cast<ImportDecl>(topLevelDecl);
716+
if (!nextImport)
717+
continue;
718+
719+
ModuleDecl *module = nextImport->getModule();
720+
if (!module)
721+
continue;
722+
723+
if (nextImport->getAttrs().hasAttribute<ImplementationOnlyAttr>()) {
724+
// We saw an implementation-only import.
725+
bool isNew =
726+
implementationOnlyImports.insert({module, nextImport}).second;
727+
if (!isNew)
728+
continue;
729+
730+
auto seenNormalImportPosition = normalImports.find(module);
731+
if (seenNormalImportPosition != normalImports.end()) {
732+
for (auto *seenNormalImport : seenNormalImportPosition->getSecond())
733+
diagnose(seenNormalImport, nextImport);
734+
735+
// We're done with these; keep the map small if possible.
736+
normalImports.erase(seenNormalImportPosition);
737+
}
738+
continue;
739+
}
740+
741+
// We saw a non-implementation-only import. Is that in conflict with what
742+
// we've seen?
743+
if (auto *seenImplementationOnlyImport =
744+
implementationOnlyImports.lookup(module)) {
745+
diagnose(nextImport, seenImplementationOnlyImport);
746+
continue;
747+
}
748+
749+
// Otherwise, record it for later.
750+
normalImports[module].push_back(nextImport);
751+
}
752+
}
753+
}
754+
677755
//===----------------------------------------------------------------------===//
678756
// MARK: Scoped imports
679757
//===----------------------------------------------------------------------===//

lib/Sema/TypeChecker.cpp

Lines changed: 0 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -406,84 +406,6 @@ bool swift::isAdditiveArithmeticConformanceDerivationEnabled(SourceFile &SF) {
406406
return isDifferentiableProgrammingEnabled(SF);
407407
}
408408

409-
void swift::checkInconsistentImplementationOnlyImports(ModuleDecl *MainModule) {
410-
bool hasAnyImplementationOnlyImports =
411-
llvm::any_of(MainModule->getFiles(), [](const FileUnit *F) -> bool {
412-
auto *SF = dyn_cast<SourceFile>(F);
413-
return SF && SF->hasImplementationOnlyImports();
414-
});
415-
if (!hasAnyImplementationOnlyImports)
416-
return;
417-
418-
auto diagnose = [MainModule](const ImportDecl *normalImport,
419-
const ImportDecl *implementationOnlyImport) {
420-
auto &diags = MainModule->getDiags();
421-
{
422-
InFlightDiagnostic warning =
423-
diags.diagnose(normalImport, diag::warn_implementation_only_conflict,
424-
normalImport->getModule()->getName());
425-
if (normalImport->getAttrs().isEmpty()) {
426-
// Only try to add a fix-it if there's no other annotations on the
427-
// import to avoid creating things like
428-
// `@_implementationOnly @_exported import Foo`. The developer can
429-
// resolve those manually.
430-
warning.fixItInsert(normalImport->getStartLoc(),
431-
"@_implementationOnly ");
432-
}
433-
}
434-
diags.diagnose(implementationOnlyImport,
435-
diag::implementation_only_conflict_here);
436-
};
437-
438-
llvm::DenseMap<ModuleDecl *, std::vector<const ImportDecl *>> normalImports;
439-
llvm::DenseMap<ModuleDecl *, const ImportDecl *> implementationOnlyImports;
440-
441-
for (const FileUnit *file : MainModule->getFiles()) {
442-
auto *SF = dyn_cast<SourceFile>(file);
443-
if (!SF)
444-
continue;
445-
446-
for (auto *topLevelDecl : SF->getTopLevelDecls()) {
447-
auto *nextImport = dyn_cast<ImportDecl>(topLevelDecl);
448-
if (!nextImport)
449-
continue;
450-
451-
ModuleDecl *module = nextImport->getModule();
452-
if (!module)
453-
continue;
454-
455-
if (nextImport->getAttrs().hasAttribute<ImplementationOnlyAttr>()) {
456-
// We saw an implementation-only import.
457-
bool isNew =
458-
implementationOnlyImports.insert({module, nextImport}).second;
459-
if (!isNew)
460-
continue;
461-
462-
auto seenNormalImportPosition = normalImports.find(module);
463-
if (seenNormalImportPosition != normalImports.end()) {
464-
for (auto *seenNormalImport : seenNormalImportPosition->getSecond())
465-
diagnose(seenNormalImport, nextImport);
466-
467-
// We're done with these; keep the map small if possible.
468-
normalImports.erase(seenNormalImportPosition);
469-
}
470-
continue;
471-
}
472-
473-
// We saw a non-implementation-only import. Is that in conflict with what
474-
// we've seen?
475-
if (auto *seenImplementationOnlyImport =
476-
implementationOnlyImports.lookup(module)) {
477-
diagnose(nextImport, seenImplementationOnlyImport);
478-
continue;
479-
}
480-
481-
// Otherwise, record it for later.
482-
normalImports[module].push_back(nextImport);
483-
}
484-
}
485-
}
486-
487409
bool swift::performTypeLocChecking(ASTContext &Ctx, TypeLoc &T,
488410
DeclContext *DC,
489411
bool ProduceDiagnostics) {

0 commit comments

Comments
 (0)