Skip to content

Commit

Permalink
Fix instanceof statement reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
biboudis committed Jan 23, 2025
1 parent 2d58793 commit 1831b6c
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3036,8 +3036,19 @@ List<JCStatement> blockStatement() {
} else {
if (t.getTag() == TYPETEST && allowPatternDeclarations) {
t = term2Rest(t, TreeInfo.orPrec);

accept(SEMI);
return List.of(toP(F.at(pos).TypeTestStatement(((JCInstanceOf) t).expr, ((JCInstanceOf)t ).getPattern())));

JCExpression expr = ((JCInstanceOf) t).expr;
JCPattern pattern = ((JCInstanceOf) t).getPattern();

if (pattern == null) {
log.error(DiagnosticFlag.SYNTAX, expr, Errors.InstanceofStatementPatternOnly);
return List.of(toP(F.at(pos).Exec(checkExprStat(expr))));
}

return List.of(toP(F.at(pos).TypeTestStatement(expr, pattern)));

} else {
// This Exec is an "ExpressionStatement"; it subsumes the terminating semicolon
t = checkExprStat(t);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1548,6 +1548,9 @@ compiler.misc.varargs.trustme.on.reifiable.varargs=\
compiler.err.instanceof.reifiable.not.safe=\
{0} cannot be safely cast to {1}

compiler.err.instanceof.statement.pattern.only=\
instanceof as a statement should act as a pattern matching operator and not as a type comparison operator

# 0: symbol
compiler.misc.varargs.trustme.on.non.varargs.meth=\
Method {0} is not a varargs method.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (c) 2019, 2020, 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.instanceof.statement.pattern.only
// options: --enable-preview -source ${jdk.version} -Xlint:preview

import java.util.List;

class InstanceofStatementPatternOnly {
static void patterns_only(Object point) {
point instanceof int;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@

public class InstanceOfStatementErrors {
static void exhaustivity_error1(Object point) {
point instanceof Point(var x, var y);
point instanceof Point(var x, var y); // error
}

sealed interface IPoint permits Point {}
record Point(Integer x, Integer y) implements IPoint { }
record OPoint(Object x, Object y) { }

static void patterns_only(Object point) {
point instanceof int; // error
}

}
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
InstanceOfStatementErrors.java:20:9: compiler.err.instanceof.statement.pattern.only
InstanceOfStatementErrors.java:12:9: compiler.err.not.exhaustive.statement
1 error
2 errors

0 comments on commit 1831b6c

Please sign in to comment.