Skip to content

Commit 955e130

Browse files
committed
Diagnose self-imports of the module being compiled.
The case where this comes up is when people name their app and framework targets the same thing, or when they've renamed their test target module in an attempt to avoid issues with NSClassFromString and differing runtime names. We currently do various wrong things when this happens, so just emit an error instead. I left a hole for our overlays, which use '@exported import <the-current-module>' to get at their Clang modules. The previous commit means this can be replaced by -import-underlying-module, but that doesn't help our tests, which use -enable-source-import for their overlays. Which we should stop doing. rdar://problem/21254367 Swift SVN r29440
1 parent ff7c3f0 commit 955e130

File tree

8 files changed

+31
-9
lines changed

8 files changed

+31
-9
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,8 @@ NOTE(sema_no_import_no_sdk,sema_nb,none,
182182
NOTE(sema_no_import_no_sdk_xcrun,sema_nb,none,
183183
"use \"xcrun -sdk macosx swift\" to select the default OS X SDK installed "
184184
"with Xcode", ())
185+
ERROR(sema_import_current_module,sema_nb,none,
186+
"cannot import module being compiled", ())
185187
ERROR(sema_opening_import,sema_nb,Fatal,
186188
"opening import file for module %0: %1", (Identifier, StringRef))
187189

lib/Sema/NameBinding.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,11 @@ NameBinder::getModule(ArrayRef<std::pair<Identifier, SourceLoc>> modulePath) {
7676
// If the imported module name is the same as the current module,
7777
// skip the Swift module loader and use the Clang module loader instead.
7878
// This allows a Swift module to extend a Clang module of the same name.
79-
if (moduleID.first == SF.getParentModule()->getName() && modulePath.size() == 1) {
79+
//
80+
// FIXME: We'd like to only use this in SIL mode, but unfortunately we use it
81+
// for our fake overlays as well.
82+
if (moduleID.first == SF.getParentModule()->getName() &&
83+
modulePath.size() == 1) {
8084
if (auto importer = Context.getClangModuleLoader())
8185
return importer->loadModule(moduleID.second, modulePath);
8286
return nullptr;
@@ -130,9 +134,27 @@ static const char *getImportKindString(ImportKind kind) {
130134
}
131135
}
132136

137+
static bool shouldImportSelfImportClang(const ImportDecl *ID,
138+
const SourceFile &SF) {
139+
// FIXME: We use '@exported' for fake overlays in testing.
140+
if (ID->isExported())
141+
return true;
142+
if (SF.Kind == SourceFileKind::SIL)
143+
return true;
144+
return false;
145+
}
146+
133147
void NameBinder::addImport(
134148
SmallVectorImpl<std::pair<ImportedModule, ImportOptions>> &imports,
135149
ImportDecl *ID) {
150+
if (ID->getModulePath().front().first == SF.getParentModule()->getName() &&
151+
ID->getModulePath().size() == 1 && !shouldImportSelfImportClang(ID, SF)) {
152+
// If the imported module name is the same as the current module,
153+
// produce an error.
154+
Context.Diags.diagnose(ID, diag::sema_import_current_module);
155+
return;
156+
}
157+
136158
Module *M = getModule(ID->getModulePath());
137159
if (!M) {
138160
SmallString<64> modulePathStr;

stdlib/public/SDK/Foundation/NSError.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import Foundation
21
import CoreFoundation
32
import Darwin
43

stdlib/public/SDK/simd/simd.swift.gyb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
//===----------------------------------------------------------------------===//
1414

1515
import Darwin
16-
import simd
1716

1817
% component = ['x','y','z','w']
1918
% scalar_types = ['Float','Double','Int32']

test/ClangModules/nullability.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -parse -I %S/Inputs/custom-modules %s -verify
1+
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -parse -I %S/Inputs/custom-modules %s -import-underlying-module -verify
22

33
// REQUIRES: objc_interop
44

5-
import nullability;
65
import CoreCooling
76

87
func testSomeClass(sc: SomeClass, osc: SomeClass?) {

test/ClangModules/script.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -parse -verify -I %S/Inputs/custom-modules %s
2-
3-
import script // Clang module
1+
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -parse -verify -I %S/Inputs/custom-modules %s -import-underlying-module
42

53
var _ : ScriptTy
4+
print()

test/decl/import/import.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// RUN: echo "public struct X {}; public var x = X()" | %target-swift-frontend -module-name import_builtin -parse-stdlib -emit-module -o %t -
44
// RUN: echo "public func foo() -> Int { return false }" > %t/import_text.swift
55
// RUN: echo "public func pho$(printf '\xC3\xBB')x() -> Int { return false }" > %t/fran$(printf '\xC3\xA7')ais.swift
6-
// RUN: %target-swift-frontend -parse %s -I %t -sdk "" -enable-source-import -verify -show-diagnostics-after-fatal
6+
// RUN: %target-swift-frontend -parse %s -I %t -sdk "" -enable-source-import -module-name main -verify -show-diagnostics-after-fatal
77

88
import Builtin // expected-error {{no such module 'Builtin'}}
99

@@ -55,3 +55,5 @@ var _ : Int = foo()
5555

5656
import français
5757
import func français.phoûx
58+
59+
import main // expected-error {{cannot import module being compiled}}
File renamed without changes.

0 commit comments

Comments
 (0)