Skip to content

Commit e2f7366

Browse files
committed
8329951: var emits deprecation warnings that do not point to the file or position
Reviewed-by: vromero
1 parent d3f54da commit e2f7366

File tree

4 files changed

+39
-8
lines changed

4 files changed

+39
-8
lines changed

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Analyzer.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -365,10 +365,6 @@ abstract class RedundantLocalVarTypeAnalyzerBase<X extends JCStatement> extends
365365
super(AnalyzerMode.LOCAL, tag);
366366
}
367367

368-
boolean isImplicitlyTyped(JCVariableDecl decl) {
369-
return decl.vartype.pos == Position.NOPOS;
370-
}
371-
372368
/**
373369
* Map a variable tree into a new declaration using implicit type.
374370
*/
@@ -401,7 +397,7 @@ class RedundantLocalVarTypeAnalyzer extends RedundantLocalVarTypeAnalyzerBase<JC
401397

402398
boolean match(JCVariableDecl tree){
403399
return tree.sym.owner.kind == Kind.MTH &&
404-
tree.init != null && !isImplicitlyTyped(tree) &&
400+
tree.init != null && !tree.declaredUsingVar() &&
405401
attr.canInferLocalVarType(tree) == null;
406402
}
407403
@Override
@@ -425,7 +421,7 @@ class RedundantLocalVarTypeAnalyzerForEach extends RedundantLocalVarTypeAnalyzer
425421

426422
@Override
427423
boolean match(JCEnhancedForLoop tree){
428-
return !isImplicitlyTyped(tree.var);
424+
return !tree.var.declaredUsingVar();
429425
}
430426
@Override
431427
List<JCEnhancedForLoop> rewrite(JCEnhancedForLoop oldTree) {

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5716,9 +5716,9 @@ private Type capture(Type type) {
57165716

57175717
private void setSyntheticVariableType(JCVariableDecl tree, Type type) {
57185718
if (type.isErroneous()) {
5719-
tree.vartype = make.at(Position.NOPOS).Erroneous();
5719+
tree.vartype = make.at(tree.pos()).Erroneous();
57205720
} else {
5721-
tree.vartype = make.at(Position.NOPOS).Type(type);
5721+
tree.vartype = make.at(tree.pos()).Type(type);
57225722
}
57235723
}
57245724

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* @test /nodynamiccopyright/
3+
* @bug 8329951
4+
* @summary Check that "var" variable synthetic types have a source position
5+
* @compile/process/ref=VarWarnPosition.out -Xlint:deprecation -XDrawDiagnostics VarWarnPosition.java
6+
*/
7+
8+
import java.util.*;
9+
import java.util.function.*;
10+
11+
public class VarWarnPosition {
12+
13+
VarWarnPosition() {
14+
15+
// Test 1
16+
@SuppressWarnings("deprecation")
17+
List<Depr> deprecatedList = null;
18+
for (var deprValue : deprecatedList) { }
19+
20+
// Test 2
21+
Consumer<Depr> c = d -> { };
22+
23+
// Test 3
24+
Consumer<Depr> c2 = (var d) -> { };
25+
}
26+
}
27+
28+
@Deprecated
29+
class Depr {}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
VarWarnPosition.java:18:14: compiler.warn.has.been.deprecated: Depr, compiler.misc.unnamed.package
2+
VarWarnPosition.java:21:18: compiler.warn.has.been.deprecated: Depr, compiler.misc.unnamed.package
3+
VarWarnPosition.java:21:28: compiler.warn.has.been.deprecated: Depr, compiler.misc.unnamed.package
4+
VarWarnPosition.java:24:18: compiler.warn.has.been.deprecated: Depr, compiler.misc.unnamed.package
5+
VarWarnPosition.java:24:30: compiler.warn.has.been.deprecated: Depr, compiler.misc.unnamed.package
6+
5 warnings

0 commit comments

Comments
 (0)