Skip to content

Analyzer: it is an error if a type argument is a generic function type #33343

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

Closed
leafpetersen opened this issue Jun 5, 2018 · 5 comments
Closed
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
Milestone

Comments

@leafpetersen
Copy link
Member

The analyzer is missing the following errors from https://github.com/dart-lang/sdk/blob/master/docs/language/informal/generic-function-type-alias.md :

 It is a compile-time error if a generic function type is used as a bound for a formal type parameter of a class or a function. It is a compile-time error if a generic function type is used as an actual type argument.
@leafpetersen leafpetersen added the area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. label Jun 5, 2018
@leafpetersen leafpetersen added this to the Dart2Stable milestone Jun 8, 2018
@MichaelRFairhurst
Copy link
Contributor

@eernstg @bwilkerson

I firmly believe that good advice is a huge part of good error messages. I'd like help brainstorming a simple "correction" message to use in the implementation.

Part of that principle is that bad suggestions are bad. So maybe here I should not try to offer any suggestions at all. Regardless, here's what I have so far.

For using a generic function as a bound, here's what I have: "Try making the free variable in the function type part of the larger declaration signature"

If that's not clear don't let my explanation convince you otherwise. However, I'll describe what I mean by that in case anyone has further suggestions etc.

List<T> map<T extends S Function<S>(S)>(T f);
// should be?
List<T> map<T extends Function(T), S>(T f);

This is obviously not a catch-all suggestion.

For disabling parameterization with a generic function type, I have (yikes) "Try resorting to dynamic programming if you need to have generic function types in your consuming code."

Trying to think through some circumstance which is maybe a guideline for what else to suggest, I imagine:

class Container<T> {
  T val;
  Container<S> map<S>(S Function(T));
}
final List<T Function<T>(T)> identities = ...;
List<Container<List<int>> x = new List<Container<int>>(...).expand((c) => identities.map((i) => c.map(i)));
List<Container<List<String>> x = new List<Container<String>>(...).expand((c) => identities.map((i) => c.map(i)));

In this case it seems like the only solution will involve casting. Reification is prone to go wrong in any workarounds. Therefore suggesting "dynamic programming" seems like essentially the only option, especially given that "dynamic programming" is on a sort of scale that potentially includes:

final List<Object Function(Object)> identities ...;
List<Container<List<int>> x = new List<Container<int>>(...).expand((c) => identities.map((i) => c.map(i).map((r) => r as int)));

Anyways, feedback welcome. Either in terms of user-facing advice being adequate, lacking, convoluted, or wrong, etc.

@MichaelRFairhurst
Copy link
Contributor

I didn't read the paper on why generic function types as bounds or parameters create problems. So forgive me for asking rather than researching...

What do those problems mean for this case:

class C<T extends void Function(S Function<S>(S))> { ... }

the generic function type is being used as a bound indirectly, but the bound is not a generic function type. Should this be illegal, and should the informal spec be updated to account for this?

@MichaelRFairhurst MichaelRFairhurst self-assigned this Jun 19, 2018
@MichaelRFairhurst
Copy link
Contributor

I'm also assuming that this the second part of this (if a type argument is a generic function type that's a compile time error) should be part of the inference engine?

@MichaelRFairhurst
Copy link
Contributor

Just saw this test case:

  // Type inference must also give an error.                                     
  var inferredList = [foo];  //# 02: compile-time error   

well that's good to have my answer regarding inference!

dart-bot pushed a commit that referenced this issue Jun 25, 2018
Does not handle the other half of #33343, rejecting generic function
types as type arguments.

This revealed a secondary minor issue which was easy enough to fix. Type
arguments were not resolved within bounds:

class C<T extends S Function<S>(S)> {}

this would report 'undefined class S' for the return and parameter types
`S`. I almost split this into a separate CL, but, these two CLs are tied
together inherently by the tests case. Easy to solve at once.

Bug: 33343
Change-Id: Ib34a04d90be08d8d6c6f21a9d485a452017585ba
Reviewed-on: https://dart-review.googlesource.com/61103
Commit-Queue: Mike Fairhurst <mfairhurst@google.com>
Reviewed-by: Paul Berry <paulberry@google.com>
dart-bot pushed a commit that referenced this issue Jun 25, 2018
There is a seeming parse error in typedefs, and an open question on the
issue (33343) about what to do when a generic function type is inferred.

Otherwise this seems ready to go.

Bug: 33343
Change-Id: I10d2ea9b6ca26ed2c6ff6b24ffe5008fc4797ef2
Reviewed-on: https://dart-review.googlesource.com/61109
Commit-Queue: Mike Fairhurst <mfairhurst@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
@bwilkerson bwilkerson added the P2 A bug or feature request we're likely to work on label Jun 26, 2018
@MichaelRFairhurst
Copy link
Contributor

Aside from the inference case, this is fixed in https://dart-review.googlesource.com/c/sdk/+/61109 and https://dart-review.googlesource.com/c/sdk/+/61103.

I'll open a new bug for the inference side and put it in 2.1.

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
Projects
None yet
Development

No branches or pull requests

3 participants