Skip to content

Analyzer doesn't throw errors when passing parameter with supertype to functions. #35785

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
Means88 opened this issue Jan 28, 2019 · 2 comments
Closed
Labels
closed-as-intended Closed as the reported issue is expected behavior

Comments

@Means88
Copy link

Means88 commented Jan 28, 2019

Dart VM version: 2.1.0 (Tue Nov 13 18:22:02 2018 +0100) on "macos_x64"
dartanalyzer version 2.1.0

void stringListFunction(List<String> list) {}

void genericListFunction<T>(List<T> list) {}

void stringFunction(String s) {}

main() {
  final List<String> list = ['a', 'b'];
  final Iterable<String> iterable = list.map((item) => item);
  final Object object = {};

  stringListFunction(iterable);
  stringListFunction(object);
  stringFunction(object);
}
➜ dartanalyzer main.dart
Analyzing main.dart...
No issues found!
➜ dart main.dart
Unhandled exception:
type 'MappedListIterable<String, String>' is not a subtype of type 'List<String>'
#0      main (file:///Users/means88/code/test/main.dart:12:22)
#1      _startIsolate.<anonymous closure> (dart:isolate/runtime/libisolate_patch.dart:289:19)
#2      _RawReceivePortImpl._handleMessage (dart:isolate/runtime/libisolate_patch.dart:171:12)
@lrhn
Copy link
Member

lrhn commented Jan 29, 2019

Dart allows you to use values with a static type that is a supertypes of the type that is expected. The object is implicitly down-cast to the subtype. If you expect a List<int> and you provide an Iterable<int>, it is as if there is an implicit as List<int> added.
That cast can then fail at run-time.

So, this is working as intended.

@lrhn lrhn closed this as completed Jan 29, 2019
@lrhn lrhn added the closed-as-intended Closed as the reported issue is expected behavior label Jan 29, 2019
@zoechi
Copy link
Contributor

zoechi commented Jan 29, 2019

You might want to track and thumbs-up #31410

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-as-intended Closed as the reported issue is expected behavior
Projects
None yet
Development

No branches or pull requests

3 participants