Skip to content

Commit d5ef256

Browse files
authored
Merge pull request #81751 from tshortli/migratable-member-import-visibility
2 parents ee41fa5 + aca6046 commit d5ef256

21 files changed

+342
-81
lines changed

include/swift/AST/DiagnosticEngine.h

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -475,9 +475,6 @@ namespace swift {
475475

476476
static const char *fixItStringFor(const FixItID id);
477477

478-
/// Get the best location where an 'import' fixit might be offered.
479-
SourceLoc getBestAddImportFixItLoc(const Decl *Member) const;
480-
481478
/// Add a token-based replacement fix-it to the currently-active
482479
/// diagnostic.
483480
template <typename... ArgTypes>
@@ -1195,11 +1192,7 @@ namespace swift {
11951192
SourceLoc getDefaultDiagnosticLoc() const {
11961193
return bufferIndirectlyCausingDiagnostic;
11971194
}
1198-
SourceLoc getBestAddImportFixItLoc(const Decl *Member,
1199-
SourceFile *sourceFile) const;
1200-
SourceLoc getBestAddImportFixItLoc(const Decl *Member) const {
1201-
return getBestAddImportFixItLoc(Member, nullptr);
1202-
}
1195+
SourceLoc getBestAddImportFixItLoc(SourceFile *sf) const;
12031196
};
12041197

12051198
inline SourceManager &InFlightDiagnostic::getSourceManager() {

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/DiagnosticEngine.cpp

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -313,18 +313,12 @@ InFlightDiagnostic::fixItReplaceChars(SourceLoc Start, SourceLoc End,
313313
return *this;
314314
}
315315

316-
SourceLoc
317-
DiagnosticEngine::getBestAddImportFixItLoc(const Decl *Member,
318-
SourceFile *sourceFile) const {
316+
SourceLoc DiagnosticEngine::getBestAddImportFixItLoc(SourceFile *SF) const {
319317
auto &SM = SourceMgr;
320318

321319
SourceLoc bestLoc;
322-
323-
auto SF =
324-
sourceFile ? sourceFile : Member->getDeclContext()->getParentSourceFile();
325-
if (!SF) {
320+
if (!SF)
326321
return bestLoc;
327-
}
328322

329323
for (auto item : SF->getTopLevelItems()) {
330324
// If we found an import declaration, we want to insert after it.
@@ -356,30 +350,21 @@ DiagnosticEngine::getBestAddImportFixItLoc(const Decl *Member,
356350

357351
InFlightDiagnostic &InFlightDiagnostic::fixItAddImport(StringRef ModuleName) {
358352
assert(IsActive && "Cannot modify an inactive diagnostic");
359-
auto Member = Engine->ActiveDiagnostic->getDecl();
360-
SourceLoc bestLoc = Engine->getBestAddImportFixItLoc(Member);
361-
362-
if (bestLoc.isValid()) {
363-
llvm::SmallString<64> importText;
364-
365-
// @_spi imports.
366-
if (Member->isSPI()) {
367-
auto spiGroups = Member->getSPIGroups();
368-
if (!spiGroups.empty()) {
369-
importText += "@_spi(";
370-
importText += spiGroups[0].str();
371-
importText += ") ";
372-
}
373-
}
353+
auto decl = Engine->ActiveDiagnostic->getDecl();
354+
if (!decl)
355+
return *this;
374356

375-
importText += "import ";
376-
importText += ModuleName;
377-
importText += "\n";
357+
auto bestLoc = Engine->getBestAddImportFixItLoc(
358+
decl->getDeclContext()->getOutermostParentSourceFile());
359+
if (!bestLoc.isValid())
360+
return *this;
378361

379-
return fixItInsert(bestLoc, importText);
380-
}
362+
llvm::SmallString<64> importText;
363+
importText += "import ";
364+
importText += ModuleName;
365+
importText += "\n";
381366

382-
return *this;
367+
return fixItInsert(bestLoc, importText);
383368
}
384369

385370
InFlightDiagnostic &

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: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,8 +340,10 @@ bool LangOptions::hasFeature(Feature feature, bool allowMigration) const {
340340
if (state.isEnabled())
341341
return true;
342342

343-
if (auto version = feature.getLanguageVersion())
344-
return isSwiftVersionAtLeast(*version);
343+
if (auto version = feature.getLanguageVersion()) {
344+
if (isSwiftVersionAtLeast(*version))
345+
return true;
346+
}
345347

346348
if (allowMigration && state.isEnabledForMigration())
347349
return true;
@@ -361,6 +363,10 @@ bool LangOptions::hasFeature(llvm::StringRef featureName) const {
361363
return false;
362364
}
363365

366+
bool LangOptions::isMigratingToFeature(Feature feature) const {
367+
return featureStates.getState(feature).isEnabledForMigration();
368+
}
369+
364370
void LangOptions::enableFeature(Feature feature, bool forMigration) {
365371
if (forMigration) {
366372
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)