From 6a3e17ac5b1e64e74f31427dc72a31591abf952e Mon Sep 17 00:00:00 2001 From: mcimadamore Date: Wed, 25 Mar 2009 10:29:28 +0000 Subject: [PATCH] 6400189: raw types and inference Summary: Fixed resolution problem with raw overriding (CCC) Reviewed-by: jjg --- .../com/sun/tools/javac/comp/Resolve.java | 51 +++++++++++++----- .../OverrideChecks/6400189/T6400189a.java | 38 +++++++++++++ .../OverrideChecks/6400189/T6400189a.out | 4 ++ .../OverrideChecks/6400189/T6400189b.java | 49 +++++++++++++++++ .../OverrideChecks/6400189/T6400189b.out | 4 ++ .../OverrideChecks/6400189/T6400189c.java | 45 ++++++++++++++++ .../OverrideChecks/6400189/T6400189d.java | 53 +++++++++++++++++++ 7 files changed, 230 insertions(+), 14 deletions(-) create mode 100644 test/tools/javac/OverrideChecks/6400189/T6400189a.java create mode 100644 test/tools/javac/OverrideChecks/6400189/T6400189a.out create mode 100644 test/tools/javac/OverrideChecks/6400189/T6400189b.java create mode 100644 test/tools/javac/OverrideChecks/6400189/T6400189b.out create mode 100644 test/tools/javac/OverrideChecks/6400189/T6400189c.java create mode 100644 test/tools/javac/OverrideChecks/6400189/T6400189d.java diff --git a/src/share/classes/com/sun/tools/javac/comp/Resolve.java b/src/share/classes/com/sun/tools/javac/comp/Resolve.java index 1a141aa5..ca490883 100644 --- a/src/share/classes/com/sun/tools/javac/comp/Resolve.java +++ b/src/share/classes/com/sun/tools/javac/comp/Resolve.java @@ -216,7 +216,9 @@ public class Resolve { && isAccessible(env, site) && - sym.isInheritedIn(site.tsym, types); + sym.isInheritedIn(site.tsym, types) + && + notOverriddenIn(site, sym); case PROTECTED: return (env.toplevel.packge == sym.owner.owner // fast special case @@ -231,14 +233,23 @@ public class Resolve { && isAccessible(env, site) && - // `sym' is accessible only if not overridden by - // another symbol which is a member of `site' - // (because, if it is overridden, `sym' is not strictly - // speaking a member of `site'.) - (sym.kind != MTH || sym.isConstructor() || sym.isStatic() || - ((MethodSymbol)sym).implementation(site.tsym, types, true) == sym); + notOverriddenIn(site, sym); default: // this case includes erroneous combinations as well - return isAccessible(env, site); + return isAccessible(env, site) && notOverriddenIn(site, sym); + } + } + //where + /* `sym' is accessible only if not overridden by + * another symbol which is a member of `site' + * (because, if it is overridden, `sym' is not strictly + * speaking a member of `site'.) + */ + private boolean notOverriddenIn(Type site, Symbol sym) { + if (sym.kind != MTH || sym.isConstructor() || sym.isStatic()) + return true; + else { + Symbol s2 = ((MethodSymbol)sym).implementation(site.tsym, types, true); + return (s2 == null || s2 == sym); } } //where @@ -605,7 +616,7 @@ public class Resolve { Symbol mostSpecific(Symbol m1, Symbol m2, Env env, - Type site, + final Type site, boolean allowBoxing, boolean useVarargs) { switch (m2.kind) { @@ -661,21 +672,33 @@ public class Resolve { m2.erasure(types).getParameterTypes())) return new AmbiguityError(m1, m2); // both abstract, neither overridden; merge throws clause and result type - Symbol result; + Symbol mostSpecific; Type result2 = mt2.getReturnType(); if (mt2.tag == FORALL) result2 = types.subst(result2, ((ForAll)mt2).tvars, ((ForAll)mt1).tvars); if (types.isSubtype(mt1.getReturnType(), result2)) { - result = m1; + mostSpecific = m1; } else if (types.isSubtype(result2, mt1.getReturnType())) { - result = m2; + mostSpecific = m2; } else { // Theoretically, this can't happen, but it is possible // due to error recovery or mixing incompatible class files return new AmbiguityError(m1, m2); } - result = result.clone(result.owner); - result.type = (Type)result.type.clone(); + MethodSymbol result = new MethodSymbol( + mostSpecific.flags(), + mostSpecific.name, + null, + mostSpecific.owner) { + @Override + public MethodSymbol implementation(TypeSymbol origin, Types types, boolean checkResult) { + if (origin == site.tsym) + return this; + else + return super.implementation(origin, types, checkResult); + } + }; + result.type = (Type)mostSpecific.type.clone(); result.type.setThrown(chk.intersect(mt1.getThrownTypes(), mt2.getThrownTypes())); return result; diff --git a/test/tools/javac/OverrideChecks/6400189/T6400189a.java b/test/tools/javac/OverrideChecks/6400189/T6400189a.java new file mode 100644 index 00000000..a1808b81 --- /dev/null +++ b/test/tools/javac/OverrideChecks/6400189/T6400189a.java @@ -0,0 +1,38 @@ +/* + * Copyright 2009 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, + * CA 95054 USA or visit www.sun.com if you need additional information or + * have any questions. + */ + +/* + * @test + * @bug 6400189 + * @summary raw types and inference + * @author mcimadamore + * @compile/fail/ref=T6400189a.out T6400189a.java -Xlint:unchecked -XDrawDiagnostics + */ + +import java.lang.reflect.Constructor; +import java.lang.annotation.Documented; + +class T6400189a { + Constructor c = null; + Documented d = c.getAnnotation(Documented.class); +} diff --git a/test/tools/javac/OverrideChecks/6400189/T6400189a.out b/test/tools/javac/OverrideChecks/6400189/T6400189a.out new file mode 100644 index 00000000..39938023 --- /dev/null +++ b/test/tools/javac/OverrideChecks/6400189/T6400189a.out @@ -0,0 +1,4 @@ +T6400189a.java:37:35: compiler.warn.unchecked.call.mbr.of.raw.type: getAnnotation(java.lang.Class), java.lang.reflect.Constructor +T6400189a.java:37:35: compiler.err.prob.found.req: (compiler.misc.incompatible.types), java.lang.annotation.Annotation, java.lang.annotation.Documented +1 error +1 warning diff --git a/test/tools/javac/OverrideChecks/6400189/T6400189b.java b/test/tools/javac/OverrideChecks/6400189/T6400189b.java new file mode 100644 index 00000000..c7a6993d --- /dev/null +++ b/test/tools/javac/OverrideChecks/6400189/T6400189b.java @@ -0,0 +1,49 @@ +/* + * Copyright 2009 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, + * CA 95054 USA or visit www.sun.com if you need additional information or + * have any questions. + */ + +/* + * @test + * @bug 6400189 + * @summary raw types and inference + * @author mcimadamore + * @compile/fail/ref=T6400189b.out T6400189b.java -Xlint:unchecked -XDrawDiagnostics + */ + +class T6400189b { + + static class A { + T m(T6400189b x) { + return null; + } + } + + static class B extends A { + T m(T6400189b x) { + return null; + } + } + + void test(B b) { + Integer i = b.m(new T6400189b()); + } +} diff --git a/test/tools/javac/OverrideChecks/6400189/T6400189b.out b/test/tools/javac/OverrideChecks/6400189/T6400189b.out new file mode 100644 index 00000000..8d7711a1 --- /dev/null +++ b/test/tools/javac/OverrideChecks/6400189/T6400189b.out @@ -0,0 +1,4 @@ +T6400189b.java:47:24: compiler.warn.unchecked.call.mbr.of.raw.type: m(T6400189b), T6400189b.B +T6400189b.java:47:24: compiler.err.prob.found.req: (compiler.misc.incompatible.types), java.lang.Object, java.lang.Integer +1 error +1 warning diff --git a/test/tools/javac/OverrideChecks/6400189/T6400189c.java b/test/tools/javac/OverrideChecks/6400189/T6400189c.java new file mode 100644 index 00000000..5d95c10c --- /dev/null +++ b/test/tools/javac/OverrideChecks/6400189/T6400189c.java @@ -0,0 +1,45 @@ +/* + * Copyright 2009 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, + * CA 95054 USA or visit www.sun.com if you need additional information or + * have any questions. + */ + +/* + * @test + * @bug 6400189 + * @summary raw types and inference + * @author mcimadamore + * @compile T6400189c.java + */ + +class T6400189c { + + static class A { + T m(T6400189c x) { + return null; + } + } + + static class B extends A {} + + void test(B b) { + Integer i = b.m(new T6400189c()); + } +} diff --git a/test/tools/javac/OverrideChecks/6400189/T6400189d.java b/test/tools/javac/OverrideChecks/6400189/T6400189d.java new file mode 100644 index 00000000..12ab97f6 --- /dev/null +++ b/test/tools/javac/OverrideChecks/6400189/T6400189d.java @@ -0,0 +1,53 @@ +/* + * Copyright 2009 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, + * CA 95054 USA or visit www.sun.com if you need additional information or + * have any questions. + */ + +/* + * @test + * @bug 6400189 + * @summary raw types and inference + * @author mcimadamore + * @compile T6400189d.java + */ + +import java.util.Iterator; + +class T6400189c { + + interface A extends Iterable { + Iterator iterator(); + } + + interface A2 extends A { + Iterator iterator(); + } + + static abstract class B implements A { + public abstract Iterator iterator(); + } + + static abstract class C extends B implements A2 { + Iterator test() { + return iterator(); + } + } +} -- GitLab