Skip to content

const asserts with == null #31140

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
goderbauer opened this issue Oct 18, 2017 · 9 comments
Closed

const asserts with == null #31140

goderbauer opened this issue Oct 18, 2017 · 9 comments
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language).

Comments

@goderbauer
Copy link
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.

@bwilkerson
Copy link
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.

@bwilkerson bwilkerson added the area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). label Oct 18, 2017
@lrhn
Copy link
Member

lrhn commented Oct 19, 2017

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
Copy link
Contributor

a14n commented Oct 19, 2017

Dup of #30288 ?

whesse pushed a commit that referenced this issue Oct 26, 2017
…m/string/bool/null.

See issue #31140

Bug: http://dartbug.com/31140
Change-Id: I141cc3856a1758313de102084f74f4fa41412586
Reviewed-on: https://dart-review.googlesource.com/15100
Commit-Queue: Lasse R.H. Nielsen <[email protected]>
Reviewed-by: Erik Ernst <[email protected]>
@srawlins
Copy link
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
Copy link
Contributor

Hixie commented Aug 17, 2018

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
Copy link
Member

lrhn commented Aug 20, 2018

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.

@lrhn lrhn reopened this Aug 20, 2018
@yjbanov
Copy link

yjbanov commented Dec 4, 2018

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

@srawlins
Copy link
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
Copy link

yjbanov commented Sep 12, 2019

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

@lrhn lrhn added the closed-stale Closed as the issue or PR is assumed stale label Apr 9, 2025
@lrhn lrhn closed this as not planned Won't fix, can't repro, duplicate, stale Apr 9, 2025
@eernstg eernstg removed the closed-stale Closed as the issue or PR is assumed stale label Apr 10, 2025
@eernstg eernstg reopened this Apr 10, 2025
@eernstg eernstg closed this as completed Apr 10, 2025
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

8 participants