Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8269121: Type inference bug with method references #5406

Closed
@@ -44,6 +44,7 @@
import com.sun.tools.javac.code.Symbol.VarSymbol;
import com.sun.tools.javac.code.Symtab;
import com.sun.tools.javac.code.Type;
import com.sun.tools.javac.code.Type.IntersectionClassType;
import com.sun.tools.javac.code.Type.MethodType;
import com.sun.tools.javac.code.Type.TypeVar;
import com.sun.tools.javac.code.Types;
@@ -1084,11 +1085,42 @@ private VarSymbol addParameter(String name, Type p, boolean genArg) {
}

private MethodType typeToMethodType(Type mt) {
return typeToMethodType(mt, null);
}

private MethodType typeToMethodType(Type mt, MethodType interfaceMethodType) {
Copy link
Contributor

@mcimadamore mcimadamore Sep 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is nice that we can put all the complexity in this method. I wonder if we could somehow make this more general - e.g. instead of detecting whether there is an intersection type - simply compare the erased signature of the function descriptor with the erased signature of the lambda method, and see if the two are compatible. I believe that, maybe, a resolution check might work (e.g. try to pass the erased parameter to the erased function descriptor and see if we get a resolution error or not). In Lower we do something similar - see Lower::lookupMethod.

Another option could be to always use the erased function descriptor type, and then add the required casts inside the lambda method implementation body (in most cases there would be no casts) - e.g. do not rely on the conversions handled by the lambda metafactory, but generate code that is more 1-1 with what the metafactory expects (and fix it up on the javac side). But doing this might change signature for some of the lambda bodies - which, if we take into account serializable lambdas, could be problematic.

Type type = types.erasure(mt);
return new MethodType(type.getParameterTypes(),
type.getReturnType(),
type.getThrownTypes(),
syms.methodClass);
ListBuffer<Type> parameterTypes = new ListBuffer<>();
if (interfaceMethodType != null && mt.getParameterTypes().stream().anyMatch(Type::isIntersection)) {
// we need to make sure that we are selecting the right component of the intersection to erase to
List<Type> interfaceParamTypes = interfaceMethodType.getParameterTypes();
for (Type paramType : mt.getParameterTypes()) {
if (paramType.isIntersection()) {
parameterTypes.add(selectIntersectionComponent((IntersectionClassType)paramType, interfaceParamTypes.head));
} else {
parameterTypes.add(types.erasure(paramType));
}
interfaceParamTypes = interfaceParamTypes.tail;
}
}
return new MethodType(
parameterTypes.isEmpty() ? type.getParameterTypes() : parameterTypes.toList(),
type.getReturnType(),
type.getThrownTypes(),
syms.methodClass);
}

private Type selectIntersectionComponent(IntersectionClassType intersection, Type interfaceParamType) {
for (Type component : intersection.getComponents()) {
// skip object
if (component.tsym == syms.objectType.tsym) { continue; }
Type erasedComponent = types.erasure(component);
if (types.isAssignable(erasedComponent, interfaceParamType)) {
return erasedComponent;
}
}
// bail out
return types.erasure(intersection);
}

/**
@@ -1099,10 +1131,11 @@ private JCExpression makeMetafactoryIndyCall(TranslationContext<?> context,
JCFunctionalExpression tree = context.tree;
//determine the static bsm args
MethodSymbol samSym = (MethodSymbol) types.findDescriptorSymbol(tree.target.tsym);
MethodType intMethodType = typeToMethodType(samSym.type);
List<LoadableConstant> staticArgs = List.of(
typeToMethodType(samSym.type),
intMethodType,
refSym.asHandle(),
typeToMethodType(tree.getDescriptorType(types)));
typeToMethodType(tree.getDescriptorType(types), intMethodType));

//computed indy arg types
ListBuffer<Type> indy_args_types = new ListBuffer<>();
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2021, 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.
*
* 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 8269121
* @summary Type inference bug with method references
*/

public class MethodReferenceIntersection5 {
interface StringLiteral {}

interface Variable {}

class MyFact {
static Object make (StringLiteral v) { return null; }
}

interface OneVariableQuery<VarType extends Variable> {
Object query(VarType var1);
}

static class Interpreter {
<VarType extends Variable> Object query(OneVariableQuery<VarType> query) { return null; }
}

public static void main(String[] args) {
new Interpreter().query(MyFact::make);
}
}