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

Conversation

tshortli
Copy link
Contributor

@tshortli tshortli commented May 23, 2025

The migration to MemberImportVisibility can be performed mechanically by adding missing import declarations, so offer automatic migration for the feature.

Also fixes a bug in the migratable feature infrastructure where hasFeature() would return false incorrectly when a feature is enabled for migration.

Resolves rdar://151931597.

@tshortli
Copy link
Contributor Author

@swift-ci please smoke test

@tshortli
Copy link
Contributor Author

@xedin @AnthonyLatsis @DougGregor I'd appreciate a review as I plan to cherry-pick this to release/6.2.

@tshortli tshortli force-pushed the migratable-member-import-visibility branch from 34369c8 to c7cb8ef Compare May 27, 2025 19:32
@tshortli
Copy link
Contributor Author

@swift-ci please smoke test

@tshortli tshortli requested review from xymus, beccadax and artemcm May 27, 2025 19:35
diagnoseMissingImportsForMember(const ValueDecl *decl,
SmallVectorImpl<ModuleDecl *> &modulesToImport,
SourceFile *sf, SourceLoc loc) {
auto &ctx = sf->getASTContext();
auto count = modulesToImport.size();
ASSERT(count > 0);

bool isMigrating =
ctx.LangOpts.getFeatureState(Feature::MemberImportVisibility)
.isEnabledForMigration();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AnthonyLatsis is it currently possible to coalesce the fix-it from multiple different diagnostics (possibilities here are - if I understand correctly - the same id but different arguments, locations, different ids), or we'd just end up adding multiple import <<module>> from that?

Copy link
Contributor

@xedin xedin May 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd expect we add a walker to detect this similarly to our NonisolatedNonsending one and keep track of unique imports we need to introduce.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, so you think I'd need to change this to emit a single warning per import that needs to be added?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, something like that, let's wait for @AnthonyLatsis to confirm but I have a suspicion that we might not be able to coalesce the fix-its...

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the same id but different arguments, locations, different ids), or we'd just end up adding multiple import <<module>> from that?

Yes, we always let primary diagnostics with different locations or messages through, together with their notes. The swift-syntax FixItApplier does not filter out equal insertions either. It totally makes sense to diagnose — and thus point out — all sites that require an import, even if they share the same fix-it. I think we need to resolve this on the tooling side instead of emitting a single, detached diagnostic per fix-it just to help out the tooling.

The question is, can we come up with a counterexample to the conjecture that discarding an insertion (location, replacement text) that we’ve already seen is always the right move?

Copy link
Contributor

@xedin xedin May 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking that we could have an error with a fix-it per file that has a note per use site in that file, this way we preserve all of the locations and don’t have to coalesce anything.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I could invert the diagnostics that way for the purposes of migration only. I'll explore implementing it that way, thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've implemented the suggested approach where in migration mode one warning is emitted per module that needs to be imported. Even if the migration infrastructure can handle the duplicate diagnostics that were emitted previously, I think this is going to yield the best developer experience.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you!

Copy link
Collaborator

@AnthonyLatsis AnthonyLatsis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Thanks, Allan.

tshortli added 4 commits May 30, 2025 15:34
The was never invoked because inaccessibility due to SPI protection level is
always diagnosed before missing imports are diagnosed. The functionality could
therefore not be tested and should be removed.
…ItLoc()`.

For clarity, it should just take a `SourceFile`.
Don't skip checking if a feature is enabled for migration when the feature also
has an associated language version.
The migration to `MemberImportVisibility` can be performed mechanically by
adding missing import declarations, so offer automatic migration for the
feature.

Resolves rdar://151931597.
@tshortli tshortli force-pushed the migratable-member-import-visibility branch from c7cb8ef to aca6046 Compare May 30, 2025 22:34
@tshortli
Copy link
Contributor Author

@swift-ci please smoke test

@tshortli
Copy link
Contributor Author

There have been significant updates, so please take a look again if you reviewed this previously.

Copy link
Member

@DougGregor DougGregor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks fantastic now, thank you

@tshortli tshortli merged commit d5ef256 into swiftlang:main Jun 1, 2025
3 checks passed
@tshortli tshortli deleted the migratable-member-import-visibility branch June 2, 2025 14:26
}
}

// Emit one warning for each module that needcs to be imported and emit notes

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: needcs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants