Skip to content

Commit 669026a

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

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, 2023, 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
@@ -394,6 +394,11 @@ public static EnumSet<Flag> asFlagSet(long flags) {
394394
*/
395395
public static final long NON_SEALED = 1L<<63; // ClassSymbols
396396

397+
/**
398+
* Flag to indicate type annotations have been queued for field initializers.
399+
*/
400+
public static final long FIELD_INIT_TYPE_ANNOTATIONS_QUEUED = 1L<<53; // VarSymbols
401+
397402
/**
398403
* Describe modifier flags as they migh appear in source code, i.e.,
399404
* separated by spaces and in the order suggested by JLS 8.1.1.

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, 2023, 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
@@ -1781,10 +1781,11 @@ public Object getConstantValue() { // Mirror API
17811781
}
17821782

17831783
public void setLazyConstValue(final Env<AttrContext> env,
1784+
final Env<AttrContext> enclosingEnv,
17841785
final Attr attr,
17851786
final JCVariableDecl variable)
17861787
{
1787-
setData((Callable<Object>)() -> attr.attribLazyConstantValue(env, variable, type));
1788+
setData((Callable<Object>)() -> attr.attribLazyConstantValue(env, enclosingEnv, variable, type));
17881789
}
17891790

17901791
/**

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
@@ -836,6 +836,7 @@ void attribAnnotationTypes(List<JCAnnotation> annotations,
836836
* @see VarSymbol#setLazyConstValue
837837
*/
838838
public Object attribLazyConstantValue(Env<AttrContext> env,
839+
Env<AttrContext> enclosingEnv,
839840
JCVariableDecl variable,
840841
Type type) {
841842

@@ -844,6 +845,7 @@ public Object attribLazyConstantValue(Env<AttrContext> env,
844845

845846
final JavaFileObject prevSource = log.useSource(env.toplevel.sourcefile);
846847
try {
848+
doQueueScanTreeAndTypeAnnotateForVarInit(variable, enclosingEnv);
847849
Type itype = attribExpr(variable.init, env, type);
848850
if (variable.isImplicitlyTyped()) {
849851
//fixup local variable type
@@ -1267,11 +1269,7 @@ public void visitVarDef(JCVariableDecl tree) {
12671269
}
12681270
}
12691271
} else {
1270-
if (tree.init != null) {
1271-
// Field initializer expression need to be entered.
1272-
annotate.queueScanTreeAndTypeAnnotate(tree.init, env, tree.sym, tree.pos());
1273-
annotate.flush();
1274-
}
1272+
doQueueScanTreeAndTypeAnnotateForVarInit(tree, env);
12751273
}
12761274

12771275
VarSymbol v = tree.sym;
@@ -1324,6 +1322,17 @@ public void visitVarDef(JCVariableDecl tree) {
13241322
}
13251323
}
13261324

1325+
private void doQueueScanTreeAndTypeAnnotateForVarInit(JCVariableDecl tree, Env<AttrContext> env) {
1326+
if (tree.init != null &&
1327+
(tree.mods.flags & Flags.FIELD_INIT_TYPE_ANNOTATIONS_QUEUED) == 0 &&
1328+
env.info.scope.owner.kind != MTH && env.info.scope.owner.kind != VAR) {
1329+
tree.mods.flags |= Flags.FIELD_INIT_TYPE_ANNOTATIONS_QUEUED;
1330+
// Field initializer expression need to be entered.
1331+
annotate.queueScanTreeAndTypeAnnotate(tree.init, env, tree.sym, tree.pos());
1332+
annotate.flush();
1333+
}
1334+
}
1335+
13271336
private boolean isNonArgsMethodInObject(Name name) {
13281337
for (Symbol s : syms.objectType.tsym.members().getSymbolsByName(name, s -> s.kind == MTH)) {
13291338
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
@@ -301,7 +301,7 @@ public void visitVarDef(JCVariableDecl tree) {
301301
needsLazyConstValue(tree.init)) {
302302
Env<AttrContext> initEnv = getInitEnv(tree, env);
303303
initEnv.info.enclVar = v;
304-
v.setLazyConstValue(initEnv(tree, initEnv), attr, tree);
304+
v.setLazyConstValue(initEnv(tree, initEnv), env, attr, tree);
305305
}
306306
}
307307

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)