Skip to content

Commit 2ab55a8

Browse files
committed
Sema: Omit internal from MemberImportVisibility fix-its when appropriate.
Omit an explicit access level from `MemberImportVisibility` fix-its under the following conditions: - `InternalImportsByDefault` is enabled. - The required import needs an `internal` access level or lower. - The module is not yet imported explicitly in any other file. Resolves rdar://149577615.
1 parent 85cf35c commit 2ab55a8

File tree

2 files changed

+42
-14
lines changed

2 files changed

+42
-14
lines changed

lib/Sema/TypeCheckNameLookup.cpp

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,7 @@ struct MissingImportFixItInfo {
850850
class MissingImportFixItCache {
851851
SourceFile &sf;
852852
llvm::DenseMap<const ModuleDecl *, MissingImportFixItInfo> infos;
853+
bool internalImportsByDefaultEnabled;
853854

854855
MissingImportFixItInfo getFixItInfo(ModuleDecl *mod) {
855856
auto existing = infos.find(mod);
@@ -881,15 +882,29 @@ class MissingImportFixItCache {
881882

882883
// Add an appropriate access level as long as it would not conflict with
883884
// existing imports that lack access levels.
884-
if (!foundImport || anyImportHasAccessLevel)
885-
info.accessLevel = sf.getMaxAccessLevelUsingImport(mod);
885+
auto accessLevelForImport = [&]() -> std::optional<AccessLevel> {
886+
auto maxAccessLevel = sf.getMaxAccessLevelUsingImport(mod);
887+
if (internalImportsByDefaultEnabled) {
888+
if (!foundImport && maxAccessLevel <= AccessLevel::Internal)
889+
return std::nullopt;
890+
}
891+
892+
if (foundImport && !anyImportHasAccessLevel)
893+
return std::nullopt;
886894

895+
return maxAccessLevel;
896+
};
897+
898+
info.accessLevel = accessLevelForImport();
887899
infos[mod] = info;
888900
return info;
889901
}
890902

891903
public:
892-
MissingImportFixItCache(SourceFile &sf) : sf(sf) {};
904+
MissingImportFixItCache(SourceFile &sf) : sf(sf) {
905+
internalImportsByDefaultEnabled = sf.getASTContext().LangOpts.hasFeature(
906+
Feature::InternalImportsByDefault);
907+
};
893908

894909
std::pair<SmallVector<ModuleDecl *, 2>,
895910
SmallVector<MissingImportFixItInfo, 2>>

test/NameLookup/member_import_visibility_multifile_access_level.swift

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
// RUN: %target-swift-frontend -emit-module -o %t %t/PublicUsesOnly.swift
77
// RUN: %target-swift-frontend -emit-module -o %t %t/PublicUsesOnlyDefaultedImport.swift
88
// RUN: %target-swift-frontend -emit-module -o %t %t/MixedUses.swift
9+
// RUN: %target-swift-frontend -emit-module -o %t %t/InternalUsesOnlyReexported.swift
910
// RUN: %target-swift-frontend -emit-module -o %t %t/InternalUsesOnlyTransitivelyImported.swift
10-
// RUN: %target-swift-frontend -emit-module -o %t %t/Exports.swift -I %t
11+
// RUN: %target-swift-frontend -emit-module -o %t %t/ImportsOtherModules.swift -I %t
1112
// RUN: %target-swift-frontend -emit-module -o %t %t/InternalUsesOnlySPIOnly.swift -I %t
1213
// RUN: %target-swift-frontend -emit-module -o %t %t/InternalUsesOnlyDefaultedImportSPIOnly.swift -I %t
1314
// RUN: %target-swift-frontend -emit-module -o %t %t/PublicUsesOnlySPIOnly.swift -I %t
@@ -46,20 +47,23 @@ import Swift // Just here to anchor the fix-its
4647
// expected-note@-3 {{add import of module 'PublicUsesOnly'}}{{1-1=public import PublicUsesOnly\n}}
4748
// expected-note@-4 {{add import of module 'PublicUsesOnlyDefaultedImport'}}{{1-1=import PublicUsesOnlyDefaultedImport\n}}
4849
// expected-note@-5 3 {{add import of module 'MixedUses'}}{{1-1=public import MixedUses\n}}
49-
// expected-note@-6 {{add import of module 'InternalUsesOnlyTransitivelyImported'}}{{1-1=internal import InternalUsesOnlyTransitivelyImported\n}}
50-
// expected-note@-7 {{add import of module 'InternalUsesOnlySPIOnly'}}{{1-1=internal import InternalUsesOnlySPIOnly\n}}
51-
// expected-public-by-default-note@-8 {{add import of module 'InternalUsesOnlyDefaultedImportSPIOnly'}}{{1-1=@_spiOnly import InternalUsesOnlyDefaultedImportSPIOnly\n}}
52-
// expected-internal-by-default-note@-9 {{add import of module 'InternalUsesOnlyDefaultedImportSPIOnly'}}{{1-1=import InternalUsesOnlyDefaultedImportSPIOnly\n}}
53-
// expected-note@-10 {{add import of module 'PublicUsesOnlySPIOnly'}}{{1-1=@_spiOnly public import PublicUsesOnlySPIOnly\n}}
54-
50+
// expected-public-by-default-note@-6 {{add import of module 'InternalUsesOnlyReexported'}}{{1-1=internal import InternalUsesOnlyReexported\n}}
51+
// expected-internal-by-default-note@-7 {{add import of module 'InternalUsesOnlyReexported'}}{{1-1=import InternalUsesOnlyReexported\n}}
52+
// expected-public-by-default-note@-8 {{add import of module 'InternalUsesOnlyTransitivelyImported'}}{{1-1=internal import InternalUsesOnlyTransitivelyImported\n}}
53+
// expected-internal-by-default-note@-9 {{add import of module 'InternalUsesOnlyTransitivelyImported'}}{{1-1=import InternalUsesOnlyTransitivelyImported\n}}
54+
// expected-note@-10 {{add import of module 'InternalUsesOnlySPIOnly'}}{{1-1=internal import InternalUsesOnlySPIOnly\n}}
55+
// expected-public-by-default-note@-11 {{add import of module 'InternalUsesOnlyDefaultedImportSPIOnly'}}{{1-1=@_spiOnly import InternalUsesOnlyDefaultedImportSPIOnly\n}}
56+
// expected-internal-by-default-note@-12 {{add import of module 'InternalUsesOnlyDefaultedImportSPIOnly'}}{{1-1=import InternalUsesOnlyDefaultedImportSPIOnly\n}}
57+
// expected-note@-13 {{add import of module 'PublicUsesOnlySPIOnly'}}{{1-1=@_spiOnly public import PublicUsesOnlySPIOnly\n}}
5558

5659
func internalFunc(_ x: Int) {
5760
_ = x.memberInInternalUsesOnly // expected-error {{property 'memberInInternalUsesOnly' is not available due to missing import of defining module 'InternalUsesOnly'}}
5861
_ = x.memberInInternalUsesOnlyDefaultedImport // expected-error {{property 'memberInInternalUsesOnlyDefaultedImport' is not available due to missing import of defining module 'InternalUsesOnlyDefaultedImport'}}
5962
_ = x.memberInMixedUses // expected-error {{property 'memberInMixedUses' is not available due to missing import of defining module 'MixedUses'}}
60-
_ = x.memberInInternalUsesOnlyTransitivelyImported // expected-error {{property 'memberInInternalUsesOnlyTransitivelyImported' is not available due to missing import of defining module 'InternalUsesOnlyTransitivelyImported'}}
63+
_ = x.memberInInternalUsesOnlyReexported // expected-error {{property 'memberInInternalUsesOnlyReexported' is not available due to missing import of defining module 'InternalUsesOnlyReexported'}}
6164
_ = x.memberInInternalUsesOnlySPIOnly // expected-error {{property 'memberInInternalUsesOnlySPIOnly' is not available due to missing import of defining module 'InternalUsesOnlySPIOnly'}}
6265
_ = x.memberInInternalUsesOnlyDefaultedImportSPIOnly // expected-error {{property 'memberInInternalUsesOnlyDefaultedImportSPIOnly' is not available due to missing import of defining module 'InternalUsesOnlyDefaultedImportSPIOnly'}}
66+
_ = x.memberInInternalUsesOnlyTransitivelyImported // expected-error {{property 'memberInInternalUsesOnlyTransitivelyImported' is not available due to missing import of defining module 'InternalUsesOnlyTransitivelyImported'}}
6367
}
6468

6569
@inlinable package func packageInlinableFunc(_ x: Int) {
@@ -146,7 +150,7 @@ internal import PackageUsesOnly
146150
internal import PublicUsesOnly
147151
import PublicUsesOnlyDefaultedImport
148152
internal import MixedUses
149-
internal import Exports
153+
internal import ImportsOtherModules
150154
@_spiOnly public import InternalUsesOnlySPIOnly
151155
@_spiOnly import InternalUsesOnlyDefaultedImportSPIOnly
152156
@_spiOnly public import PublicUsesOnlySPIOnly
@@ -199,6 +203,14 @@ extension Int {
199203
public var memberInMixedUses: Int { return self }
200204
}
201205

206+
//--- InternalUsesOnlyReexported.swift
207+
208+
extension Int {
209+
public typealias TypealiasInInternalUsesOnlyReexported = Self
210+
public struct NestedInInternalUsesOnlyReexported {}
211+
public var memberInInternalUsesOnlyReexported: Int { return self }
212+
}
213+
202214
//--- InternalUsesOnlyTransitivelyImported.swift
203215

204216
extension Int {
@@ -207,9 +219,10 @@ extension Int {
207219
public var memberInInternalUsesOnlyTransitivelyImported: Int { return self }
208220
}
209221

210-
//--- Exports.swift
222+
//--- ImportsOtherModules.swift
211223

212-
@_exported import InternalUsesOnlyTransitivelyImported
224+
@_exported import InternalUsesOnlyReexported
225+
import InternalUsesOnlyTransitivelyImported
213226

214227
//--- InternalUsesOnlySPIOnly.swift
215228

0 commit comments

Comments
 (0)