Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6471599

Type of rhs cannot be obtained when assigning to erroneous symbol

XMLWordPrintable

    • b03
    • generic
    • generic
    • Verified

      This program throws an exception

      myfo:/Test.java:1: cannot find symbol
      symbol : variable x
      location: class Test
      public class Test { { x = java.util.Collections.emptySet(); } }
                            ^
      Exception in thread "main" java.lang.AssertionError: java.util.Collections.emptySet()
              at toy.Main$MyVisitor.visitAssignment(Main.java:44)
              at toy.Main$MyVisitor.visitAssignment(Main.java:39)
              at com.sun.tools.javac.tree.JCTree$JCAssign.accept(JCTree.java:1349)
              at com.sun.source.util.TreePathScanner.scan(TreePathScanner.java:52)
              at com.sun.source.util.TreeScanner.visitExpressionStatement(TreeScanner.java:223)
              at com.sun.tools.javac.tree.JCTree$JCExpressionStatement.accept(JCTree.java:1080)
              at com.sun.source.util.TreePathScanner.scan(TreePathScanner.java:52)
              at com.sun.source.util.TreeScanner.scan(TreeScanner.java:75)
              at com.sun.source.util.TreeScanner.visitBlock(TreeScanner.java:143)
              at com.sun.tools.javac.tree.JCTree$JCBlock.accept(JCTree.java:748)
              at com.sun.source.util.TreePathScanner.scan(TreePathScanner.java:52)
              at com.sun.source.util.TreeScanner.scanAndReduce(TreeScanner.java:65)
              at com.sun.source.util.TreeScanner.scan(TreeScanner.java:75)
              at com.sun.source.util.TreeScanner.scanAndReduce(TreeScanner.java:83)
              at com.sun.source.util.TreeScanner.visitClass(TreeScanner.java:117)
              at com.sun.tools.javac.tree.JCTree$JCClassDecl.accept(JCTree.java:592)
              at com.sun.source.util.TreePathScanner.scan(TreePathScanner.java:52)
              at com.sun.source.util.TreeScanner.scan(TreeScanner.java:75)
              at com.sun.source.util.TreeScanner.scanAndReduce(TreeScanner.java:83)
              at com.sun.source.util.TreeScanner.visitCompilationUnit(TreeScanner.java:104)
              at com.sun.tools.javac.tree.JCTree$JCCompilationUnit.accept(JCTree.java:478)
              at com.sun.source.util.TreePathScanner.scan(TreePathScanner.java:52)
              at toy.Main.main(Main.java:35)

      package toy;

      import com.sun.source.tree.AssignmentTree;
      import com.sun.source.tree.CompilationUnitTree;
      import com.sun.source.util.JavacTask;
      import com.sun.source.util.TreePath;
      import com.sun.source.util.TreePathScanner;
      import com.sun.source.util.Trees;
      import com.sun.tools.javac.util.List;
      import java.io.IOException;
      import java.net.URI;
      import javax.lang.model.type.TypeKind;
      import javax.tools.JavaCompiler;
      import javax.tools.JavaFileObject;
      import javax.tools.SimpleJavaFileObject;
      import javax.tools.ToolProvider;

      public class Main {
          static class MyFileObject extends SimpleJavaFileObject {
              public MyFileObject() {
                  super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
              }
              public CharSequence getCharContent(boolean ignoreEncodingErrors) {
                  return "public class Test { { x = java.util.Collections.emptySet(); } }";
              }
          }
          static Trees trees;
          public static void main(String[] args) throws IOException {
              JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
              JavacTask task = (JavacTask) compiler.getTask(null, null, null, null, null, List.of(new MyFileObject()));
              trees = Trees.instance(task);
              Iterable<? extends CompilationUnitTree> asts = task.parse();
              task.analyze();
              for (CompilationUnitTree ast : asts) {
                  new MyVisitor().scan(ast, null);
              }
          }
          
          static class MyVisitor extends TreePathScanner<Void,Void> {
              @Override
              public Void visitAssignment(AssignmentTree node, Void ignored) {
                  TreePath path = TreePath.getPath(getCurrentPath(), node.getExpression());
                  if (trees.getTypeMirror(path).getKind() == TypeKind.ERROR)
                      throw new AssertionError(path.getLeaf());
                  return null;
              }

          }
      }

            ahe Peter Ahe
            ahe Peter Ahe
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: