Skip to content

Commit c3db946

Browse files
committed
sema: diagnose importation from disallowed modules
1 parent 67bbab7 commit c3db946

File tree

5 files changed

+54
-0
lines changed

5 files changed

+54
-0
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,10 @@ WARNING(module_not_compiled_with_library_evolution,none,
984984
"using it means binary compatibility for %1 can't be guaranteed",
985985
(Identifier, Identifier))
986986

987+
ERROR(module_allowable_client_violation,none,
988+
"module %0 doesn't allow importation from module %1",
989+
(Identifier, Identifier))
990+
987991
REMARK(cross_import_added,none,
988992
"import of %0 and %1 triggered a cross-import of %2",
989993
(Identifier, Identifier, Identifier))

include/swift/AST/Module.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,7 @@ class ModuleDecl
424424
ArrayRef<Identifier> getAllowableClientNames() const {
425425
return allowableClientNames;
426426
}
427+
bool allowImportedBy(ModuleDecl *importer) const;
427428
private:
428429

429430
/// An array of module names that are allowed to import this one.

lib/AST/Module.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1923,6 +1923,18 @@ Identifier ModuleDecl::getRealName() const {
19231923
return getASTContext().getRealModuleName(getName());
19241924
}
19251925

1926+
bool ModuleDecl::allowImportedBy(ModuleDecl *importer) const {
1927+
if (allowableClientNames.empty())
1928+
return true;
1929+
for (auto id: allowableClientNames) {
1930+
if (importer->getRealName() == id)
1931+
return true;
1932+
if (importer->getABIName() == id)
1933+
return true;
1934+
}
1935+
return false;
1936+
}
1937+
19261938
Identifier ModuleDecl::getABIName() const {
19271939
if (!ModuleABIName.empty())
19281940
return ModuleABIName;

lib/Sema/ImportResolution.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ struct UnboundImport {
137137
void validateTestable(ModuleDecl *topLevelModule);
138138
void validateResilience(NullablePtr<ModuleDecl> topLevelModule,
139139
SourceFile &SF);
140+
void validateAllowableClient(ModuleDecl *topLevelModule, SourceFile &SF);
140141

141142
/// Diagnoses an inability to import \p modulePath in this situation and, if
142143
/// \p attrs is provided and has an \p attrKind, invalidates the attribute and
@@ -621,6 +622,7 @@ void UnboundImport::validateOptions(NullablePtr<ModuleDecl> topLevelModule,
621622
// changing behavior, but it smells funny.
622623
validateTestable(top);
623624
validatePrivate(top);
625+
validateAllowableClient(top, SF);
624626
}
625627

626628
validateResilience(topLevelModule, SF);
@@ -712,6 +714,19 @@ void UnboundImport::validateTestable(ModuleDecl *topLevelModule) {
712714
diagnoseInvalidAttr(DAK_Testable, ctx.Diags, diag::module_not_testable);
713715
}
714716

717+
void UnboundImport::validateAllowableClient(ModuleDecl *importee,
718+
SourceFile &SF) {
719+
assert(importee);
720+
auto *importer = SF.getParentModule();
721+
if (!importee->allowImportedBy(importer)) {
722+
ASTContext &ctx = SF.getASTContext();
723+
ctx.Diags.diagnose(import.module.getModulePath().front().Loc,
724+
diag::module_allowable_client_violation,
725+
importee->getName(),
726+
importer->getName());
727+
}
728+
}
729+
715730
void UnboundImport::validateResilience(NullablePtr<ModuleDecl> topLevelModule,
716731
SourceFile &SF) {
717732
if (import.options.contains(ImportFlags::ImplementationOnly))
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %empty-directory(%t/binary)
3+
// RUN: %empty-directory(%t/textual)
4+
// RUN: %empty-directory(%t/module-cache)
5+
// RUN: %target-swift-frontend -emit-module -emit-module-path %t/binary/Foo.swiftmodule -parse-as-library %s -enable-library-evolution -allowable-client Bar -allowable-client FooBar -module-name Foo -DFOO -emit-module-interface-path %t/textual/Foo.swiftinterface
6+
7+
// RUN: %target-swift-frontend -typecheck %s -I %t/binary -module-name Bar
8+
// RUN: %target-swift-frontend -typecheck %s -I %t/binary -module-name FooBar
9+
// RUN: %target-typecheck-verify-swift -I %t/binary -module-name Blocked
10+
// RUN: %target-typecheck-verify-swift -I %t/textual -module-name Blocked -module-cache-path %t/module-cache
11+
12+
#if FOO
13+
14+
public func foo() {}
15+
16+
#else
17+
18+
import Foo // expected-error {{module 'Foo' doesn't allow importation from module 'Blocked'}}
19+
20+
func bar() { foo() }
21+
22+
#endif

0 commit comments

Comments
 (0)