JDK-8160928: javac incorrectly copies over interior type annotations to bridge method

Evgeny Mandrikov mandrikov at gmail.com
Thu Aug 30 14:16:15 UTC 2018


Please see updated patch in attachment.
Now it is even less different from original fix.
Tests pass as before.


Regards,
Evgeny


On Thu, Aug 30, 2018 at 3:58 PM Evgeny Mandrikov <mandrikov at gmail.com>
wrote:

> Actually it exists in 8 -
> http://hg.openjdk.java.net/jdk8u/jdk8u-dev/langtools/file/7031ed34a604/test/tools/javac/lib/ToolBox.java
> Unfortunately I initially missed this, because of difference in
> location/package compared to 9.
> Thank you, Vicente, for pointing this out. Will send updated patch shortly.
>
> On Thu, Aug 30, 2018 at 3:47 PM Maurizio Cimadamore <
> maurizio.cimadamore at oracle.com> wrote:
>
>> Hi Vicente,
>> Is Toolbox available in 8u-dev ? I assume that is why the workaround has
>> been put in place?
>>
>> Maurizio
>>
>> On 30/08/18 14:36, Vicente Romero wrote:
>>
>> why not using ToolBox in the test? We introduced it with the idea of
>> using it for these cases
>>
>> Thanks,
>> Vicente
>>
>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/compiler-dev/attachments/20180830/c5208a95/attachment-0001.html>
-------------- next part --------------
# HG changeset patch
# User Evgeny Mandrikov <mandrikov at gmail.com>
# Date 1535499247 -7200
#      Wed Aug 29 01:34:07 2018 +0200
# Node ID 5dce2796a9c54e4a692a3f38af1b01cd16910d6c
# Parent  7031ed34a60400768d5136df03cbf8f05e6d9a6b
8160928: javac incorrectly copies over interior type annotations to bridge method

diff -r 7031ed34a604 -r 5dce2796a9c5 src/share/classes/com/sun/tools/javac/code/SymbolMetadata.java
--- a/src/share/classes/com/sun/tools/javac/code/SymbolMetadata.java	Tue Jul 24 09:52:12 2018 -0700
+++ b/src/share/classes/com/sun/tools/javac/code/SymbolMetadata.java	Wed Aug 29 01:34:07 2018 +0200
@@ -31,10 +31,13 @@
 
 import com.sun.tools.javac.comp.Annotate;
 import com.sun.tools.javac.comp.AttrContext;
+import com.sun.tools.javac.code.Attribute.TypeCompound;
+import com.sun.tools.javac.code.Kinds;
 import com.sun.tools.javac.comp.Env;
 import com.sun.tools.javac.util.*;
 import com.sun.tools.javac.util.Assert;
 import com.sun.tools.javac.util.List;
+import com.sun.tools.javac.util.ListBuffer;
 import com.sun.tools.javac.util.Log;
 import com.sun.tools.javac.util.Pair;
 import static com.sun.tools.javac.code.Kinds.PCK;
@@ -152,9 +155,22 @@
             throw new NullPointerException();
         }
         setDeclarationAttributes(other.getDeclarationAttributes());
-        setTypeAttributes(other.getTypeAttributes());
-        setInitTypeAttributes(other.getInitTypeAttributes());
-        setClassInitTypeAttributes(other.getClassInitTypeAttributes());
+        if ((sym.flags() & Flags.BRIDGE) != 0) {
+            Assert.check(other.sym.kind == Kinds.MTH);
+            ListBuffer<TypeCompound> typeAttributes = new ListBuffer<>();
+            for (TypeCompound tc : other.getTypeAttributes()) {
+                // Carry over only contractual type annotations: i.e nothing interior to method body.
+                if (!tc.position.type.isLocal())
+                    typeAttributes.append(tc);
+            }
+            setTypeAttributes(typeAttributes.toList());
+        } else {
+            setTypeAttributes(other.getTypeAttributes());
+        }
+        if (sym.kind == Kinds.TYP) {
+            setInitTypeAttributes(other.getInitTypeAttributes());
+            setClassInitTypeAttributes(other.getClassInitTypeAttributes());
+        }
     }
 
     public void setDeclarationAttributesWithCompletion(final Annotate.AnnotateRepeatedContext<Attribute.Compound> ctx) {
diff -r 7031ed34a604 -r 5dce2796a9c5 test/tools/javac/annotations/typeAnnotations/classfile/BridgeShouldHaveNoInteriorAnnotationsTest.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/annotations/typeAnnotations/classfile/BridgeShouldHaveNoInteriorAnnotationsTest.java	Wed Aug 29 01:34:07 2018 +0200
@@ -0,0 +1,97 @@
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 8160928
+ * @summary javac incorrectly copies over interior type annotations to bridge method
+ * @library /tools/javac/lib
+ * @build ToolBox
+ * @run compile -g BridgeShouldHaveNoInteriorAnnotationsTest.java
+ * @run main BridgeShouldHaveNoInteriorAnnotationsTest
+ */
+
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Target;
+import java.io.*;
+
+class Pair_8160928<T1, T2> {
+}
+
+public class BridgeShouldHaveNoInteriorAnnotationsTest implements java.util.Iterator<Pair_8160928<Object, Object>> {
+
+    @Override
+    public boolean hasNext() {
+        throw new RuntimeException();
+    }
+
+    @Override
+    public Pair_8160928<@NonNull Object, Object> next() {
+        Comparable<@NonNull Object> cble1 = (Comparable<@NonNull Object>) null;
+        return null;
+    }
+
+    @Override
+    public void remove() {
+        throw new RuntimeException();
+    }
+
+    @Target(ElementType.TYPE_USE)
+    public @interface NonNull {
+    }
+
+    static class OutputExpectedOnceHolder {
+        public String[] outputs = {
+            "0: #55(): CAST, offset=0, type_index=0, location=[TYPE_ARGUMENT(0)]",
+            "1: #55(): LOCAL_VARIABLE, {start_pc=5, length=2, index=1}, location=[TYPE_ARGUMENT(0)]",
+        };
+    }
+
+    static class OutputExpectedTwiceHolder {
+        public String[] outputs = {
+            "0: #55(): METHOD_RETURN, location=[TYPE_ARGUMENT(0)]",
+        };
+    }
+
+    public static void main(String[] args) throws Exception {
+        Path classPath = Paths.get(System.getProperty("test.classes"), "BridgeShouldHaveNoInteriorAnnotationsTest.class");
+        String javapOut = ToolBox.javap(new ToolBox.JavaToolArgs().setAllArgs("-v", "-p", classPath.toString()));
+
+        OutputExpectedOnceHolder holder = new OutputExpectedOnceHolder();
+        for (String s : holder.outputs) {
+            String newOutput = javapOut.replace(s, "");
+            if (((javapOut.length() - newOutput.length()) / s.length()) != 1)
+                throw new AssertionError("Interior annotations carried over to bridge ?");
+        }
+
+        OutputExpectedTwiceHolder holder2 = new OutputExpectedTwiceHolder();
+        for (String s : holder2.outputs) {
+            String newOutput = javapOut.replace(s, "");
+            if (((javapOut.length() - newOutput.length()) / s.length()) != 2)
+                throw new AssertionError("Exterior annotations not properly carried over to bridge");
+        }
+    }
+
+}


More information about the compiler-dev mailing list