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] Potential quick fix for incomplete function literal #46972

Open
eernstg opened this issue Aug 24, 2021 · 0 comments
Open

[analyzer] Potential quick fix for incomplete function literal #46972

eernstg opened this issue Aug 24, 2021 · 0 comments
Labels
analyzer-quick-fix analyzer-server area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P3 A lower priority bug or feature request type-enhancement A request for a change that isn't a bug

Comments

@eernstg
Copy link
Member

eernstg commented Aug 24, 2021

Inspired by the discussion here, I noted that there is little help in IntelliJ if we're writing an actual argument to a function f which is a function literal, and the given function literal has the wrong type because some formal parameters are missing:

void f(void Function(int, int, {bool name}) {}

void main() {
  f(); // At first, I get 'One positional argument expected, ...': Fine!
  f(() {}); // Just the usual 'The argument type can't be assigned to the parameter type'.
}

I think it could be helpful if this situation could be detected:

  1. An expression e occurs in a position where it represents a data flow with a context type T; this could be an actual parameter, an initializing expression in a variable declaration, a returned expression, etc.
  2. e can be parsed as a function literal (if possible, we may even allow for syntax errors in the body, but the formal parameters must parse, and they cannot have any compile-time errors, e.g., if there are any types then they must exist and be well-bounded). If we cannot determine the return type of the function literal then we use dynamic (we might even use dynamic always, because this feature is not going to touch the function body anyway).
  3. The type of e is not assignable to T.
  4. Assignability can be achieved by copying formal parameters from the context type into the function literal:
  • Required positional parameters from the context type can be added after the last required positional parameter in the function literal.
  • Optional positional parameters can be added after the last optional positional parameter in the function literal, in order, or a new [] element can be added to the parameter list in the function literal if there are no optional positional and no named parameters.
  • Named parameters can be added at the end of the {} element in the function literal parameter list, or a new one can be introduced if there are no optional positional parameters.

The names of newly added non-named parameters would be fresh. E.g., they could be named 'pN' where N is a numeral that makes the name fresh in the given scope), and they could also be taken from the declaration of the context type (e.g., the relevant parameter type could be written as the type of the parameter g in void f(void Function(int height, int width) g), where the type itself contains a meaningful choice of names, possibly extended with a numeral if that name is in scope already.

I think this could be helpful in a significant number of situations that actually occur. It would allow developers to write just some parameters if they don't actually care about the rest, and it allows developers to get those parameter declarations for free in a situation where it might be inconvenient to obtain them in other ways.

@eernstg eernstg added area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. analyzer-quick-fix labels Aug 24, 2021
@scheglov scheglov added analyzer-server P3 A lower priority bug or feature request labels Aug 24, 2021
@srawlins srawlins added the type-enhancement A request for a change that isn't a bug label Mar 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
analyzer-quick-fix analyzer-server area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P3 A lower priority bug or feature request type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

3 participants