Misleading error message when using diamond operator with private constructor

Eddie Aftandilian eaftan at google.com
Wed Aug 21 11:32:50 PDT 2013


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.

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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.openjdk.java.net/pipermail/compiler-dev/attachments/20130821/c4f210ef/attachment.html 


More information about the compiler-dev mailing list