Skip to content
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

analyzer complains that "toInt()", when used in initializer lists of a constant constructor, isn't a constant expression #8756

Closed
DartBot opened this issue Feb 24, 2013 · 8 comments
Assignees
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-as-intended Closed as the reported issue is expected behavior

Comments

@DartBot
Copy link

DartBot commented Feb 24, 2013

This issue was originally filed by googlegroups...@kaioa.com


With r18717 the analyzer/editor will complain about the use of toInt() in initializer lists of constant constructors:

  class Foo {
    final int x;
    const Foo (double bar) :
      x = bar.toInt(); // Expected constant expression
  }
  void main () {
    print(new Foo(4.2).x);
  }

The VM, however, will run the program in checked mode and will print "4" without complaining.

The same issue exists with other methods from num like toDouble or clamp.

@bwilkerson
Copy link
Member

Given section 12.1 in the specification, I believe that this is the correct behavior. Please confirm.


Added Area-Language, Triaged labels.

@lrhn
Copy link
Member

lrhn commented Feb 25, 2013

This is correct behavior for the analyzer.
The VM is incorrect: bar.toInt() is not a potentially constant expression, and that is a compile-time error:
"Any expression that appears within the initializer list of a constant constructor must be a potentially constant expression, or a compile-time error occurs." (Constant Constructors). This is independent of whether the constructor is invoked with "new" or with "const".

(So this is a bug report. If it was meant as a request to make the code valid, we should use a separate language issue for that.)


Removed Area-Language label.
Added Area-VM label.

@DartBot
Copy link
Author

DartBot commented Feb 25, 2013

This comment was originally written by googlegroups...@kaioa.com


It's true that the VM's behavior differs from the specs (if I'm reading them right). However, this is something which really should work. Otherwise, you won't be able to do anything in a static initializer list.

According to the specs, are also methods ("Operators are instance methods with special names"). So, you can't even do basic math. That doesn't sound right.

@DartBot
Copy link
Author

DartBot commented Feb 25, 2013

This comment was originally written by googlegroups...@kaioa.com


It seems like this stuff is actually covered by "potentially constant expressions" from section 7.6.3.

#­392 appears to be related.

@bwilkerson
Copy link
Member

It seems like this stuff is actually covered by "potentially constant expressions" from section 7.6.3.

The specification reads:

   Any expression that appears within the initializer list of a constant constructor must be a
   potentially constant expression, or a compile-time error occurs.

   A potentially constant expression is an expression e that would be a valid constant expression
   if all formal parameters of e’s immediately enclosing constant constructor were treated as
   compile-time constants that were guaranteed to evaluate to an integer, boolean or string value
   as required by their immediately enclosing superexpression.

I don't think that helps in this situation. The expression "bar.toInt()" is not a valid constant expression even if we assume that "bar" is a constant value that meets those criteria. The only invocations allowed in a constant expression are the function "identical" and most of the operators (but they are only allowed under specific conditions).

@iposva-google
Copy link
Contributor

Marked this as blocking #8777.

@iposva-google
Copy link
Contributor

As this issue is revolving around the language specification and its usefulness I am assigning back to Area-Language. Once this issue is resolved we can properly fix the associated issue #8777.


Set owner to @gbracha.
Removed Area-VM label.
Added Area-Language, Accepted labels.

@gbracha
Copy link
Contributor

gbracha commented Feb 25, 2013

The spec is fine. You should be able to achieve the desired effect using integer division: ~/.


Added AsDesigned label.

@DartBot DartBot added Type-Defect area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-as-intended Closed as the reported issue is expected behavior labels Feb 25, 2013
This issue was closed.
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). closed-as-intended Closed as the reported issue is expected behavior
Projects
None yet
Development

No branches or pull requests

5 participants