Skip to content

[Sema] Diagnose exportability of decls limited by a non-public import #64014

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -2064,6 +2064,9 @@ ERROR(access_level_on_import_unsupported, none,
ERROR(access_level_conflict_with_exported,none,
"'%0' is incompatible with %1; it can only be applied to public imports",
(DeclAttribute, DeclAttribute))
NOTE(module_imported_here,none,
"module %0 imported as '%select{private|fileprivate|internal|package|%ERROR|%ERROR}1' here",
(Identifier, AccessLevel))

// Opaque return types
ERROR(opaque_type_invalid_constraint,none,
Expand Down
7 changes: 7 additions & 0 deletions include/swift/AST/SourceFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ enum class RestrictedImportKind {
None // No restriction, i.e. the module is imported publicly.
};

/// Import that limits the access level of imported entities.
using ImportAccessLevel = Optional<AttributedImport<ImportedModule>>;

/// A file containing Swift source code.
///
/// This is a .swift or .sil file (or a virtual file, such as the contents of
Expand Down Expand Up @@ -382,6 +385,10 @@ class SourceFile final : public FileUnit {
/// Get the most permissive restriction applied to the imports of \p module.
RestrictedImportKind getRestrictedImportKind(const ModuleDecl *module) const;

/// Return the import of \p targetModule from this file with the most
/// permissive access level.
ImportAccessLevel getImportAccessLevel(const ModuleDecl *targetModule) const;

/// Find all SPI names imported from \p importedModule by this file,
/// collecting the identifiers in \p spiGroups.
virtual void
Expand Down
23 changes: 23 additions & 0 deletions lib/AST/Module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2944,6 +2944,29 @@ RestrictedImportKind SourceFile::getRestrictedImportKind(const ModuleDecl *modul
return importKind;
}

ImportAccessLevel
SourceFile::getImportAccessLevel(const ModuleDecl *targetModule) const {
assert(Imports.hasValue());

// Leave it to the caller to avoid calling this service for a self import.
// We want to return AccessLevel::Public, but there's no import site to return.
assert(targetModule != getParentModule() &&
"getImportAccessLevel doesn't support checking for a self-import");

auto &imports = getASTContext().getImportCache();
ImportAccessLevel restrictiveImport = None;

for (auto &import : *Imports) {
if ((!restrictiveImport.has_value() ||
import.accessLevel > restrictiveImport->accessLevel) &&
imports.isImportedBy(targetModule, import.module.importedModule)) {
restrictiveImport = import;
}
}

return restrictiveImport;
}

bool ModuleDecl::isImportedImplementationOnly(const ModuleDecl *module) const {
if (module == this) return false;

Expand Down
31 changes: 27 additions & 4 deletions lib/Sema/TypeAccessScopeChecker.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class TypeAccessScopeChecker {
bool TreatUsableFromInlineAsPublic;

Optional<AccessScope> Scope = AccessScope::getPublic();
ImportAccessLevel ImportRestriction = None;

TypeAccessScopeChecker(const DeclContext *useDC,
bool treatUsableFromInlineAsPublic)
Expand All @@ -43,21 +44,41 @@ class TypeAccessScopeChecker {

auto AS = VD->getFormalAccessScope(File, TreatUsableFromInlineAsPublic);
Scope = Scope->intersectWith(AS);

auto targetModule = VD->getDeclContext()->getParentModule();
if (targetModule != File->getParentModule()) {
auto localImportRestriction = File->getImportAccessLevel(targetModule);

if (localImportRestriction.has_value() &&
(!ImportRestriction.has_value() ||
localImportRestriction.value().accessLevel <
ImportRestriction.value().accessLevel)) {
ImportRestriction = localImportRestriction;
}
}

return Scope.has_value();
}

public:
static Optional<AccessScope>

struct Result {
Optional<AccessScope> Scope;
ImportAccessLevel Import;
};

static Result
getAccessScope(TypeRepr *TR, const DeclContext *useDC,
bool treatUsableFromInlineAsPublic = false) {
TypeAccessScopeChecker checker(useDC, treatUsableFromInlineAsPublic);
TR->walk(TypeReprIdentFinder([&](const IdentTypeRepr *typeRepr) {
return checker.visitDecl(typeRepr->getBoundDecl());
}));
return checker.Scope;
return {checker.Scope,
checker.ImportRestriction};
}

static Optional<AccessScope>
static Result
getAccessScope(Type T, const DeclContext *useDC,
bool treatUsableFromInlineAsPublic = false) {
TypeAccessScopeChecker checker(useDC, treatUsableFromInlineAsPublic);
Expand All @@ -66,7 +87,9 @@ class TypeAccessScopeChecker {
return TypeWalker::Action::Continue;
return TypeWalker::Action::Stop;
}));
return checker.Scope;

return {checker.Scope,
checker.ImportRestriction};
}
};

Expand Down
Loading