Skip to content

Sema: Don't warn on package imports providing a type extended by a package extension #73125

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 1 commit into from
Apr 22, 2024

Conversation

xymus
Copy link
Contributor

@xymus xymus commented Apr 18, 2024

The warnings on superfluously public/package imports has a hole where it could report a package import as not being used by package decls even if it provides a type extended by a package extension. Since the exportability checker does not run on package decls it does not triggering the usual logic registering this reference.

Address this issue by adding a check specifically for package extensions. We can remove this check once we have exportability checking for package decls.

rdar://126712864

The warnings on superfluously public/package imports has a hole where
it could report a package import as not being used by package decls
even if it provides a type extended by a package extension. This is
caused by the exportability checker not being enabled for package
decls, thus not triggering the usual logic registering this reference.

Address this issue by adding a check specifically for package extensions.
We can remove this check once we have exportability checking for package
decls.

rdar://126712864
@xymus
Copy link
Contributor Author

xymus commented Apr 18, 2024

@swift-ci Please smoke test

@xymus xymus requested review from elsh and tshortli April 19, 2024 17:08
@xymus xymus changed the title Sema: Register the extended type of a package-level extension so we don't warn on the import Sema: Don't warn on package imports providing a type extended in the local file Apr 19, 2024
@xymus
Copy link
Contributor Author

xymus commented Apr 19, 2024

@swift-ci Please smoke test Windows

@xymus xymus requested a review from nkcsgexi April 19, 2024 18:08
Copy link
Contributor

@nkcsgexi nkcsgexi left a comment

Choose a reason for hiding this comment

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

Thank you!

@@ -96,6 +97,9 @@ public struct NonPublicExtendedType {}
//--- UnusedImport.swift

//--- UnusedPackageImport.swift
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it intentional for UnusedPackageImport.swift to be an empty file?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, it's imported but nothing it would contain would be used, but the import does show the diagnostic that we're testing. I could have had a single Empty.swift file and compile it into many different modules, but I find it useful to have a file with the module name.

if (!extendedType)
return;

bool hasPackageMembers = llvm::any_of(ED->getMembers(),
Copy link
Contributor

Choose a reason for hiding this comment

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

I think there should be another check to see if this ED doesn't have members but the nominal type it's extending has a package access level; otherwise the warning will still be thrown.

@@ -96,6 +97,9 @@ public struct NonPublicExtendedType {}
//--- UnusedImport.swift

//--- UnusedPackageImport.swift
//--- ExtendedPackageTypeImport.swift

public struct ExtendedPackageType {}
Copy link
Contributor

@elsh elsh Apr 19, 2024

Choose a reason for hiding this comment

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

Re: comment above, package struct P {} could be extended without any members like extension P {} and the warning will still be shown.

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 believe that warning would be appropriate then as an empty extension would be internal or below.

package import DefinesP // warning: package import of 'DefinesP' was not used in package declarations

extension P {} 

It would fall in the same scenario as extending the type with internal decls where there's no requirement to make the import package or public as these services are not visible to other modules. Does that make sense or am I missing something?

Copy link
Contributor

Choose a reason for hiding this comment

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

I see. For some reason I thought the default access level of the extension is whatever access level its nominal type has, but it looks to be internal when not explicitly marked. FYI package exportability PR #73161

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 expect #73161 may be more challenging to land as it adds errors. So I'll merge this one but please feel free to revert it as part of #73161 if you see it interfering with the diagnostics.

@xymus xymus changed the title Sema: Don't warn on package imports providing a type extended in the local file Sema: Don't warn on package imports providing a type extended by a package extension Apr 20, 2024
@xymus xymus merged commit 909c9c1 into swiftlang:main Apr 22, 2024
@xymus xymus deleted the superfluous-package-imports branch April 22, 2024 20:16
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.

4 participants