Skip to content

Commit 5e38d06

Browse files
authored
Merge pull request #68644 from xymus/skip-superfluous-import-notes
Sema: Only complain about the import when it does restrict the access level
2 parents 23e6149 + af478fd commit 5e38d06

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

lib/Sema/ResilienceDiagnostics.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ bool TypeChecker::diagnoseInlinableDeclRefAccess(SourceLoc loc,
114114

115115
ImportAccessLevel problematicImport = D->getImportAccessFrom(DC);
116116
if (problematicImport.has_value() &&
117-
diagAccessLevel == problematicImport->accessLevel) {
117+
problematicImport->accessLevel < D->getFormalAccess()) {
118118
Context.Diags.diagnose(problematicImport->accessLevelLoc,
119119
diag::decl_import_via_here, D,
120120
problematicImport->accessLevel,

lib/Sema/TypeCheckAccess.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,12 @@ void AccessControlCheckerBase::checkTypeAccessImpl(
268268
static_cast<const IdentTypeRepr*>(complainRepr)->getBoundDecl();
269269
assert(VD && "findTypeWithScope should return bound TypeReprs only");
270270
complainImport = VD->getImportAccessFrom(useDC);
271+
272+
// Don't complain about an import that doesn't restrict the access
273+
// level of the decl. This can happen with imported `package` decls.
274+
if (complainImport.has_value() &&
275+
complainImport->accessLevel >= VD->getFormalAccess())
276+
complainImport = llvm::None;
271277
}
272278

273279
diagnose(problematicAccessScope, complainRepr, downgradeToWarning,

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

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
// RUN: split-file --leading-lines %s %t
55

66
/// Build the libraries.
7-
// RUN: %target-swift-frontend -emit-module %t/PublicLib.swift -o %t
8-
// RUN: %target-swift-frontend -emit-module %t/PackageLib.swift -o %t
7+
// RUN: %target-swift-frontend -emit-module %t/PublicLib.swift -o %t \
8+
// RUN: -package-name pkg
9+
// RUN: %target-swift-frontend -emit-module %t/PackageLib.swift -o %t \
10+
// RUN: -package-name pkg
911
// RUN: %target-swift-frontend -emit-module %t/InternalLib.swift -o %t
1012
// RUN: %target-swift-frontend -emit-module %t/FileprivateLib.swift -o %t
1113
// RUN: %target-swift-frontend -emit-module %t/PrivateLib.swift -o %t
@@ -14,15 +16,20 @@
1416
// RUN: %target-swift-frontend -typecheck %t/Client.swift -I %t \
1517
// RUN: -enable-experimental-feature AccessLevelOnImport -verify \
1618
// RUN: -package-name pkg
19+
// RUN: %target-swift-frontend -typecheck %t/PackageTypeImportedAsPackageClient.swift -I %t \
20+
// RUN: -enable-experimental-feature AccessLevelOnImport -verify \
21+
// RUN: -package-name pkg
1722
// RUN: %target-swift-frontend -typecheck %t/LocalVsImportClient.swift -I %t \
1823
// RUN: -enable-experimental-feature AccessLevelOnImport -verify \
1924
// RUN: -package-name pkg
2025

2126
//--- PublicLib.swift
2227
public struct PublicImportType {}
28+
package struct PackageLevelPublicImportedType {}
2329

2430
//--- PackageLib.swift
2531
public struct PackageImportType {}
32+
package struct PackageLevelPackageImportedType {}
2633

2734
//--- InternalLib.swift
2835
public struct InternalImportType {}
@@ -62,6 +69,26 @@ public func publicFuncUsesPrivateScambled(_ a: PublicImportType, d: FileprivateI
6269
var _: PrivateImportType
6370
}
6471

72+
//--- PackageTypeImportedAsPackageClient.swift
73+
/// Report errors about using package decls in public but don't note the import
74+
/// as it doesn't affect the access level of the decls.
75+
76+
public import PublicLib
77+
package import PackageLib
78+
79+
public func publicFuncUsesPackageLevelPublicImportedType(_ a: PackageLevelPublicImportedType) {} // expected-error {{function cannot be declared public because its parameter uses a package type}}
80+
public func publicFuncUsesPackageLevelPackageImportedType(_ a: PackageLevelPackageImportedType) {} // expected-error {{function cannot be declared public because its parameter uses a package type}}
81+
82+
@inlinable public func funcInlinableReferenceToPublicImportedType() {
83+
var _: PackageLevelPublicImportedType // expected-error {{struct 'PackageLevelPublicImportedType' is package and cannot be referenced from an '@inlinable' function}}
84+
var _: Array<PackageLevelPublicImportedType> // expected-error {{struct 'PackageLevelPublicImportedType' is package and cannot be referenced from an '@inlinable' function}}
85+
}
86+
87+
@inlinable public func funcInlinableReferenceToPackageImportedType() {
88+
var _: PackageLevelPackageImportedType // expected-error {{struct 'PackageLevelPackageImportedType' is package and cannot be referenced from an '@inlinable' function}}
89+
var _: Array<PackageLevelPackageImportedType> // expected-error {{struct 'PackageLevelPackageImportedType' is package and cannot be referenced from an '@inlinable' function}}
90+
}
91+
6592
/// Local vs imports
6693
//--- LocalVsImportClient.swift
6794
public import PublicLib

0 commit comments

Comments
 (0)