Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type argument inference in nested conditional operator #4007

Closed
Stephan202 opened this issue Dec 13, 2020 · 1 comment · Fixed by #6615
Closed

Type argument inference in nested conditional operator #4007

Stephan202 opened this issue Dec 13, 2020 · 1 comment · Fixed by #6615

Comments

@Stephan202
Copy link
Contributor

Consider the following dummy code:

$ cat -n Dummy.java 
     1	import java.util.List;
     2	import java.util.Optional;
     3	import org.checkerframework.checker.nullness.qual.NonNull;
     4	
     5	final class Dummy {
     6	  Optional<String> foo(List<String> list) {
     7	    return list.isEmpty() ? Optional.empty() : Optional.of(list.get(0));
     8	  }
     9	
    10	  Optional<Optional<String>> bar(List<String> list) {
    11	    return Optional.of(list.isEmpty() ? Optional.empty() : Optional.of(list.get(0)));
    12	  }
    13	
    14	  Optional<Optional<String>> baz(List<String> list) {
    15	    return Optional.of(
    16	        list.isEmpty() ? Optional.<@NonNull String>empty() : Optional.of(list.get(0)));
    17	  }
    18	
    19	  Optional<Optional<String>> qux(List<String> list) {
    20	    return Optional.of(
    21	        list.isEmpty() ? Optional.empty() : Optional.<@NonNull String>of(list.get(0)));
    22	  }
    23	
    24	  Optional<Optional<String>> quux(List<String> list) {
    25	    return Optional.of(
    26	        list.isEmpty()
    27	            ? Optional.<@NonNull String>empty()
    28	            : Optional.<@NonNull String>of(list.get(0)));
    29	  }
    30	}

Compiling this code using the NullnessChecker we get the following output:

$ javac \
    -J--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED \
    -processorpath ~/.m2/repository/org/checkerframework/checker/3.8.0/checker-3.8.0.jar \
    -cp ~/.m2/repository/org/checkerframework/checker-qual
/3.8.0/checker-qual-3.8.0.jar \
    -processor org.checkerframework.checker.nullness.NullnessChecker \
    Dummy.java
Dummy.java:11: error: [return.type.incompatible] incompatible types in return.
    return Optional.of(list.isEmpty() ? Optional.empty() : Optional.of(list.get(0)));
                      ^
  type of expression: @Initialized @NonNull Optional<@Initialized @NonNull Optional<@Initialized @Nullable String>>
  method return type: @Initialized @NonNull Optional<@Initialized @NonNull Optional<@Initialized @NonNull String>>
Dummy.java:15: error: [return.type.incompatible] incompatible types in return.
    return Optional.of(
                      ^
  type of expression: @Initialized @NonNull Optional<@Initialized @NonNull Optional<@Initialized @Nullable String>>
  method return type: @Initialized @NonNull Optional<@Initialized @NonNull Optional<@Initialized @NonNull String>>
Dummy.java:20: error: [return.type.incompatible] incompatible types in return.
    return Optional.of(
                      ^
  type of expression: @Initialized @NonNull Optional<@Initialized @NonNull Optional<@Initialized @Nullable String>>
  method return type: @Initialized @NonNull Optional<@Initialized @NonNull Optional<@Initialized @NonNull String>>
3 errors

Note that the checker complains about methods 2, 3 and 4 (i.e. bar, baz and qux) but not about methods 1 and 5 (i.e. foo and quuz). Somehow wrapping the conditional operator in Optional.of(...) requires one to add type hints to both branches.

@mernst mernst changed the title NullnessChecker false positive with nested conditional operator Type argument inference in nested conditional operator Dec 14, 2020
mernst added a commit that referenced this issue Dec 14, 2020
@mernst
Copy link
Member

mernst commented Dec 14, 2020

Thanks for the bug report. I have committed the test case as file checker/tests/nullness/Issue4007.java.

The problem seems to be with inference of type arguments rather than with the Nullness Checker per se.

@smillst smillst self-assigned this Dec 14, 2020
@smillst smillst mentioned this issue May 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants