Files
sdk/tests/language/function
Paul Berry 88ae929cf8 Analyzer: resolve type to bound in FunctionExpressionInvocationResolver.
Consider the following code:

    void f<T extends void Function(int)>(List<T> x) {
      x.first(0);
    }

While analyzing the expression `x.first(0)`, the analyzer has to do two things:

- Convert the AST representation from a MethodInvocation (which is
  what was initially parsed) to a FunctionExpressionInvocation
  targeting a PrefixedIdentifier. This reflects the fact that the
  invocation isn't a method invocation after all; it's a function call
  invocation applied to a property get.

- Convert the static type of `x.first` from `T` to its bound, `void
  Function(int)`, in order to type check the invocation. This is done
  using the `TypeSystemImpl.resolveToBound` method.

Previously, `TypeSystemImpl.resolveToBound` was called as part of
converting the AST representation, and the resolved bound (`void
Function(int)` in this example) was stored as the static type of the
FunctionExpressionInvocation target. This led to some minor
inaccuracies in the AST representation (since the type returned by
`TypeSystemImpl.resolveToBound` is *not* the correct type of the
FunctionExpressionInvocation target).

With this change, the call to `TypeSystemImpl.resolveToBound` happens
during resolution of the FunctionExpressionInvocation instead,
allowing the target of the FunctionExpressionInvocation to retain its
correct static type.

I'm in the middle of a larger arc of work trying to introduce a new,
simpler mechanism for flow analysis to be told about the static types
of property gets, and part of that arc of work will involve
introducing a temporary check to verify that the old and new
mechanisms see the same static types. Fixing this incorrect type will
allow the temporary check to pass.

This change also has the side effect of fixing
https://github.com/dart-lang/sdk/issues/56907 (Analyzer fails to
propery type check invocations of complex expressions whose type is a
type parameter). This bug was happening in circumstances where a
FunctionExpressionInvocation arises directly from parsing (rather than
being created from a MethodInvocation), and the static type of the
target is a type variable. Previously, the analyzer was failing to
convert the static type to its bound when analyzing the
FunctionExpressionInvocation, so it was failing to type check the
invocation. Moving the call to `TypeSystemImpl.resolveToBound` into
FunctionExpressionInvocation resolution ensures that the type is
properly resolved to its bound in _all_ circumstances where a
FunctionExpressionInvocation occurs.

Fixes https://github.com/dart-lang/sdk/issues/56907.

Bug: https://github.com/dart-lang/sdk/issues/56907
Change-Id: Id648d5289a6cabe95b8410abd19898acb97fc5e7
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/390661
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
2024-10-23 00:31:11 +00:00
..
2022-12-19 16:30:06 +00:00