Skip to content

Commit 4f3a95a

Browse files
biboudislahodaj
authored andcommitted
8309054: Parsing of erroneous patterns succeeds
Reviewed-by: jlahoda
1 parent 4f16161 commit 4f3a95a

File tree

3 files changed

+66
-37
lines changed

3 files changed

+66
-37
lines changed

src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java

+35-37
Original file line numberDiff line numberDiff line change
@@ -889,7 +889,11 @@ public void visitAnnotatedType(JCAnnotatedType tree) {
889889
} else {
890890
//type test pattern:
891891
int varPos = token.pos;
892-
JCVariableDecl var = variableDeclaratorRest(varPos, mods, e, identOrUnderscore(), false, null, false, false, true);
892+
Name name = identOrUnderscore();
893+
if (Feature.UNNAMED_VARIABLES.allowedInSource(source) && name == names.underscore) {
894+
name = names.empty;
895+
}
896+
JCVariableDecl var = toP(F.at(varPos).VarDef(mods, name, e, null));
893897
if (e == null) {
894898
var.startPos = pos;
895899
if (var.name == names.underscore && !allowVar) {
@@ -3618,7 +3622,7 @@ protected <T extends ListBuffer<? super JCVariableDecl>> T variableDeclaratorsRe
36183622
T vdefs,
36193623
boolean localDecl)
36203624
{
3621-
JCVariableDecl head = variableDeclaratorRest(pos, mods, type, name, reqInit, dc, localDecl, false, false);
3625+
JCVariableDecl head = variableDeclaratorRest(pos, mods, type, name, reqInit, dc, localDecl, false);
36223626
vdefs.append(head);
36233627
while (token.kind == COMMA) {
36243628
// All but last of multiple declarators subsume a comma
@@ -3633,7 +3637,7 @@ protected <T extends ListBuffer<? super JCVariableDecl>> T variableDeclaratorsRe
36333637
* ConstantDeclarator = Ident ConstantDeclaratorRest
36343638
*/
36353639
JCVariableDecl variableDeclarator(JCModifiers mods, JCExpression type, boolean reqInit, Comment dc, boolean localDecl) {
3636-
return variableDeclaratorRest(token.pos, mods, type, identOrUnderscore(), reqInit, dc, localDecl, true, false);
3640+
return variableDeclaratorRest(token.pos, mods, type, identOrUnderscore(), reqInit, dc, localDecl, true);
36373641
}
36383642

36393643
/** VariableDeclaratorRest = BracketsOpt ["=" VariableInitializer]
@@ -3643,13 +3647,13 @@ JCVariableDecl variableDeclarator(JCModifiers mods, JCExpression type, boolean r
36433647
* @param dc The documentation comment for the variable declarations, or null.
36443648
*/
36453649
JCVariableDecl variableDeclaratorRest(int pos, JCModifiers mods, JCExpression type, Name name,
3646-
boolean reqInit, Comment dc, boolean localDecl, boolean compound, boolean isTypePattern) {
3650+
boolean reqInit, Comment dc, boolean localDecl, boolean compound) {
36473651
boolean declaredUsingVar = false;
3648-
type = bracketsOpt(type);
36493652
JCExpression init = null;
3653+
type = bracketsOpt(type);
36503654

36513655
if (Feature.UNNAMED_VARIABLES.allowedInSource(source) && name == names.underscore) {
3652-
if (!localDecl && !isTypePattern) {
3656+
if (!localDecl) {
36533657
log.error(DiagnosticFlag.SYNTAX, pos, Errors.UseOfUnderscoreNotAllowed);
36543658
}
36553659
name = names.empty;
@@ -3668,38 +3672,32 @@ JCVariableDecl variableDeclaratorRest(int pos, JCModifiers mods, JCExpression ty
36683672
syntaxError(token.pos, Errors.Expected(EQ));
36693673
}
36703674

3671-
JCVariableDecl result;
3672-
if (!isTypePattern) {
3673-
int startPos = Position.NOPOS;
3674-
JCTree elemType = TreeInfo.innermostType(type, true);
3675-
if (elemType.hasTag(IDENT)) {
3676-
Name typeName = ((JCIdent) elemType).name;
3677-
if (restrictedTypeNameStartingAtSource(typeName, pos, !compound && localDecl) != null) {
3678-
if (typeName != names.var) {
3679-
reportSyntaxError(elemType.pos, Errors.RestrictedTypeNotAllowedHere(typeName));
3680-
} else if (type.hasTag(TYPEARRAY) && !compound) {
3681-
//error - 'var' and arrays
3682-
reportSyntaxError(elemType.pos, Errors.RestrictedTypeNotAllowedArray(typeName));
3683-
} else {
3684-
declaredUsingVar = true;
3685-
if (compound)
3686-
//error - 'var' in compound local var decl
3687-
reportSyntaxError(elemType.pos, Errors.RestrictedTypeNotAllowedCompound(typeName));
3688-
startPos = TreeInfo.getStartPos(mods);
3689-
if (startPos == Position.NOPOS)
3690-
startPos = TreeInfo.getStartPos(type);
3691-
//implicit type
3692-
type = null;
3693-
}
3675+
int startPos = Position.NOPOS;
3676+
JCTree elemType = TreeInfo.innermostType(type, true);
3677+
if (elemType.hasTag(IDENT)) {
3678+
Name typeName = ((JCIdent) elemType).name;
3679+
if (restrictedTypeNameStartingAtSource(typeName, pos, !compound && localDecl) != null) {
3680+
if (typeName != names.var) {
3681+
reportSyntaxError(elemType.pos, Errors.RestrictedTypeNotAllowedHere(typeName));
3682+
} else if (type.hasTag(TYPEARRAY) && !compound) {
3683+
//error - 'var' and arrays
3684+
reportSyntaxError(elemType.pos, Errors.RestrictedTypeNotAllowedArray(typeName));
3685+
} else {
3686+
declaredUsingVar = true;
3687+
if (compound)
3688+
//error - 'var' in compound local var decl
3689+
reportSyntaxError(elemType.pos, Errors.RestrictedTypeNotAllowedCompound(typeName));
3690+
startPos = TreeInfo.getStartPos(mods);
3691+
if (startPos == Position.NOPOS)
3692+
startPos = TreeInfo.getStartPos(type);
3693+
//implicit type
3694+
type = null;
36943695
}
36953696
}
3696-
result = toP(F.at(pos).VarDef(mods, name, type, init, declaredUsingVar));
3697-
attach(result, dc);
3698-
result.startPos = startPos;
3699-
} else {
3700-
result = toP(F.at(pos).VarDef(mods, name, type, null));
37013697
}
3702-
3698+
JCVariableDecl result = toP(F.at(pos).VarDef(mods, name, type, init, declaredUsingVar));
3699+
attach(result, dc);
3700+
result.startPos = startPos;
37033701
return result;
37043702
}
37053703

@@ -3843,12 +3841,12 @@ protected JCTree resource() {
38433841
if (token.kind == FINAL || token.kind == MONKEYS_AT) {
38443842
JCModifiers mods = optFinal(0);
38453843
JCExpression t = parseType(true);
3846-
return variableDeclaratorRest(token.pos, mods, t, identOrUnderscore(), true, null, true, false, false);
3844+
return variableDeclaratorRest(token.pos, mods, t, identOrUnderscore(), true, null, true, false);
38473845
}
38483846
JCExpression t = term(EXPR | TYPE);
38493847
if (wasTypeMode() && LAX_IDENTIFIER.test(token.kind)) {
38503848
JCModifiers mods = F.Modifiers(0);
3851-
return variableDeclaratorRest(token.pos, mods, t, identOrUnderscore(), true, null, true, false, false);
3849+
return variableDeclaratorRest(token.pos, mods, t, identOrUnderscore(), true, null, true, false);
38523850
} else {
38533851
checkSourceLevel(Feature.EFFECTIVELY_FINAL_VARIABLES_IN_TRY_WITH_RESOURCES);
38543852
if (!t.hasTag(IDENT) && !t.hasTag(SELECT)) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @test /nodynamiccopyright/
3+
* @bug 8309054
4+
* @summary Parsing of erroneous patterns succeeds
5+
* @enablePreview
6+
* @compile/fail/ref=T8309054.out -XDrawDiagnostics --should-stop=at=FLOW T8309054.java
7+
*/
8+
9+
public class T8309054 {
10+
public void test(Object obj) {
11+
boolean t1 = switch (obj) {
12+
case Long a[] -> true;
13+
default -> false;
14+
};
15+
boolean t2 = switch (obj) {
16+
case Double a[][][][] -> true;
17+
default -> false;
18+
};
19+
if (obj instanceof Float a[][]) {
20+
}
21+
if (obj instanceof Integer a = Integer.valueOf(0)) {
22+
}
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
T8309054.java:12:24: compiler.err.expected2: :, ->
2+
T8309054.java:16:26: compiler.err.expected2: :, ->
3+
T8309054.java:19:35: compiler.err.expected: ')'
4+
T8309054.java:13:13: compiler.err.switch.mixing.case.types
5+
T8309054.java:17:13: compiler.err.switch.mixing.case.types
6+
T8309054.java:21:17: compiler.err.unexpected.type: kindname.variable, kindname.value
7+
6 errors

0 commit comments

Comments
 (0)