Skip to content

Commit 6c05771

Browse files
biboudisVicente Romero
authored and
Vicente Romero
committed
8295447: NullPointerException with invalid pattern matching construct in constructor call
Reviewed-by: vromero
1 parent 76a24c3 commit 6c05771

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -4119,6 +4119,10 @@ private boolean checkCastablePattern(DiagnosticPosition pos,
41194119
Type exprType,
41204120
Type pattType) {
41214121
Warner warner = new Warner();
4122+
// if any type is erroneous, the problem is reported elsewhere
4123+
if (exprType.isErroneous() || pattType.isErroneous()) {
4124+
return false;
4125+
}
41224126
if (!types.isCastable(exprType, pattType, warner)) {
41234127
chk.basicHandler.report(pos,
41244128
diags.fragment(Fragments.InconvertibleTypes(exprType, pattType)));
@@ -4180,7 +4184,7 @@ public void visitRecordPattern(JCRecordPattern tree) {
41804184
tree.record = record;
41814185
} else {
41824186
log.error(tree.pos(), Errors.DeconstructionPatternOnlyRecords(site.tsym));
4183-
expectedRecordTypes = Stream.generate(() -> Type.noType)
4187+
expectedRecordTypes = Stream.generate(() -> types.createErrorType(tree.type))
41844188
.limit(tree.nested.size())
41854189
.collect(List.collector());
41864190
tree.record = syms.errSymbol;
+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright (c) 2010, 2022, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
/**
24+
* @test
25+
* @bug 8295447
26+
* @summary NullPointerException with invalid pattern matching construct in constructor call
27+
* @modules jdk.compiler
28+
* @compile/fail/ref=T8295447.out -XDrawDiagnostics --enable-preview -source ${jdk.version} T8295447.java
29+
*/
30+
public class T8295447 {
31+
class Foo {
32+
void m(Object o) {
33+
if(o instanceof Foo(int x)) {}
34+
}
35+
36+
Foo(Object o) {
37+
m((o instanceof Foo(int x))? 0 : 1);
38+
}
39+
void m(int i) { }
40+
}
41+
42+
class Base { int i; Base(int j) { i = j; } }
43+
class Sub extends Base {
44+
Sub(Object o) { super(o instanceof java.awt.Point(int x, int y)? x + y: 0); }
45+
}
46+
}
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
T8295447.java:33:29: compiler.err.deconstruction.pattern.only.records: T8295447.Foo
2+
T8295447.java:37:29: compiler.err.deconstruction.pattern.only.records: T8295447.Foo
3+
T8295447.java:44:44: compiler.err.deconstruction.pattern.only.records: java.awt.Point
4+
- compiler.note.preview.filename: T8295447.java, DEFAULT
5+
- compiler.note.preview.recompile
6+
3 errors

0 commit comments

Comments
 (0)