Tue, 26 Nov 2013 15:27:19 +0100
8026374: javac accepts void as a method parameter
Summary: Changing Check.validate to reject void types.
Reviewed-by: jjg, vromero
1.1 --- a/src/share/classes/com/sun/tools/javac/comp/Attr.java Tue Nov 26 13:33:33 2013 +0100 1.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Attr.java Tue Nov 26 15:27:19 2013 +0100 1.3 @@ -933,7 +933,8 @@ 1.4 chk.validate(tree.typarams, localEnv); 1.5 1.6 // Check that result type is well-formed. 1.7 - chk.validate(tree.restype, localEnv); 1.8 + if (tree.restype != null && !tree.restype.type.hasTag(VOID)) 1.9 + chk.validate(tree.restype, localEnv); 1.10 1.11 // Check that receiver type is well-formed. 1.12 if (tree.recvparam != null) {
2.1 --- a/src/share/classes/com/sun/tools/javac/comp/Check.java Tue Nov 26 13:33:33 2013 +0100 2.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Check.java Tue Nov 26 15:27:19 2013 +0100 2.3 @@ -1326,6 +1326,14 @@ 2.4 tree.underlyingType.accept(this); 2.5 } 2.6 2.7 + @Override 2.8 + public void visitTypeIdent(JCPrimitiveTypeTree that) { 2.9 + if (that.type.hasTag(TypeTag.VOID)) { 2.10 + log.error(that.pos(), "void.not.allowed.here"); 2.11 + } 2.12 + super.visitTypeIdent(that); 2.13 + } 2.14 + 2.15 /** Default visitor method: do nothing. 2.16 */ 2.17 @Override
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/test/tools/javac/declaration/method/MethodVoidParameter.java Tue Nov 26 15:27:19 2013 +0100 3.3 @@ -0,0 +1,9 @@ 3.4 +/* @test /nodynamiccopyright/ 3.5 + * @bug 8026374 3.6 + * @summary Cannot use void as a variable type 3.7 + * @compile/fail/ref=MethodVoidParameter.out -XDrawDiagnostics MethodVoidParameter.java 3.8 + */ 3.9 +public class MethodVoidParameter { 3.10 + void method(void v) { } 3.11 + void method(void... v) { } 3.12 +}
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/test/tools/javac/declaration/method/MethodVoidParameter.out Tue Nov 26 15:27:19 2013 +0100 4.3 @@ -0,0 +1,3 @@ 4.4 +MethodVoidParameter.java:7:17: compiler.err.void.not.allowed.here 4.5 +MethodVoidParameter.java:8:17: compiler.err.void.not.allowed.here 4.6 +2 errors