Skip to content

Type inference is dependent on the order that type parameters are defined #40423

Open
@leonsenft

Description

@leonsenft

Versions:

  • DartPad on SDK 2.7.0
  • VM/dart2js/DDC on 2.8.292505767-edge+google3-v2 (google3)

Here's a working reproduction https://dartpad.dev/a8a92c56149499dd479488f1cca5bff9.

class Value {}

class Renderer<T> {}

// When V is defined before R, type inference works in all cases.
class WorkingOrder<V extends Value, R extends Renderer<V>> {}

// When V is defined after R, type inference fails in some cases.
class BrokenOrder<R extends Renderer<V>, V extends Value> {}

void main() {
  // Correctly infers type.
  var working1 = WorkingOrder();
  print(working1.runtimeType); // WorkingOrder<Value, Renderer<Value>>.

  // Correctly infers type arguments to bounds.
  WorkingOrder working2 = WorkingOrder();
  print(working2.runtimeType); // WorkingOrder<Value, Renderer<Value>>.

  // ERROR: Incorrectly infers BrokenOrder<Renderer<dynamic>, Value>.
  var broken = BrokenOrder();

  // Correctly infers type arguments to bounds.
  BrokenOrder working3 = BrokenOrder();
  print(working3.runtimeType); // BrokenOrder<Renderer<Value>, Value>.
}
  • Inference behaves differently when inferring the type of a variable declared as var versus the raw type - this is unexpected.
  • The inference algorithm used for inferring the type of a variable declared as var appears to be dependent on the order in which type parameters are declared on the resulting type, when one of those type parameters depends on the other.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3A lower priority bug or feature requestarea-dart-modelFor issues related to conformance to the language spec in the parser, compilers or the CLI analyzer.contributions-welcomeContributions welcome to help resolve this (the resolution is expected to be clear from the issue)dart-model-analyzer-specIssues with the analyzer's implementation of the language speclegacy-area-front-endLegacy: Use area-dart-model instead.type-bugIncorrect behavior (everything from a crash to more subtle misbehavior)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions