Skip to content

Commit 125962c

Browse files
authored
Merge pull request #68336 from xymus/package-import-require-package
[Sema] Report uses of `package import` without defining a package name
2 parents afc2bab + ea2465d commit 125962c

10 files changed

+57
-12
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1931,6 +1931,10 @@ WARNING(access_control_non_objc_open_member,none,
19311931
ERROR(access_control_requires_package_name, none,
19321932
"%0 has a package access level but no -package-name was specified: %1",
19331933
(Identifier, StringRef))
1934+
ERROR(access_control_requires_package_name_import, none,
1935+
"package import can only be used from a module with a package name; "
1936+
"set it with the compiler flag -package-name",
1937+
())
19341938
ERROR(invalid_decl_attribute,none,
19351939
"'%0' attribute cannot be applied to this declaration", (DeclAttribute))
19361940
ERROR(attr_invalid_on_decl_kind,none,

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1965,6 +1965,14 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
19651965
target->getModuleFilename());
19661966
}
19671967

1968+
// Report use of package import when no package name is set.
1969+
if (ID->getAccessLevel() == AccessLevel::Package &&
1970+
getASTContext().LangOpts.PackageName.empty()) {
1971+
auto &diags = ID->getASTContext().Diags;
1972+
diags.diagnose(ID->getLoc(),
1973+
diag::access_control_requires_package_name_import);
1974+
}
1975+
19681976
// Report the public import of a private module.
19691977
if (ID->getASTContext().LangOpts.LibraryLevel == LibraryLevel::API) {
19701978
auto importer = ID->getModuleContext();

test/Sema/access-level-and-non-resilient-import.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,22 @@
1717
/// A resilient client will error on public imports.
1818
// RUN: %target-swift-frontend -typecheck %t/Client_Swift5.swift -I %t \
1919
// RUN: -enable-library-evolution -swift-version 5 \
20-
// RUN: -enable-experimental-feature AccessLevelOnImport -verify
20+
// RUN: -enable-experimental-feature AccessLevelOnImport -verify \
21+
// RUN: -package-name pkg
2122
// RUN: %target-swift-frontend -typecheck %t/Client_Swift6.swift -I %t \
2223
// RUN: -enable-library-evolution -swift-version 6 \
23-
// RUN: -enable-experimental-feature AccessLevelOnImport -verify
24+
// RUN: -enable-experimental-feature AccessLevelOnImport -verify \
25+
// RUN: -package-name pkg
2426

2527
/// A non-resilient client doesn't complain.
2628
// RUN: %target-swift-frontend -typecheck %t/Client_Swift5.swift -I %t \
2729
// RUN: -swift-version 5 \
28-
// RUN: -enable-experimental-feature AccessLevelOnImport
30+
// RUN: -enable-experimental-feature AccessLevelOnImport \
31+
// RUN: -package-name pkg
2932
// RUN: %target-swift-frontend -typecheck %t/Client_Swift6.swift -I %t \
3033
// RUN: -swift-version 6 \
31-
// RUN: -enable-experimental-feature AccessLevelOnImport
34+
// RUN: -enable-experimental-feature AccessLevelOnImport \
35+
// RUN: -package-name pkg
3236

3337
//--- DefaultLib.swift
3438
//--- PublicLib.swift

test/Sema/access-level-import-diag-priority.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212

1313
/// Check diagnostics.
1414
// RUN: %target-swift-frontend -typecheck %t/Client.swift -I %t \
15-
// RUN: -enable-experimental-feature AccessLevelOnImport -verify
15+
// RUN: -enable-experimental-feature AccessLevelOnImport -verify \
16+
// RUN: -package-name pkg
1617
// RUN: %target-swift-frontend -typecheck %t/LocalVsImportClient.swift -I %t \
17-
// RUN: -enable-experimental-feature AccessLevelOnImport -verify
18+
// RUN: -enable-experimental-feature AccessLevelOnImport -verify \
19+
// RUN: -package-name pkg
1820

1921
//--- PublicLib.swift
2022
public struct PublicImportType {}

test/Sema/access-level-import-flag-check.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
// RUN: %target-swift-frontend -emit-module %t/PrivateLib.swift -o %t
1010

1111
/// Check flag requirement, without and with the flag.
12-
// RUN: %target-swift-frontend -typecheck %t/ClientWithoutTheFlag.swift -I %t -verify
12+
// RUN: %target-swift-frontend -typecheck %t/ClientWithoutTheFlag.swift -I %t -verify \
13+
// RUN: -package-name package
1314
// RUN: %target-swift-frontend -typecheck %t/ClientWithoutTheFlag.swift -I %t \
14-
// RUN: -enable-experimental-feature AccessLevelOnImport
15+
// RUN: -enable-experimental-feature AccessLevelOnImport \
16+
// RUN: -package-name package
1517
// REQUIRES: asserts
1618

1719
//--- PublicLib.swift

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
public struct LibType {}
2222

2323
// RUN: %target-swift-frontend -typecheck %t/OneFile_AllExplicit.swift -I %t \
24-
// RUN: -enable-experimental-feature AccessLevelOnImport -verify
24+
// RUN: -enable-experimental-feature AccessLevelOnImport -verify \
25+
// RUN: -package-name package
2526
//--- OneFile_AllExplicit.swift
2627
public import Lib
2728
package import Lib
@@ -30,7 +31,8 @@ fileprivate import Lib
3031
private import Lib
3132

3233
// RUN: %target-swift-frontend -typecheck %t/ManyFiles_AllExplicit_File?.swift -I %t \
33-
// RUN: -enable-experimental-feature AccessLevelOnImport -verify
34+
// RUN: -enable-experimental-feature AccessLevelOnImport -verify \
35+
// RUN: -package-name package
3436
//--- ManyFiles_AllExplicit_FileA.swift
3537
public import Lib
3638
//--- ManyFiles_AllExplicit_FileB.swift

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
/// Check diagnostics.
1919
// RUN: %target-swift-frontend -typecheck %t/Client.swift -I %t \
2020
// RUN: -enable-library-evolution -swift-version 5 \
21-
// RUN: -enable-experimental-feature AccessLevelOnImport -verify
21+
// RUN: -enable-experimental-feature AccessLevelOnImport -verify \
22+
// RUN: -package-name package
2223

2324
//--- PublicLib.swift
2425
public protocol PublicImportProto {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111

1212
/// Check that all access levels are accepted, except for 'open'.
1313
// RUN: %target-swift-frontend -typecheck %t/Client.swift -I %t \
14-
// RUN: -enable-experimental-feature AccessLevelOnImport -verify
14+
// RUN: -enable-experimental-feature AccessLevelOnImport -verify \
15+
// RUN: -package-name package
1516

1617
//--- PublicLib.swift
1718
//--- PackageLib.swift
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/// Report uses of package import without a package.
2+
3+
// RUN: %empty-directory(%t)
4+
// RUN: split-file --leading-lines %s %t
5+
6+
// RUN: %target-swift-frontend -emit-module %t/PackageLib.swift -o %t
7+
// RUN: %target-swift-frontend -typecheck %t/Client.swift -I %t \
8+
// RUN: -enable-experimental-feature AccessLevelOnImport -verify
9+
// RUN: %target-swift-frontend -typecheck %t/Client.swift -I %t \
10+
// RUN: -enable-experimental-feature AccessLevelOnImport \
11+
// RUN: -package-name pkg
12+
13+
//--- PackageLib.swift
14+
public struct PackageImportType {}
15+
16+
//--- Client.swift
17+
package import PackageLib // expected-error {{package import can only be used from a module with a package name; set it with the compiler flag -package-name}}

test/Serialization/access-level-import-dependencies.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ private import HiddenDep
4949
import PublicDep
5050

5151
// RUN: %target-swift-frontend -typecheck %t/ClientOfNonPublic.swift -I %t \
52+
// RUN: -package-name pkg \
5253
// RUN: -Rmodule-loading 2>&1 | %FileCheck -check-prefix=HIDDEN-DEP %s
5354
// HIDDEN-DEP-NOT: loaded module 'HiddenDep'
5455
//--- ClientOfNonPublic.swift
@@ -61,6 +62,7 @@ import PrivateDep
6162
// RUN: %target-swift-frontend -emit-module %t/PublicDep.swift -o %t -I %t \
6263
// RUN: -enable-experimental-feature AccessLevelOnImport
6364
// RUN: %target-swift-frontend -emit-module %t/PackageDep.swift -o %t -I %t \
65+
// RUN: -package-name MyPackage \
6466
// RUN: -enable-experimental-feature AccessLevelOnImport
6567
// RUN: %target-swift-frontend -emit-module %t/InternalDep.swift -o %t -I %t \
6668
// RUN: -enable-experimental-feature AccessLevelOnImport
@@ -110,6 +112,7 @@ import PrivateDep
110112
// RUN: %target-swift-frontend -typecheck %t/TestableClientOfPublic.swift -I %t \
111113
// RUN: -Rmodule-loading 2>&1 | %FileCheck -check-prefix=VISIBLE-DEP %s
112114
// RUN: %target-swift-frontend -typecheck %t/TestableClientOfNonPublic.swift -I %t \
115+
// RUN: -package-name pkg \
113116
// RUN: -Rmodule-loading 2>&1 | %FileCheck -check-prefix=VISIBLE-DEP %s
114117

115118
/// In the case of a testable of a module reexporting another Swift module,
@@ -132,6 +135,7 @@ import PrivateDep
132135
/// Fail if the transitive dependency is missing.
133136
// RUN: rm %t/HiddenDep.swiftmodule
134137
// RUN: %target-swift-frontend -typecheck %t/TestableClientOfNonPublic.swift -I %t \
138+
// RUN: -package-name pkg \
135139
// RUN: -verify -show-diagnostics-after-fatal
136140

137141
/// In a multi-file scenario, we try and fail to load the transitive dependency

0 commit comments

Comments
 (0)