Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8292625: jshell crash on "var a = a" #11263

Closed
wants to merge 4 commits into from

Conversation

blindpirate
Copy link
Contributor

@blindpirate blindpirate commented Nov 21, 2022

This commit adds the missing type assignment when attributing a variable's initial value. In some cases, this issue causes an NPE.

This issue has existed for a long time, but the NPE was swallowed until 4333942 stopped swallowing such exceptions.


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk pull/11263/head:pull/11263
$ git checkout pull/11263

Update a local copy of the PR:
$ git checkout pull/11263
$ git pull https://git.openjdk.org/jdk pull/11263/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 11263

View PR using the GUI difftool:
$ git pr show -t 11263

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/11263.diff

This commit adds the missing `type` assignment after attributing a
variable's initial value. In some cases, this issue causes an NPE.

This issue has existed for a long time, but the NPE was swallowed
until openjdk@4333942
stopped swallowing such exceptions.
@bridgekeeper
Copy link

bridgekeeper bot commented Nov 21, 2022

👋 Welcome back blindpirate! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk openjdk bot added the rfr Pull request is ready for review label Nov 21, 2022
@openjdk
Copy link

openjdk bot commented Nov 21, 2022

@blindpirate The following labels will be automatically applied to this pull request:

  • compiler
  • kulla

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing lists. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added the compiler compiler-dev@openjdk.org label Nov 21, 2022
@blindpirate
Copy link
Contributor Author

@mcimadamore Can you please take a look?

@openjdk openjdk bot added the kulla kulla-dev@openjdk.org label Nov 21, 2022
@mlbridge
Copy link

mlbridge bot commented Nov 21, 2022

Webrevs

@lahodaj
Copy link
Contributor

lahodaj commented Nov 21, 2022

Good catch, but I would personally prefer if attribExpr would set the type (as it does under normal circumstances), presumably here?

return v.type = types.createErrorType(v.type);

Also, might be nice to have a (core javac) test that would not rely on JShell, if possible.

@blindpirate
Copy link
Contributor Author

@lahodaj Done moving the assignment. However, this was not a problem with javac, probably due to different code path:

# zhb @ zhb in ~/Projects/jdk on git:jdk-8292625 x [7:03:45] C:1
$ cat Test.java
public class Test {
  public static void main() {
    var a = a;
  }
}

# zhb @ zhb in ~/Projects/jdk on git:jdk-8292625 x [7:03:48]
$ /Library/Java/JavaVirtualMachines/temurin-19.jdk/Contents/Home/bin/javac Test.java
Test.java:3: error: cannot infer type for local variable a
    var a = a;
        ^
  (cannot use 'var' on self-referencing variable)
1 error
printing javac parameters to: /Users/zhb/Projects/jdk/javac.20221122_070353.args

That's why I put the test into jshell tests.

@lahodaj
Copy link
Contributor

lahodaj commented Nov 28, 2022

@lahodaj Done moving the assignment. However, this was not a problem with javac, probably due to different code path:

# zhb @ zhb in ~/Projects/jdk on git:jdk-8292625 x [7:03:45] C:1
$ cat Test.java
public class Test {
  public static void main() {
    var a = a;
  }
}

# zhb @ zhb in ~/Projects/jdk on git:jdk-8292625 x [7:03:48]
$ /Library/Java/JavaVirtualMachines/temurin-19.jdk/Contents/Home/bin/javac Test.java
Test.java:3: error: cannot infer type for local variable a
    var a = a;
        ^
  (cannot use 'var' on self-referencing variable)
1 error
printing javac parameters to: /Users/zhb/Projects/jdk/javac.20221122_070353.args

That's why I put the test into jshell tests.

Thanks for moving the assignment. For tests, the test in JShell is fine, but there are other ways to the attributes in addition to that, like actually checking the type is assigned in the AST:

diff --git a/test/langtools/tools/javac/attr/AttrRecoveryTest.java b/test/langtools/tools/javac/attr/AttrRecoveryTest.java
index b364cc28001..f5589a8093f 100644
--- a/test/langtools/tools/javac/attr/AttrRecoveryTest.java
+++ b/test/langtools/tools/javac/attr/AttrRecoveryTest.java
@@ -36,6 +36,7 @@
 import com.sun.source.tree.AnnotationTree;
 import com.sun.source.tree.CompilationUnitTree;
 import com.sun.source.tree.ErroneousTree;
+import com.sun.source.tree.VariableTree;
 import com.sun.source.util.TaskEvent;
 import com.sun.source.util.TaskListener;
 import com.sun.source.util.TreePath;
@@ -45,7 +46,9 @@ import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
 import java.util.List;
+import java.util.concurrent.atomic.AtomicInteger;
 import javax.lang.model.element.Element;
+import javax.lang.model.type.TypeMirror;
 
 import toolbox.TestRunner;
 import toolbox.JavacTask;
@@ -162,4 +165,61 @@ public class AttrRecoveryTest extends TestRunner {
             throw new AssertionError();
         }
     }
+
+    @Test
+    public void testVarAssignment2Self(Path base) throws Exception {
+        Path current = base;
+        Path src = current.resolve("src");
+        Path classes = current.resolve("classes");
+        tb.writeJavaFiles(src,
+                          """
+                          public class Test {
+                              void t() {
+                                  var v = v;
+                              }
+                          }
+                          """);
+
+        Files.createDirectories(classes);
+
+        AtomicInteger seenVariables = new AtomicInteger();
+        TreePathScanner<Void, Trees> checkTypes = new TreePathScanner<>() {
+            @Override
+            public Void visitVariable(VariableTree node, Trees trees) {
+                if (node.getName().contentEquals("v")) {
+                    TypeMirror type = trees.getTypeMirror(getCurrentPath());
+                    if (type == null) {
+                        throw new AssertionError("Unexpected null type!");
+                    }
+                    seenVariables.incrementAndGet();
+                }
+                return super.visitVariable(node, trees);
+            }
+        };
+
+        new JavacTask(tb)
+            .options("-XDrawDiagnostics")
+            .outdir(classes)
+            .files(tb.findJavaFiles(src))
+            .callback(t -> {
+                t.addTaskListener(new TaskListener() {
+                    CompilationUnitTree parsed;
+                    @Override
+                    public void finished(TaskEvent e) {
+                        switch (e.getKind()) {
+                            case PARSE -> parsed = e.getCompilationUnit();
+                            case COMPILATION ->
+                                checkTypes.scan(parsed, Trees.instance(t));
+                        }
+                    }
+                });
+            })
+            .run(Task.Expect.FAIL)
+            .writeAll()
+            .getOutputLines(Task.OutputKind.DIRECT);
+
+        if (seenVariables.get() != 1) {
+            throw new AssertionError("Didn't see enough variables: " + seenVariables);
+        }
+    }
 }

I think the advantage of having the test in the javac tests (in addition to the JShell test) is that if someone modifies javac, and only runs javac tests, it would still fail.

@blindpirate
Copy link
Contributor Author

Thanks for the guidance @lahodaj !

@openjdk
Copy link

openjdk bot commented Nov 28, 2022

@blindpirate This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8292625: jshell crash on "var a = a"

Reviewed-by: jlaskey, jlahoda

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 107 new commits pushed to the master branch:

  • 05128c2: 8286185: The Java manpage can be more platform inclusive
  • d450314: 8297276: Remove thread text from Subject.current
  • cdf9ed0: 8297528: java/io/File/TempDirDoesNotExist.java test failing on windows-x64
  • 105d9d7: 8295351: java/lang/Float/Binary16Conversion.java fails with "Unexpected result of converting"
  • a80552e: 8236919: Refactor com.sun.tools.javac.main.CommandLine into a reusable module for other JDK tools
  • a249a52: 8296754: AutoCreateSharedArchive in JDK 20 is not compatible with JDK 19
  • 405b188: 8297570: jdk/jfr/threading/TestDeepVirtualStackTrace.java fails with -XX:-UseTLAB
  • ba0a252: 8297717: Remove jdk/internal/misc/TerminatingThreadLocal/TestTerminatingThreadLocal.java from ProblemList
  • c05dc80: 8297660: x86: Redundant test+jump in C1 allocateArray
  • eff4c03: 8297343: TestStress*.java fail with "got different traces for the same seed"
  • ... and 97 more: https://git.openjdk.org/jdk/compare/3ea89711e34639fef36b6cc143e37b5133aa80f8...master

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

As you do not have Committer status in this project an existing Committer must agree to sponsor your change. Possible candidates are the reviewers of this PR (@JimLaskey, @lahodaj) but any other Committer may sponsor as well.

➡️ To flag this PR as ready for integration with the above commit message, type /integrate in a new comment. (Afterwards, your sponsor types /sponsor in a new comment to perform the integration).

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Nov 28, 2022
Copy link
Contributor

@lahodaj lahodaj left a comment

Choose a reason for hiding this comment

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

Looks good to me!

@blindpirate
Copy link
Contributor Author

/integrate

@blindpirate
Copy link
Contributor Author

Thanks @lahodaj . Can you please sponsor this one?

@openjdk openjdk bot added the sponsor Pull request is ready to be sponsored label Nov 29, 2022
@openjdk
Copy link

openjdk bot commented Nov 29, 2022

@blindpirate
Your change (at version 0e1d803) is now ready to be sponsored by a Committer.

@lahodaj
Copy link
Contributor

lahodaj commented Nov 29, 2022

/sponsor

@openjdk
Copy link

openjdk bot commented Nov 29, 2022

Going to push as commit 33587ff.
Since your change was applied there have been 120 commits pushed to the master branch:

  • 2deb318: 8297065: DerOutputStream operations should not throw IOExceptions
  • d83a07b: 8297200: java/net/httpclient/SpecialHeadersTest.java failed once in AssertionError due to selector thread remaining alive
  • 5d2772a: 8297424: java/net/httpclient/AsyncExecutorShutdown.java fails in AssertionError due to misplaced assert
  • 361b50e: 8292594: Use CSS custom properties for all fonts and colors
  • 42b60ed: 8297030: Reduce Default Keep-Alive Timeout Value for httpclient
  • 1301fb0: 8296470: Refactor VMError::report STEP macro to improve readability
  • 48017b1: 8296804: Document HttpClient configuration properties in java.net.http module-info
  • bd51b7e: 8296645: org.openjdk.bench.javax.tools.Javac leaves class files in current directory
  • b27a61e: 8297216: Search results omit some methods
  • 33dfc7d: 8296954: G1: Enable parallel scanning for heap region remset
  • ... and 110 more: https://git.openjdk.org/jdk/compare/3ea89711e34639fef36b6cc143e37b5133aa80f8...master

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Nov 29, 2022
@openjdk openjdk bot closed this Nov 29, 2022
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review sponsor Pull request is ready to be sponsored labels Nov 29, 2022
@openjdk
Copy link

openjdk bot commented Nov 29, 2022

@lahodaj @blindpirate Pushed as commit 33587ff.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler compiler-dev@openjdk.org integrated Pull request has been integrated kulla kulla-dev@openjdk.org
3 participants