mirror of
https://github.com/openjdk/jdk.git
synced 2025-09-20 02:54:35 +02:00
8142876: Javac does not correctly implement wildcards removal from functional interfaces
Rewrite code for removing wildcard from target functional interface to be in sync with JLS 9.9 Reviewed-by: vromero, dlsmith
This commit is contained in:
parent
8f0d6f6b81
commit
1e3900cbfb
2 changed files with 77 additions and 26 deletions
|
@ -597,36 +597,42 @@ public class Types {
|
||||||
}
|
}
|
||||||
|
|
||||||
public Type removeWildcards(Type site) {
|
public Type removeWildcards(Type site) {
|
||||||
Type capturedSite = capture(site);
|
if (site.getTypeArguments().stream().anyMatch(t -> t.hasTag(WILDCARD))) {
|
||||||
if (capturedSite != site) {
|
//compute non-wildcard parameterization - JLS 9.9
|
||||||
Type formalInterface = site.tsym.type;
|
List<Type> actuals = site.getTypeArguments();
|
||||||
ListBuffer<Type> typeargs = new ListBuffer<>();
|
List<Type> formals = site.tsym.type.getTypeArguments();
|
||||||
List<Type> actualTypeargs = site.getTypeArguments();
|
ListBuffer<Type> targs = new ListBuffer<>();
|
||||||
List<Type> capturedTypeargs = capturedSite.getTypeArguments();
|
for (Type formal : formals) {
|
||||||
//simply replace the wildcards with its bound
|
Type actual = actuals.head;
|
||||||
for (Type t : formalInterface.getTypeArguments()) {
|
Type bound = formal.getUpperBound();
|
||||||
if (actualTypeargs.head.hasTag(WILDCARD)) {
|
if (actuals.head.hasTag(WILDCARD)) {
|
||||||
WildcardType wt = (WildcardType)actualTypeargs.head;
|
WildcardType wt = (WildcardType)actual;
|
||||||
Type bound;
|
//check that bound does not contain other formals
|
||||||
switch (wt.kind) {
|
if (bound.containsAny(formals)) {
|
||||||
case EXTENDS:
|
targs.add(wt.type);
|
||||||
case UNBOUND:
|
} else {
|
||||||
CapturedType capVar = (CapturedType)capturedTypeargs.head;
|
//compute new type-argument based on declared bound and wildcard bound
|
||||||
//use declared bound if it doesn't depend on formal type-args
|
switch (wt.kind) {
|
||||||
bound = capVar.bound.containsAny(capturedSite.getTypeArguments()) ?
|
case UNBOUND:
|
||||||
wt.type : capVar.bound;
|
targs.add(bound);
|
||||||
break;
|
break;
|
||||||
default:
|
case EXTENDS:
|
||||||
bound = wt.type;
|
targs.add(glb(bound, wt.type));
|
||||||
|
break;
|
||||||
|
case SUPER:
|
||||||
|
targs.add(wt.type);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
Assert.error("Cannot get here!");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
typeargs.append(bound);
|
|
||||||
} else {
|
} else {
|
||||||
typeargs.append(actualTypeargs.head);
|
//not a wildcard - the new type argument remains unchanged
|
||||||
|
targs.add(actual);
|
||||||
}
|
}
|
||||||
actualTypeargs = actualTypeargs.tail;
|
actuals = actuals.tail;
|
||||||
capturedTypeargs = capturedTypeargs.tail;
|
|
||||||
}
|
}
|
||||||
return subst(formalInterface, formalInterface.getTypeArguments(), typeargs.toList());
|
return subst(site.tsym.type, formals, targs.toList());
|
||||||
} else {
|
} else {
|
||||||
return site;
|
return site;
|
||||||
}
|
}
|
||||||
|
|
45
langtools/test/tools/javac/lambda/8142876/T8142876.java
Normal file
45
langtools/test/tools/javac/lambda/8142876/T8142876.java
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
/*
|
||||||
|
* 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(){}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue