Skip to content

Commit 1db4ed1

Browse files
cushonPaul Hohensee
authored and
Paul Hohensee
committed
8261205: AssertionError: Cannot add metadata to an intersection type
Reviewed-by: phh Backport-of: 81f39ed
1 parent 18a0883 commit 1db4ed1

File tree

5 files changed

+168
-5
lines changed

5 files changed

+168
-5
lines changed

src/jdk.compiler/share/classes/com/sun/tools/javac/code/TypeAnnotations.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -1209,7 +1209,9 @@ public void visitLambda(JCLambda tree) {
12091209
.methodParameter(tree, i, param.vartype.pos);
12101210
push(param);
12111211
try {
1212-
separateAnnotationsKinds(param.vartype, param.sym.type, param.sym, pos);
1212+
if (!param.declaredUsingVar()) {
1213+
separateAnnotationsKinds(param.vartype, param.sym.type, param.sym, pos);
1214+
}
12131215
} finally {
12141216
pop();
12151217
}
@@ -1247,7 +1249,7 @@ public void visitVarDef(final JCVariableDecl tree) {
12471249
final TypeAnnotationPosition pos =
12481250
TypeAnnotationPosition.localVariable(currentLambda,
12491251
tree.pos);
1250-
if (!tree.isImplicitlyTyped()) {
1252+
if (!tree.declaredUsingVar()) {
12511253
separateAnnotationsKinds(tree.vartype, tree.sym.type, tree.sym, pos);
12521254
}
12531255
} else if (tree.sym.getKind() == ElementKind.EXCEPTION_PARAMETER) {

src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -3054,6 +3054,7 @@ JCVariableDecl variableDeclarator(JCModifiers mods, JCExpression type, boolean r
30543054
*/
30553055
JCVariableDecl variableDeclaratorRest(int pos, JCModifiers mods, JCExpression type, Name name,
30563056
boolean reqInit, Comment dc, boolean localDecl, boolean compound) {
3057+
boolean declaredUsingVar = false;
30573058
type = bracketsOpt(type);
30583059
JCExpression init = null;
30593060
if (token.kind == EQ) {
@@ -3073,6 +3074,7 @@ JCVariableDecl variableDeclaratorRest(int pos, JCModifiers mods, JCExpression ty
30733074
//error - 'var' and arrays
30743075
reportSyntaxError(pos, Errors.VarNotAllowedArray);
30753076
} else {
3077+
declaredUsingVar = true;
30763078
startPos = TreeInfo.getStartPos(mods);
30773079
if (startPos == Position.NOPOS)
30783080
startPos = TreeInfo.getStartPos(type);
@@ -3082,7 +3084,7 @@ JCVariableDecl variableDeclaratorRest(int pos, JCModifiers mods, JCExpression ty
30823084
}
30833085
}
30843086
JCVariableDecl result =
3085-
toP(F.at(pos).VarDef(mods, name, type, init));
3087+
toP(F.at(pos).VarDef(mods, name, type, init, declaredUsingVar));
30863088
attach(result, dc);
30873089
result.startPos = startPos;
30883090
return result;
@@ -3162,7 +3164,8 @@ JCVariableDecl variableDeclaratorId(JCModifiers mods, JCExpression type, boolean
31623164
log.error(token.pos, Errors.VarargsAndOldArraySyntax);
31633165
}
31643166
type = bracketsOpt(type);
3165-
return toP(F.at(pos).VarDef(mods, name, type, null));
3167+
return toP(F.at(pos).VarDef(mods, name, type, null,
3168+
type != null && type.hasTag(IDENT) && ((JCIdent)type).name == names.var));
31663169
}
31673170

31683171
/** Resources = Resource { ";" Resources }

src/jdk.compiler/share/classes/com/sun/tools/javac/tree/JCTree.java

+17-1
Original file line numberDiff line numberDiff line change
@@ -922,23 +922,35 @@ public static class JCVariableDecl extends JCStatement implements VariableTree {
922922
public VarSymbol sym;
923923
/** explicit start pos */
924924
public int startPos = Position.NOPOS;
925+
/** declared using `var` */
926+
private boolean declaredUsingVar;
925927

926928
protected JCVariableDecl(JCModifiers mods,
927929
Name name,
928930
JCExpression vartype,
929931
JCExpression init,
930932
VarSymbol sym) {
933+
this(mods, name, vartype, init, sym, false);
934+
}
935+
936+
protected JCVariableDecl(JCModifiers mods,
937+
Name name,
938+
JCExpression vartype,
939+
JCExpression init,
940+
VarSymbol sym,
941+
boolean declaredUsingVar) {
931942
this.mods = mods;
932943
this.name = name;
933944
this.vartype = vartype;
934945
this.init = init;
935946
this.sym = sym;
947+
this.declaredUsingVar = declaredUsingVar;
936948
}
937949

938950
protected JCVariableDecl(JCModifiers mods,
939951
JCExpression nameexpr,
940952
JCExpression vartype) {
941-
this(mods, null, vartype, null, null);
953+
this(mods, null, vartype, null, null, false);
942954
this.nameexpr = nameexpr;
943955
if (nameexpr.hasTag(Tag.IDENT)) {
944956
this.name = ((JCIdent)nameexpr).name;
@@ -952,6 +964,10 @@ public boolean isImplicitlyTyped() {
952964
return vartype == null;
953965
}
954966

967+
public boolean declaredUsingVar() {
968+
return declaredUsingVar;
969+
}
970+
955971
@Override
956972
public void accept(Visitor v) { v.visitVarDef(this); }
957973

src/jdk.compiler/share/classes/com/sun/tools/javac/tree/TreeMaker.java

+6
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,12 @@ public JCVariableDecl VarDef(JCModifiers mods, Name name, JCExpression vartype,
215215
return tree;
216216
}
217217

218+
public JCVariableDecl VarDef(JCModifiers mods, Name name, JCExpression vartype, JCExpression init, boolean declaredUsingVar) {
219+
JCVariableDecl tree = new JCVariableDecl(mods, name, vartype, init, null, declaredUsingVar);
220+
tree.pos = pos;
221+
return tree;
222+
}
223+
218224
public JCVariableDecl ReceiverVarDef(JCModifiers mods, JCExpression name, JCExpression vartype) {
219225
JCVariableDecl tree = new JCVariableDecl(mods, name, vartype);
220226
tree.pos = pos;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*
2+
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 8261205
27+
* @summary check that potentially applicable type annotations are skip if the variable or parameter was declared with var
28+
* @library /tools/lib
29+
* @modules
30+
* jdk.jdeps/com.sun.tools.classfile
31+
* jdk.compiler/com.sun.tools.javac.api
32+
* jdk.compiler/com.sun.tools.javac.main
33+
* jdk.compiler/com.sun.tools.javac.code
34+
* jdk.compiler/com.sun.tools.javac.util
35+
* @build toolbox.ToolBox toolbox.JavacTask
36+
* @run main VariablesDeclaredWithVarTest
37+
*/
38+
39+
import java.util.List;
40+
import java.util.ArrayList;
41+
42+
import java.io.File;
43+
import java.nio.file.Paths;
44+
45+
import java.lang.annotation.*;
46+
import java.util.Arrays;
47+
48+
import com.sun.tools.classfile.*;
49+
import com.sun.tools.javac.util.Assert;
50+
51+
import toolbox.JavacTask;
52+
import toolbox.ToolBox;
53+
54+
public class VariablesDeclaredWithVarTest {
55+
ToolBox tb = new ToolBox();
56+
57+
final String src =
58+
"import java.util.function.*;\n" +
59+
"import java.lang.annotation.ElementType;\n" +
60+
"import java.lang.annotation.Target;\n" +
61+
"\n" +
62+
"@Target({ElementType.TYPE_USE, ElementType.PARAMETER, ElementType.LOCAL_VARIABLE})\n" +
63+
"@interface A {}\n" +
64+
"\n" +
65+
"class Test {\n" +
66+
" void kaa() {\n" +
67+
" @A var c = g(1, 1L);\n" +
68+
" }\n" +
69+
"\n" +
70+
" <X> X g(X a, X b) {\n" +
71+
" return a;\n" +
72+
" }\n" +
73+
"\n" +
74+
" void foo() {\n" +
75+
" bar((@A var s) -> s);\n" +
76+
" }\n" +
77+
"\n" +
78+
" void bar(Function<String, String> f) {}\n" +
79+
"}\n";
80+
81+
public static void main(String... args) throws Exception {
82+
new VariablesDeclaredWithVarTest().run();
83+
}
84+
85+
void run() throws Exception {
86+
compileTestClass();
87+
checkClassFile(new File(Paths.get(System.getProperty("user.dir"),
88+
"Test.class").toUri()), 0);
89+
}
90+
91+
void compileTestClass() throws Exception {
92+
new JavacTask(tb)
93+
.sources(src)
94+
.run();
95+
}
96+
97+
void checkClassFile(final File cfile, int... taPositions) throws Exception {
98+
ClassFile classFile = ClassFile.read(cfile);
99+
List<TypeAnnotation> annos = new ArrayList<>();
100+
for (Method method : classFile.methods) {
101+
findAnnotations(classFile, method, annos);
102+
String methodName = method.getName(classFile.constant_pool);
103+
Assert.check(annos.size() == 0, "there shouldn't be any type annotations in any method, found " + annos.size() +
104+
" type annotations at method " + methodName);
105+
}
106+
}
107+
108+
void findAnnotations(ClassFile cf, Method m, List<TypeAnnotation> annos) {
109+
findAnnotations(cf, m, Attribute.RuntimeVisibleTypeAnnotations, annos);
110+
findAnnotations(cf, m, Attribute.RuntimeInvisibleTypeAnnotations, annos);
111+
}
112+
113+
void findAnnotations(ClassFile cf, Method m, String name, List<TypeAnnotation> annos) {
114+
int index = m.attributes.getIndex(cf.constant_pool, name);
115+
if (index != -1) {
116+
Attribute attr = m.attributes.get(index);
117+
assert attr instanceof RuntimeTypeAnnotations_attribute;
118+
RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr;
119+
annos.addAll(Arrays.asList(tAttr.annotations));
120+
}
121+
122+
int cindex = m.attributes.getIndex(cf.constant_pool, Attribute.Code);
123+
if (cindex != -1) {
124+
Attribute cattr = m.attributes.get(cindex);
125+
assert cattr instanceof Code_attribute;
126+
Code_attribute cAttr = (Code_attribute)cattr;
127+
index = cAttr.attributes.getIndex(cf.constant_pool, name);
128+
if (index != -1) {
129+
Attribute attr = cAttr.attributes.get(index);
130+
assert attr instanceof RuntimeTypeAnnotations_attribute;
131+
RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr;
132+
annos.addAll(Arrays.asList(tAttr.annotations));
133+
}
134+
}
135+
}
136+
}

0 commit comments

Comments
 (0)