Skip to content

const asserts with == null #31140

@goderbauer

Description

@goderbauer
Contributor

The following code produces the analyzer error "In constant expressions, operands of this operator must be of type 'bool', 'num', 'String' or 'null'. (const_eval_type_bool_num_string)" on the child == null part.

class Foo {
  const Foo({ this.child, this.text }) : assert(child == null || text == null);

  final Widget child;
  final String text;
}

Seems like this should be allowed though since I can easily transform the condition with deMorgan into something equivalent that the analyzer doesn't complain on. The transformed condition is just harder to read/comprehend.

Activity

bwilkerson

bwilkerson commented on Oct 18, 2017

@bwilkerson
Member

This restriction comes directly from the language spec. I think the spec would need to be updated before we could update analyzer, so I'm marking this as a language issue.

added
area-languageDart language related items (some items might be better tracked at github.com/dart-lang/language).
on Oct 18, 2017
lrhn

lrhn commented on Oct 19, 2017

@lrhn
Member

Seems like a bug/oversight in the specification. I'm sympathetic to fixing it.
There'll likely be a clean-up of the const/potentially const specification for Dart 2, to iron out a few other problematic cases, but I see no reason not to commit to this immediately.

a14n

a14n commented on Oct 19, 2017

@a14n
Contributor

Dup of #30288 ?

srawlins

srawlins commented on Jun 22, 2018

@srawlins
Member

The example no longer reports an error:

$ dartanalyzer --version
dartanalyzer version 2.0.0-dev.63.0
$ cat 31140.dart 
class Foo {
  const Foo({ this.child, this.text }) : assert(child == null || text == null);

  final String child;
  final String text;
}
$ dartanalyzer 31140.dart 
Analyzing 31140.dart...
No issues found!
Hixie

Hixie commented on Aug 17, 2018

@Hixie
Contributor

If I try to revert our workaround for this and use assert(child == null || text == null), I get:

[error] In constant expressions, operands of this operator must be of type 'bool' (/usr/local/google/home/ianh/dev/flutter/packages/flutter/lib/src/ma\
terial/tabs.dart:64:15)
[error] In constant expressions, operands of this operator must be of type 'bool', 'num', 'String' or 'null' (/usr/local/google/home/ianh/dev/flutter/\
packages/flutter/lib/src/material/tabs.dart:64:31)
lrhn

lrhn commented on Aug 20, 2018

@lrhn
Member

Issue is definitely still here. The problem does not occur in the test above because the constructor is never called with an object which isn't of those types.

reopened this on Aug 20, 2018
yjbanov

yjbanov commented on Dec 4, 2018

@yjbanov

FWIW this issue affects Hummingbird. We have to comment out some asserts to make the code run in DDC.

srawlins

srawlins commented on Sep 12, 2019

@srawlins
Member

I think analyzer 2.5.0, with the const-update-2018, produces the correct behavior. Using the example above:

class Foo {
  const Foo({ this.child, this.text }) : assert(child == null || text == null);

  final Widget child;
  final String text;
}

class Widget {
  const Widget();
}

void main() {
  const Foo(child: null, text: null);
  const Foo(child: null, text: 'yay');
  const Foo(child: const Widget(), text: null);
  const Foo(child: const Widget(), text: 'yay');
}

The analyzer only reports an error on the last line:

Evaluation of this constant expression throws an exception.

yjbanov

yjbanov commented on Sep 12, 2019

@yjbanov

We merged into the main Flutter repo and that repo passes the analyzer, so I guess this is fixed.

added
closed-staleClosed as the issue or PR is assumed stale
on Apr 9, 2025

4 remaining items

Loading
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

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @srawlins@yjbanov@Hixie@a14n@goderbauer

        Issue actions

          const asserts with `== null` · Issue #31140 · dart-lang/sdk