From 22e2343e572f9ed58201ddbf547f1502e1f6b18c Mon Sep 17 00:00:00 2001 From: Srikanth Adayapalam Date: Tue, 20 Jan 2015 21:49:55 +0100 Subject: [PATCH] 8046977: ClassCastException: typing information needed for method reference bridging not preserved Reviewed-by: mcimadamore --- .../classes/com/sun/tools/javac/comp/Attr.java | 15 +++++++++++++-- .../com/sun/tools/javac/comp/LambdaToMethod.java | 4 +++- .../classes/com/sun/tools/javac/tree/JCTree.java | 1 + 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java index a7303bef959..09b83fdc35b 100644 --- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java +++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java @@ -2783,7 +2783,8 @@ public class Attr extends JCTree.Visitor { @SuppressWarnings("fallthrough") void checkReferenceCompatible(JCMemberReference tree, Type descriptor, Type refType, CheckContext checkContext, boolean speculativeAttr) { - Type returnType = checkContext.inferenceContext().asUndetVar(descriptor.getReturnType()); + InferenceContext inferenceContext = checkContext.inferenceContext(); + Type returnType = inferenceContext.asUndetVar(descriptor.getReturnType()); Type resType; switch (tree.getMode()) { @@ -2812,10 +2813,20 @@ public class Attr extends JCTree.Visitor { if (incompatibleReturnType != null) { checkContext.report(tree, diags.fragment("incompatible.ret.type.in.mref", diags.fragment("inconvertible.types", resType, descriptor.getReturnType()))); + } else { + if (inferenceContext.free(refType)) { + // we need to wait for inference to finish and then replace inference vars in the referent type + inferenceContext.addFreeTypeListener(List.of(refType), + instantiatedContext -> { + tree.referentType = instantiatedContext.asInstType(refType); + }); + } else { + tree.referentType = refType; + } } if (!speculativeAttr) { - List thrownTypes = checkContext.inferenceContext().asUndetVars(descriptor.getThrownTypes()); + List thrownTypes = inferenceContext.asUndetVars(descriptor.getThrownTypes()); if (chk.unhandled(refType.getThrownTypes(), thrownTypes).nonEmpty()) { log.error(tree, "incompatible.thrown.types.in.mref", refType.getThrownTypes()); } diff --git a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java index b450b9f7f59..5ea3e5253ad 100644 --- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java +++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java @@ -889,7 +889,9 @@ public class LambdaToMethod extends TreeTranslator { convertArgs(tree.sym, args.toList(), tree.varargsElement)). setType(tree.sym.erasure(types).getReturnType()); - apply = transTypes.coerce(apply, localContext.generatedRefSig().getReturnType()); + apply = transTypes.coerce(attrEnv, apply, + types.erasure(localContext.tree.referentType.getReturnType())); + setVarargsIfNeeded(apply, tree.varargsElement); return apply; } diff --git a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/JCTree.java b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/JCTree.java index 11fff6cdd69..f1c6549247d 100644 --- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/JCTree.java +++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/tree/JCTree.java @@ -2101,6 +2101,7 @@ public abstract class JCTree implements Tree, Cloneable, DiagnosticPosition { public PolyKind refPolyKind; public boolean ownerAccessible; public OverloadKind overloadKind; + public Type referentType; public enum OverloadKind { OVERLOADED,