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

Passing type-parameterized function to ArgumentError.checkNotNull broken in 2.8.1 #41871

Closed
jonasfj opened this issue May 12, 2020 · 5 comments
Closed
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language).

Comments

@jonasfj
Copy link
Member

jonasfj commented May 12, 2020

I might be using more type-parameters than is useful :)
But I ran into this curious case, and wonder if it's a known regression.

typedef WrapperFn = Future<T> Function<T>(Future<T> Function() fn);

void acceptsWrapper(WrapperFn wrap) async {
  // This works on Dart 2.7.0, but not Dart 2.8.1
  ArgumentError.checkNotNull(wrap);

  // This works fine on both Dart 2.7.0 and 2.8.1
  if (wrap == null) {
    throw ArgumentError.notNull();
  }

  await wrap(() async {
    // Do something async that we want [wrap] to be able to initiate and
    // return the result from, including catching exceptions.
  });
}

void main() => print('hello world');

On dart 2.7.0 it works, but on 2.8.1 I get (before it starts running):

hack.dart:5:17: Error: Generic function type 'Future<T> Function<T>(Future<T> Function())' inferred as a type argument.
 - 'Future' is from 'dart:async'.
Try providing a non-generic function type explicitly.
  ArgumentError.checkNotNull(wrap);
)
@jonasfj
Copy link
Member Author

jonasfj commented May 12, 2020

@lrhn might this have to do with signature change from dart 2.7.1 which has:

static void checkNotNull(Object argument, [String name]);

to dart 2.8.1 which has:

static T checkNotNull<@Since("2.8") T>(T argument, [String name]);

@lrhn
Copy link
Member

lrhn commented May 13, 2020

It is because of that change, yes. I did not anticipate that change being breaking. Just goes to prove that no change is too small to break someone!

Dart prohibits a generic function type from being a type argument.
However, it does not stop itself from inferring such a type as a type argument.

We should just stop having that restriction, or at least stop inferring generic types as arguments (just inferring Function would work).

The workaround would be to pass an explicit type argument (just void would be fine, since no-one is using the return value).

@mit-mit mit-mit added the area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. label May 13, 2020
@jonasfj
Copy link
Member Author

jonasfj commented May 13, 2020

For work around, one can also do:

if (wrap == null) {
  throw ArgumentError.notNull();
}

That works fine :)

But for existing code fixing this would be nice, at it's a static error for any code using package:acyclic_steps (I'll probably publish a patch version with the work around).

@lrhn lrhn added area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). and removed area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. labels Oct 3, 2020
@lrhn
Copy link
Member

lrhn commented Oct 3, 2020

Closing in favor of dart-lang/language#496

@lrhn lrhn closed this as completed Oct 3, 2020
@mit-mit
Copy link
Member

mit-mit commented Sep 1, 2021

This is supported as of Dart 2.14.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language).
Projects
None yet
Development

No branches or pull requests

3 participants