8286057: Make javac error on a generic enum friendlier

Reviewed-by: jlahoda
This commit is contained in:
Aggelos Biboudis 2022-05-24 10:31:11 +00:00 committed by Jan Lahoda
parent a0f6dd3291
commit 9473c383c6
5 changed files with 69 additions and 0 deletions
src/jdk.compiler/share/classes/com/sun/tools/javac
test/langtools/tools/javac

@ -4032,6 +4032,15 @@ public class JavacParser implements Parser {
Name name = typeName();
int typeNamePos = token.pos;
List<JCTypeParameter> typarams = typeParametersOpt(true);
if (typarams == null || !typarams.isEmpty()) {
int errorPosition = typarams == null
? typeNamePos
: typarams.head.pos;
log.error(DiagnosticFlag.SYNTAX, errorPosition, Errors.EnumCantBeGeneric);
}
List<JCExpression> implementing = List.nil();
if (token.kind == IMPLEMENTS) {
nextToken();
@ -4534,9 +4543,21 @@ public class JavacParser implements Parser {
* }
*/
protected List<JCTypeParameter> typeParametersOpt() {
return typeParametersOpt(false);
}
/** Parses a potentially empty type parameter list if needed with `allowEmpty`.
* The caller is free to choose the desirable error message in this (erroneous) case.
*/
protected List<JCTypeParameter> typeParametersOpt(boolean parseEmpty) {
if (token.kind == LT) {
ListBuffer<JCTypeParameter> typarams = new ListBuffer<>();
nextToken();
if (parseEmpty && token.kind == GT) {
accept(GT);
return null;
}
typarams.append(typeParameter());
while (token.kind == COMMA) {
nextToken();

@ -553,6 +553,9 @@ compiler.err.enum.types.not.extensible=\
compiler.err.enum.no.finalize=\
enums cannot have finalize methods
compiler.err.enum.cant.be.generic=\
enums cannot be generic
# 0: file name, 1: string
compiler.err.error.reading.file=\
error reading {0}; {1}

@ -0,0 +1,13 @@
/*
* @test /nodynamiccopyright/
* @bug 8286057
* @summary Make javac error on a generic enum friendlier
*
* @compile/fail/ref=T8286057.out -XDrawDiagnostics T8286057.java
*/
class EnumsCantBeGeneric {
public enum E1<> {}
public enum E2<T> {}
public enum E3<T, T> {}
}

@ -0,0 +1,4 @@
T8286057.java:10:17: compiler.err.enum.cant.be.generic
T8286057.java:11:18: compiler.err.enum.cant.be.generic
T8286057.java:12:18: compiler.err.enum.cant.be.generic
3 errors

@ -0,0 +1,28 @@
/*
* Copyright (c) 2022, 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.
*/
// key: compiler.err.enum.cant.be.generic
class EnumsCantBeGeneric {
public enum E<T> {}
}