Bug Report - Type check involving wildcards not sound

Andreas Stadelmeier andi at paulus.haus
Tue Feb 14 16:56:08 UTC 2023


Hello everybody,


I hope this is the right place. I just wanted to report a bug in the javac type-check.

My javac version: javac 19.0.1
My java version: OpenJDK Runtime Environment (build 19.0.1+10-Ubuntu-1ubuntu122.04)


Compiling the following program and running it leads to a runtime error.

I think this is an error, because javac should to declare this program not type correct during compilation time:


import java.util.Vector;


class Matrix<T> extends Vector<Vector<T>> {

    public static void main(String args[]) {
        Vector<Matrix<Integer>> vmInt=new Vector<Matrix<Integer>> ();
        vmInt.add(new Matrix<Integer>());
        Vector<? extends Matrix<?>> vm = vmInt;
	//The following assignment should not be possible:
        Vector<? extends Vector<Vector<?>>> vv = vm; 
        Vector<String> vS = new Vector<String>();
        vS.add("String");
	// adding Vector<String> to a Vector<Vector<Integer>>:
        vv.get(0).add(vS); 
                            
	//Runtime-Error:
        Integer notAnInteger = vmInt.get(0).get(0).get(0); 
    }
}


But there is no error during compilation. It generates a class file, which crashes when executed.
The following:
 javac Matrix.java
 java Matrix

spawns the error:

Exception in thread "main" java.lang.ClassCastException: class java.lang.String cannot be cast to class java.lang.Integer (java.lang.String and java.lang.Integer are in module java.base of loader 'bootstrap')
	at Matrix.main(Test.java:15)


Best Regards,
-Andi


More information about the compiler-dev mailing list