Skip to content

Commit aca6046

Browse files
committed
AST/Sema: Make MemberImportVisibility a migratable feature.
The migration to `MemberImportVisibility` can be performed mechanically by adding missing import declarations, so offer automatic migration for the feature. Resolves rdar://151931597.
1 parent 522fee4 commit aca6046

15 files changed

+310
-30
lines changed

include/swift/AST/DiagnosticGroups.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ GROUP(ErrorInFutureSwiftVersion, "error-in-future-swift-version")
4949
GROUP(ExistentialAny, "existential-any")
5050
GROUP(ExistentialMemberAccess, "existential-member-access-limitations")
5151
GROUP(IsolatedConformances, "isolated-conformances")
52+
GROUP(MemberImportVisibility, "member-import-visibility")
5253
GROUP(MultipleInheritance, "multiple-inheritance")
5354
GROUP(MutableGlobalVariable, "mutable-global-variable")
5455
GROUP(NominalTypes, "nominal-types")

include/swift/AST/DiagnosticsSema.def

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,16 +167,21 @@ ERROR(init_candidate_inaccessible,none,
167167
"'%select{private|fileprivate|internal|package|@_spi|@_spi}1' protection level",
168168
(Type, AccessLevel))
169169

170-
ERROR(candidate_from_missing_import,none,
170+
GROUPED_ERROR(member_from_missing_import,MemberImportVisibility,none,
171171
"%kind0 is not available due to missing import of defining module %1",
172172
(const ValueDecl *, const ModuleDecl *))
173-
ERROR(candidate_from_missing_imports_2_or_more,none,
173+
GROUPED_ERROR(member_from_missing_imports_2_or_more,MemberImportVisibility,none,
174174
"%kind0 is not available due to missing imports of defining modules "
175175
"%2%select{ and|, }1 %3%select{|, and others}1",
176176
(const ValueDecl *, bool, const ModuleDecl *, const ModuleDecl *))
177177
NOTE(candidate_add_import,none,
178178
"add import of module %0", (const ModuleDecl *))
179179

180+
GROUPED_WARNING(add_required_import_for_member,MemberImportVisibility,none,
181+
"import of module %0 is required", (const ModuleDecl *))
182+
NOTE(decl_from_module_used_here,none,
183+
"%kind0 from %1 used here", (const ValueDecl *, const ModuleDecl *))
184+
180185
ERROR(cannot_pass_rvalue_mutating_subelement,none,
181186
"cannot use mutating member on immutable value: %0",
182187
(StringRef))

include/swift/Basic/Features.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ UPCOMING_FEATURE(GlobalActorIsolatedTypesUsability, 0434, 6)
287287
// Swift 7
288288
MIGRATABLE_UPCOMING_FEATURE(ExistentialAny, 335, 7)
289289
UPCOMING_FEATURE(InternalImportsByDefault, 409, 7)
290-
UPCOMING_FEATURE(MemberImportVisibility, 444, 7)
290+
MIGRATABLE_UPCOMING_FEATURE(MemberImportVisibility, 444, 7)
291291
MIGRATABLE_UPCOMING_FEATURE(InferIsolatedConformances, 470, 7)
292292
MIGRATABLE_UPCOMING_FEATURE(NonisolatedNonsendingByDefault, 461, 7)
293293

include/swift/Basic/LangOptions.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -873,13 +873,17 @@ namespace swift {
873873

874874
/// Returns whether the given feature is enabled.
875875
///
876-
/// If allowMigration is set, also returns true when the feature has been enabled for migration.
876+
/// If allowMigration is set, also returns true when the feature has been
877+
/// enabled for migration.
877878
bool hasFeature(Feature feature, bool allowMigration = false) const;
878879

879880
/// Returns whether a feature with the given name is enabled. Returns
880881
/// `false` if a feature by this name is not known.
881882
bool hasFeature(llvm::StringRef featureName) const;
882883

884+
/// Returns whether the given feature is enabled for migration.
885+
bool isMigratingToFeature(Feature feature) const;
886+
883887
/// Enables the given feature (enables in migration mode if `forMigration`
884888
/// is `true`).
885889
void enableFeature(Feature feature, bool forMigration = false);

lib/AST/NameLookup.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2371,7 +2371,9 @@ ObjCCategoryNameMap ClassDecl::getObjCCategoryNameMap() {
23712371
/// the given context.
23722372
static bool shouldRequireImportsInContext(const DeclContext *lookupContext) {
23732373
auto &ctx = lookupContext->getASTContext();
2374-
if (!ctx.LangOpts.hasFeature(Feature::MemberImportVisibility))
2374+
2375+
if (!ctx.LangOpts.hasFeature(Feature::MemberImportVisibility,
2376+
/*allowMigration=*/true))
23752377
return false;
23762378

23772379
// Code outside of the main module (which is often synthesized) isn't subject
@@ -3591,7 +3593,8 @@ ExtendedNominalRequest::evaluate(Evaluator &evaluator,
35913593
// inaccessible due to missing imports. The missing imports will be diagnosed
35923594
// elsewhere.
35933595
if (referenced.first.empty() &&
3594-
ctx.LangOpts.hasFeature(Feature::MemberImportVisibility)) {
3596+
ctx.LangOpts.hasFeature(Feature::MemberImportVisibility,
3597+
/*allowMigration=*/true)) {
35953598
options |= DirectlyReferencedTypeLookupFlags::IgnoreMissingImports;
35963599
referenced = directReferencesForTypeRepr(evaluator, ctx, typeRepr,
35973600
ext->getParent(), options);

lib/Basic/LangOptions.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,10 @@ bool LangOptions::hasFeature(llvm::StringRef featureName) const {
363363
return false;
364364
}
365365

366+
bool LangOptions::isMigratingToFeature(Feature feature) const {
367+
return featureStates.getState(feature).isEnabledForMigration();
368+
}
369+
366370
void LangOptions::enableFeature(Feature feature, bool forMigration) {
367371
if (forMigration) {
368372
ASSERT(feature.isMigratable());

lib/Basic/SupportedFeatures.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ static std::vector<DiagGroupID> migratableCategories(Feature feature) {
3535
return { DiagGroupID::ExistentialAny };
3636
case Feature::InnerKind::InferIsolatedConformances:
3737
return { DiagGroupID::IsolatedConformances };
38+
case Feature::InnerKind::MemberImportVisibility:
39+
return { DiagGroupID::MemberImportVisibility };
3840
case Feature::InnerKind::NonisolatedNonsendingByDefault:
3941
return { DiagGroupID::NonisolatedNonsendingByDefault };
4042
case Feature::InnerKind::StrictMemorySafety:

lib/Sema/MiscDiagnostics.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6322,7 +6322,8 @@ static void diagnoseMissingMemberImports(const Expr *E, const DeclContext *DC) {
63226322
};
63236323

63246324
auto &ctx = DC->getASTContext();
6325-
if (!ctx.LangOpts.hasFeature(Feature::MemberImportVisibility))
6325+
if (!ctx.LangOpts.hasFeature(Feature::MemberImportVisibility,
6326+
/*allowMigration=*/true))
63266327
return;
63276328

63286329
DiagnoseWalker walker(DC);

lib/Sema/ResilienceDiagnostics.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,9 @@ static bool diagnoseValueDeclRefExportability(SourceLoc loc, const ValueDecl *D,
279279
// Some diagnostics emitted with the `MemberImportVisibility` feature enabled
280280
// subsume these diagnostics.
281281
if (originKind == DisallowedOriginKind::MissingImport &&
282-
ctx.LangOpts.hasFeature(Feature::MemberImportVisibility) && SF)
282+
ctx.LangOpts.hasFeature(Feature::MemberImportVisibility,
283+
/*allowMigration=*/true) &&
284+
SF)
283285
return false;
284286

285287
if (auto accessor = dyn_cast<AccessorDecl>(D)) {

lib/Sema/TypeCheckDeclOverride.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1532,7 +1532,8 @@ bool swift::checkOverrides(ValueDecl *decl) {
15321532

15331533
auto &ctx = decl->getASTContext();
15341534
if (overridden.empty() &&
1535-
ctx.LangOpts.hasFeature(Feature::MemberImportVisibility)) {
1535+
ctx.LangOpts.hasFeature(Feature::MemberImportVisibility,
1536+
/*allowMigration=*/true)) {
15361537
// If we didn't find anything, try broadening the search by ignoring missing
15371538
// imports.
15381539
if (!checkPotentialOverrides(decl, overridden,
@@ -2529,7 +2530,8 @@ OverriddenDeclsRequest::evaluate(Evaluator &evaluator, ValueDecl *decl) const {
25292530
// If we didn't find anything, try broadening the search by ignoring missing
25302531
// imports.
25312532
if (overridden.empty() &&
2532-
ctx.LangOpts.hasFeature(Feature::MemberImportVisibility)) {
2533+
ctx.LangOpts.hasFeature(Feature::MemberImportVisibility,
2534+
/*allowMigration=*/true)) {
25332535
overridden = computeOverriddenDecls(decl, true);
25342536
if (!overridden.empty()) {
25352537
auto first = overridden.front();

0 commit comments

Comments
 (0)