Skip to content

AST/Sema: Make MemberImportVisibility a migratable feature #81751

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions include/swift/AST/DiagnosticEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -475,9 +475,6 @@ namespace swift {

static const char *fixItStringFor(const FixItID id);

/// Get the best location where an 'import' fixit might be offered.
SourceLoc getBestAddImportFixItLoc(const Decl *Member) const;

/// Add a token-based replacement fix-it to the currently-active
/// diagnostic.
template <typename... ArgTypes>
Expand Down Expand Up @@ -1195,11 +1192,7 @@ namespace swift {
SourceLoc getDefaultDiagnosticLoc() const {
return bufferIndirectlyCausingDiagnostic;
}
SourceLoc getBestAddImportFixItLoc(const Decl *Member,
SourceFile *sourceFile) const;
SourceLoc getBestAddImportFixItLoc(const Decl *Member) const {
return getBestAddImportFixItLoc(Member, nullptr);
}
SourceLoc getBestAddImportFixItLoc(SourceFile *sf) const;
};

inline SourceManager &InFlightDiagnostic::getSourceManager() {
Expand Down
1 change: 1 addition & 0 deletions include/swift/AST/DiagnosticGroups.def
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ GROUP(ErrorInFutureSwiftVersion, "error-in-future-swift-version")
GROUP(ExistentialAny, "existential-any")
GROUP(ExistentialMemberAccess, "existential-member-access-limitations")
GROUP(IsolatedConformances, "isolated-conformances")
GROUP(MemberImportVisibility, "member-import-visibility")
GROUP(MultipleInheritance, "multiple-inheritance")
GROUP(MutableGlobalVariable, "mutable-global-variable")
GROUP(NominalTypes, "nominal-types")
Expand Down
9 changes: 7 additions & 2 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -167,16 +167,21 @@ ERROR(init_candidate_inaccessible,none,
"'%select{private|fileprivate|internal|package|@_spi|@_spi}1' protection level",
(Type, AccessLevel))

ERROR(candidate_from_missing_import,none,
GROUPED_ERROR(member_from_missing_import,MemberImportVisibility,none,
"%kind0 is not available due to missing import of defining module %1",
(const ValueDecl *, const ModuleDecl *))
ERROR(candidate_from_missing_imports_2_or_more,none,
GROUPED_ERROR(member_from_missing_imports_2_or_more,MemberImportVisibility,none,
"%kind0 is not available due to missing imports of defining modules "
"%2%select{ and|, }1 %3%select{|, and others}1",
(const ValueDecl *, bool, const ModuleDecl *, const ModuleDecl *))
NOTE(candidate_add_import,none,
"add import of module %0", (const ModuleDecl *))

GROUPED_WARNING(add_required_import_for_member,MemberImportVisibility,none,
"import of module %0 is required", (const ModuleDecl *))
NOTE(decl_from_module_used_here,none,
"%kind0 from %1 used here", (const ValueDecl *, const ModuleDecl *))

ERROR(cannot_pass_rvalue_mutating_subelement,none,
"cannot use mutating member on immutable value: %0",
(StringRef))
Expand Down
2 changes: 1 addition & 1 deletion include/swift/Basic/Features.def
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ UPCOMING_FEATURE(GlobalActorIsolatedTypesUsability, 0434, 6)
// Swift 7
MIGRATABLE_UPCOMING_FEATURE(ExistentialAny, 335, 7)
UPCOMING_FEATURE(InternalImportsByDefault, 409, 7)
UPCOMING_FEATURE(MemberImportVisibility, 444, 7)
MIGRATABLE_UPCOMING_FEATURE(MemberImportVisibility, 444, 7)
MIGRATABLE_UPCOMING_FEATURE(InferIsolatedConformances, 470, 7)
MIGRATABLE_UPCOMING_FEATURE(NonisolatedNonsendingByDefault, 461, 7)

Expand Down
6 changes: 5 additions & 1 deletion include/swift/Basic/LangOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -873,13 +873,17 @@ namespace swift {

/// Returns whether the given feature is enabled.
///
/// If allowMigration is set, also returns true when the feature has been enabled for migration.
/// If allowMigration is set, also returns true when the feature has been
/// enabled for migration.
bool hasFeature(Feature feature, bool allowMigration = false) const;

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

/// Returns whether the given feature is enabled for migration.
bool isMigratingToFeature(Feature feature) const;

/// Enables the given feature (enables in migration mode if `forMigration`
/// is `true`).
void enableFeature(Feature feature, bool forMigration = false);
Expand Down
43 changes: 14 additions & 29 deletions lib/AST/DiagnosticEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,18 +313,12 @@ InFlightDiagnostic::fixItReplaceChars(SourceLoc Start, SourceLoc End,
return *this;
}

SourceLoc
DiagnosticEngine::getBestAddImportFixItLoc(const Decl *Member,
SourceFile *sourceFile) const {
SourceLoc DiagnosticEngine::getBestAddImportFixItLoc(SourceFile *SF) const {
auto &SM = SourceMgr;

SourceLoc bestLoc;

auto SF =
sourceFile ? sourceFile : Member->getDeclContext()->getParentSourceFile();
if (!SF) {
if (!SF)
return bestLoc;
}

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

InFlightDiagnostic &InFlightDiagnostic::fixItAddImport(StringRef ModuleName) {
assert(IsActive && "Cannot modify an inactive diagnostic");
auto Member = Engine->ActiveDiagnostic->getDecl();
SourceLoc bestLoc = Engine->getBestAddImportFixItLoc(Member);

if (bestLoc.isValid()) {
llvm::SmallString<64> importText;

// @_spi imports.
if (Member->isSPI()) {
auto spiGroups = Member->getSPIGroups();
if (!spiGroups.empty()) {
importText += "@_spi(";
importText += spiGroups[0].str();
importText += ") ";
}
}
auto decl = Engine->ActiveDiagnostic->getDecl();
if (!decl)
return *this;

importText += "import ";
importText += ModuleName;
importText += "\n";
auto bestLoc = Engine->getBestAddImportFixItLoc(
decl->getDeclContext()->getOutermostParentSourceFile());
if (!bestLoc.isValid())
return *this;

return fixItInsert(bestLoc, importText);
}
llvm::SmallString<64> importText;
importText += "import ";
importText += ModuleName;
importText += "\n";

return *this;
return fixItInsert(bestLoc, importText);
}

InFlightDiagnostic &
Expand Down
7 changes: 5 additions & 2 deletions lib/AST/NameLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2371,7 +2371,9 @@ ObjCCategoryNameMap ClassDecl::getObjCCategoryNameMap() {
/// the given context.
static bool shouldRequireImportsInContext(const DeclContext *lookupContext) {
auto &ctx = lookupContext->getASTContext();
if (!ctx.LangOpts.hasFeature(Feature::MemberImportVisibility))

if (!ctx.LangOpts.hasFeature(Feature::MemberImportVisibility,
/*allowMigration=*/true))
return false;

// Code outside of the main module (which is often synthesized) isn't subject
Expand Down Expand Up @@ -3591,7 +3593,8 @@ ExtendedNominalRequest::evaluate(Evaluator &evaluator,
// inaccessible due to missing imports. The missing imports will be diagnosed
// elsewhere.
if (referenced.first.empty() &&
ctx.LangOpts.hasFeature(Feature::MemberImportVisibility)) {
ctx.LangOpts.hasFeature(Feature::MemberImportVisibility,
/*allowMigration=*/true)) {
options |= DirectlyReferencedTypeLookupFlags::IgnoreMissingImports;
referenced = directReferencesForTypeRepr(evaluator, ctx, typeRepr,
ext->getParent(), options);
Expand Down
10 changes: 8 additions & 2 deletions lib/Basic/LangOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -340,8 +340,10 @@ bool LangOptions::hasFeature(Feature feature, bool allowMigration) const {
if (state.isEnabled())
return true;

if (auto version = feature.getLanguageVersion())
return isSwiftVersionAtLeast(*version);
if (auto version = feature.getLanguageVersion()) {
if (isSwiftVersionAtLeast(*version))
return true;
}

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

bool LangOptions::isMigratingToFeature(Feature feature) const {
return featureStates.getState(feature).isEnabledForMigration();
}

void LangOptions::enableFeature(Feature feature, bool forMigration) {
if (forMigration) {
ASSERT(feature.isMigratable());
Expand Down
2 changes: 2 additions & 0 deletions lib/Basic/SupportedFeatures.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ static std::vector<DiagGroupID> migratableCategories(Feature feature) {
return { DiagGroupID::ExistentialAny };
case Feature::InnerKind::InferIsolatedConformances:
return { DiagGroupID::IsolatedConformances };
case Feature::InnerKind::MemberImportVisibility:
return { DiagGroupID::MemberImportVisibility };
case Feature::InnerKind::NonisolatedNonsendingByDefault:
return { DiagGroupID::NonisolatedNonsendingByDefault };
case Feature::InnerKind::StrictMemorySafety:
Expand Down
3 changes: 2 additions & 1 deletion lib/Sema/MiscDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6322,7 +6322,8 @@ static void diagnoseMissingMemberImports(const Expr *E, const DeclContext *DC) {
};

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

DiagnoseWalker walker(DC);
Expand Down
4 changes: 3 additions & 1 deletion lib/Sema/ResilienceDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,9 @@ static bool diagnoseValueDeclRefExportability(SourceLoc loc, const ValueDecl *D,
// Some diagnostics emitted with the `MemberImportVisibility` feature enabled
// subsume these diagnostics.
if (originKind == DisallowedOriginKind::MissingImport &&
ctx.LangOpts.hasFeature(Feature::MemberImportVisibility) && SF)
ctx.LangOpts.hasFeature(Feature::MemberImportVisibility,
/*allowMigration=*/true) &&
SF)
return false;

if (auto accessor = dyn_cast<AccessorDecl>(D)) {
Expand Down
6 changes: 4 additions & 2 deletions lib/Sema/TypeCheckDeclOverride.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1532,7 +1532,8 @@ bool swift::checkOverrides(ValueDecl *decl) {

auto &ctx = decl->getASTContext();
if (overridden.empty() &&
ctx.LangOpts.hasFeature(Feature::MemberImportVisibility)) {
ctx.LangOpts.hasFeature(Feature::MemberImportVisibility,
/*allowMigration=*/true)) {
// If we didn't find anything, try broadening the search by ignoring missing
// imports.
if (!checkPotentialOverrides(decl, overridden,
Expand Down Expand Up @@ -2529,7 +2530,8 @@ OverriddenDeclsRequest::evaluate(Evaluator &evaluator, ValueDecl *decl) const {
// If we didn't find anything, try broadening the search by ignoring missing
// imports.
if (overridden.empty() &&
ctx.LangOpts.hasFeature(Feature::MemberImportVisibility)) {
ctx.LangOpts.hasFeature(Feature::MemberImportVisibility,
/*allowMigration=*/true)) {
overridden = computeOverriddenDecls(decl, true);
if (!overridden.empty()) {
auto first = overridden.front();
Expand Down
Loading