[Issue 3187] New: Nested foreach over opApply doesn't work

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Jul 17 06:05:13 PDT 2009


http://d.puremagic.com/issues/show_bug.cgi?id=3187

           Summary: Nested foreach over opApply doesn't work
           Product: D
           Version: unspecified
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody at puremagic.com
        ReportedBy: johnch_atms at hotmail.com


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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list