Misleading error message when using diamond operator with private constructor
Maurizio Cimadamore
maurizio.cimadamore at oracle.com
Thu Aug 22 03:09:29 PDT 2013
On 21/08/13 19:32, Eddie Aftandilian wrote:
> Hi compiler-dev,
>
> I work with Jeremy on javac at Google. A user pointed out to us that
> compiling code that uses the diamond operator and attempts to access a
> private constructor fails with a misleading error message:
>
> Test.java:3: error: cannot infer type arguments for MyList<>
> MyList<Object> foo = new MyList<>();
> ^
> 1 error
>
> The error is not that the types cannot be inferred, but that the
> constructor for MyList is private. If you explicitly specify the type
> parameter in the call to the MyList constructor, you get the correct
> error message telling you that the constructor is private.
Thanks for the report - it's true that error messages in diamond are
suboptimal when the failure occurs for reasons other than inference.
I'll file a bug on this one.
Maurizio
>
> Steps to reproduce:
>
> I'm using javac version 1.7.0_25.
>
> 1) Create these 2 files.
>
> MyList.java:
> public class MyList<E> {
> private MyList() { }
> }
>
> Test.java:
> public class Test {
> public void foo() {
> MyList<Object> foo = new MyList<>();
> }
> }
>
> 2) Compile.
>
> $ javac Test.java
> Test.java:3: error: cannot infer type arguments for MyList<>
> MyList<Object> foo = new MyList<>();
> ^
> 1 error
>
> It would be great if javac produced a more helpful error message in
> this case.
>
> Thanks,
> Eddie
More information about the compiler-dev
mailing list