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
Comments
Given section 12.1 in the specification, I believe that this is the correct behavior. Please confirm. Added Area-Language, Triaged labels. |
This is correct behavior for the analyzer. (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. |
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. |
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. |
The specification reads: Any expression that appears within the initializer list of a constant constructor must be a A potentially constant expression is an expression e that would be a valid constant expression 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). |
Marked this as blocking #8777. |
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. |
The spec is fine. You should be able to achieve the desired effect using integer division: ~/. Added AsDesigned label. |
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
liketoDouble
orclamp
.The text was updated successfully, but these errors were encountered: