www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 7736] New: opApply for immutable structs too

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

           Summary: opApply for immutable structs too
           Product: D
           Version: D2
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Keywords: rejects-valid
          Severity: regression
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: bearophile_hugs eml.cc



This code used to work not too much time ago (maybe 2.056?):


immutable struct Foo {
    char stop;
    int opApply(int delegate(ref int) dg) {
        int result;
        for (int i = 0; i < stop; i++) {
            result = dg(i);
            if (result) break;
        }
        return result;
    }
}
void main() {
    foreach (i; Foo(10)) {}
}



DMD 2.059 gives:

test.d(13): Error: cannot uniquely infer foreach argument types

The code compiles and works if you remove the "immutable" and replace it with
const:


const struct Foo {
    char stop;
    int opApply(int delegate(ref int) dg) {
        int result;
        for (int i = 0; i < stop; i++) {
            result = dg(i);
            if (result) break;
        }
        return result;
    }
}
void main() {
    foreach (i; Foo(10)) {}
}

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


Don <clugdbug yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |clugdbug yahoo.com.au
            Summary|opApply for immutable       |Regression(2.059 beta):
                   |structs too                 |opApply for immutable
                   |                            |structs too



Worked in 2.058. This bug hasn't appeared in an official release.

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


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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |INVALID



This issue is introduced by fixing bug 7038.


 This code used to work not too much time ago (maybe 2.056?):
 
 
 immutable struct Foo {
     char stop;
     int opApply(int delegate(ref int) dg) {
         int result;
         for (int i = 0; i < stop; i++) {
             result = dg(i);
             if (result) break;
         }
         return result;
     }
 }
 void main() {
     foreach (i; Foo(10)) {}
 }
In 2.058 and before, This code was equivalent to the following: struct __Foo { immutable: // a type qualifier for whole the struct affects to all its members [snip] // same as original code } alias immutable(__Foo) Foo; // Foo is always immutable(__Foo) void main() { foreach (i; Foo(10)) {} // Foo(10) is an immutable object, so immutable opApply works as well. }
 DMD 2.059 gives:
 
 test.d(13): Error: cannot uniquely infer foreach argument types
After fixing bug 7038, 'Foo' is always mutable type. struct Foo { immutable: // same as 2.058 and earlier [snip] // same as original code } void main() { foreach (i; Foo(10)) {} // Foo(10) is a mutable object, so immutable opApply *doesn't work* as well. }
 The code compiles and works if you remove the "immutable" and replace it with
 const:
 
 const struct Foo {
     char stop;
     int opApply(int delegate(ref int) dg) {
         int result;
         for (int i = 0; i < stop; i++) {
             result = dg(i);
             if (result) break;
         }
         return result;
     }
 }
 void main() {
     foreach (i; Foo(10)) {}
 }
Because const opApply works with mutable object. Therefore, this is a "resolved-invalid bug" in 2.059, not a regression. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Mar 29 2012