shortenFullyQualifiedTypes: skip FQNs whose simple name clashes with types declared in the same file - #3031
Open
maxandersen wants to merge 1 commit into
Open
Conversation
… the same file Skip shortening when the simple name of a fully-qualified type matches a class, interface, enum, or record declared in the same compilation unit. Fixes compilation errors in projects like jbang where e.g. dev.jbang.cli.Alias intentionally uses dev.jbang.catalog.Alias as a FQN to avoid clashing with the enclosing class name.
Contributor
Author
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.
Turns out my original PR missed a case that affects my own projects — ironic! 😅
In jbang, several packages have classes with the same simple name (e.g.
dev.jbang.cli.Aliasanddev.jbang.catalog.Alias). The code intentionally uses FQNs to disambiguate, butshortenFullyQualifiedTypes()was happily replacing them with the short name and adding an import that fails with "Alias is already defined in this compilation unit". Sorry about that!Fix: Before deciding which FQNs are safe to shorten, collect all type names declared in the file (classes, interfaces, enums, records — including nested ones). Skip any FQN whose simple name appears in that set.
Tests added:
fqnCollisionWithEnclosingClassName— the jbang scenariofqnCollisionWithInnerClassName— simple name matches a nested classfqnNoCollisionWithDifferentSimpleName— confirms non-conflicting FQNs still shorten