Merge pull request #70675 from xymus/access-level-import-same-file

This commit is contained in:
Alexis Laferrière
2024-01-03 12:12:03 -08:00
committed by GitHub
8 changed files with 216 additions and 0 deletions

View File

@@ -971,6 +971,58 @@ CheckInconsistentSPIOnlyImportsRequest::evaluate(
return {};
}
evaluator::SideEffect
CheckInconsistentAccessLevelOnImportSameFileRequest::evaluate(
Evaluator &evaluator, SourceFile *SF) const {
// Gather the most permissive import decl for each imported module.
llvm::DenseMap<ModuleDecl *, const ImportDecl *> mostPermissiveImports;
for (auto *topLevelDecl : SF->getTopLevelDecls()) {
auto *importDecl = dyn_cast<ImportDecl>(topLevelDecl);
if (!importDecl)
continue;
ModuleDecl *importedModule = importDecl->getModule();
if (!importedModule)
continue;
auto otherImportDecl = mostPermissiveImports.find(importedModule);
if (otherImportDecl == mostPermissiveImports.end() ||
otherImportDecl->second->getAccessLevel() <
importDecl->getAccessLevel()) {
mostPermissiveImports[importedModule] = importDecl;
}
}
// Report import decls that are not the most permissive.
auto &diags = SF->getASTContext().Diags;
for (auto *topLevelDecl : SF->getTopLevelDecls()) {
auto *importDecl = dyn_cast<ImportDecl>(topLevelDecl);
if (!importDecl)
continue;
ModuleDecl *importedModule = importDecl->getModule();
if (!importedModule)
continue;
auto otherImportDecl = mostPermissiveImports.find(importedModule);
if (otherImportDecl != mostPermissiveImports.end() &&
otherImportDecl->second != importDecl &&
otherImportDecl->second->getAccessLevel() >
importDecl->getAccessLevel()) {
diags.diagnose(importDecl, diag::inconsistent_import_access_levels,
importedModule,
otherImportDecl->second->getAccessLevel(),
importDecl->getAccessLevel());
diags.diagnose(otherImportDecl->second,
diag::inconsistent_implicit_access_level_on_import_here,
otherImportDecl->second->getAccessLevel());
}
}
return {};
}
evaluator::SideEffect
CheckInconsistentAccessLevelOnImport::evaluate(
Evaluator &evaluator, SourceFile *SF) const {