Bug 4090 – No foreach type inference with const, ref etc modifiers
Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2010-04-14T07:44:00Z
Last change time
2012-11-10T05:10:01Z
Keywords
diagnostic, pull, spec
Assigned to
k.hara.pg
Creator
bearophile_hugs
Comments
Comment #0 by bearophile_hugs — 2010-04-14T07:44:09Z
This is a D2 program:
void main() {
double[10] arr;
double tot = 0;
foreach (const ref x; arr)
tot += x;
}
dmd 2.043 prints:
test.d(4): basic type expected, not ref
test.d(4): no identifier for declarator const(int)
test.d(4): found 'ref' when expecting ';'
test.d(4): found ';' when expecting ')'
test.d(4): found ')' when expecting ';' following 'statement'
Those error messages are too many, and they don't describe what the problem is.
Replacing the foreach line with the following line there are no compile errors:
foreach (ref const(double) x; arr)
Also note this program compiles with no errors:
void foo(const ref int x) {}
void main() {}
Comment #1 by yebblies — 2011-09-07T01:14:50Z
*** Issue 6616 has been marked as a duplicate of this issue. ***
Comment #2 by yebblies — 2011-09-07T01:15:11Z
*** Issue 5255 has been marked as a duplicate of this issue. ***
Comment #3 by k.hara.pg — 2012-07-01T18:46:53Z
https://github.com/D-Programming-Language/dmd/pull/1033
This is not supported by the current spec, so it is an enhancement..
http://dlang.org/statement#ForeachStatement
> ForeachStatement:
> Foreach (ForeachTypeList ; Aggregate) NoScopeNonEmptyStatement
>
> Foreach:
> foreach
> foreach_reverse
>
> ForeachTypeList:
> ForeachType
> ForeachType , ForeachTypeList
>
> ForeachType:
> ref(opt) BasicType Declarator
> ref(opt) Identifier
ForeachType can have only 'ref' as a storage class.
Others, 'const', 'immutable', 'shared', and 'inout' are disallowed by the grammar.
Comment #4 by k.hara.pg — 2012-09-16T04:53:49Z
*** Issue 8649 has been marked as a duplicate of this issue. ***
Comment #5 by github-bugzilla — 2012-10-28T03:45:55Z
Comment #10 by bearophile_hugs — 2012-11-05T04:23:23Z
void main() {
foreach (const i; 0 .. 10)
i++;
}
Now it prints both the warning and the error:
test.d(3): Warning: variable modified in foreach body requires ref storage class
test.d(3): Error: cannot modify const expression i
Closed again. Thank you Hara, Don, yebblies, and others.
Comment #11 by bearophile_hugs — 2012-11-05T18:56:37Z
void main() {
int[] array = [10, 20, 30];
foreach (const i, x; array) {}
foreach (immutable i, x; array) {}
}
It gives:
test.d(3): Error: cannot modify const expression __key5
test.d(4): Error: cannot modify immutable expression __key7
Do you want me to reopen this bug report?
Comment #12 by k.hara.pg — 2012-11-06T06:27:12Z
(In reply to comment #11)
> void main() {
> int[] array = [10, 20, 30];
> foreach (const i, x; array) {}
> foreach (immutable i, x; array) {}
> }
>
>
> It gives:
>
> test.d(3): Error: cannot modify const expression __key5
> test.d(4): Error: cannot modify immutable expression __key7
>
>
> Do you want me to reopen this bug report?
Ouch... sorry, will fix and post the 3rd pull.
Comment #13 by bearophile_hugs — 2012-11-06T10:47:12Z
(In reply to comment #12)
> Ouch... sorry, will fix and post the 3rd pull.
Thank you Hara. Your new pull request, with a ton of unittests is:
https://github.com/9rnsr/dmd/commit/4bc728c24256d11454b580b82a3201e32b8e286d
For my code I write unittests in the same way as you, I try all combinations, in a grid. Because as soon as I miss one combination in the unittests, it sometimes contains a bug :-)
Comment #14 by github-bugzilla — 2012-11-07T01:32:44Z
Comment #15 by bearophile_hugs — 2012-11-07T04:47:43Z
Now this code:
void main() {
int[] array = [10, 20, 30];
foreach (const i, x; array) i++;
foreach (immutable i, x; array) i++;
}
Gives:
test.d(3): Warning: variable modified in foreach body requires ref storage class
test.d(3): Error: cannot modify const expression i
test.d(4): Warning: variable modified in foreach body requires ref storage class
test.d(4): Error: cannot modify immutable expression i
Why is it giving both the warning and the error?
Closed again. Thank you Hara and others.
Comment #16 by k.hara.pg — 2012-11-10T05:10:01Z
(In reply to comment #15)
> Now this code:
>
> void main() {
> int[] array = [10, 20, 30];
> foreach (const i, x; array) i++;
> foreach (immutable i, x; array) i++;
> }
>
>
>
> Gives:
>
> test.d(3): Warning: variable modified in foreach body requires ref storage
> class
> test.d(3): Error: cannot modify const expression i
> test.d(4): Warning: variable modified in foreach body requires ref storage
> class
> test.d(4): Error: cannot modify immutable expression i
>
> Why is it giving both the warning and the error?
Posted a fix-up pull.
https://github.com/D-Programming-Language/dmd/pull/1276