| Thread overview | |||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
May 21, 2014 [Issue 9570] Wrong foreach index implicit conversion error | ||||
|---|---|---|---|---|
| ||||
https://issues.dlang.org/show_bug.cgi?id=9570 Lionello Lunesu <lio+bugzilla@lunesu.com> changed: What |Removed |Added ---------------------------------------------------------------------------- Keywords| |pull CC| |lio+bugzilla@lunesu.com --- Comment #3 from Lionello Lunesu <lio+bugzilla@lunesu.com> --- https://github.com/D-Programming-Language/dmd/pull/3567 -- | ||||
May 21, 2014 [Issue 9570] Wrong foreach index implicit conversion error | ||||
|---|---|---|---|---|
| ||||
https://issues.dlang.org/show_bug.cgi?id=9570 --- Comment #4 from bearophile_hugs@eml.cc --- (In reply to Lionello Lunesu from comment #3) > https://github.com/D-Programming-Language/dmd/pull/3567 Is this supported? void main() { ubyte[256] data; foreach (ubyte i, ref x; data) x = i; } -- | ||||
May 21, 2014 [Issue 9570] Wrong foreach index implicit conversion error | ||||
|---|---|---|---|---|
| ||||
https://issues.dlang.org/show_bug.cgi?id=9570 --- Comment #5 from Lionello Lunesu <lio+bugzilla@lunesu.com> --- (In reply to bearophile_hugs from comment #4) > (In reply to Lionello Lunesu from comment #3) > > https://github.com/D-Programming-Language/dmd/pull/3567 > > Is this supported? > > void main() { > ubyte[256] data; > foreach (ubyte i, ref x; data) > x = i; > } No, the key must be declared const or immutable. Otherwise we need flow analysis to ensure the key doesn't get mutated in the scope. -- | ||||
May 21, 2014 [Issue 9570] Wrong foreach index implicit conversion error | ||||
|---|---|---|---|---|
| ||||
https://issues.dlang.org/show_bug.cgi?id=9570 --- Comment #6 from bearophile_hugs@eml.cc --- (In reply to Lionello Lunesu from comment #5) > No, the key must be declared const or immutable. Otherwise we need flow analysis to ensure the key doesn't get mutated in the scope. I don't understand what's the problem. What kind of bad things do happen if the key gets mutated in the scope? void main() { ubyte[256] data; foreach (ubyte i, ref x; data) { x = i; i++; // overflow here } } -- | ||||
May 21, 2014 [Issue 9570] Wrong foreach index implicit conversion error | ||||
|---|---|---|---|---|
| ||||
https://issues.dlang.org/show_bug.cgi?id=9570 --- Comment #7 from yebblies <yebblies@gmail.com> --- (In reply to bearophile_hugs from comment #6) > (In reply to Lionello Lunesu from comment #5) > > > No, the key must be declared const or immutable. Otherwise we need flow analysis to ensure the key doesn't get mutated in the scope. > > I don't understand what's the problem. What kind of bad things do happen if the key gets mutated in the scope? > > void main() { > ubyte[256] data; > foreach (ubyte i, ref x; data) { > x = i; > i++; // overflow here > } > } If i is mutated before it's used, the range information from the foreach aggregate might not be accurate. Your example is fine, but to tell it apart from the bad ones requires flow analysis for non-trivial cases. void main() { ubyte[256] data; foreach (ubyte i, ref x; data) { i = 99999; x = i; // information lost } } -- | ||||
May 21, 2014 [Issue 9570] Wrong foreach index implicit conversion error | ||||
|---|---|---|---|---|
| ||||
https://issues.dlang.org/show_bug.cgi?id=9570 --- Comment #8 from bearophile_hugs@eml.cc --- (In reply to yebblies from comment #7) > void main() { > ubyte[256] data; > foreach (ubyte i, ref x; data) { > i = 99999; > x = i; // information lost > } > } This code should give (as it currently gives): temp.d(4,13): Error: cannot implicitly convert expression (99999) of type int to ubyte Here i scan a ubyte range 0 .. 255, so tagging it as ubyte is OK. I think it's irrelevant that later I try to overflow the contents of i. Even this is OK for D, despite 'i' gets overflowed: void main() { ubyte[256] data; foreach (ubyte i, ref x; data) { i += 200; i = 200; x = i; } } So I still don't see the problem. -- | ||||
May 21, 2014 [Issue 9570] Wrong foreach index implicit conversion error | ||||
|---|---|---|---|---|
| ||||
https://issues.dlang.org/show_bug.cgi?id=9570 --- Comment #9 from yebblies <yebblies@gmail.com> --- Oh right, I misread your example. That won't be fixed by this pull. -- | ||||
May 21, 2014 [Issue 9570] Wrong foreach index implicit conversion error | ||||
|---|---|---|---|---|
| ||||
https://issues.dlang.org/show_bug.cgi?id=9570 --- Comment #10 from Lionello Lunesu <lio+bugzilla@lunesu.com> --- I also misread. The problem here is that the internal iterator (the one named __key##, declared by the compiler) needs to be able to count to 256 inclusive (for the comparison the be false and terminate the for.) There's no reason why the internal iterator and the declared one are the same type (in fact, they need not be), but at the moment the compiler uses the declared type for both the declared iterator and the internal one. It's an unrelated fix. -- | ||||
May 21, 2014 [Issue 9570] Wrong foreach index implicit conversion error | ||||
|---|---|---|---|---|
| ||||
https://issues.dlang.org/show_bug.cgi?id=9570 --- Comment #11 from Lionello Lunesu <lio+bugzilla@lunesu.com> --- In fact, we should probably open a new bug for that case, since it's not the same. -- | ||||
May 21, 2014 [Issue 9570] Wrong foreach index implicit conversion error | ||||
|---|---|---|---|---|
| ||||
https://issues.dlang.org/show_bug.cgi?id=9570 --- Comment #12 from bearophile_hugs@eml.cc --- (In reply to Lionello Lunesu from comment #11) > In fact, we should probably open a new bug for that case, since it's not the same. OK, it's Issue 12782 -- | ||||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply