Skip to content

Commit 34d94a6

Browse files
committed
Sema: Offer solution to silence inconsistent import access-level warnings
Adoption InternalImportsByDefault provides a safe access-level by default to imports, as such ambiguities are not a risk and showing this warning is superflous. When this warning is shown, make sure we note this alternative.
1 parent 58c386c commit 34d94a6

File tree

5 files changed

+16
-3
lines changed

5 files changed

+16
-3
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3678,6 +3678,9 @@ ERROR(inconsistent_implicit_access_level_on_import,none,
36783678
"ambiguous implicit access level for import of %0; it is imported as "
36793679
"'%select{private|fileprivate|internal|package|public|%error}1' elsewhere",
36803680
(Identifier, AccessLevel))
3681+
NOTE(inconsistent_implicit_access_level_on_import_silence,none,
3682+
"silence these warnings by adopting 'InternalImportsByDefault'",
3683+
())
36813684
NOTE(inconsistent_implicit_access_level_on_import_here,none,
36823685
"imported "
36833686
"'%select{private|fileprivate|internal|package|public|%error}0' here",

lib/Sema/ImportResolution.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,11 +1066,16 @@ CheckInconsistentAccessLevelOnImport::evaluate(
10661066
auto &diags = mod->getDiags();
10671067
{
10681068
InFlightDiagnostic error =
1069-
diags.diagnose(implicitImport, diag::inconsistent_implicit_access_level_on_import,
1070-
implicitImport->getModule()->getName(), otherAccessLevel);
1069+
diags.diagnose(implicitImport,
1070+
diag::inconsistent_implicit_access_level_on_import,
1071+
implicitImport->getModule()->getName(),
1072+
otherAccessLevel);
10711073
error.fixItInsert(implicitImport->getStartLoc(),
10721074
diag::inconsistent_implicit_access_level_on_import_fixit,
10731075
otherAccessLevel);
1076+
error.flush();
1077+
diags.diagnose(implicitImport,
1078+
diag::inconsistent_implicit_access_level_on_import_silence);
10741079
}
10751080

10761081
SourceLoc accessLevelLoc = otherImport->getStartLoc();

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,15 @@ private import Lib
4949
// RUN: -primary-file %t/ManyFiles_ImplicitVsInternal_FileA.swift -verify
5050
//--- ManyFiles_ImplicitVsInternal_FileA.swift
5151
import Lib // expected-error {{ambiguous implicit access level for import of 'Lib'; it is imported as 'internal' elsewhere}}
52+
// expected-note @-1 {{silence these warnings by adopting 'InternalImportsByDefault'}}
5253
//--- ManyFiles_ImplicitVsInternal_FileB.swift
5354
internal import Lib // expected-note {{imported 'internal' here}}
5455

5556
// RUN: %target-swift-frontend -typecheck %t/ManyFiles_ImplicitVsPackage_FileB.swift -I %t \
5657
// RUN: -primary-file %t/ManyFiles_ImplicitVsPackage_FileA.swift -verify
5758
//--- ManyFiles_ImplicitVsPackage_FileA.swift
5859
import Lib // expected-error {{ambiguous implicit access level for import of 'Lib'; it is imported as 'package' elsewhere}}
60+
// expected-note @-1 {{silence these warnings by adopting 'InternalImportsByDefault'}}
5961
//--- ManyFiles_ImplicitVsPackage_FileB.swift
6062
package import Lib // expected-note {{imported 'package' here}} @:1
6163

test/Sema/access-level-import-inconsistent-same-file.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// RUN: %empty-directory(%t)
2-
// RUN: split-file %s %t
2+
// RUN: split-file %s %t --leading-lines
33

44
/// Build the library.
55
// RUN: %target-swift-frontend -emit-module %t/Lib.swift -module-name Lib1 -o %t
@@ -57,6 +57,7 @@ public func dummyAPI(t1: Lib1.Type1, t2: Lib2.Type1, t3: Lib3.Type1) {}
5757
/// Simple public vs internal, imports defaults to public.
5858
import Lib1 // expected-note {{imported 'public' here}}
5959
// expected-error @-1 {{ambiguous implicit access level for import of 'Lib1'; it is imported as 'internal' elsewhere}}
60+
// expected-note @-2 {{silence these warnings by adopting 'InternalImportsByDefault'}}
6061
internal import Lib1 // expected-warning {{module 'Lib1' is imported as 'public' from the same file; this 'internal' access level will be ignored}}
6162
// expected-note @-1 {{imported 'internal' here}}
6263

@@ -65,6 +66,7 @@ public import Lib2
6566
// expected-note @-1 {{imported 'public' here}}
6667
import Lib2
6768
// expected-error @-1 {{ambiguous implicit access level for import of 'Lib2'; it is imported as 'public' elsewhere}}
69+
// expected-note @-2 {{silence these warnings by adopting 'InternalImportsByDefault'}}
6870

6971
public func dummyAPI(t: Lib1.Type1, t2: Lib2.Type1) {}
7072

test/Sema/superfluously-public-imports.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ public struct Extended {
124124
//--- Client_Swift5.swift
125125
/// No diagnostics should be raised on the implicit access level.
126126
import UnusedImport // expected-error {{ambiguous implicit access level for import of 'UnusedImport'; it is imported as 'public' elsewhere}}
127+
// expected-note @-1 {{silence these warnings by adopting 'InternalImportsByDefault'}}
127128
public import UnusedImport // expected-warning {{public import of 'UnusedImport' was not used in public declarations or inlinable code}} {{1-7=internal}}
128129
// expected-note @-1 {{imported 'public' here}}
129130

0 commit comments

Comments
 (0)