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

'if' conditions are treated like assignments by inference



      JLS 8 treats a method invocation as a poly expressions if, among other things, "the invocation appears in an assignment context" (15.12).

      JLS 3 and 7 do not have the notion of "poly expression", but do perform additional inference where "the method result occurs in a context where it will be subject to assignment conversion" (

      When checking an 'if' statement, javac behaves as if the condition were in an assignment to a variable of type boolean:

      public class Test {
        private static <T> T get(String key) {
            @SuppressWarnings("unchecked")T result = (T) Boolean.TRUE;
            return result;

        public static void main(String[] args) {
            if(get("key")) {

      This compiles in recent versions of javac (7, 8, 9).

      There is no assignment here; if our definition of "assignment" is so loose as to allow it, that definition would have to include things like method invocations, too (which did not impact inference prior to 8).

      javac should reject this program for all "-source" versions.

      (Reported to me by Stephan Herrmann)


          Issue Links



              • Assignee:
                dlsmith Dan Smith
                dlsmith Dan Smith
              • Votes:
                0 Vote for this issue
                1 Start watching this issue


                • Created: