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

Analyzer fails to propery type check invocations of complex expressions whose type is a type parameter #56907

Closed
stereotype441 opened this issue Oct 16, 2024 · 2 comments
Assignees
Labels
area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P2 A bug or feature request we're likely to work on type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)

Comments

@stereotype441
Copy link
Member

This code is (correctly) rejected by both the analyzer and the CFE:

int test<T extends int Function(int)>(T Function() createT) {
  var tValue = createT();
  return tValue(''); // ERROR: 'String' not assignable to 'int'
}

This code is equivalent (the only difference is that the local variable tValue has been inlined), so it should be rejected too. It is correctly rejected by the CFE, but it is accepted by the analyzer:

int test<T extends int Function(int)>(T Function() createT) {
  return createT()('');
}
@dart-github-bot
Copy link
Collaborator

Summary: The analyzer incorrectly accepts code with an inlined function call where the type parameter is a function type, leading to a type error when the function is invoked with an incorrect argument. The CFE correctly rejects both code examples.

@dart-github-bot dart-github-bot added area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) labels Oct 16, 2024
@stereotype441 stereotype441 self-assigned this Oct 16, 2024
@stereotype441
Copy link
Member Author

I believe the problem in the second example is that the analyzer's FunctionExpressionInvocationResolver doesn't properly resolve the type of the "function" (createT() in this case) from a type parameter type (T) to its bound (int Function(int)), so it treats it as a dynamic invocation and fails to check the argument ''.

The reason the problem doesn't show up in the first example is that the analyzer parses tValue('') as a MethodInvocation rather than a FunctionExpressionInvocation, so its first step in trying to resolve it is to use the MethodInvocationResolver, which does resolve the type of tValue to its bound.

I found this bug in the process of trying to do some other refactoring to MethodInvocationResolver and FunctionExpressionInvocationResolver, so I'll work on a fix in order to avoid blocking that work.

@pq pq added the P2 A bug or feature request we're likely to work on label Oct 17, 2024
@mraleph mraleph removed the triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. label Oct 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P2 A bug or feature request we're likely to work on type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
None yet
Development

No branches or pull requests

4 participants