D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 7736 - Regression(2.059 beta): opApply for immutable structs too
Summary: Regression(2.059 beta): opApply for immutable structs too
Status: RESOLVED INVALID
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86 Windows
: P2 regression
Assignee: No Owner
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2012-03-19 16:29 UTC by bearophile_hugs
Modified: 2012-03-29 21:28 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description bearophile_hugs 2012-03-19 16:29:47 UTC
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)) {}
}
Comment 1 Don 2012-03-19 22:42:26 UTC
Worked in 2.058. This bug hasn't appeared in an official release.
Comment 2 Kenji Hara 2012-03-29 21:28:19 UTC
This issue is introduced by fixing bug 7038.

(In reply to comment #0)
> 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.