Bug 3187 – Nested foreach over opApply doesn't work
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
x86
OS
Windows
Creation time
2009-07-17T06:05:00Z
Last change time
2014-02-15T13:12:27Z
Keywords
diagnostic, patch, rejects-valid
Assigned to
nobody
Creator
johnch_atms
Comments
Comment #0 by johnch_atms — 2009-07-17T06:05:12Z
Calling nested foreach statements on types that implement opApply causes an error. There appears to be a bug in the code generated by the compiler.
class Collection {
int opApply(int delegate(ref Object) a) {
return 0;
}
}
Object testForeach(Collection level1, Collection level2) {
foreach (first; level1) {
foreach (second; level2)
return second;
}
return null;
}
void main() {
testForeach(new Collection, new Collection);
}
Error: cannot implicitly convert expression (second) of type object.Object to int
This is the code DMD generates for the testForeach method, with the problem line highlighted:
Object testForeach(Collection level1, Collection level2) {
switch(level1.opApply(delegate (Object __applyArg0) {
{
Object first = __applyArg0;
switch(level2.opApply(delegate (Object __applyArg0) {
{
Object second = __applyArg0;
{
// ********* HERE'S THE PROBLEM *********
__result = cast(Object) cast(int) second;
return 2;
}
}
return 0;
} )) {
default:
break;
case 2:
__result = __result;
return 2;
}
}
return 0;
} )) {
default:
break;
case 2:
return __result;
}
return cast(Object) null;
}
This problem occurs with both DMD 1.x and 2.x compilers.
Comment #1 by clugdbug — 2010-08-06T00:05:14Z
I'm changing this from wrong-code to ICE, since it never reaches code generation.
Comment #2 by clugdbug — 2011-02-15T18:56:15Z
(In reply to comment #1)
> I'm changing this from wrong-code to ICE, since it never reaches code
> generation.
Actually although the compiler fouls this up badly, it isn't an ICE. It's a rejects-valid with a really bad diagnostic.