Skip to content

Commit e695177

Browse files
committed
8346751: Internal java compiler error with type annotations in constants expression in constant fields
Backport-of: 0395593a8a1c01a87ae36552c0f2cc9c67e8bbd8
1 parent addec14 commit e695177

File tree

5 files changed

+106
-10
lines changed

5 files changed

+106
-10
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1999, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -385,6 +385,11 @@ public static EnumSet<Flag> asFlagSet(long flags) {
385385
*/
386386
public static final long SEALED = 1L<<62; // ClassSymbols
387387

388+
/**
389+
* Flag to indicate type annotations have been queued for field initializers.
390+
*/
391+
public static final long FIELD_INIT_TYPE_ANNOTATIONS_QUEUED = 1L<<53; // VarSymbols
392+
388393
/**
389394
* Flag to indicate that the class/interface was declared with the non-sealed modifier.
390395
*/

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1999, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -1733,10 +1733,11 @@ public Object getConstantValue() { // Mirror API
17331733
}
17341734

17351735
public void setLazyConstValue(final Env<AttrContext> env,
1736+
final Env<AttrContext> enclosingEnv,
17361737
final Attr attr,
17371738
final JCVariableDecl variable)
17381739
{
1739-
setData((Callable<Object>)() -> attr.attribLazyConstantValue(env, variable, type));
1740+
setData((Callable<Object>)() -> attr.attribLazyConstantValue(env, enclosingEnv, variable, type));
17401741
}
17411742

17421743
/**

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,7 @@ void attribAnnotationTypes(List<JCAnnotation> annotations,
851851
* @see VarSymbol#setLazyConstValue
852852
*/
853853
public Object attribLazyConstantValue(Env<AttrContext> env,
854+
Env<AttrContext> enclosingEnv,
854855
JCVariableDecl variable,
855856
Type type) {
856857

@@ -859,6 +860,7 @@ public Object attribLazyConstantValue(Env<AttrContext> env,
859860

860861
final JavaFileObject prevSource = log.useSource(env.toplevel.sourcefile);
861862
try {
863+
doQueueScanTreeAndTypeAnnotateForVarInit(variable, enclosingEnv);
862864
Type itype = attribExpr(variable.init, env, type);
863865
if (variable.isImplicitlyTyped()) {
864866
//fixup local variable type
@@ -1277,11 +1279,7 @@ public void visitVarDef(JCVariableDecl tree) {
12771279
}
12781280
}
12791281
} else {
1280-
if (tree.init != null) {
1281-
// Field initializer expression need to be entered.
1282-
annotate.queueScanTreeAndTypeAnnotate(tree.init, env, tree.sym, tree.pos());
1283-
annotate.flush();
1284-
}
1282+
doQueueScanTreeAndTypeAnnotateForVarInit(tree, env);
12851283
}
12861284

12871285
VarSymbol v = tree.sym;
@@ -1334,6 +1332,17 @@ public void visitVarDef(JCVariableDecl tree) {
13341332
}
13351333
}
13361334

1335+
private void doQueueScanTreeAndTypeAnnotateForVarInit(JCVariableDecl tree, Env<AttrContext> env) {
1336+
if (tree.init != null &&
1337+
(tree.mods.flags & Flags.FIELD_INIT_TYPE_ANNOTATIONS_QUEUED) == 0 &&
1338+
env.info.scope.owner.kind != MTH && env.info.scope.owner.kind != VAR) {
1339+
tree.mods.flags |= Flags.FIELD_INIT_TYPE_ANNOTATIONS_QUEUED;
1340+
// Field initializer expression need to be entered.
1341+
annotate.queueScanTreeAndTypeAnnotate(tree.init, env, tree.sym, tree.pos());
1342+
annotate.flush();
1343+
}
1344+
}
1345+
13371346
private boolean isNonArgsMethodInObject(Name name) {
13381347
for (Symbol s : syms.objectType.tsym.members().getSymbolsByName(name, s -> s.kind == MTH)) {
13391348
if (s.type.getParameterTypes().isEmpty()) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2003, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -293,7 +293,7 @@ public void visitVarDef(JCVariableDecl tree) {
293293
needsLazyConstValue(tree.init)) {
294294
Env<AttrContext> initEnv = getInitEnv(tree, env);
295295
initEnv.info.enclVar = v;
296-
v.setLazyConstValue(initEnv(tree, initEnv), attr, tree);
296+
v.setLazyConstValue(initEnv(tree, initEnv), env, attr, tree);
297297
}
298298
}
299299
if (chk.checkUnique(tree.pos(), v, enclScope)) {
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright (c) 2025, 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 8346751
27+
* @summary Verify type annotations inside constant expression field initializers
28+
are handled correctly
29+
* @library /tools/lib
30+
* @modules
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 TypeAnnotationsInConstantInit
37+
*/
38+
39+
import java.nio.file.Files;
40+
import java.nio.file.Path;
41+
import java.nio.file.Paths;
42+
import toolbox.JavacTask;
43+
import toolbox.ToolBox;
44+
45+
public class TypeAnnotationsInConstantInit {
46+
47+
public static void main(String... args) throws Exception {
48+
new TypeAnnotationsInConstantInit().run();
49+
}
50+
51+
ToolBox tb = new ToolBox();
52+
53+
void run() throws Exception {
54+
typeAnnotationInConstantExpressionFieldInit(Paths.get("."));
55+
}
56+
57+
void typeAnnotationInConstantExpressionFieldInit(Path base) throws Exception {
58+
Path src = base.resolve("src");
59+
Path classes = base.resolve("classes");
60+
tb.writeJavaFiles(src,
61+
"""
62+
import java.lang.annotation.*;
63+
64+
@SuppressWarnings(Decl.VALUE)
65+
public class Decl {
66+
public static final @Nullable String VALUE = (@Nullable String) "";
67+
}
68+
69+
@Retention(RetentionPolicy.RUNTIME)
70+
@Target({ ElementType.TYPE_USE })
71+
@interface Nullable {}
72+
""");
73+
Files.createDirectories(classes);
74+
new JavacTask(tb)
75+
.options("-d", classes.toString())
76+
.files(tb.findJavaFiles(src))
77+
.run()
78+
.writeAll();
79+
}
80+
81+
}

0 commit comments

Comments
 (0)