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

Strong mode allows Object to be inserted into List<String> #26437

Closed
harryterkelsen opened this issue May 10, 2016 · 5 comments
Closed

Strong mode allows Object to be inserted into List<String> #26437

harryterkelsen opened this issue May 10, 2016 · 5 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

@harryterkelsen
Copy link
Contributor

a.dart:

Object dummy() => new Object();
main() {
  var l = new List<String>();
  l.add(dummy());
}
$ dartanalyzer --strong a.dart
Analyzing [a.dart]...
No issues found

@leafpetersen @vsmenon @jmesserly

@harryterkelsen harryterkelsen added analyzer-strong-mode area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. labels May 10, 2016
@vsmenon
Copy link
Member

vsmenon commented May 10, 2016

Yeah, that's probably because we allow implicit casting. Can you try running in DDC? You should get a runtime error.

@jmesserly
Copy link

another way to see the implicit downcast is to use a composite type:

Object dummy() => new Object();
main() {
  var l = new List<List<String>>();
  l.add(dummy());
}
[warning] Unsound implicit cast from Object to List<String> (test.dart, line 4, col 9)

Right now this behavior is By Design. Whether it will be that way in the future, not sure :)

@harryterkelsen
Copy link
Contributor Author

I think there should be a warning for any code that would crash in checked mode

@jmesserly
Copy link

I think there should be a warning for any code that would crash in checked mode

Ironically, the current warning heuristics are the exact opposite of that.

We assume that no one has existing Dart1 code like above, because it would crash in VM/dart2js checked mode. That's why we allow it in Strong Mode.

We warn for composite types, because that could be passing Dart1 checked mode but will fail at runtime in Strong Mode.

It was a useful distinction to get code ported over to Strong Mode with minimal changes, but I agree in the long term I would love to see this changed and require explicit casts.

@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) and removed bug labels May 11, 2016
@leafpetersen
Copy link
Member

I'm going to close this as a duplicate and make #26583 the canonical issue for this.

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

5 participants