Repository navigation
Don't remove java.lang imports that disambiguate a simple name - #1778
Closed
ZayanKhan-12 wants to merge 3 commits into
Closed
ZayanKhan-12 wants to merge 3 commits into
ZayanKhan-12 wants to merge 3 commits into
Conversation
`RemoveUnusedImports` deleted every import qualified by `java.lang`
unconditionally. That is unsound: per JLS 6.4.1 a single-type-import
declaration shadows both types declared elsewhere in the current package
and types brought in by an import-on-demand, so `import java.lang.Byte;`
can be the only thing binding `Byte` to the JDK class.
Removing it either makes the reference ambiguous:
error: reference to Byte is ambiguous
both class com.company.project.foo.Byte in com.company.project.foo
and class java.lang.Byte in java.lang match
or silently rebinds the name to a same-package class, which then fails to
compile against the wrong type.
`RemoveUnusedImports` analyses one compilation unit with no classpath and
no symbol resolution, so it cannot prove that no clashing type exists.
Only remove a `java.lang` single-type-import when its simple name is not
referenced at all.
Unchanged: `import java.lang.*;` is still removed (java.lang is implicitly
imported on demand), unused `java.lang` imports are still removed, and
imports of same-package types are still removed -- package members already
shadow import-on-demand declarations, so those can never be disambiguating.
One existing test case encoded the old behaviour and has been updated.
Fixes #1334
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Documents the self-provisioning JDK build, the module map, the parse -> ops -> doc -> layout -> output pipeline, the file-based golden test workflow, and the single-file-analysis constraint that #1334 is an instance of. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Member
|
Thanks for your interest in palantir/palantir-java-format, @ZayanKhan-12! Before we can accept your pull request, you need to sign our contributor license agreement - just visit https://cla.palantir.com/ and follow the instructions. Once you sign, I'll automatically update this pull request. |
Generate changelog in
|
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This was referenced Sep 15, 2026
ZayanKhan-12
force-pushed
the
fix/1334-keep-disambiguating-java-lang-imports
branch
from
September 15, 2026 21:10
79a9c62 to
b1a84cb
Compare
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.
Fixes #1334.
Before this PR
RemoveUnusedImportstreated every import whose qualifier isjava.langas unused and deleted it unconditionally:That is not sound. Per JLS 6.4.1, a
single-type-import declaration shadows both types declared elsewhere in the current package and types brought in by
an import-on-demand. So
import java.lang.Byte;can be the only thing making a file compile, and removing it breaksthe build.
Both cases from the issue reproduce against
javac(Corretto 21):1. Disambiguating an on-demand import
Removing the import:
2. Disambiguating a class in the same package
Removing the import silently rebinds
Bytetocom.company.project.bar.Byte:This is the worse of the two, because it is not a workaround away — there is no wildcard import to expand.
After this PR
A
java.langsingle-type import is now only removed when its simple name is not referenced anywhere in the file(code or javadoc) — i.e. when it is genuinely unused.
RemoveUnusedImportssees a single compilation unit with no classpath and no symbol resolution, so it cannot provethat no clashing type exists in the current package. Where it cannot prove the edit is semantics-preserving, it now
declines to make it. A formatter silently changing which class a name binds to is a much worse failure than leaving a
redundant import in place.
Unchanged behaviour:
import java.lang.*;is still removed —java.langis implicitly imported on demand, so it is always redundant.java.lang.Fooimports are still removed.import java.lang.Foo.Bar;were never on this code path (their qualifier isjava.lang.Foo).import-on-demand declarations, so a same-package single-type import can never be disambiguating anything.
The only intentional behaviour change is that a used
java.lang.Ximport is now preserved rather than stripped.One existing test case encoded the old behaviour and has been updated with a comment explaining why.
While in here I also pulled the
*-check and the used-name check into named helpers (isOnDemand,isUsed) so thethree branches read uniformly.
Testing
RemoveUnusedImportsTestcovering: both scenarios from the issue, javadoc-only usage,import java.lang.*;, and an unusedjava.langimport alongside a wildcard import.javacbefore and after the change../gradlew buildpasses.Also included
A
CLAUDE.mdat the repo root documenting build/test commands, the module map, the formatter pipeline, thefile-based golden-test workflow, and the single-file-analysis constraint that this bug is an instance of — so that
agent-assisted contributions start from accurate context.
🤖 Generated with Claude Code