www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 4090] New: foreach(const ref x; a) error message

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4090

           Summary: foreach(const ref x; a) error message
           Product: D
           Version: future
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Keywords: diagnostic
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: bearophile_hugs eml.cc



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() {}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 14 2010
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4090




*** Issue 6616 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Sep 07 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4090




*** Issue 5255 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Sep 07 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4090


Kenji Hara <k.hara.pg gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |spec
           Platform|x86                         |All
         OS/Version|Windows                     |All
           Severity|normal                      |enhancement



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. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jul 01 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4090




*** Issue 8649 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Sep 16 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4090




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


Issue 4090 - No foreach type inference with const, ref etc modifiers

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 28 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4090


yebblies <yebblies gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED
         AssignedTo|nobody puremagic.com        |k.hara.pg gmail.com


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 28 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4090




But this program compiles with no errors, do you want me to reopen this issue?


void main() {
    foreach (const i; 0 .. 10)
        i++;
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 28 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4090


yebblies <yebblies gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |REOPENED
         Resolution|FIXED                       |



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

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 28 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4090




Posted an additional pull request:
https://github.com/D-Programming-Language/dmd/pull/1249

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 02 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4090




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


Additional fix for Issue 4090 - No foreach type inference with const, ref etc
modifiers

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 05 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4090


bearophile_hugs eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|REOPENED                    |RESOLVED
         Resolution|                            |FIXED



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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 05 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4090




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?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 05 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4090


Kenji Hara <k.hara.pg gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |REOPENED
         Resolution|FIXED                       |



---

 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. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Nov 06 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4090





 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 :-) -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Nov 06 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4090




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


3rd fix of Issue 4090 - No foreach type inference with const, ref etc modifiers

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 07 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4090


bearophile_hugs eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|REOPENED                    |RESOLVED
         Resolution|                            |FIXED



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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 07 2012
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4090




---

 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 -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Nov 10 2012