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

Mockito's "any" incompatible with strong mode analyzer #26036

Closed
charliereams opened this issue Mar 18, 2016 · 6 comments
Closed

Mockito's "any" incompatible with strong mode analyzer #26036

charliereams opened this issue Mar 18, 2016 · 6 comments
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 type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)

Comments

@charliereams
Copy link

There are various contexts in which the analyzer complains about the "any" matcher, e.g. named arguments, function-typed parameters, and certain others, with the error message Unsound implicit cast from dynamic to <some type>. Is there a suggested work around for this?

cc/ @ochafik

@zoechi
Copy link
Contributor

zoechi commented Mar 18, 2016

maybe adding this comment before the line with the warning

// ignore: DOWN_CAST_COMPOSITE

@leafpetersen
Copy link
Member

For context, we generally warn about these because they are places where you might get unexpected results from the cast because of a reified dynamic. For example:

dynamic x = [3];
List<String> l = x;

This will run fine on the VM (at least until you start using the contents of l as Strings), but will throw a runtime type error on DDC. If you're confident that the cast in question is correct, and you don't want to/can't write the code in such a way as to make that apparent to the typechecker, you have three options:

  1. Ignore the error directly at the use site as suggested above (though I think you probably need STRONG_MODE_DOWN_CAST_COMPOSITE)
  2. Put in an explicit cast.
  3. Use a helper function, such as one of the ones below. The advantage of this over the explicit cast is that on the VM/dart2js, these ought to be fully inlined away resulting in no runtime cost.
// ignore: STRONG_MODE_DOWN_CAST_COMPOSITE
/*=To*/ downcast/*<From, To extends From>*/(From x) => x;
// ignore: STRONG_MODE_DOWN_CAST_COMPOSITE
/*=To*/ cast/*<To>*/(dynamic x) => x;

void test2() {
  dynamic d;
  List<int> x = downcast/*<dynamic, List<int>>*/(d);
  List<int> y = cast/*<List<int>>*/(d);
}

@sgjesse sgjesse added area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. analyzer-strong-mode labels Mar 29, 2016
@charliereams
Copy link
Author

AFAICT casting won't work, since "any" really isn't an object of the appropriate type. Disabling the error will work, but it's not great to have to disable warnings all over the place, even in tests. Is there any route to a better solution in the future? (Note that in other languages's implementations of Mockito, "any" is a generic method rather than a static constant, which should be possible to replicate in Dart soon.)

@leafpetersen
Copy link
Member

Other than making any into a generic method (which you can do now with the comment based syntax), I don't have any immediate suggestions. Obviously that would be a breaking change in the API.

I guess this is a use case for generic getters, which we haven't planned to support so far. I'd have to think about how they would work out.

@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 Mar 31, 2016
@ochafik
Copy link
Contributor

ochafik commented Apr 13, 2016

cc/ @vsmenon

@srawlins
Copy link
Member

srawlins commented Aug 4, 2016

This has been fixed via https://github.com/fibulwinter/dart-mockito/pull/26, #26863, and dart-archive/dev_compiler@e5c2aac. Thanks @leafpetersen, @TedSander, @jmesserly, and @dskloetg for lots of work and input!

@srawlins srawlins closed this as completed Aug 4, 2016
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 type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
None yet
Development

No branches or pull requests

7 participants