Skip to content

Commit

Permalink
8253944: Certain method references to VarHandle methods should fail
Browse files Browse the repository at this point in the history
Reviewed-by: mcimadamore
  • Loading branch information
Paul Sandoz committed Oct 5, 2020
1 parent 88d75c9 commit b29e108
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 33 deletions.
51 changes: 19 additions & 32 deletions src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Infer.java
@@ -1,5 +1,5 @@
/* /*
* Copyright (c) 1999, 2019, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -557,37 +557,24 @@ Type instantiatePolymorphicSignatureInstance(Env<AttrContext> env,
List<Type> argtypes) { List<Type> argtypes) {
final Type restype; final Type restype;


if (spMethod == null || types.isSameType(spMethod.getReturnType(), syms.objectType)) { Type spType = spMethod == null ? syms.objectType : spMethod.getReturnType();
// The return type of the polymorphic signature is polymorphic,
// and is computed from the enclosing tree E, as follows: switch (env.next.tree.getTag()) {
// if E is a cast, then use the target type of the cast expression case TYPECAST:
// as a return type; if E is an expression statement, the return JCTypeCast castTree = (JCTypeCast)env.next.tree;
// type is 'void'; otherwise restype = (TreeInfo.skipParens(castTree.expr) == env.tree) ?
// the return type is simply 'Object'. A correctness check ensures castTree.clazz.type :
// that env.next refers to the lexically enclosing environment in spType;
// which the polymorphic signature call environment is nested. break;

case EXEC:
switch (env.next.tree.getTag()) { JCTree.JCExpressionStatement execTree =
case TYPECAST: (JCTree.JCExpressionStatement)env.next.tree;
JCTypeCast castTree = (JCTypeCast)env.next.tree; restype = (TreeInfo.skipParens(execTree.expr) == env.tree) ?
restype = (TreeInfo.skipParens(castTree.expr) == env.tree) ? syms.voidType :
castTree.clazz.type : spType;
syms.objectType; break;
break; default:
case EXEC: restype = spType;
JCTree.JCExpressionStatement execTree =
(JCTree.JCExpressionStatement)env.next.tree;
restype = (TreeInfo.skipParens(execTree.expr) == env.tree) ?
syms.voidType :
syms.objectType;
break;
default:
restype = syms.objectType;
}
} else {
// The return type of the polymorphic signature is fixed
// (not polymorphic)
restype = spMethod.getReturnType();
} }


List<Type> paramtypes = argtypes.map(new ImplicitArgType(spMethod, resolveContext.step)); List<Type> paramtypes = argtypes.map(new ImplicitArgType(spMethod, resolveContext.step));
Expand Down
Expand Up @@ -2742,11 +2742,23 @@ Symbol findPolymorphicSignatureInstance(final Symbol spMethod,
// Check that there is already a method symbol for the method // Check that there is already a method symbol for the method
// type and owner // type and owner
if (types.isSameType(mtype, sym.type) && if (types.isSameType(mtype, sym.type) &&
spMethod.owner == sym.owner) { spMethod.owner == sym.owner) {
return sym; return sym;
} }
} }


Type spReturnType = spMethod.asType().getReturnType();
if (types.isSameType(spReturnType, syms.objectType)) {
// Polymorphic return, pass through mtype
} else if (!types.isSameType(spReturnType, mtype.getReturnType())) {
// Retain the sig poly method's return type, which differs from that of mtype
// Will result in an incompatible return type error
mtype = new MethodType(mtype.getParameterTypes(),
spReturnType,
mtype.getThrownTypes(),
syms.methodClass);
}

// Create the desired method // Create the desired method
// Retain static modifier is to support invocations to // Retain static modifier is to support invocations to
// MethodHandle.linkTo* methods // MethodHandle.linkTo* methods
Expand Down
@@ -0,0 +1,66 @@
/*
* Copyright (c) 2020, 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
* @summary test for VarHandle signature polymorphic methods
* @run testng MethodReferenceTestVarHandle
*/

import java.lang.invoke.*;
import java.util.*;

import org.testng.annotations.Test;
import static org.testng.Assert.assertEquals;

@Test
public class MethodReferenceTestVarHandle {

interface Setter {
void apply(int[] arr, int idx, int val);
}

interface Getter {
int apply(int[] arr, int idx);
}

public void testSet() throws Throwable {
VarHandle vh = MethodHandles.arrayElementVarHandle(int[].class);

Setter f = vh::set;

int[] data = {0};
f.apply(data, 0, 42);
assertEquals(42, data[0]);
}

public void testGet() throws Throwable {
VarHandle vh = MethodHandles.arrayElementVarHandle(int[].class);

Getter f = vh::get;

int[] data = {42};
int v = f.apply(data, 0);
assertEquals(42, v);
}
}
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2020, 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
* @summary test for VarHandle signature polymorphic methods with wrong return type
* @compile/fail/ref=MethodReferenceTestVarHandle_neg.out -XDrawDiagnostics MethodReferenceTestVarHandle_neg.java
*/

import java.lang.invoke.*;
import java.util.*;

public class MethodReferenceTestVarHandle_neg {

interface Setter {
int apply(int[] arr, int idx, int val);
}

public static void main(String[] args) {
VarHandle vh = MethodHandles.arrayElementVarHandle(int[].class);

// Return type of Setter::apply does not match return type of VarHandle::set
Setter f = vh::set;
}
}
@@ -0,0 +1,2 @@
MethodReferenceTestVarHandle_neg.java:43:18: compiler.err.prob.found.req: (compiler.misc.incompatible.ret.type.in.mref: (compiler.misc.inconvertible.types: void, int))
1 error

1 comment on commit b29e108

@bridgekeeper
Copy link

@bridgekeeper bridgekeeper bot commented on b29e108 Oct 5, 2020

Choose a reason for hiding this comment

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

Please sign in to comment.