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

Front end infers dynamic for ".then" invocations #33044

Closed
stereotype441 opened this issue May 3, 2018 · 5 comments
Closed

Front end infers dynamic for ".then" invocations #33044

stereotype441 opened this issue May 3, 2018 · 5 comments
Labels
area-front-end Use area-front-end for front end / CFE / kernel format related issues.

Comments

@stereotype441
Copy link
Member

Consider the following code:

void test(Future<int> f) {
  var t1 = f.then((int x) {});
  var t2 = f.then((int x) { return; });
  var t3 = f.then((int x) { return null; });
  var t4 = f.then((int x) => null);
  var t5 = f.then((int x) { return x.toString(); });
  var t6 = f.then((int x) => x.toString());
  var t7 = f.then((x) {});
  var t8 = f.then((x) { return; });
  var t9 = f.then((x) { return null; });
  var t10 = f.then((x) => null);
  var t11 = f.then((x) { return x.toString(); });
  var t12 = f.then((x) => x.toString());
}

The analyzer infers t5, t6, t11, and t12 to have type Future<String>, and the others to have type Future<Null>. The front end infers the type of t1 through t12 to be dynamic.

This has user-visible consequences. For example, the analyzer considers this to be an error, and the front end doesn't:

void test(Future<int> f) async {
  var t = f.then((int x) { return x.toString(); });
  print(t.foo);
}

I believe the analyzer behavior is correct. @leafpetersen can you confirm?

@stereotype441 stereotype441 added the area-front-end Use area-front-end for front end / CFE / kernel format related issues. label May 3, 2018
@leafpetersen
Copy link
Member

I suspect that all of the .toString examples are instances of this bug: #32414 .

For the remainder: Future<dynamic> is always wrong. For t1, t2, t7, and t8 the analyzer is still implementing legacy behavior: we should infer Future<void> there, and I will be working on that change as soon as a fix for return warnings has rolled internally (unblocking the fixes for this).

For the rest, Future<Null> is correct.

@leafpetersen
Copy link
Member

#32843 is the issue for void inference.

@stereotype441
Copy link
Member Author

I suspect that all of the .toString examples are instances of this bug: #32414 .

@leafpetersen in that case I would have expected the front end to infer a type of Future<dynamic>. But it's inferring a type of dynamic.

@leafpetersen
Copy link
Member

sorry, I didn't read closely enough. I can't reproduce this though: the code below, run with dart --preview-dart-2 from fairly recent bleeding edge, has the expected errors (I think it stops reporting errors after some limit, but if you delete the first 5, you get the next 5 as expected). Am I still missing something?

import 'dart:async';

void test(Future<int> f) {
  int x;
  var t1 = f.then((int x) {});
  x = t1;
  t1.foo;
  var t2 = f.then((int x) { return; });
  x = t2;
  t2.foo;
  var t3 = f.then((int x) { return null; });
  x = t3;
  t3.foo;
  var t4 = f.then((int x) => null);
  x = t4;
  t4.foo;
  var t5 = f.then((int x) { return x.toString(); });
  x = t5;
  t5.foo;
  var t6 = f.then((int x) => x.toString());
  x = t6;
  t6.foo;
  var t7 = f.then((x) {});
  x = t7;
  t7.foo;
  var t8 = f.then((x) { return; });
  x = t8;
  t8.foo;
  var t9 = f.then((x) { return null; });
  x = t9;
  t9.foo;
  var t10 = f.then((x) => null);
  x = t10;
  t10.foo;
  var t11 = f.then((x) { return x.toString(); });
  x = t11;
  t11.foo;
  var t12 = f.then((x) => x.toString());
  x = t12;
  t12.foo;
}

void main() {
  test(new Future.value(3));
}

@stereotype441
Copy link
Member Author

@leafpetersen Oops, you are right. I forgot to include import 'dart:async'; in my test script (and failed to notice the error message the front end issued).

When I include the correct import, I get the same results as you. Since the remaining problems are already covered by open bugs, I'm closing this bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-front-end Use area-front-end for front end / CFE / kernel format related issues.
Projects
None yet
Development

No branches or pull requests

2 participants