Bug 7736 – Regression(2.059 beta): opApply for immutable structs too

Status
RESOLVED
Resolution
INVALID
Severity
regression
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2012-03-19T16:29:00Z
Last change time
2012-03-29T21:28:19Z
Keywords
rejects-valid
Assigned to
nobody
Creator
bearophile_hugs

Comments

Comment #0 by bearophile_hugs — 2012-03-19T16:29:47Z
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 by clugdbug — 2012-03-19T22:42:26Z
Worked in 2.058. This bug hasn't appeared in an official release.
Comment #2 by k.hara.pg — 2012-03-29T21:28:19Z
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.