Dart 2.5 analyzer is confused by parametric methods on parametric classes

The code below does not analyze cleanly in DartPad running against Dart 2.5.0. We have seen a similar issue in our code that uses the built_redux package, presumably because it contains this pattern.

Note that the top-level function call analyzes just fine. Also, if the type parameter is removed from the Rectifier class the method call analyzes cleanly as well, even though T is never actually used, so that’s a little confusing.

The error message is below.

Couldn’t infer type parameter ‘S extends State<S, B>’. Tried to infer ‘FooState’ for ‘S extends State<S, B>’ which doesn’t work: Type parameter ‘S extends State<S, B>’ declared to extend ‘State<FooState, B extends Builder<S, B>>’. The type ‘FooState’ was inferred from: Parameter ‘state’ declared as ‘S’ but argument is ‘FooState’. Consider passing explicit type argument(s) to the generic.

If I try to specify values for the type parameters, as suggested, I see a different error:

‘FooState’ doesn’t extend ‘State<FooState, B>’.

class State<S extends State<S, B>, B extends Builder<S, B>> {}

class Builder<S extends State<S, B>, B extends Builder<S, B>> {}

class FooState implements State<FooState, FooBuilder> {}

class FooBuilder implements Builder<FooState, FooBuilder> {}

// Calling this analyzes cleanly
void rectify<S extends State<S, B>, B extends Builder<S, B>>(
  S state,
  B builder,
) {
  print('barf');
}

// If we get rid of this type parameter and update the instantiation the error goes away
class Rectifier<T> {
  // Calling this produces a static error
  void rectify<S extends State<S, B>, B extends Builder<S, B>>(
    S state,
    B builder,
  ) {
    print('barf');
  }
}

void main() {
  rectify(new FooState(), new FooBuilder()); // No static error

  final rectifier = new Rectifier<Null>();
  // Adding `<FooState, FooBuilder>` changes the error
  rectifier.rectify(new FooState(), new FooBuilder()); // Static error
}

Metadata…

$ dart --version
Dart VM version: 2.5.0 (Fri Sep 6 20:39:27 2019 +0200) on "macos_x64"

$ dartanalyzer --version
dartanalyzer version 2.5.0

Author: Fantashit

1 thought on “Dart 2.5 analyzer is confused by parametric methods on parametric classes

  1. I verified this fixes the analysis errors for us. Any chance this could be in a 2.5.1 patch release?

Comments are closed.