Skip to content

Commit 1a4a47f

Browse files
committed
[Sema] Merge diagnostics about package modifier outside of a package
Show the same error for both decls and imports when using the package access level and no package name is set. Also brings up this check to run once on decls and avoid repeated diagnostics.
1 parent 97668d5 commit 1a4a47f

File tree

7 files changed

+39
-47
lines changed

7 files changed

+39
-47
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1935,10 +1935,7 @@ WARNING(access_control_non_objc_open_member,none,
19351935
"non-'@objc' %0 in extensions cannot be overridden; use 'public' instead",
19361936
(DescriptiveDeclKind))
19371937
ERROR(access_control_requires_package_name, none,
1938-
"%0 has a package access level but no -package-name was specified: %1",
1939-
(Identifier, StringRef))
1940-
ERROR(access_control_requires_package_name_import, none,
1941-
"package import can only be used from a module with a package name; "
1938+
"the package access level requires a package name; "
19421939
"set it with the compiler flag -package-name",
19431940
())
19441941
ERROR(invalid_decl_attribute,none,

lib/AST/Decl.cpp

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4091,21 +4091,6 @@ getAccessScopeForFormalAccess(const ValueDecl *VD,
40914091
case AccessLevel::Package: {
40924092
auto pkg = resultDC->getPackageContext(/*lookupIfNotCurrent*/ true);
40934093
if (!pkg) {
4094-
auto srcFile = resultDC->getOutermostParentSourceFile();
4095-
// Check if the file containing package decls is an interface file; if an
4096-
// interface file contains package decls, they must be usableFromInline or
4097-
// inlinable and are accessed within the defining module, so package-name
4098-
// is not needed; do not show diagnostics in such case.
4099-
auto shouldSkipDiag = srcFile && srcFile->Kind == SourceFileKind::Interface;
4100-
if (!shouldSkipDiag) {
4101-
// No package context was found; show diagnostics
4102-
auto &d = VD->getASTContext().Diags;
4103-
auto filename = srcFile ? srcFile->getFilename() : resultDC->getParentModule()->getBaseIdentifier().str();
4104-
d.diagnose(VD->getLoc(),
4105-
diag::access_control_requires_package_name,
4106-
VD->getBaseIdentifier(),
4107-
filename);
4108-
}
41094094
// Instead of reporting and failing early, return the scope of resultDC to
41104095
// allow continuation (should still non-zero exit later if in script mode)
41114096
return AccessScope(resultDC);

lib/Sema/TypeCheckAttr.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -996,8 +996,8 @@ bool AttributeChecker::visitAbstractAccessControlAttr(
996996
return true;
997997
}
998998

999+
SourceFile *File = D->getDeclContext()->getParentSourceFile();
9991000
if (auto importDecl = dyn_cast<ImportDecl>(D)) {
1000-
SourceFile *File = D->getDeclContext()->getParentSourceFile();
10011001
if (!D->getASTContext().LangOpts.hasFeature(Feature::AccessLevelOnImport) &&
10021002
File && File->Kind != SourceFileKind::Interface) {
10031003
diagnoseAndRemoveAttr(attr, diag::access_level_on_import_not_enabled);
@@ -1019,6 +1019,14 @@ bool AttributeChecker::visitAbstractAccessControlAttr(
10191019
}
10201020
}
10211021

1022+
if (attr->getAccess() == AccessLevel::Package &&
1023+
D->getASTContext().LangOpts.PackageName.empty() &&
1024+
File && File->Kind != SourceFileKind::Interface) {
1025+
// `package` modifier used outside of a package.
1026+
diagnose(attr->getLocation(), diag::access_control_requires_package_name);
1027+
return true;
1028+
}
1029+
10221030
return false;
10231031
}
10241032

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1965,14 +1965,6 @@ 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-
19761968
// Report the public import of a private module.
19771969
if (ID->getASTContext().LangOpts.LibraryLevel == LibraryLevel::API) {
19781970
auto importer = ID->getModuleContext();

test/Sema/package-import-no-package-name.swift

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/// Report uses of the package access-level modifier without a package name.
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 {{the package access level requires a package name; set it with the compiler flag -package-name}}
18+
19+
package struct PackageStruct { // expected-error {{the package access level requires a package name; set it with the compiler flag -package-name}}
20+
package func explicitlyPackage() {} // expected-error {{the package access level requires a package name; set it with the compiler flag -package-name}}
21+
}
22+
23+
public struct PublicStruct {}
24+
25+
package extension PublicStruct { // expected-error {{the package access level requires a package name; set it with the compiler flag -package-name}}
26+
func implicitlyPackage() {}
27+
}

test/diagnostics/package-name-diagnostics.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
// Package name should not be empty
44
// RUN: not %target-swift-frontend -typecheck %s -package-name "" 2>&1 | %FileCheck %s -check-prefix CHECK-EMPTY
55
// CHECK-EMPTY: error: package-name is empty
6-
// CHECK-EMPTY: error: 'log' has a package access level but no -package-name was specified: {{.*}}.swift
6+
// CHECK-EMPTY: error: the package access level requires a package name; set it with the compiler flag -package-name
77

88
// If package access level is used but no package-name is passed, it should error
99
// RUN: not %target-swift-frontend -typecheck %s 2>&1 | %FileCheck %s -check-prefix CHECK-MISSING
10-
// CHECK-MISSING: error: 'log' has a package access level but no -package-name was specified: {{.*}}.swift
10+
// CHECK-MISSING: error: the package access level requires a package name; set it with the compiler flag -package-name
1111

1212
// Package name can be same as the module name
1313
// RUN: %target-swift-frontend -module-name Logging -package-name Logging %s -emit-module -emit-module-path %t/Logging.swiftmodule

0 commit comments

Comments
 (0)