Skip to content

Commit 94a34f7

Browse files
committed
Sema: Only add @_spiOnly to import fix-its with a public access level.
It doesn't make sense to add the `@_spiOnly` attribute to an `internal` import. Part of rdar://126637855.
1 parent 00cac25 commit 94a34f7

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
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: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
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
1114
// RUN: %target-swift-frontend -typecheck -verify -swift-version 5 \
1215
// RUN: -primary-file %t/function_bodies.swift \
1316
// RUN: -primary-file %t/function_signatures_unqualified.swift \
@@ -28,12 +31,18 @@ import Swift // Just here to anchor the fix-its
2831
// expected-note@-4 {{add import of module 'PublicUsesOnlyDefaultedImport'}}{{1-1=import PublicUsesOnlyDefaultedImport\n}}
2932
// expected-note@-5 3 {{add import of module 'MixedUses'}}{{1-1=internal import MixedUses\n}}
3033
// expected-note@-6 {{add import of module 'InternalUsesOnlyTransitivelyImported'}}{{1-1=internal import InternalUsesOnlyTransitivelyImported\n}}
34+
// expected-note@-7 {{add import of module 'InternalUsesOnlySPIOnly'}}{{1-1=internal import InternalUsesOnlySPIOnly\n}}
35+
// expected-note@-8 {{add import of module 'InternalUsesOnlyDefaultedImportSPIOnly'}}{{1-1=@_spiOnly import InternalUsesOnlyDefaultedImportSPIOnly\n}}
36+
// expected-note@-9 {{add import of module 'PublicUsesOnlySPIOnly'}}{{1-1=internal import PublicUsesOnlySPIOnly\n}}
37+
3138

3239
func internalFunc(_ x: Int) {
3340
_ = x.memberInInternalUsesOnly // expected-error {{property 'memberInInternalUsesOnly' is not available due to missing import of defining module 'InternalUsesOnly'}}
3441
_ = x.memberInInternalUsesOnlyDefaultedImport // expected-error {{property 'memberInInternalUsesOnlyDefaultedImport' is not available due to missing import of defining module 'InternalUsesOnlyDefaultedImport'}}
3542
_ = x.memberInMixedUses // expected-error {{property 'memberInMixedUses' is not available due to missing import of defining module 'MixedUses'}}
3643
_ = x.memberInInternalUsesOnlyTransitivelyImported // expected-error {{property 'memberInInternalUsesOnlyTransitivelyImported' is not available due to missing import of defining module 'InternalUsesOnlyTransitivelyImported'}}
44+
_ = x.memberInInternalUsesOnlySPIOnly // expected-error {{property 'memberInInternalUsesOnlySPIOnly' is not available due to missing import of defining module 'InternalUsesOnlySPIOnly'}}
45+
_ = x.memberInInternalUsesOnlyDefaultedImportSPIOnly // expected-error {{property 'memberInInternalUsesOnlyDefaultedImportSPIOnly' is not available due to missing import of defining module 'InternalUsesOnlyDefaultedImportSPIOnly'}}
3746
}
3847

3948
@inlinable package func packageInlinableFunc(_ x: Int) {
@@ -45,6 +54,7 @@ func internalFunc(_ x: Int) {
4554
_ = x.memberInPublicUsesOnly // expected-error {{property 'memberInPublicUsesOnly' is not available due to missing import of defining module 'PublicUsesOnly'}}
4655
_ = x.memberInPublicUsesOnlyDefaultedImport // expected-error {{property 'memberInPublicUsesOnlyDefaultedImport' is not available due to missing import of defining module 'PublicUsesOnlyDefaultedImport'}}
4756
_ = x.memberInMixedUses // expected-error {{property 'memberInMixedUses' is not available due to missing import of defining module 'MixedUses'}}
57+
_ = x.memberInPublicUsesOnlySPIOnly // expected-error {{property 'memberInPublicUsesOnlySPIOnly' is not available due to missing import of defining module 'PublicUsesOnlySPIOnly'}}
4858
}
4959

5060
//--- function_signatures_unqualified.swift
@@ -120,6 +130,9 @@ internal import PublicUsesOnly
120130
import PublicUsesOnlyDefaultedImport
121131
internal import MixedUses
122132
internal import Exports
133+
@_spiOnly public import InternalUsesOnlySPIOnly
134+
@_spiOnly import InternalUsesOnlyDefaultedImportSPIOnly
135+
@_spiOnly public import PublicUsesOnlySPIOnly
123136

124137
//--- InternalUsesOnly.swift
125138

@@ -180,3 +193,21 @@ extension Int {
180193
//--- Exports.swift
181194

182195
@_exported import InternalUsesOnlyTransitivelyImported
196+
197+
//--- InternalUsesOnlySPIOnly.swift
198+
199+
extension Int {
200+
public var memberInInternalUsesOnlySPIOnly: Int { return self }
201+
}
202+
203+
//--- InternalUsesOnlyDefaultedImportSPIOnly.swift
204+
205+
extension Int {
206+
public var memberInInternalUsesOnlyDefaultedImportSPIOnly: Int { return self }
207+
}
208+
209+
//--- PublicUsesOnlySPIOnly.swift
210+
211+
extension Int {
212+
public var memberInPublicUsesOnlySPIOnly: Int { return self }
213+
}

0 commit comments

Comments
 (0)