Skip to content

Commit ed8bb98

Browse files
authored
Merge pull request #75899 from tshortli/member-import-visibility-spi
Sema: Only add `@_spiOnly` to import fix-its with a public access level
2 parents 0ce9104 + 317b0e7 commit ed8bb98

File tree

2 files changed

+63
-7
lines changed

2 files changed

+63
-7
lines changed

lib/Sema/TypeCheckNameLookup.cpp

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -830,15 +830,26 @@ diagnoseAndFixMissingImportForMember(const ValueDecl *decl, SourceFile *sf,
830830

831831
llvm::SmallString<64> importText;
832832

833+
// Add flags that must be used consistently on every import in every file.
833834
auto fixItInfo = fixItCache.getInfo(definingModule);
834835
if (fixItInfo.flags.contains(ImportFlags::ImplementationOnly))
835836
importText += "@_implementationOnly ";
836837
if (fixItInfo.flags.contains(ImportFlags::WeakLinked))
837838
importText += "@_weakLinked ";
838-
if (fixItInfo.flags.contains(ImportFlags::SPIOnly))
839-
importText += "@_spiOnly ";
840839

841-
// @_spi imports.
840+
auto explicitAccessLevel = fixItInfo.accessLevel;
841+
bool isPublicImport =
842+
explicitAccessLevel
843+
? *explicitAccessLevel >= AccessLevel::Public
844+
: !ctx.LangOpts.hasFeature(Feature::InternalImportsByDefault);
845+
846+
// Add flags that are only appropriate on public imports.
847+
if (isPublicImport) {
848+
if (fixItInfo.flags.contains(ImportFlags::SPIOnly))
849+
importText += "@_spiOnly ";
850+
}
851+
852+
// Add @_spi groups if needed for the declaration.
842853
if (decl->isSPI()) {
843854
auto spiGroups = decl->getSPIGroups();
844855
if (!spiGroups.empty()) {
@@ -848,8 +859,8 @@ diagnoseAndFixMissingImportForMember(const ValueDecl *decl, SourceFile *sf,
848859
}
849860
}
850861

851-
if (auto accessLevel = fixItInfo.accessLevel) {
852-
importText += getAccessLevelSpelling(*accessLevel);
862+
if (explicitAccessLevel) {
863+
importText += getAccessLevelSpelling(*explicitAccessLevel);
853864
importText += " ";
854865
}
855866

test/NameLookup/members_transitive_multifile_access_level.swift

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,34 @@
88
// RUN: %target-swift-frontend -emit-module -o %t %t/MixedUses.swift
99
// RUN: %target-swift-frontend -emit-module -o %t %t/InternalUsesOnlyTransitivelyImported.swift
1010
// RUN: %target-swift-frontend -emit-module -o %t %t/Exports.swift -I %t
11+
// RUN: %target-swift-frontend -emit-module -o %t %t/InternalUsesOnlySPIOnly.swift -I %t
12+
// RUN: %target-swift-frontend -emit-module -o %t %t/InternalUsesOnlyDefaultedImportSPIOnly.swift -I %t
13+
// RUN: %target-swift-frontend -emit-module -o %t %t/PublicUsesOnlySPIOnly.swift -I %t
14+
1115
// RUN: %target-swift-frontend -typecheck -verify -swift-version 5 \
1216
// RUN: -primary-file %t/function_bodies.swift \
1317
// RUN: -primary-file %t/function_signatures_unqualified.swift \
1418
// RUN: -primary-file %t/function_signatures_qualified.swift \
1519
// RUN: -primary-file %t/extensions.swift \
1620
// RUN: %t/imports.swift \
1721
// RUN: -I %t -package-name Package \
18-
// RUN: -enable-experimental-feature MemberImportVisibility
22+
// RUN: -enable-experimental-feature MemberImportVisibility \
23+
// RUN: -verify-additional-prefix public-by-default-
24+
25+
// RUN: %target-swift-frontend -typecheck -verify -swift-version 5 \
26+
// RUN: -primary-file %t/function_bodies.swift \
27+
// RUN: -primary-file %t/function_signatures_unqualified.swift \
28+
// RUN: -primary-file %t/function_signatures_qualified.swift \
29+
// RUN: -primary-file %t/extensions.swift \
30+
// RUN: %t/imports.swift \
31+
// RUN: -I %t -package-name Package \
32+
// RUN: -enable-experimental-feature MemberImportVisibility \
33+
// RUN: -enable-upcoming-feature InternalImportsByDefault \
34+
// RUN: -verify-additional-prefix internal-by-default-
1935

2036
//--- function_bodies.swift
2137

22-
// FIXME: The access level is wrong on many of these fix-its.
38+
// FIXME: The access level of some of these fix-its should be public/package instead of internal.
2339
import Swift // Just here to anchor the fix-its
2440
// expected-note {{add import of module 'InternalUsesOnly'}}{{1-1=internal import InternalUsesOnly\n}}
2541
// expected-note@-1 {{add import of module 'InternalUsesOnlyDefaultedImport'}}{{1-1=import InternalUsesOnlyDefaultedImport\n}}
@@ -28,12 +44,19 @@ import Swift // Just here to anchor the fix-its
2844
// expected-note@-4 {{add import of module 'PublicUsesOnlyDefaultedImport'}}{{1-1=import PublicUsesOnlyDefaultedImport\n}}
2945
// expected-note@-5 3 {{add import of module 'MixedUses'}}{{1-1=internal import MixedUses\n}}
3046
// expected-note@-6 {{add import of module 'InternalUsesOnlyTransitivelyImported'}}{{1-1=internal import InternalUsesOnlyTransitivelyImported\n}}
47+
// expected-note@-7 {{add import of module 'InternalUsesOnlySPIOnly'}}{{1-1=internal import InternalUsesOnlySPIOnly\n}}
48+
// expected-public-by-default-note@-8 {{add import of module 'InternalUsesOnlyDefaultedImportSPIOnly'}}{{1-1=@_spiOnly import InternalUsesOnlyDefaultedImportSPIOnly\n}}
49+
// expected-internal-by-default-note@-9 {{add import of module 'InternalUsesOnlyDefaultedImportSPIOnly'}}{{1-1=import InternalUsesOnlyDefaultedImportSPIOnly\n}}
50+
// expected-note@-10 {{add import of module 'PublicUsesOnlySPIOnly'}}{{1-1=internal import PublicUsesOnlySPIOnly\n}}
51+
3152

3253
func internalFunc(_ x: Int) {
3354
_ = x.memberInInternalUsesOnly // expected-error {{property 'memberInInternalUsesOnly' is not available due to missing import of defining module 'InternalUsesOnly'}}
3455
_ = x.memberInInternalUsesOnlyDefaultedImport // expected-error {{property 'memberInInternalUsesOnlyDefaultedImport' is not available due to missing import of defining module 'InternalUsesOnlyDefaultedImport'}}
3556
_ = x.memberInMixedUses // expected-error {{property 'memberInMixedUses' is not available due to missing import of defining module 'MixedUses'}}
3657
_ = x.memberInInternalUsesOnlyTransitivelyImported // expected-error {{property 'memberInInternalUsesOnlyTransitivelyImported' is not available due to missing import of defining module 'InternalUsesOnlyTransitivelyImported'}}
58+
_ = x.memberInInternalUsesOnlySPIOnly // expected-error {{property 'memberInInternalUsesOnlySPIOnly' is not available due to missing import of defining module 'InternalUsesOnlySPIOnly'}}
59+
_ = x.memberInInternalUsesOnlyDefaultedImportSPIOnly // expected-error {{property 'memberInInternalUsesOnlyDefaultedImportSPIOnly' is not available due to missing import of defining module 'InternalUsesOnlyDefaultedImportSPIOnly'}}
3760
}
3861

3962
@inlinable package func packageInlinableFunc(_ x: Int) {
@@ -45,6 +68,7 @@ func internalFunc(_ x: Int) {
4568
_ = x.memberInPublicUsesOnly // expected-error {{property 'memberInPublicUsesOnly' is not available due to missing import of defining module 'PublicUsesOnly'}}
4669
_ = x.memberInPublicUsesOnlyDefaultedImport // expected-error {{property 'memberInPublicUsesOnlyDefaultedImport' is not available due to missing import of defining module 'PublicUsesOnlyDefaultedImport'}}
4770
_ = x.memberInMixedUses // expected-error {{property 'memberInMixedUses' is not available due to missing import of defining module 'MixedUses'}}
71+
_ = x.memberInPublicUsesOnlySPIOnly // expected-error {{property 'memberInPublicUsesOnlySPIOnly' is not available due to missing import of defining module 'PublicUsesOnlySPIOnly'}}
4872
}
4973

5074
//--- function_signatures_unqualified.swift
@@ -120,6 +144,9 @@ internal import PublicUsesOnly
120144
import PublicUsesOnlyDefaultedImport
121145
internal import MixedUses
122146
internal import Exports
147+
@_spiOnly public import InternalUsesOnlySPIOnly
148+
@_spiOnly import InternalUsesOnlyDefaultedImportSPIOnly
149+
@_spiOnly public import PublicUsesOnlySPIOnly
123150

124151
//--- InternalUsesOnly.swift
125152

@@ -180,3 +207,21 @@ extension Int {
180207
//--- Exports.swift
181208

182209
@_exported import InternalUsesOnlyTransitivelyImported
210+
211+
//--- InternalUsesOnlySPIOnly.swift
212+
213+
extension Int {
214+
public var memberInInternalUsesOnlySPIOnly: Int { return self }
215+
}
216+
217+
//--- InternalUsesOnlyDefaultedImportSPIOnly.swift
218+
219+
extension Int {
220+
public var memberInInternalUsesOnlyDefaultedImportSPIOnly: Int { return self }
221+
}
222+
223+
//--- PublicUsesOnlySPIOnly.swift
224+
225+
extension Int {
226+
public var memberInPublicUsesOnlySPIOnly: Int { return self }
227+
}

0 commit comments

Comments
 (0)