Skip to content

synthetic function types are not substituted correctly #29778

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 1, 2017 · 5 comments
Closed

synthetic function types are not substituted correctly #29778

leafpetersen opened this issue Jun 1, 2017 · 5 comments
Labels
legacy-area-analyzer Use area-devexp instead. 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

@leafpetersen
Copy link
Member

leafpetersen commented Jun 1, 2017

EDIT by @jmesserly: see comment here #29778 (comment) for description of the problem.

This bug has been recurring rather often and showed up in several other bugs, #30822 and #30207

original bug description by Leaf follows:


The following program:

void main() {
  Null Function<R>(R Function(int)) f = <S>(g) {};
}

produces the following incorrect error when run against bleeding edge analyzer:

  error • A value of type '<S₀>((int) → S) → Null' can't be assigned to a variable of type '<R>((int) → R) → Null' at /Users/leafp/tmp/test.dart:2:41 • invalid_assignment

The inferred type for the lambda is <S₀>((int) → S) → Null, instead of the presumably correct <S₀>((int) → S₀) → Null .

It looks like downwards inference is correctly choosing a new canonical type variable S₀ to use to match up R and S, but is then constructing the final inferred type using a mix of S and
S₀ to produce an invalid type.

@bwilkerson bwilkerson added P2 A bug or feature request we're likely to work on type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) labels Jun 2, 2017
@jmesserly jmesserly self-assigned this Jul 21, 2017
@jmesserly
Copy link

possibly related substitution problem: #30207

@jmesserly
Copy link

jmesserly commented Jul 22, 2017

so, this happens any time we construct a (synthetic) function type, such as in TypeSystem. The resulting object cannot be substituted correctly because its free variable list (FunctionTypeImpl.typeParameters) is empty (because it did not have an enclosingElement scope chain available to populate the free variables).

This is going to be tricky to fix, but I'm working on it.

Other interesting finds:

  • function types are not considered equal unless their typeArguments arrays are. So int -> int is not the same as {int/T} T -> T ... I thought we'd fixed this, but it's back (edit: and test_instanceField_functionTypeAlias_doesNotUseItsTypeParameter depends on this behavior somehow)
  • GLB/LUB seems to ignore(!!) typeFormals of a generic function (filed as GLB/LUB do not handle generic functions #30237)

EDIT: also we need to fix TypeParameterElement.==/hashCode so they work correctly for synthetic type parameters.

@jmesserly
Copy link

Here are two more repros from #30822

main() {
  T Function() Function<T>() g =
      <S>() {
    S Function() f;
    if (f != null) return f;
    return () => f();
  };
}

and

// Wants a function that takes a function and returns a same-typed function.
void foo(T Function() Function<T>(T Function() f) parameterName) {}

// Using a function literal. -> the return type needs to be inferred.
foo(<T>(T Function() f) {
  if (true) return f;
  return () => f();
});

@jmesserly
Copy link

another example from #30207 is tests/language_2/generic_methods_generic_function_result_test.dart

@jmesserly jmesserly changed the title Analyzer downwards inference doesn't match up type variables correctly synthetic function types are not substituted correctly Sep 25, 2017
@jmesserly jmesserly removed their assignment Oct 4, 2017
@jmesserly
Copy link

this was recently fixed https://dart-review.googlesource.com/c/sdk/+/59160, #33159

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
legacy-area-analyzer Use area-devexp instead. 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

3 participants