|
16 | 16 | #include "swift/AST/ModuleDependencies.h" |
17 | 17 | #include "swift/AST/Decl.h" |
18 | 18 | #include "swift/AST/DiagnosticsFrontend.h" |
| 19 | +#include "swift/AST/DiagnosticsSema.h" |
19 | 20 | #include "swift/AST/SourceFile.h" |
20 | 21 | #include "swift/Frontend/Frontend.h" |
21 | 22 | #include "llvm/CAS/CASProvidingFileSystem.h" |
@@ -145,6 +146,11 @@ void ModuleDependencyInfo::addModuleImport( |
145 | 146 | if (importedModuleName == BUILTIN_NAME) |
146 | 147 | continue; |
147 | 148 |
|
| 149 | + // Ignore/diagnose tautological imports akin to import resolution |
| 150 | + if (!swift::dependencies::checkImportNotTautological( |
| 151 | + realPath, importDecl->getLoc(), sf, importDecl->isExported())) |
| 152 | + continue; |
| 153 | + |
148 | 154 | addModuleImport(realPath, &alreadyAddedModules); |
149 | 155 |
|
150 | 156 | // Additionally, keep track of which dependencies of a Source |
@@ -408,6 +414,35 @@ SwiftDependencyScanningService::SwiftDependencyScanningService() { |
408 | 414 | SharedFilesystemCache.emplace(); |
409 | 415 | } |
410 | 416 |
|
| 417 | +bool |
| 418 | +swift::dependencies::checkImportNotTautological(const ImportPath::Module modulePath, |
| 419 | + const SourceLoc importLoc, |
| 420 | + const SourceFile &SF, |
| 421 | + bool isExported) { |
| 422 | + if (modulePath.front().Item != SF.getParentModule()->getName() || |
| 423 | + // Overlays use an @_exported self-import to load their clang module. |
| 424 | + isExported || |
| 425 | + // Imports of your own submodules are allowed in cross-language libraries. |
| 426 | + modulePath.size() != 1 || |
| 427 | + // SIL files self-import to get decls from the rest of the module. |
| 428 | + SF.Kind == SourceFileKind::SIL) |
| 429 | + return true; |
| 430 | + |
| 431 | + ASTContext &ctx = SF.getASTContext(); |
| 432 | + |
| 433 | + StringRef filename = llvm::sys::path::filename(SF.getFilename()); |
| 434 | + if (filename.empty()) |
| 435 | + ctx.Diags.diagnose(importLoc, diag::sema_import_current_module, |
| 436 | + modulePath.front().Item); |
| 437 | + else |
| 438 | + ctx.Diags.diagnose(importLoc, diag::sema_import_current_module_with_file, |
| 439 | + filename, modulePath.front().Item); |
| 440 | + |
| 441 | + return false; |
| 442 | + |
| 443 | + return false; |
| 444 | +} |
| 445 | + |
411 | 446 | void SwiftDependencyTracker::addCommonSearchPathDeps( |
412 | 447 | const SearchPathOptions &Opts) { |
413 | 448 | // Add SDKSetting file. |
|
0 commit comments