diff options
author | pgovereau <none@none> | 2014-05-27 21:15:06 +0100 |
---|---|---|
committer | pgovereau <none@none> | 2014-05-27 21:15:06 +0100 |
commit | 11d26cfe1f110194ba157302f5bf0effb87457b2 (patch) | |
tree | 74588f6f71e8644fcc9e0a9c7f5405fa604bce6f | |
parent | e4694f4d5a6409e1efd320fc8b3335d7ad839889 (diff) |
8041704: wrong error message when mixing lambda expression and inner class
Reviewed-by: vromero
4 files changed, 32 insertions, 5 deletions
diff --git a/src/share/classes/com/sun/tools/javac/comp/Attr.java b/src/share/classes/com/sun/tools/javac/comp/Attr.java index a0e0b3cc..928c3dac 100644 --- a/src/share/classes/com/sun/tools/javac/comp/Attr.java +++ b/src/share/classes/com/sun/tools/javac/comp/Attr.java @@ -4671,16 +4671,30 @@ public class Attr extends JCTree.Visitor { private void initTypeIfNeeded(JCTree that) { if (that.type == null) { if (that.hasTag(METHODDEF)) { - that.type = dummyMethodType(); + that.type = dummyMethodType((JCMethodDecl)that); } else { that.type = syms.unknownType; } } } + /* Construct a dummy method type. If we have a method declaration, + * and the declared return type is void, then use that return type + * instead of UNKNOWN to avoid spurious error messages in lambda + * bodies (see:JDK-8041704). + */ + private Type dummyMethodType(JCMethodDecl md) { + Type restype = syms.unknownType; + if (md != null && md.restype.hasTag(TYPEIDENT)) { + JCPrimitiveTypeTree prim = (JCPrimitiveTypeTree)md.restype; + if (prim.typetag == VOID) + restype = syms.voidType; + } + return new MethodType(List.<Type>nil(), restype, + List.<Type>nil(), syms.methodClass); + } private Type dummyMethodType() { - return new MethodType(List.<Type>nil(), syms.unknownType, - List.<Type>nil(), syms.methodClass); + return dummyMethodType(null); } @Override diff --git a/test/tools/javac/T8030816/CrashLambdaExpressionWithNonAccessibleIdTest.out b/test/tools/javac/T8030816/CrashLambdaExpressionWithNonAccessibleIdTest.out index d253b9c6..3b2266c0 100644 --- a/test/tools/javac/T8030816/CrashLambdaExpressionWithNonAccessibleIdTest.out +++ b/test/tools/javac/T8030816/CrashLambdaExpressionWithNonAccessibleIdTest.out @@ -1,3 +1,2 @@ -CrashLambdaExpressionWithNonAccessibleIdTest.java:15:35: compiler.err.missing.ret.stmt CrashLambdaExpressionWithNonAccessibleIdTest.java:14:17: compiler.err.cant.resolve.location: kindname.class, A, , , (compiler.misc.location: kindname.class, CrashLambdaExpressionWithNonAccessibleIdTest, null) -2 errors +1 error diff --git a/test/tools/javac/lambda/T8041704/ErrorMessageTest.java b/test/tools/javac/lambda/T8041704/ErrorMessageTest.java new file mode 100644 index 00000000..5bd38c79 --- /dev/null +++ b/test/tools/javac/lambda/T8041704/ErrorMessageTest.java @@ -0,0 +1,12 @@ +/** + * @test /nodynamiccopyright/ + * @bug 8041704 + * @summary wrong error message when mixing lambda expression and inner class + * @compile/fail/ref=ErrorMessageTest.out -XDrawDiagnostics ErrorMessageTest.java + */ + +public class ErrorMessageTest { + void f(Runnable r) { + f(() -> { f(new MISSING() { public void run() {} }); }); + } +} diff --git a/test/tools/javac/lambda/T8041704/ErrorMessageTest.out b/test/tools/javac/lambda/T8041704/ErrorMessageTest.out new file mode 100644 index 00000000..bdd25993 --- /dev/null +++ b/test/tools/javac/lambda/T8041704/ErrorMessageTest.out @@ -0,0 +1,2 @@ +ErrorMessageTest.java:10:25: compiler.err.cant.resolve.location: kindname.class, MISSING, , , (compiler.misc.location: kindname.class, ErrorMessageTest, null) +1 error |