Skip to content

no type error when one side of a conditional expression is the wrong type #27753

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
skybrian opened this issue Nov 3, 2016 · 5 comments
Closed
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language).

Comments

@skybrian
Copy link

skybrian commented Nov 3, 2016

In DartPad, there is no type error for this code, even in strong mode:

main() {
  String x1 = "x" == 3 ? true : "hello";
  String x2 = "x" == 3 ? "hi" : true;
  print(x1);
  print(x2);
}

It seems like it should be an error to assign a boolean to a string.

@a14n
Copy link
Contributor

a14n commented Nov 4, 2016

I think it's because of implicit casts. "x" == 3 ? true : "hello" has type Object that can be cast to String.

It's reported as error with :

analyzer:
  strong-mode:
    implicit-casts: false
ERROR: A value of type 'Object' cannot be assigned to a variable of type 'String' (test.dart:2)
ERROR: A value of type 'Object' cannot be assigned to a variable of type 'String' (test.dart:3)

@lrhn
Copy link
Member

lrhn commented Nov 4, 2016

This is indeed an unfavorable interaction between finding the least upper bound of the conditional expression branches to be Object and implicit down-casts allowing Object to be assigned to anything.
It is working as the specification says it should.
I hope we'll eventually be able to do better.

@skybrian
Copy link
Author

skybrian commented Nov 4, 2016

That's unfortunate. I wonder if we should handle this as a special case in strong mode?

@kevmoo kevmoo added area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). analyzer-strong-mode labels Nov 4, 2016
@jmesserly
Copy link

jmesserly commented Nov 4, 2016

@skybrian Indeed! That's covered by #25368

(edit: untagging strong mode because above issue covers that request)

@skybrian
Copy link
Author

skybrian commented Nov 4, 2016

Okay, I guess this one is a dup then.

@skybrian skybrian closed this as completed Nov 4, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language).
Projects
None yet
Development

No branches or pull requests

5 participants