Skip to content

Analyzer incorrectly reports "In constant expressions, operands of this operator must be of type 'bool', 'num', 'String' or 'null'" for == operator with enum operand #26980

Closed

Description

@Hixie
enum MyEnum { a, b }

class Test {
  const Test(MyEnum arg) : value = arg == MyEnum.a ? 0 : 1;
  final int value;
}

void main() {
  Test q = const Test(MyEnum.a);
  Test r = const Test(MyEnum.a);
  print(identical(q, r));
}

VM prints "true", showing that the const expression is fine.

Analyzer says:

[error] In constant expressions, operands of this operator must be of type 'bool', 'num', 'String' or 'null' (test.dart, line 4, col 36)

Activity

  1. bwilkerson commented on Jul 28, 2016

    @bwilkerson
    Member

    Or showing that the VM has a bug :-)

    The language spec says that a constant expression can be (among other things):

    An expression of one of the forms e1 == e2 or e1 != e2 where e1 and e2 are constant expressions that evaluate to a numeric, string or boolean value or to null.

    It doesn't whitelist enum values. Actually, it doesn't allow enum constants anywhere in constant expressions. I'm guessing this was an oversight.

  2. added
    area-languageDart language related items (some items might be better tracked at github.com/dart-lang/language).
    on Jul 28, 2016
  3. Hixie commented on Jul 28, 2016

    @Hixie
    ContributorAuthor

    I think if there's a bug, it's in the spec and the analyzer, not in the VM. :-)

  4. lrhn commented on Jul 29, 2016

    @lrhn
    Member

    We could allow any compile-time constant value of a class that doesn't override Object.== as the left-hand side of a constant == operation, but that's probably a little too clever to be actually useful (that would make it a breaking change to add operator== to a class with a const constructor, which it isn't now).

    Allowing enums is probably fine and useful.

    (The spec, by definition, cannot have bugs - only "features" :)

  5. added a commit that references this issue on May 19, 2017
  6. dnfield commented on Jun 11, 2018

    @dnfield
    Contributor

    It seems like the following should be possible (or at least it seems to make sense), but it is not as a result of this:

    class Test2 {
      const Test2({this.opt1, this.opt2}) : assert(opt1 == null || opt2 == null);
      final Object opt1;
      final Object opt2;
    }
    

    The goal being to have a const constructor where only one of two parameters is ever specified.

  7. dnfield commented on Jun 11, 2018

    @dnfield
    Contributor

    After reading #30288 (comment) - it seems like I just need to use identical instead of == and it's all fine. While that makes some sense, it seems like it should be possible to interpret opt1 == null as being identical to identical(opt1, null) (or at least hint the user that that would work here).

  8. eernstg commented on Jun 11, 2018

    @eernstg
    Member

    Apparently, the implementation of the added support for comparison with null in constant expressions was never requested explicitly. I just created #33408 to make that happen. I'm closing this because the 'area-language' part was resolved already in Oct 2017 with commit f11f2ed.

  9. eernstg commented on Jun 11, 2018

    @eernstg
    Member

    Oops, I was looking at this comment and concluding that this issue was about comparison with null. It actually started out as an issue about comparison of enum values, and that has not yet been addressed: Reopening.

  10. dnfield commented on Jun 11, 2018

    @dnfield
    Contributor

    Yes, sorry I got confused between the two issues as well.

  11. a-siva commented on Apr 11, 2019

    @a-siva
    Contributor

    Flutter code has been fixed and a language issue has been created allowing '==' on Enum values, closing this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-languageDart language related items (some items might be better tracked at github.com/dart-lang/language).type-enhancementA request for a change that isn't a bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions