# HG changeset patch # User mcimadamore # Date 1511540073 0 # Node ID 6373d9a0ad825732b48450e014e91c27fba84918 # Parent 19122c10fc5285863014432f75790c2a74ef439f 8191834: Assigning a void expression to a "var" crashes the compiler Summary: local variable type inference should give error on void initializers Reviewed-by: sundar diff -r 19122c10fc52 -r 6373d9a0ad82 src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java Fri Nov 24 09:45:39 2017 +0100 +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java Fri Nov 24 16:14:33 2017 +0000 @@ -952,6 +952,9 @@ if (t.hasTag(BOT)) { log.error(pos, Errors.CantInferLocalVarType(name, Fragments.LocalCantInferNull)); return types.createErrorType(t); + } else if (t.hasTag(VOID)) { + log.error(pos, Errors.CantInferLocalVarType(name, Fragments.LocalCantInferVoid)); + return types.createErrorType(t); } return t; } diff -r 19122c10fc52 -r 6373d9a0ad82 src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties Fri Nov 24 09:45:39 2017 +0100 +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties Fri Nov 24 16:14:33 2017 +0000 @@ -1216,6 +1216,9 @@ compiler.misc.local.cant.infer.null=\ variable initializer is ''null'' +compiler.misc.local.cant.infer.void=\ + variable initializer is ''void'' + compiler.misc.local.missing.init=\ cannot use ''var'' on variable without initializer diff -r 19122c10fc52 -r 6373d9a0ad82 test/langtools/tools/javac/diags/examples/LocalCantInferVoid.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/langtools/tools/javac/diags/examples/LocalCantInferVoid.java Fri Nov 24 16:14:33 2017 +0000 @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2017, 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. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * 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. + */ + +// key: compiler.err.cant.infer.local.var.type +// key: compiler.misc.local.cant.infer.void + +class LocalCantInferVoid { + void test() { + var s = test(); + } +} diff -r 19122c10fc52 -r 6373d9a0ad82 test/langtools/tools/javac/lvti/BadLocalVarInferenceTest.java --- a/test/langtools/tools/javac/lvti/BadLocalVarInferenceTest.java Fri Nov 24 09:45:39 2017 +0100 +++ b/test/langtools/tools/javac/lvti/BadLocalVarInferenceTest.java Fri Nov 24 16:14:33 2017 +0000 @@ -1,6 +1,6 @@ /* * @test /nodynamiccopyright/ - * @bug 8177466 + * @bug 8177466 8191834 * @summary Add compiler support for local variable type-inference * @compile/fail/ref=BadLocalVarInferenceTest.out -XDrawDiagnostics BadLocalVarInferenceTest.java */ @@ -27,7 +27,10 @@ void m(String s) { } }; var s = f(x -> { x.charAt(0); }); //LHS was String + var t = m(); //void } Z f(Supplier sz) { return null; } + + void m() { } } diff -r 19122c10fc52 -r 6373d9a0ad82 test/langtools/tools/javac/lvti/BadLocalVarInferenceTest.out --- a/test/langtools/tools/javac/lvti/BadLocalVarInferenceTest.out Fri Nov 24 09:45:39 2017 +0100 +++ b/test/langtools/tools/javac/lvti/BadLocalVarInferenceTest.out Fri Nov 24 16:14:33 2017 +0000 @@ -7,4 +7,5 @@ BadLocalVarInferenceTest.java:25:29: compiler.err.does.not.override.abstract: compiler.misc.anonymous.class: BadLocalVarInferenceTest$1, m(java.lang.Object), BadLocalVarInferenceTest.Foo BadLocalVarInferenceTest.java:26:13: compiler.err.method.does.not.override.superclass BadLocalVarInferenceTest.java:29:27: compiler.err.cant.resolve.location.args: kindname.method, charAt, , int, (compiler.misc.location.1: kindname.variable, x, java.lang.Object) -9 errors +BadLocalVarInferenceTest.java:30:13: compiler.err.cant.infer.local.var.type: t, (compiler.misc.local.cant.infer.void) +10 errors