From 1ceb99939251b5da99906d425eeb7d6c91c4f4c5 Mon Sep 17 00:00:00 2001 From: Guoxiong Li Date: Sat, 28 Nov 2020 17:31:58 +0800 Subject: [PATCH 1/5] 8257037: No javac warning when calling deprecated constructor with diamond --- .../com/sun/tools/javac/comp/Resolve.java | 47 +++++++++++++++---- .../tools/javac/T8257037/T8257037.java | 38 +++++++++++++++ .../tools/javac/T8257037/T8257037.out | 2 + 3 files changed, 77 insertions(+), 10 deletions(-) create mode 100644 test/langtools/tools/javac/T8257037/T8257037.java create mode 100644 test/langtools/tools/javac/T8257037/T8257037.out diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java index 727d2989adf4b..cecfca219e12f 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java @@ -2883,7 +2883,7 @@ Symbol resolveDiamond(DiagnosticPosition pos, new BasicLookupHelper(names.init, site, argtypes, typeargtypes) { @Override Symbol doLookup(Env env, MethodResolutionPhase phase) { - return findDiamond(env, site, argtypes, typeargtypes, + return findDiamond(pos, env, site, argtypes, typeargtypes, phase.isBoxingRequired(), phase.isVarargsRequired()); } @@ -2896,23 +2896,50 @@ Symbol access(Env env, DiagnosticPosition pos, Symbol location, Sym } else { final JCDiagnostic details = sym.kind == WRONG_MTH ? ((InapplicableSymbolError)sym.baseSymbol()).errCandidate().snd : - null; + null; sym = new DiamondError(sym, currentResolutionContext); sym = accessMethod(sym, pos, site, names.init, true, argtypes, typeargtypes); env.info.pendingResolutionPhase = currentResolutionContext.step; } } return sym; - }}); + } + }); } - /** This method scans all the constructor symbol in a given class scope - - * assuming that the original scope contains a constructor of the kind: - * {@code Foo(X x, Y y)}, where X,Y are class type-variables declared in Foo, - * a method check is executed against the modified constructor type: - * {@code Foo(X x, Y y)}. This is crucial in order to enable diamond - * inference. The inferred return type of the synthetic constructor IS - * the inferred type for the diamond operator. + /** + * Find the constructor using diamond inference and do some checks(deprecated and preview). + * + * @param pos The position to use for error reporting. + * @param env The environment current at the constructor invocation. + * @param site The type of class for which a constructor is searched. + * The scope of this class has been touched in attribution. + * @param argtypes The types of the constructor invocation's value arguments. + * @param typeargtypes The types of the constructor invocation's type arguments. + * @param allowBoxing Allow boxing conversions of arguments. + * @param useVarargs Box trailing arguments into an array for varargs. + */ + private Symbol findDiamond(DiagnosticPosition pos, + Env env, + Type site, + List argtypes, + List typeargtypes, + boolean allowBoxing, + boolean useVarargs) { + Symbol sym = findDiamond(env, site, argtypes, typeargtypes, allowBoxing, useVarargs); + chk.checkDeprecated(pos, env.info.scope.owner, sym); + chk.checkPreview(pos, sym); + return sym; + } + + /** + * This method scans all the constructor symbol in a given class scope - + * assuming that the original scope contains a constructor of the kind: + * {@code Foo(X x, Y y)}, where X,Y are class type-variables declared in Foo, + * a method check is executed against the modified constructor type: + * {@code Foo(X x, Y y)}. This is crucial in order to enable diamond + * inference. The inferred return type of the synthetic constructor IS + * the inferred type for the diamond operator. */ private Symbol findDiamond(Env env, Type site, diff --git a/test/langtools/tools/javac/T8257037/T8257037.java b/test/langtools/tools/javac/T8257037/T8257037.java new file mode 100644 index 0000000000000..31206033553c3 --- /dev/null +++ b/test/langtools/tools/javac/T8257037/T8257037.java @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2020, 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 8257307 + * @summary No javac warning when calling deprecated constructor with diamond + * @run compile/ref=T8257037.out -Xlint -XDrawDiagnostics T8257037.java + */ + +public class T8257037 { + T8257037_GenericClass test = new T8257037_GenericClass<>(); // use diamond +} + +class T8257037_GenericClass { + @Deprecated + public T8257037_GenericClass() {} +} diff --git a/test/langtools/tools/javac/T8257037/T8257037.out b/test/langtools/tools/javac/T8257037/T8257037.out new file mode 100644 index 0000000000000..13497256d1fec --- /dev/null +++ b/test/langtools/tools/javac/T8257037/T8257037.out @@ -0,0 +1,2 @@ +T8257037.java:32:42: compiler.warn.has.been.deprecated: T8257037_GenericClass(), T8257037_GenericClass +1 warning From 1d2a73c53c67147daa18aaa2caf81587e6de0ddb Mon Sep 17 00:00:00 2001 From: Guoxiong Li Date: Sat, 28 Nov 2020 19:41:48 +0800 Subject: [PATCH 2/5] Polish --- .../com/sun/tools/javac/comp/Resolve.java | 40 +++++++++---------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java index cecfca219e12f..e519043d96c39 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java @@ -2896,28 +2896,25 @@ Symbol access(Env env, DiagnosticPosition pos, Symbol location, Sym } else { final JCDiagnostic details = sym.kind == WRONG_MTH ? ((InapplicableSymbolError)sym.baseSymbol()).errCandidate().snd : - null; + null; sym = new DiamondError(sym, currentResolutionContext); sym = accessMethod(sym, pos, site, names.init, true, argtypes, typeargtypes); env.info.pendingResolutionPhase = currentResolutionContext.step; } } return sym; - } - }); + }}); } - /** - * Find the constructor using diamond inference and do some checks(deprecated and preview). - * - * @param pos The position to use for error reporting. - * @param env The environment current at the constructor invocation. - * @param site The type of class for which a constructor is searched. - * The scope of this class has been touched in attribution. - * @param argtypes The types of the constructor invocation's value arguments. - * @param typeargtypes The types of the constructor invocation's type arguments. - * @param allowBoxing Allow boxing conversions of arguments. - * @param useVarargs Box trailing arguments into an array for varargs. + /** Find the constructor using diamond inference and do some checks(deprecated and preview). + * @param pos The position to use for error reporting. + * @param env The environment current at the constructor invocation. + * @param site The type of class for which a constructor is searched. + * The scope of this class has been touched in attribution. + * @param argtypes The types of the constructor invocation's value arguments. + * @param typeargtypes The types of the constructor invocation's type arguments. + * @param allowBoxing Allow boxing conversions of arguments. + * @param useVarargs Box trailing arguments into an array for varargs. */ private Symbol findDiamond(DiagnosticPosition pos, Env env, @@ -2932,14 +2929,13 @@ private Symbol findDiamond(DiagnosticPosition pos, return sym; } - /** - * This method scans all the constructor symbol in a given class scope - - * assuming that the original scope contains a constructor of the kind: - * {@code Foo(X x, Y y)}, where X,Y are class type-variables declared in Foo, - * a method check is executed against the modified constructor type: - * {@code Foo(X x, Y y)}. This is crucial in order to enable diamond - * inference. The inferred return type of the synthetic constructor IS - * the inferred type for the diamond operator. + /** This method scans all the constructor symbol in a given class scope - + * assuming that the original scope contains a constructor of the kind: + * {@code Foo(X x, Y y)}, where X,Y are class type-variables declared in Foo, + * a method check is executed against the modified constructor type: + * {@code Foo(X x, Y y)}. This is crucial in order to enable diamond + * inference. The inferred return type of the synthetic constructor IS + * the inferred type for the diamond operator. */ private Symbol findDiamond(Env env, Type site, From 66671b02c41fb1353a521883b30ea5f0c004b1fd Mon Sep 17 00:00:00 2001 From: Guoxiong Li Date: Tue, 1 Dec 2020 20:20:14 +0800 Subject: [PATCH 3/5] Check @Deprecated annotation in method Attr.checkIdInternal. Remove the redundant warning code --- .../com/sun/tools/javac/comp/Attr.java | 13 +++---- .../com/sun/tools/javac/comp/Resolve.java | 34 ++----------------- 2 files changed, 7 insertions(+), 40 deletions(-) diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java index df21524229c0e..ea1853851d0cc 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java @@ -4459,15 +4459,10 @@ else if (ownOuter.hasTag(CLASS) && site != ownOuter) { } // Emit a `deprecation' warning if symbol is deprecated. - // (for constructors (but not for constructor references), the error - // was given when the constructor was resolved) - - if (sym.name != names.init || tree.hasTag(REFERENCE)) { - chk.checkDeprecated(tree.pos(), env.info.scope.owner, sym); - chk.checkSunAPI(tree.pos(), sym); - chk.checkProfile(tree.pos(), sym); - chk.checkPreview(tree.pos(), sym); - } + chk.checkDeprecated(tree.pos(), env.info.scope.owner, sym); + chk.checkSunAPI(tree.pos(), sym); + chk.checkProfile(tree.pos(), sym); + chk.checkPreview(tree.pos(), sym); // If symbol is a variable, check that its type and // kind are compatible with the prototype and protokind. diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java index e519043d96c39..640c79dabdea2 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java @@ -2855,13 +2855,8 @@ Symbol findConstructor(DiagnosticPosition pos, Env env, List typeargtypes, boolean allowBoxing, boolean useVarargs) { - Symbol sym = findMethod(env, site, - names.init, argtypes, - typeargtypes, allowBoxing, - useVarargs); - chk.checkDeprecated(pos, env.info.scope.owner, sym); - chk.checkPreview(pos, sym); - return sym; + return findMethod(env, site, names.init, argtypes, + typeargtypes, allowBoxing, useVarargs); } /** Resolve constructor using diamond inference. @@ -2883,7 +2878,7 @@ Symbol resolveDiamond(DiagnosticPosition pos, new BasicLookupHelper(names.init, site, argtypes, typeargtypes) { @Override Symbol doLookup(Env env, MethodResolutionPhase phase) { - return findDiamond(pos, env, site, argtypes, typeargtypes, + return findDiamond(env, site, argtypes, typeargtypes, phase.isBoxingRequired(), phase.isVarargsRequired()); } @@ -2906,29 +2901,6 @@ Symbol access(Env env, DiagnosticPosition pos, Symbol location, Sym }}); } - /** Find the constructor using diamond inference and do some checks(deprecated and preview). - * @param pos The position to use for error reporting. - * @param env The environment current at the constructor invocation. - * @param site The type of class for which a constructor is searched. - * The scope of this class has been touched in attribution. - * @param argtypes The types of the constructor invocation's value arguments. - * @param typeargtypes The types of the constructor invocation's type arguments. - * @param allowBoxing Allow boxing conversions of arguments. - * @param useVarargs Box trailing arguments into an array for varargs. - */ - private Symbol findDiamond(DiagnosticPosition pos, - Env env, - Type site, - List argtypes, - List typeargtypes, - boolean allowBoxing, - boolean useVarargs) { - Symbol sym = findDiamond(env, site, argtypes, typeargtypes, allowBoxing, useVarargs); - chk.checkDeprecated(pos, env.info.scope.owner, sym); - chk.checkPreview(pos, sym); - return sym; - } - /** This method scans all the constructor symbol in a given class scope - * assuming that the original scope contains a constructor of the kind: * {@code Foo(X x, Y y)}, where X,Y are class type-variables declared in Foo, From 1d06ad1cf803f8bdc0cac6928d91b14869f8dde9 Mon Sep 17 00:00:00 2001 From: Guoxiong Li Date: Sat, 5 Dec 2020 03:07:00 +0800 Subject: [PATCH 4/5] Revise test. Remove legal header and add /nodynamiccopyright/. --- .../tools/javac/T8257037/T8257037.java | 25 +------------------ .../tools/javac/T8257037/T8257037.out | 2 +- 2 files changed, 2 insertions(+), 25 deletions(-) diff --git a/test/langtools/tools/javac/T8257037/T8257037.java b/test/langtools/tools/javac/T8257037/T8257037.java index 31206033553c3..cb2730c683892 100644 --- a/test/langtools/tools/javac/T8257037/T8257037.java +++ b/test/langtools/tools/javac/T8257037/T8257037.java @@ -1,28 +1,5 @@ /* - * Copyright (c) 2020, 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 + * @test /nodynamiccopyright/ * @bug 8257307 * @summary No javac warning when calling deprecated constructor with diamond * @run compile/ref=T8257037.out -Xlint -XDrawDiagnostics T8257037.java diff --git a/test/langtools/tools/javac/T8257037/T8257037.out b/test/langtools/tools/javac/T8257037/T8257037.out index 13497256d1fec..b5def45e5c37d 100644 --- a/test/langtools/tools/javac/T8257037/T8257037.out +++ b/test/langtools/tools/javac/T8257037/T8257037.out @@ -1,2 +1,2 @@ -T8257037.java:32:42: compiler.warn.has.been.deprecated: T8257037_GenericClass(), T8257037_GenericClass +T8257037.java:9:42: compiler.warn.has.been.deprecated: T8257037_GenericClass(), T8257037_GenericClass 1 warning From c4de5fa5a8858fbca6b996238a51aee4a4d22ddd Mon Sep 17 00:00:00 2001 From: Guoxiong Li Date: Mon, 7 Dec 2020 19:52:14 +0800 Subject: [PATCH 5/5] Fall back to the previos version. --- .../com/sun/tools/javac/comp/Attr.java | 13 ++++--- .../com/sun/tools/javac/comp/Resolve.java | 34 +++++++++++++++++-- 2 files changed, 40 insertions(+), 7 deletions(-) diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java index ea1853851d0cc..df21524229c0e 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java @@ -4459,10 +4459,15 @@ else if (ownOuter.hasTag(CLASS) && site != ownOuter) { } // Emit a `deprecation' warning if symbol is deprecated. - chk.checkDeprecated(tree.pos(), env.info.scope.owner, sym); - chk.checkSunAPI(tree.pos(), sym); - chk.checkProfile(tree.pos(), sym); - chk.checkPreview(tree.pos(), sym); + // (for constructors (but not for constructor references), the error + // was given when the constructor was resolved) + + if (sym.name != names.init || tree.hasTag(REFERENCE)) { + chk.checkDeprecated(tree.pos(), env.info.scope.owner, sym); + chk.checkSunAPI(tree.pos(), sym); + chk.checkProfile(tree.pos(), sym); + chk.checkPreview(tree.pos(), sym); + } // If symbol is a variable, check that its type and // kind are compatible with the prototype and protokind. diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java index 640c79dabdea2..e519043d96c39 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Resolve.java @@ -2855,8 +2855,13 @@ Symbol findConstructor(DiagnosticPosition pos, Env env, List typeargtypes, boolean allowBoxing, boolean useVarargs) { - return findMethod(env, site, names.init, argtypes, - typeargtypes, allowBoxing, useVarargs); + Symbol sym = findMethod(env, site, + names.init, argtypes, + typeargtypes, allowBoxing, + useVarargs); + chk.checkDeprecated(pos, env.info.scope.owner, sym); + chk.checkPreview(pos, sym); + return sym; } /** Resolve constructor using diamond inference. @@ -2878,7 +2883,7 @@ Symbol resolveDiamond(DiagnosticPosition pos, new BasicLookupHelper(names.init, site, argtypes, typeargtypes) { @Override Symbol doLookup(Env env, MethodResolutionPhase phase) { - return findDiamond(env, site, argtypes, typeargtypes, + return findDiamond(pos, env, site, argtypes, typeargtypes, phase.isBoxingRequired(), phase.isVarargsRequired()); } @@ -2901,6 +2906,29 @@ Symbol access(Env env, DiagnosticPosition pos, Symbol location, Sym }}); } + /** Find the constructor using diamond inference and do some checks(deprecated and preview). + * @param pos The position to use for error reporting. + * @param env The environment current at the constructor invocation. + * @param site The type of class for which a constructor is searched. + * The scope of this class has been touched in attribution. + * @param argtypes The types of the constructor invocation's value arguments. + * @param typeargtypes The types of the constructor invocation's type arguments. + * @param allowBoxing Allow boxing conversions of arguments. + * @param useVarargs Box trailing arguments into an array for varargs. + */ + private Symbol findDiamond(DiagnosticPosition pos, + Env env, + Type site, + List argtypes, + List typeargtypes, + boolean allowBoxing, + boolean useVarargs) { + Symbol sym = findDiamond(env, site, argtypes, typeargtypes, allowBoxing, useVarargs); + chk.checkDeprecated(pos, env.info.scope.owner, sym); + chk.checkPreview(pos, sym); + return sym; + } + /** This method scans all the constructor symbol in a given class scope - * assuming that the original scope contains a constructor of the kind: * {@code Foo(X x, Y y)}, where X,Y are class type-variables declared in Foo,