Fix 'Package import was not used in package declarations' warning#135
Closed
gage-halverson-fetch wants to merge 1 commit intomainfrom
Closed
Fix 'Package import was not used in package declarations' warning#135gage-halverson-fetch wants to merge 1 commit intomainfrom
gage-halverson-fetch wants to merge 1 commit intomainfrom
Conversation
For `package` protocols, generate a top-level variable that references the `_MockingModule` type from Mocking to ensure the compiler recognizes the package import as used. This works around a Swift compiler limitation where `package import` statements are incorrectly flagged as unused when the imported module's types are only referenced in macro-generated code. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Collaborator
|
So here are my thoughts:
|
Author
|
Thanks for the feedback! I'll be closing this in favor of getting swiftlang/swift#86376 merged into Swift |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes the Swift 6 compiler warning "Package import of 'Mocking' was not used in package declarations" that occurs when using
package import Mockingwith the@Mockedmacro.The Problem
With Swift 6's
InternalImportsByDefaultfeature, the compiler incorrectly flags package imports as unused when the imported module's types are only referenced in macro-generated code. This is because macro expansions happen after the import usage check.Related Swift Forums discussion: https://forums.swift.org/t/internal-imports-by-default-is-very-problematic-for-generated-code/82722/4
The Solution
For protocols with
packageaccess level, the@Mockedmacro now generates a top-level variable that explicitly references a type from the Mocking module:This ensures the compiler recognizes that the package import is being used in a package declaration.
Changes
_MockingModulemarker enum toSources/Mocking/Models/@Mockedmacro to generate_noOpvariable for package access level onlyprefixed(_noOp)to the macro's peer namesTest plan
package import Mockingin consuming package🤖 Generated with Claude Code