Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
8295447: NullPointerException with invalid pattern matching construct…
… in constructor call

Reviewed-by: vromero
  • Loading branch information
biboudis authored and Vicente Romero committed Nov 26, 2022
1 parent 76a24c3 commit 6c05771
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
Expand Up @@ -4119,6 +4119,10 @@ private boolean checkCastablePattern(DiagnosticPosition pos,
Type exprType,
Type pattType) {
Warner warner = new Warner();
// if any type is erroneous, the problem is reported elsewhere
if (exprType.isErroneous() || pattType.isErroneous()) {
return false;
}
if (!types.isCastable(exprType, pattType, warner)) {
chk.basicHandler.report(pos,
diags.fragment(Fragments.InconvertibleTypes(exprType, pattType)));
Expand Down Expand Up @@ -4180,7 +4184,7 @@ public void visitRecordPattern(JCRecordPattern tree) {
tree.record = record;
} else {
log.error(tree.pos(), Errors.DeconstructionPatternOnlyRecords(site.tsym));
expectedRecordTypes = Stream.generate(() -> Type.noType)
expectedRecordTypes = Stream.generate(() -> types.createErrorType(tree.type))
.limit(tree.nested.size())
.collect(List.collector());
tree.record = syms.errSymbol;
Expand Down
46 changes: 46 additions & 0 deletions test/langtools/tools/javac/T8295447.java
@@ -0,0 +1,46 @@
/*
* Copyright (c) 2010, 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.
*/
/**
* @test
* @bug 8295447
* @summary NullPointerException with invalid pattern matching construct in constructor call
* @modules jdk.compiler
* @compile/fail/ref=T8295447.out -XDrawDiagnostics --enable-preview -source ${jdk.version} T8295447.java
*/
public class T8295447 {
class Foo {
void m(Object o) {
if(o instanceof Foo(int x)) {}
}

Foo(Object o) {
m((o instanceof Foo(int x))? 0 : 1);
}
void m(int i) { }
}

class Base { int i; Base(int j) { i = j; } }
class Sub extends Base {
Sub(Object o) { super(o instanceof java.awt.Point(int x, int y)? x + y: 0); }
}
}
6 changes: 6 additions & 0 deletions test/langtools/tools/javac/T8295447.out
@@ -0,0 +1,6 @@
T8295447.java:33:29: compiler.err.deconstruction.pattern.only.records: T8295447.Foo
T8295447.java:37:29: compiler.err.deconstruction.pattern.only.records: T8295447.Foo
T8295447.java:44:44: compiler.err.deconstruction.pattern.only.records: java.awt.Point
- compiler.note.preview.filename: T8295447.java, DEFAULT
- compiler.note.preview.recompile
3 errors

1 comment on commit 6c05771

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.