Skip to content

No analyser warning on incompatible assignment of a ternary condition result. #25366

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
aabluedragon opened this issue Jan 6, 2016 · 2 comments
Labels
legacy-area-analyzer Use area-devexp instead. 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

@aabluedragon
Copy link

Dart VM version: 1.14.0-dev.6.0

Dart analyser does not raise an error about type incompatibility in the following case:

int rand = new Random().nextInt(3);
int test = rand>0? "123" : 123;
@aabluedragon aabluedragon changed the title No analyser warning on assignment of a ternary condition result. No analyser warning on incompatibly assignment of ternary condition results. Jan 6, 2016
@aabluedragon aabluedragon changed the title No analyser warning on incompatibly assignment of ternary condition results. No analyser warning on incompatible assignment of a ternary condition result. Jan 6, 2016
@bwilkerson bwilkerson added Type-Defect legacy-area-analyzer Use area-devexp instead. labels Jan 6, 2016
@bwilkerson
Copy link
Member

While a warning would be nice, this is actually working according to the specification. The static type of a conditional expression is the least upper bound of the static types of the "then" and "else" expressions. The least upper bound of String and int is Object. Object is assignable to int, hence there is no warning.

This is a result of Dart having adopted an unsound type system. Strong mode uses a less unsound type system that should disallow that assignment, so I believe that you can get the warning you want by enabling strong mode.

@leafpetersen
Copy link
Member

Unfortunately, strong mode won't catch this right now, since we also allow implicit downcasts. We could (and should) do better though. Filed an issue here:

#25368

@kevmoo kevmoo 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 Priority-Medium labels Mar 1, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
legacy-area-analyzer Use area-devexp instead. 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

4 participants