RFR: 8329951: `var` emits deprecation warnings that do not point to the file or position [v2]

Archie Cobbs acobbs at openjdk.org
Mon Jun 2 22:07:20 UTC 2025


> This PR fixes a bug that causes certain warnings to be emitted without source file location information.
> 
> Consider this example:
> 
> import java.util.*;
> import java.util.function.*;
> public class Example {
> 
>     @SuppressWarnings("deprecation")
>     List<Observable> m1() { return null; }
> 
>     void m2() {
>         for (var o : m1()) { }
>     }
> }
> 
> the compiler outputs this:
> 
> $ javac -Xlint:deprecation Example.java 
> warning: [deprecation] Observable in java.util has been deprecated
> 1 warning
> 
> but it should instead output this:
> 
> Example.java:9: warning: [deprecation] Observable in java.util has been deprecated
>         for (var o : m1()) { }
>              ^
> 1 warning
> 
> The bug happens because the "synthetic" type that is installed dynamically for implicitly typed variables is not given a source code position. This makes some sense (?) because that type doesn't literally appear in the variable declaration; however, certain error messages expect to be able to point to the type portion of a variable declaration, so it also causes this bug (and presumably similar variants). To fix this, we just copy the position of the variable declaration itself to the synthetic type.
> 
> However, this change necessitated another change: The fix for [JDK-8200199](https://bugs.openjdk.org/browse/JDK-8200199) added back in 2018 relies on the fact that the position of an implicitly typed variable is always null, so this change breaks that fix. But since then, a new method `JCVariableDecl.declaredUsingVar()` was added, so we can just use that test instead. This also makes the code a little clearer.

Archie Cobbs has updated the pull request with a new target base due to a merge or a rebase. The incremental webrev excludes the unrelated changes brought in by the merge/rebase. The pull request contains two additional commits since the last revision:

 - Merge branch 'master' into JDK-8329951
 - Fix bug where some warnings didn't have a source file position.

-------------

Changes:
  - all: https://git.openjdk.org/jdk/pull/23683/files
  - new: https://git.openjdk.org/jdk/pull/23683/files/daacc44c..40ed15b1

Webrevs:
 - full: https://webrevs.openjdk.org/?repo=jdk&pr=23683&range=01
 - incr: https://webrevs.openjdk.org/?repo=jdk&pr=23683&range=00-01

  Stats: 635116 lines in 8619 files changed: 262319 ins; 320457 del; 52340 mod
  Patch: https://git.openjdk.org/jdk/pull/23683.diff
  Fetch: git fetch https://git.openjdk.org/jdk.git pull/23683/head:pull/23683

PR: https://git.openjdk.org/jdk/pull/23683


More information about the compiler-dev mailing list