Skip to content

Don't remove java.lang imports that disambiguate a simple name - #1778

Closed
ZayanKhan-12 wants to merge 3 commits into
palantir:developfrom
ZayanKhan-12:fix/1334-keep-disambiguating-java-lang-imports
Closed

ZayanKhan-12 wants to merge 3 commits into
palantir:developfrom
ZayanKhan-12:fix/1334-keep-disambiguating-java-lang-imports

Conversation

@ZayanKhan-12

Copy link
Copy Markdown

Fixes #1334.

Before this PR

RemoveUnusedImports treated every import whose qualifier is java.lang as unused and deleted it unconditionally:

if (qualifier.equals("java.lang")) {
    return true;
}

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 breaks
the build.

Both cases from the issue reproduce against javac (Corretto 21):

1. Disambiguating an on-demand import

package com.company.project.bar;
import com.company.project.foo.*;   // also declares a `Byte`
import java.lang.Byte;
public class Test { Byte foo() { ... } }

Removing the import:

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

2. Disambiguating a class in the same package

package com.company.project.bar;    // also contains Byte.java
import java.lang.Byte;
public class Test { Byte foo() { ... } }

Removing the import silently rebinds Byte to com.company.project.bar.Byte:

error: cannot find symbol
  symbol:   method valueOf(byte)
  location: class 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.lang single-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.

RemoveUnusedImports sees a single compilation unit with no classpath and no symbol resolution, so it cannot prove
that 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.lang is implicitly imported on demand, so it is always redundant.
  • Unused java.lang.Foo imports are still removed.
  • Nested types such as import java.lang.Foo.Bar; were never on this code path (their qualifier is java.lang.Foo).
  • Imports of types in the current package are still removed. Verified this is safe: package members already shadow
    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.X import 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 the
three branches read uniformly.

Testing

  • Five new cases in RemoveUnusedImportsTest covering: both scenarios from the issue, javadoc-only usage,
    import java.lang.*;, and an unused java.lang import alongside a wildcard import.
  • Both issue scenarios were verified end to end against javac before and after the change.
  • ./gradlew build passes.

Also included

A CLAUDE.md at the repo root documenting build/test commands, the module map, the formatter pipeline, the
file-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

ZayanKhan-12 and others added 2 commits September 15, 2026 13:31
`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>
@palantirtech

Copy link
Copy Markdown
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.

@changelog-app

changelog-app Bot commented Sep 15, 2026

Copy link
Copy Markdown

Generate changelog in changelog/@unreleased

Type (Select exactly one)

  • Feature (Adding new functionality)
  • Improvement (Improving existing functionality)
  • Fix (Fixing an issue with existing functionality)
  • Break (Creating a new major version by breaking public APIs)
  • Deprecation (Removing functionality in a non-breaking way)
  • Migration (Automatically moving data/functionality to a new system)

Description

Don't remove java.lang imports that disambiguate a simple name

Check the box to generate changelog(s)

  • Generate changelog entry

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
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.

Incorrectly removes java.lang import that disambiguates an import or same-package class

2 participants