8329951: var emits deprecation warnings that do not point to the file or position

Reviewed-by: vromero
This commit is contained in:
Archie Cobbs 2025-06-03 14:35:17 +00:00
parent d3f54dae30
commit e2f736658f
4 changed files with 39 additions and 8 deletions

View File

@ -365,10 +365,6 @@ public class Analyzer {
super(AnalyzerMode.LOCAL, tag);
}
boolean isImplicitlyTyped(JCVariableDecl decl) {
return decl.vartype.pos == Position.NOPOS;
}
/**
* Map a variable tree into a new declaration using implicit type.
*/
@ -401,7 +397,7 @@ public class Analyzer {
boolean match(JCVariableDecl tree){
return tree.sym.owner.kind == Kind.MTH &&
tree.init != null && !isImplicitlyTyped(tree) &&
tree.init != null && !tree.declaredUsingVar() &&
attr.canInferLocalVarType(tree) == null;
}
@Override
@ -425,7 +421,7 @@ public class Analyzer {
@Override
boolean match(JCEnhancedForLoop tree){
return !isImplicitlyTyped(tree.var);
return !tree.var.declaredUsingVar();
}
@Override
List<JCEnhancedForLoop> rewrite(JCEnhancedForLoop oldTree) {

View File

@ -5716,9 +5716,9 @@ public class Attr extends JCTree.Visitor {
private void setSyntheticVariableType(JCVariableDecl tree, Type type) {
if (type.isErroneous()) {
tree.vartype = make.at(Position.NOPOS).Erroneous();
tree.vartype = make.at(tree.pos()).Erroneous();
} else {
tree.vartype = make.at(Position.NOPOS).Type(type);
tree.vartype = make.at(tree.pos()).Type(type);
}
}

View File

@ -0,0 +1,29 @@
/*
* @test /nodynamiccopyright/
* @bug 8329951
* @summary Check that "var" variable synthetic types have a source position
* @compile/process/ref=VarWarnPosition.out -Xlint:deprecation -XDrawDiagnostics VarWarnPosition.java
*/
import java.util.*;
import java.util.function.*;
public class VarWarnPosition {
VarWarnPosition() {
// Test 1
@SuppressWarnings("deprecation")
List<Depr> deprecatedList = null;
for (var deprValue : deprecatedList) { }
// Test 2
Consumer<Depr> c = d -> { };
// Test 3
Consumer<Depr> c2 = (var d) -> { };
}
}
@Deprecated
class Depr {}

View File

@ -0,0 +1,6 @@
VarWarnPosition.java:18:14: compiler.warn.has.been.deprecated: Depr, compiler.misc.unnamed.package
VarWarnPosition.java:21:18: compiler.warn.has.been.deprecated: Depr, compiler.misc.unnamed.package
VarWarnPosition.java:21:28: compiler.warn.has.been.deprecated: Depr, compiler.misc.unnamed.package
VarWarnPosition.java:24:18: compiler.warn.has.been.deprecated: Depr, compiler.misc.unnamed.package
VarWarnPosition.java:24:30: compiler.warn.has.been.deprecated: Depr, compiler.misc.unnamed.package
5 warnings