提交 619d9cfd 编写于 作者: M mcimadamore

8142876: Javac does not correctly implement wildcards removal from functional interfaces

Summary: Rewrite code for removing wildcard from target functional interface to be in sync with JLS 9.9
Reviewed-by: vromero, dlsmith
上级 93209fb4
......@@ -597,36 +597,42 @@ public class Types {
}
public Type removeWildcards(Type site) {
Type capturedSite = capture(site);
if (capturedSite != site) {
Type formalInterface = site.tsym.type;
ListBuffer<Type> typeargs = new ListBuffer<>();
List<Type> actualTypeargs = site.getTypeArguments();
List<Type> capturedTypeargs = capturedSite.getTypeArguments();
//simply replace the wildcards with its bound
for (Type t : formalInterface.getTypeArguments()) {
if (actualTypeargs.head.hasTag(WILDCARD)) {
WildcardType wt = (WildcardType)actualTypeargs.head;
Type bound;
switch (wt.kind) {
case EXTENDS:
case UNBOUND:
CapturedType capVar = (CapturedType)capturedTypeargs.head;
//use declared bound if it doesn't depend on formal type-args
bound = capVar.bound.containsAny(capturedSite.getTypeArguments()) ?
wt.type : capVar.bound;
break;
default:
bound = wt.type;
if (site.getTypeArguments().stream().anyMatch(t -> t.hasTag(WILDCARD))) {
//compute non-wildcard parameterization - JLS 9.9
List<Type> actuals = site.getTypeArguments();
List<Type> formals = site.tsym.type.getTypeArguments();
ListBuffer<Type> targs = new ListBuffer<>();
for (Type formal : formals) {
Type actual = actuals.head;
Type bound = formal.getUpperBound();
if (actuals.head.hasTag(WILDCARD)) {
WildcardType wt = (WildcardType)actual;
//check that bound does not contain other formals
if (bound.containsAny(formals)) {
targs.add(wt.type);
} else {
//compute new type-argument based on declared bound and wildcard bound
switch (wt.kind) {
case UNBOUND:
targs.add(bound);
break;
case EXTENDS:
targs.add(glb(bound, wt.type));
break;
case SUPER:
targs.add(wt.type);
break;
default:
Assert.error("Cannot get here!");
}
}
typeargs.append(bound);
} else {
typeargs.append(actualTypeargs.head);
//not a wildcard - the new type argument remains unchanged
targs.add(actual);
}
actualTypeargs = actualTypeargs.tail;
capturedTypeargs = capturedTypeargs.tail;
actuals = actuals.tail;
}
return subst(formalInterface, formalInterface.getTypeArguments(), typeargs.toList());
return subst(site.tsym.type, formals, targs.toList());
} else {
return site;
}
......
/*
* 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 8142876
* @summary Javac does not correctly implement wildcards removal from functional interfaces
* @compile T8142876.java
*/
class T8142876 {
interface I<R extends Runnable, T> {
void m();
}
void test() {
I<? extends O, String> succeed = this::ff;
I<? extends Comparable<String>, String> failed = this::ff;
}
interface O {}
private void ff(){}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册