diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Source.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Source.java index 063d37be4e2..d0cb45ebc09 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Source.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Source.java @@ -277,6 +277,7 @@ public enum Source { JAVA_BASE_TRANSITIVE(JDK25, Fragments.FeatureJavaBaseTransitive, DiagKind.PLURAL), PRIVATE_MEMBERS_IN_PERMITS_CLAUSE(JDK19), ERASE_POLY_SIG_RETURN_TYPE(JDK24), + CAPTURE_MREF_RETURN_TYPE(JDK26), ; enum DiagKind { diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java index 1465ea652b1..4a8a8d4a324 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java @@ -124,6 +124,7 @@ public class Attr extends JCTree.Visitor { final ArgumentAttr argumentAttr; final MatchBindingsComputer matchBindingsComputer; final AttrRecover attrRecover; + final boolean captureMRefReturnType; public static Attr instance(Context context) { Attr instance = context.get(attrKey); @@ -175,6 +176,7 @@ public class Attr extends JCTree.Visitor { Feature.UNCONDITIONAL_PATTERN_IN_INSTANCEOF.allowedInSource(source); sourceName = source.name; useBeforeDeclarationWarning = options.isSet("useBeforeDeclarationWarning"); + captureMRefReturnType = Source.Feature.ERASE_POLY_SIG_RETURN_TYPE.allowedInSource(source); statInfo = new ResultInfo(KindSelector.NIL, Type.noType); varAssignmentInfo = new ResultInfo(KindSelector.ASG, Type.noType); @@ -3832,9 +3834,10 @@ public class Attr extends JCTree.Visitor { } if (!returnType.hasTag(VOID) && !resType.hasTag(VOID)) { + Type capturedResType = captureMRefReturnType ? types.capture(resType) : resType; if (resType.isErroneous() || - new FunctionalReturnContext(checkContext).compatible(resType, returnType, - checkContext.checkWarner(tree, resType, returnType))) { + new FunctionalReturnContext(checkContext).compatible(capturedResType, returnType, + checkContext.checkWarner(tree, capturedResType, returnType))) { incompatibleReturnType = null; } } diff --git a/test/langtools/tools/javac/lambda/methodReference/ResultTypeNotBeingCapturedTest.java b/test/langtools/tools/javac/lambda/methodReference/ResultTypeNotBeingCapturedTest.java new file mode 100644 index 00000000000..a87dd8b302d --- /dev/null +++ b/test/langtools/tools/javac/lambda/methodReference/ResultTypeNotBeingCapturedTest.java @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2025, 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 8369517 + * @summary Compilation mismatch for equivalent lambda and method reference + * @compile/fail/ref=ResultTypeNotBeingCapturedTest.out -XDrawDiagnostics ResultTypeNotBeingCapturedTest.java + */ + +import java.util.function.Supplier; + +class ResultTypeNotBeingCapturedTest { + interface X { + X self(); + } + + static X makeX() {return null;} + + static X create(Supplier supplier) {return null;} + + static X> methodRef() { + return create(ResultTypeNotBeingCapturedTest::makeX).self(); + } + + static X> lambda() { + return create(() -> makeX()).self(); + } +} diff --git a/test/langtools/tools/javac/lambda/methodReference/ResultTypeNotBeingCapturedTest.out b/test/langtools/tools/javac/lambda/methodReference/ResultTypeNotBeingCapturedTest.out new file mode 100644 index 00000000000..bcd4dbad07b --- /dev/null +++ b/test/langtools/tools/javac/lambda/methodReference/ResultTypeNotBeingCapturedTest.out @@ -0,0 +1,3 @@ +ResultTypeNotBeingCapturedTest.java:43:66: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: ResultTypeNotBeingCapturedTest.X>, ResultTypeNotBeingCapturedTest.X>) +ResultTypeNotBeingCapturedTest.java:47:42: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: ResultTypeNotBeingCapturedTest.X>, ResultTypeNotBeingCapturedTest.X>) +2 errors