java-21-openjdk/jdk8295447-npe_in_constructor.patch

98 lines
4.2 KiB
Diff
Raw Normal View History

commit c9d485792b99233f381dcdfd69838e7b973909bd
Author: Victor Rudometov <vrudomet@openjdk.org>
Date: Thu Dec 1 10:57:43 2022 +0000
8295447: NullPointerException with invalid pattern matching construct in constructor call
Backport-of: 6c05771b9be3dd5cbcdb40d2e53cc53959926cdd
diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java
index 21cc8e57e1f..46b076d3d03 100644
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java
@@ -4145,6 +4145,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)));
@@ -4206,7 +4210,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());
}
diff --git a/test/langtools/tools/javac/T8295447.java b/test/langtools/tools/javac/T8295447.java
new file mode 100644
index 00000000000..76fcaf10f8d
--- /dev/null
+++ b/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); }
+ }
+}
diff --git a/test/langtools/tools/javac/T8295447.out b/test/langtools/tools/javac/T8295447.out
new file mode 100644
index 00000000000..7f6746f802f
--- /dev/null
+++ b/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
\ No newline at end of file