United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-6471599 Type of rhs cannot be obtained when assigning to erroneous symbol
JDK-6471599 : Type of rhs cannot be obtained when assigning to erroneous symbol

Details
Type:
Bug
Submit Date:
2006-09-16
Status:
Closed
Updated Date:
2011-03-08
Project Name:
JDK
Resolved Date:
2011-03-08
Component:
tools
OS:
generic
Sub-Component:
javac
CPU:
generic
Priority:
P4
Resolution:
Fixed
Affected Versions:
6
Fixed Versions:

Related Reports
Relates:

Sub Tasks

Description
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;
        }

    }
}

                                    

Comments
SUGGESTED FIX

Index: j2se/src/share/classes/com/sun/tools/javac/comp/Attr.java
--- /tmp/geta10866	2006-09-15 17:46:37.000000000 -0700
+++ Attr.java	2006-09-15 17:35:36.000000000 -0700
@@ -374,7 +374,7 @@
     /** Derived visitor method: attribute an expression tree.
      */
     public Type attribExpr(JCTree tree, Env<AttrContext> env, Type pt) {
-        return attribTree(tree, env, VAL, pt);
+        return attribTree(tree, env, VAL, pt.tag != ERROR ? pt : Type.noType);
     }
 
     /** Derived visitor method: attribute an expression tree with
                                     
2006-09-16
EVALUATION

A bug.  The compiler should not pass errorneous types downwards.
Instead, it should pass Type.noType.
                                     
2006-09-16
SUGGESTED FIX

Webrev of changes: http://sa.sfbay/projects/langtools/bugid_summary.pl?bugid=6471599
Also, see attachment.
                                     
2006-09-16



Hardware and Software, Engineered to Work Together