checker-framework icon indicating copy to clipboard operation
checker-framework copied to clipboard

Unexpected `type.arguments.not.inferred` error

Open msridhar opened this issue 1 year ago • 4 comments

Test case (reduced from other code):

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

class Walker {
  List<Foo> b(Path c, ClassProvider classProvider) {
    try (Stream<Path> walk = Files.walk(c)) {
      return walk.flatMap(
              g -> {
                return StreamUtils.optionalToStream(classProvider.provide());
              })
          .collect(Collectors.toList());
    } catch (IOException e) {
      throw new IllegalArgumentException();
    }
  }

  abstract static class ClassProvider {
    abstract Optional<? extends Foo> provide();
  }

  static class StreamUtils {
    static <M> Stream<M> optionalToStream(Optional<M> n) {
      return null;
    }
  }

  static class Foo {}
}

If I run it through the Tainting Checker, I get these errors:

$ checker-framework/checker/bin/javac -processor tainting Walker.java
Walker.java:14: error: [return] incompatible types in return.
                return StreamUtils.optionalToStream(classProvider.provide());
                                                   ^
  type of expression: @Tainted Stream<@Tainted Foo>
  method return type: @Tainted Stream<? extends capture#01 extends @Tainted Foo>
Walker.java:14: error: [type.arguments.not.inferred] Could not infer type arguments for StreamUtils.optionalToStream
                return StreamUtils.optionalToStream(classProvider.provide());
                                                   ^
  unsatisfiable constraint: @Tainted Foo <: capture#01 extends @Tainted Foo
2 errors

I get similar errors with the Called Methods Checker / Resource Leak Checker, but not with the Nullness Checker (I do get the expected nullness error in optionalToStream).

Are these errors expected?

msridhar avatar Aug 26 '24 20:08 msridhar

This is a workaround:

-     return walk.flatMap(
+     return walk.<Foo>flatMap(

msridhar avatar Aug 26 '24 23:08 msridhar

@mernst and @msridhar I had a similar problem in my project as well with witness notation in #6671 .

I tried to setup a project that was not my opensource project to reproduce it but cannot.

See this comment https://github.com/typetools/checker-framework/issues/6671#issuecomment-2272093501 for that info.

(hopefully the above is helpful and not a distraction).

agentgt avatar Sep 05 '24 13:09 agentgt

Just FYI, I re-tested and this issue remains on 3.48.0.

msridhar avatar Oct 02 '24 20:10 msridhar

This remains an issue on the latest CF master branch (tested with b175894cb5d6e3e85ca560cc313fc127797e2240). FYI @smillst @mernst in case it's relevant to the major release decision

msridhar avatar Nov 09 '25 16:11 msridhar