diff options
author | Ivan Gavrilovic <gavra@google.com> | 2018-05-18 01:15:19 -0700 |
---|---|---|
committer | android-build-merger <android-build-merger@google.com> | 2018-05-18 01:15:19 -0700 |
commit | 455fb1ea77d24ba89024f5d1c32ec157fdb6737c (patch) | |
tree | 2d04071cbf26ac43ad3f33f79309131c0c1c7444 | |
parent | e800c4a2b12ab6f1c25462eb8ef8371919d83de3 (diff) | |
parent | 3897806126950e89f10fca7d59be77686275a88c (diff) | |
download | jdk8u_langtools-455fb1ea77d24ba89024f5d1c32ec157fdb6737c.tar.gz |
8073842: Invalid method reference when referencing a method on a wildcard type
am: 3897806126
Change-Id: I95014160b65148e6469360b0a3189a0744cfe182
7 files changed, 74 insertions, 33 deletions
diff --git a/src/share/classes/com/sun/tools/javac/code/Types.java b/src/share/classes/com/sun/tools/javac/code/Types.java index b385f284..fe4cce92 100644 --- a/src/share/classes/com/sun/tools/javac/code/Types.java +++ b/src/share/classes/com/sun/tools/javac/code/Types.java @@ -176,6 +176,18 @@ public class Types { } else return t.unannotatedType(); } + + /** + * Recursively skip type-variables until a class/array type is found; capture conversion is then + * (optionally) applied to the resulting type. This is useful for i.e. computing a site that is + * suitable for a method lookup. + */ + public Type skipTypeVars(Type site, boolean capture) { + while (site.hasTag(TYPEVAR)) { + site = site.getUpperBound(); + } + return capture ? capture(site) : site; + } // </editor-fold> // <editor-fold defaultstate="collapsed" desc="isUnbounded"> @@ -1810,12 +1822,9 @@ public class Types { } private Type relaxBound(Type t) { - if (t.hasTag(TYPEVAR)) { - while (t.hasTag(TYPEVAR)) - t = t.getUpperBound(); - t = rewriteQuantifiers(t, true, true); - } - return t; + return (t.hasTag(TYPEVAR)) ? + rewriteQuantifiers(skipTypeVars(t, false), true, true) : + t; } // </editor-fold> @@ -1896,10 +1905,7 @@ public class Types { */ private Mapping elemTypeFun = new Mapping ("elemTypeFun") { public Type apply(Type t) { - while (t.hasTag(TYPEVAR)) { - t = t.getUpperBound(); - } - return elemtype(t); + return elemtype(skipTypeVars(t, false)); } }; @@ -2683,8 +2689,7 @@ public class Types { private MethodSymbol implementationInternal(MethodSymbol ms, TypeSymbol origin, boolean checkResult, Filter<Symbol> implFilter) { for (Type t = origin.type; t.hasTag(CLASS) || t.hasTag(TYPEVAR); t = supertype(t)) { - while (t.hasTag(TYPEVAR)) - t = t.getUpperBound(); + t = skipTypeVars(t, false); TypeSymbol c = t.tsym; for (Scope.Entry e = c.members().lookup(ms.name, implFilter); e.scope != null; diff --git a/src/share/classes/com/sun/tools/javac/comp/Attr.java b/src/share/classes/com/sun/tools/javac/comp/Attr.java index 96148f02..16babcde 100644 --- a/src/share/classes/com/sun/tools/javac/comp/Attr.java +++ b/src/share/classes/com/sun/tools/javac/comp/Attr.java @@ -3288,8 +3288,7 @@ public class Attr extends JCTree.Visitor { tree.sym = sym; if (site.hasTag(TYPEVAR) && !isType(sym) && sym.kind != ERR) { - while (site.hasTag(TYPEVAR)) site = site.getUpperBound(); - site = capture(site); + site = types.skipTypeVars(site, true); } // If that symbol is a variable, ... diff --git a/src/share/classes/com/sun/tools/javac/comp/DeferredAttr.java b/src/share/classes/com/sun/tools/javac/comp/DeferredAttr.java index c67715e4..17b046c1 100644 --- a/src/share/classes/com/sun/tools/javac/comp/DeferredAttr.java +++ b/src/share/classes/com/sun/tools/javac/comp/DeferredAttr.java @@ -1336,11 +1336,7 @@ public class DeferredAttr extends JCTree.Visitor { site = env.enclClass.sym.type; } - while (site.hasTag(TYPEVAR)) { - site = site.getUpperBound(); - } - - site = types.capture(site); + site = types.skipTypeVars(site, true); List<Type> args = rs.dummyArgs(tree.args.length()); Name name = TreeInfo.name(tree.meth); diff --git a/src/share/classes/com/sun/tools/javac/comp/Lower.java b/src/share/classes/com/sun/tools/javac/comp/Lower.java index 717a3160..acfb89a2 100644 --- a/src/share/classes/com/sun/tools/javac/comp/Lower.java +++ b/src/share/classes/com/sun/tools/javac/comp/Lower.java @@ -3485,10 +3485,7 @@ public class Lower extends TreeTranslator { syms.iterableType.tsym); if (iterableType.getTypeArguments().nonEmpty()) iteratorTarget = types.erasure(iterableType.getTypeArguments().head); - Type eType = tree.expr.type; - while (eType.hasTag(TYPEVAR)) { - eType = eType.getUpperBound(); - } + Type eType = types.skipTypeVars(tree.expr.type, false); tree.expr.type = types.erasure(eType); if (eType.isCompound()) tree.expr = make.TypeCast(types.erasure(iterableType), tree.expr); 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 064b3bb9..e0ac3ac0 100644 --- a/src/share/classes/com/sun/tools/javac/comp/Resolve.java +++ b/src/share/classes/com/sun/tools/javac/comp/Resolve.java @@ -1802,8 +1802,7 @@ public class Resolve { !t.hasTag(TYPEVAR)) { return null; } - while (t.hasTag(TYPEVAR)) - t = t.getUpperBound(); + t = types.skipTypeVars(t, false); if (seen.contains(t.tsym)) { //degenerate case in which we have a circular //class hierarchy - because of ill-formed classfiles @@ -2779,11 +2778,10 @@ public class Resolve { InferenceContext inferenceContext) { boolean isStaticSelector = TreeInfo.isStaticSelector(referenceTree.expr, names); - site = types.capture(site); + //step 1 - bound lookup ReferenceLookupHelper boundLookupHelper = makeReferenceLookupHelper( referenceTree, site, name, argtypes, null, VARARITY); - //step 1 - bound lookup Env<AttrContext> boundEnv = env.dup(env.tree, env.info.dup()); Symbol boundSym = lookupMethod(boundEnv, env.tree.pos(), site.tsym, arityMethodCheck, boundLookupHelper); @@ -3182,9 +3180,13 @@ public class Resolve { */ class MethodReferenceLookupHelper extends ReferenceLookupHelper { + /** The original method reference lookup site. */ + Type originalSite; + MethodReferenceLookupHelper(JCMemberReference referenceTree, Name name, Type site, List<Type> argtypes, List<Type> typeargtypes, MethodResolutionPhase maxPhase) { - super(referenceTree, name, site, argtypes, typeargtypes, maxPhase); + super(referenceTree, name, types.skipTypeVars(site, true), argtypes, typeargtypes, maxPhase); + this.originalSite = site; } @Override @@ -3200,7 +3202,7 @@ public class Resolve { (argtypes.head.hasTag(NONE) || types.isSubtypeUnchecked(inferenceContext.asUndetVar(argtypes.head), site))) { return new UnboundMethodReferenceLookupHelper(referenceTree, name, - site, argtypes, typeargtypes, maxPhase); + originalSite, argtypes, typeargtypes, maxPhase); } else { return super.unboundLookup(inferenceContext); } @@ -3233,7 +3235,7 @@ public class Resolve { super(referenceTree, name, site, argtypes.tail, typeargtypes, maxPhase); if (site.isRaw() && !argtypes.head.hasTag(NONE)) { Type asSuperSite = types.asSuper(argtypes.head, site.tsym); - this.site = types.capture(asSuperSite); + this.site = types.skipTypeVars(asSuperSite, true); } } diff --git a/src/share/classes/com/sun/tools/javac/comp/TransTypes.java b/src/share/classes/com/sun/tools/javac/comp/TransTypes.java index 51c42f7e..fbbff9cc 100644 --- a/src/share/classes/com/sun/tools/javac/comp/TransTypes.java +++ b/src/share/classes/com/sun/tools/javac/comp/TransTypes.java @@ -846,9 +846,7 @@ public class TransTypes extends TreeTranslator { } public void visitSelect(JCFieldAccess tree) { - Type t = tree.selected.type; - while (t.hasTag(TYPEVAR)) - t = t.getUpperBound(); + Type t = types.skipTypeVars(tree.selected.type, false); if (t.isCompound()) { if ((tree.sym.flags() & IPROXY) != 0) { tree.sym = ((MethodSymbol)tree.sym). diff --git a/test/tools/javac/lambda/8073842/T8073842.java b/test/tools/javac/lambda/8073842/T8073842.java new file mode 100644 index 00000000..f2dde5c7 --- /dev/null +++ b/test/tools/javac/lambda/8073842/T8073842.java @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2015, 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. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * 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 8073842 + * @summary Invalid method reference when referencing a method on a wildcard type + * @compile T8073842.java + */ + +import java.util.stream.Stream; + +class T8073842 { + + static class Chunck { + public void work() { } + } + + void test(Stream<? extends Chunck> s) { + Stream<Runnable> r = s.map(o -> o::work); + } +}
\ No newline at end of file |