Skip to content

Least and greatest closures are incorrect for covariant generic typedef check<X> = X Function() #44162

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
iarkh opened this issue Nov 12, 2020 · 1 comment
Labels
legacy-area-front-end Legacy: Use area-dart-model 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

@iarkh
Copy link
Contributor

iarkh commented Nov 12, 2020

Dart SDK version: 2.12.0-29.0.dev (dev) (Mon Nov 9 06:42:00 2020 -0800) on "windows_x64"

Here is a source code example:

Type? _capturedTypeArgument;

X captureTypeArgument<X>() {
  _capturedTypeArgument = X;
  throw "Error";
}

Type? get capturedTypeArgument {
  var result = _capturedTypeArgument;
  _capturedTypeArgument = null;
  return result;
}

typedef F<X> = void Function<Y extends X>();
F<X> toF<X>(X x) => <Y extends X>() {};
Type typeOf<X>() => X;

typedef check<X> = X Function();

void main() {
  void f<X>(check<X> Function() g) => g();
  try { f(() => captureTypeArgument()); } catch(_) {};
  print (typeOf<check>());
  print(capturedTypeArgument);
}

Seems like Object? should be substituted as a bound both for check and capturedTypeArgument here, however this is not so and sample output is:

() => dynamic
() => Object?

Please note that for another covariant case - typedef check<X> = X Function() - greatest closure is Object? in both cases.

See also similar issue against contravariant case: #44161.

@keertip keertip added the legacy-area-front-end Legacy: Use area-dart-model instead. label Nov 12, 2020
@johnniwinther johnniwinther added type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) P2 A bug or feature request we're likely to work on labels Nov 12, 2020
@eernstg
Copy link
Member

eernstg commented Dec 15, 2020

Cf. #44161 for a bit more detail.

This is working as intended: typeOf<check>() is obtained by applying i2b on the raw type check, and the resulting type is dynamic Function(). capturedTypeArgument is the greatest closure of _ Function() with respect to {_}, which is Object? Function().

@eernstg eernstg closed this as completed Dec 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
legacy-area-front-end Legacy: Use area-dart-model 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

4 participants