-
Bug
-
Resolution: Unresolved
-
P4
-
7u51, 8u101, 9
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" (15.12.2.8).
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")) {
System.out.println("Passed");
}
}
}
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)
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" (15.12.2.8).
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")) {
System.out.println("Passed");
}
}
}
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)
- relates to
-
JDK-8030361 Chapter 5: Define where contexts occur
- Open