mirror of
https://github.com/openjdk/jdk.git
synced 2025-09-19 18:44:38 +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) {
|
||||
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;
|
||||
}
|
||||
|
|
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