Skip to content
This repository has been archived by the owner on Sep 19, 2023. It is now read-only.
/ jdk18 Public archive

Commit

Permalink
8278834: Error "Cannot read field "sym" because "this.lvar[od]" is nu…
Browse files Browse the repository at this point in the history
…ll" when compiling

Reviewed-by: vromero
  • Loading branch information
lahodaj committed Jan 19, 2022
1 parent f5de6fa commit be0538d
Show file tree
Hide file tree
Showing 2 changed files with 167 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -665,11 +665,14 @@ public void visitLambda(JCLambda tree) {
@Override
public void visitClassDef(JCClassDecl tree) {
ClassSymbol prevCurrentClass = currentClass;
MethodSymbol prevMethodSym = currentMethodSym;
try {
currentClass = tree.sym;
currentMethodSym = null;
super.visitClassDef(tree);
} finally {
currentClass = prevCurrentClass;
currentMethodSym = prevMethodSym;
}
}

Expand Down
164 changes: 164 additions & 0 deletions test/langtools/tools/javac/patterns/BindingsInitializer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
/*
* Copyright (c) 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 8278834
* @summary Verify pattern matching nested inside initializers of classes nested in methods
* works correctly.
* @library /tools/lib /tools/javac/lib
* @modules
* jdk.compiler/com.sun.tools.javac.api
* jdk.compiler/com.sun.tools.javac.file
* jdk.compiler/com.sun.tools.javac.main
* jdk.compiler/com.sun.tools.javac.util
* @build toolbox.ToolBox toolbox.JavacTask
* @build combo.ComboTestHelper
* @compile BindingsInitializer.java
* @run main BindingsInitializer
*/

import combo.ComboInstance;
import combo.ComboParameter;
import combo.ComboTask;
import combo.ComboTestHelper;
import java.nio.file.Path;
import java.nio.file.Paths;
import toolbox.ToolBox;

public class BindingsInitializer extends ComboInstance<BindingsInitializer> {
protected ToolBox tb;

BindingsInitializer() {
super();
tb = new ToolBox();
}

public static void main(String... args) throws Exception {
new ComboTestHelper<BindingsInitializer>()
.withDimension("OUTER", (x, outer) -> x.outer = outer, Outer.values())
.withDimension("MIDDLE", (x, middle) -> x.middle = middle, Middle.values())
.withDimension("INNER", (x, inner) -> x.inner = inner, Inner.values())
.withDimension("TEST", (x, test) -> x.test = test, Test.values())
.run(BindingsInitializer::new);
}

private Outer outer;
private Middle middle;
private Inner inner;
private Test test;

private static final String MAIN_TEMPLATE =
"""
public class Test {
private static Object obj = "";
#{OUTER}
}
""";

@Override
protected void doWork() throws Throwable {
Path base = Paths.get(".");

ComboTask task = newCompilationTask()
.withSourceFromTemplate(MAIN_TEMPLATE, pname -> switch (pname) {
case "OUTER" -> outer;
case "MIDDLE" -> middle;
case "INNER" -> inner;
case "TESST" -> test;

This comment has been minimized.

Copy link
@pepness

pepness Jan 29, 2022

There is a typo, should be "TEST" instead of "TESST"

default -> throw new UnsupportedOperationException(pname);
});

task.generate(result -> {
if (result.hasErrors()) {
throw new AssertionError("Unexpected result: " + result.compilationInfo());
}
});
}

public enum Outer implements ComboParameter {
NONE("#{MIDDLE}"),
STATIC_CLASS("static class Nested { #{MIDDLE} }"),
CLASS("class Inner { #{MIDDLE} }");
private final String code;

private Outer(String code) {
this.code = code;
}

@Override
public String expand(String optParameter) {
return code;
}
}

public enum Middle implements ComboParameter {
STATIC_INIT("static { #{INNER} }"),
INIT("{ #{INNER} }"),
METHOD("void test() { #{INNER} }");
private final String code;

private Middle(String code) {
this.code = code;
}

@Override
public String expand(String optParameter) {
return code;
}
}

public enum Inner implements ComboParameter {
DIRECT("#{TEST}"),
CLASS_STATIC_INIT("class C { static { #{TEST} } }"),
CLASS_INIT("class C { { #{TEST} } }"),
CLASS_METHOD("class C { void t() { #{TEST} } }"),
ANNONYMOUS_CLASS_STATIC_INIT("new Object() { static { #{TEST} } };"),
ANNONYMOUS_CLASS_INIT("new Object() { { #{TEST} } };"),
ANNONYMOUS_CLASS_METHOD("new Object() { void t() { #{TEST} } };");
private final String code;

private Inner(String code) {
this.code = code;
}

@Override
public String expand(String optParameter) {
return code;
}
}

public enum Test implements ComboParameter {
TEST("if (obj instanceof String str) System.err.println(str);");
private final String code;

private Test(String code) {
this.code = code;
}

@Override
public String expand(String optParameter) {
return code;
}
}
}

1 comment on commit be0538d

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