[Issue 2192] New: Returning element in an AA of AAs during nested foreach generates compiler error

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu Jul 3 23:42:10 PDT 2008


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

           Summary: Returning element in an AA of AAs during nested foreach
                    generates compiler error
           Product: D
           Version: 1.029
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla at digitalmars.com
        ReportedBy: business3 at twistedpairgaming.com


The following code generates a rather curious compiler error:

BEGIN CODE
class Foo { }
Foo[char][char] jaggedArray;

Foo Bar()
{
        foreach(Foo[char] array; jaggedArray)
        {
                foreach(Foo f; array)
                {
                        return null; // Error
                }
        }
}
END CODE

Compiler output from both DMD 1.029 and DMD 1.031:
testb1.d(10): Error: cannot implicitly convert expression (null) of type void*
to int
testb1.d(10): Error: cannot implicitly convert expression (cast(int)null) of
type int to testb1.Foo

Changes that DO NOT eliminate the error:
- Changing "jaggedArray" to "Foo[char[]][char[]]"
- Adding "return null;" to the end of the function
- Changing "return null;" to "return f;"

That last change (returning "f" instead of "null") generates the following
error:
testb1.d(10): Error: cannot implicitly convert expression (f) of type
testb1.Foo to int
testb1.d(10): Error: cannot implicitly convert expression (cast(int)f) of type
int to testb1.Foo

So, that means: The function Bar() is declared to return a Foo, and it tries to
return a Foo. But the compiler tries to turn the Foo into an int...and then
back into a Foo again.

Changes that DO cause the code to compile:
- Changing "jaggedArray" to any of the following: "Foo[][char]" "Foo[char][]"
"int[char][char]"
- Eliminating the inner foreach
- Eliminating the outer foreach and iterating over "jaggedAray['a']"

The following change also causes the code to compile:

class Foo { int var; }
Foo[char][char] jaggedArray;

Foo Bar()
{
        foreach(Foo[char] array; jaggedArray)
        {
                foreach(Foo f; array)
                {
                        f.var = 5;
                }
        }

        return null;
}


-- 



More information about the Digitalmars-d-bugs mailing list