Skip to content

Commit 6c55bb6

Browse files
committed
Sema: ignore inconsistent bare imports from a file generated by Xcode
In Swift 5 mode, the compiler reports when two files import the same module, where one file has a bare import and the other an access-level import. This avoids unintentionally promoting the import to public when preexisting code marked it otherwise. Exempt a file generated by Xcode from this check as it's not user modifiable. In the future, we should identify such generated files properly. Either with a new attribute or in the way they are passed to the compiler. rdar://122032472
1 parent 8bb6846 commit 6c55bb6

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

lib/Sema/ImportResolution.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1055,8 +1055,15 @@ CheckInconsistentAccessLevelOnImport::evaluate(
10551055
auto mod = SF->getParentModule();
10561056
auto diagnose = [mod](const ImportDecl *implicitImport,
10571057
const ImportDecl *otherImport) {
1058-
auto otherAccessLevel = otherImport->getAccessLevel();
1058+
// Ignore files generated by Xcode. We should probably identify them via
1059+
// an attribuite or frontend flag, until them match the file by name.
1060+
SourceFile *implicitSF =
1061+
implicitImport->getDeclContext()->getParentSourceFile();
1062+
StringRef basename = llvm::sys::path::filename(implicitSF->getFilename());
1063+
if (basename == "GeneratedAssetSymbols.swift")
1064+
return;
10591065

1066+
auto otherAccessLevel = otherImport->getAccessLevel();
10601067
auto &diags = mod->getDiags();
10611068
{
10621069
InFlightDiagnostic error =

test/Sema/access-level-import-inconsistencies.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,16 @@ package import Lib // expected-note {{imported 'package' here}} @:1
6969
import Lib
7070
//--- ManyFiles_AmbiguitySwift6_FileB.swift
7171
internal import Lib
72+
73+
/// Don't report inconsistencies from a file generated by Xcode.
74+
// RUN: %target-swift-frontend -typecheck -I %t \
75+
// RUN -primary-file %t/GeneratedAssetSymbols.swift \
76+
// RUN: %t/UserWrittenFile.swift -verify
77+
// RUN: %target-swift-frontend -typecheck -I %t \
78+
// RUN %t/GeneratedAssetSymbols.swift \
79+
// RUN: -primary-file %t/UserWrittenFile.swift -verify
80+
//--- UserWrittenFile.swift
81+
internal import Lib
82+
//--- GeneratedAssetSymbols.swift
83+
import Lib
84+

0 commit comments

Comments
 (0)