8295447: NullPointerException with invalid pattern matching construct in constructor call

Reviewed-by: vromero
This commit is contained in:
Aggelos Biboudis 2022-11-26 15:53:00 +00:00 committed by Vicente Romero
parent 76a24c3f90
commit 6c05771b9b
3 changed files with 57 additions and 1 deletions
src/jdk.compiler/share/classes/com/sun/tools/javac/comp
test/langtools/tools/javac

@ -4119,6 +4119,10 @@ public class Attr extends JCTree.Visitor {
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)));
@ -4180,7 +4184,7 @@ public class Attr extends JCTree.Visitor {
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;

@ -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); }
}
}

@ -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