GeezerVison is showing github.com — Ternary expression with @MonotonicNonNull fails · Issue #1256 · typetools/checker-framework
Ternary expression with @MonotonicNonNull fails #1256
Description
I would expect the following to check successfully, with bar receiving the inferred type @Nullable Object:
import org.checkerframework.checker.nullness.qual.*;
public class MonotonicNonNullTernaryIf {
@MonotonicNonNull Object defaultFoo;
public void test(Object foo, boolean condition) {
Object bar = condition ? foo : defaultFoo;
}
}
But instead I get the following error:
$ java -jar checker.jar -processor org.checkerframework.checker.nullness.NullnessChecker -version MonotonicNonNullTernaryIf.java
javac 1.8.0-jsr308-2.1.10
MonotonicNonNullTernaryIf.java:8: error: [monotonic.type.incompatible] cannot assign org.checkerframework.checker.nullness.qual.MonotonicNonNull to org.checkerframework.checker.nullness.qual.MonotonicNonNull (monotonic type).
Object bar = condition ? foo : defaultFoo;
^
full type found: @Initialized @MonotonicNonNull Object
1 error
Activity
kanha95 commented on May 2, 2017
It's similar to Issue #1170.
import org.checkerframework.checker.nullness.qual.*;
public class MonotonicNonNullTernaryIf {
@MonotonicNonNull Object defaultFoo;
public void test(Object foo, boolean condition) {
Object bar;
if(condition) {
bar=foo;
}
else {
bar=defaultFoo;
}
}
}
It passes using if-else statement but fails in case of ternary operators.
mernst commented on May 3, 2017
A workaround is to use if ... else. This is undesirable, but will work until we fix this issue.
Metadata
Metadata
Development
No branches or pull requestsIssue actions