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
Commits pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/dda2c5cd02f2b60bb58c4c56c17727fe31c2ea45 fix Issue 4090 - No foreach type inference with const, ref etc modifiers https://github.com/D-Programming-Language/dmd/commit/c6f32af2ccbc5a27d0174cc84121ba90eba6a6e2 Merge pull request #1033 from 9rnsr/fix4090 Issue 4090 - No foreach type inference with const, ref etc modifiers
Comment #6 by bearophile_hugs — 2012-10-28T04:49:53Z
But this program compiles with no errors, do you want me to reopen this issue? void main() { foreach (const i; 0 .. 10) i++; }
Comment #7 by yebblies — 2012-10-28T04:58:56Z
Urrgh, it seems it isn't fixed for foreach range statements. void main() { foreach (immutable i; 0..10) { pragma(msg, typeof(i)); ++i; } } Prints: int
Comment #8 by k.hara.pg — 2012-11-02T20:26:26Z
Comment #9 by github-bugzilla — 2012-11-05T01:35:10Z
Commits pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/423b395e24c3cb5ee6387524b40d82ceb16696c2 fix Issue 4090 - more fix for the foreach-range statement https://github.com/D-Programming-Language/dmd/commit/6d60ced035c322da3a30991b69180a768045b48f Merge pull request #1249 from 9rnsr/fix4090 Additional fix for Issue 4090 - No foreach type inference with const, ref etc modifiers
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
Commits pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/4bc728c24256d11454b580b82a3201e32b8e286d 3rd fix of Issue 4090 - No foreach type inference with const, ref etc modifiers https://github.com/D-Programming-Language/dmd/commit/5b77a1f4f46826562d8785489ac120e1f1753a15 Merge pull request #1261 from 9rnsr/fix4090 3rd fix of Issue 4090 - No foreach type inference with const, ref etc modifiers
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