Skip to content

[5.9] Handle null package context when returning access scope #64727

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3934,10 +3934,15 @@ getAccessScopeForFormalAccess(const ValueDecl *VD,
case AccessLevel::Package: {
auto pkg = resultDC->getPackageContext(/*lookupIfNotCurrent*/ true);
if (!pkg) {
// No package context was found; show diagnostics
auto &d = VD->getASTContext().Diags;
d.diagnose(VD->getLoc(), diag::access_control_requires_package_name);
// Instead of reporting and failing early, return the scope of
// resultDC to allow continuation (should still non-zero exit later)
return AccessScope(resultDC);
} else {
return AccessScope(pkg);
}
return AccessScope(pkg);
}
case AccessLevel::Public:
case AccessLevel::Open:
Expand Down
20 changes: 8 additions & 12 deletions test/diagnostics/package-name-diagnostics.swift
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
// RUN: %empty-directory(%t)
// REQUIRES: rdar106819422

// Package name should have valid characters
// RUN: not --crash %target-swift-frontend -module-name Logging -package-name My-Logging%Pkg %s -emit-module -emit-module-path %t/Logging.swiftmodule 2> %t/resultA.output
// RUN: not %target-swift-frontend -module-name Logging -package-name My-Logging%Pkg %s -emit-module -emit-module-path %t/Logging.swiftmodule 2> %t/resultA.output
// RUN: %FileCheck %s -input-file %t/resultA.output -check-prefix CHECK-BAD
// CHECK-BAD: package name "My-Logging%Pkg" is not a valid identifier
// CHECK-BAD: decl has a package access level but no -package-name was passed
// CHECK-BAD: non-public decl has no formal access scope
// CHECK-BAD: error: package name "My-Logging%Pkg" is not a valid identifier
// CHECK-BAD: error: decl has a package access level but no -package-name was passed

// Package name should not be empty
// RUN: not --crash %target-swift-frontend -typecheck %s -package-name "" 2>&1 | %FileCheck %s -check-prefix CHECK-EMPTY
// CHECK-EMPTY: package name "" is not a valid identifier
// CHECK-EMPTY: decl has a package access level but no -package-name was passed
// CHECK-EMPTY: non-public decl has no formal access scope
// RUN: not %target-swift-frontend -typecheck %s -package-name "" 2>&1 | %FileCheck %s -check-prefix CHECK-EMPTY
// CHECK-EMPTY: error: package name "" is not a valid identifier
// CHECK-EMPTY: error: decl has a package access level but no -package-name was passed

// If package access level is used but no package-name is passed, it should error
// RUN: not --crash %target-swift-frontend -typecheck %s 2>&1 | %FileCheck %s -check-prefix CHECK-MISSING
// CHECK-MISSING: decl has a package access level but no -package-name was passed
// CHECK-MISSING: non-public decl has no formal access scope
// RUN: not %target-swift-frontend -typecheck %s 2>&1 | %FileCheck %s -check-prefix CHECK-MISSING
// CHECK-MISSING: error: decl has a package access level but no -package-name was passed

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