Thread overview
[Issue 2192] New: Returning element in an AA of AAs during nested foreach generates compiler error
Jul 04, 2008
d-bugmail
Jul 04, 2008
d-bugmail
Jul 04, 2008
d-bugmail
Jul 09, 2009
Serg Kovrov
[Issue 2192] Returning from inside foreach body delegate only returns from inner delegate
Feb 03, 2012
yebblies
Feb 03, 2012
Kenji Hara
Feb 03, 2012
yebblies
July 04, 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@digitalmars.com
        ReportedBy: business3@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;
}


-- 

July 04, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=2192





------- Comment #1 from business3@twistedpairgaming.com  2008-07-04 02:22 -------
Changing the function's return type to something else, like 'bool' also eliminates the problem.

Making the return statement conditional (ex "if(f.var == 1) return null;") does
NOT solve to problem.

If you need to return f, the following workaround does work (although it will result in extra unnecessary iterations):

Foo Bar()
{
        Foo ret = null;
        foreach(Foo[char] array; jaggedArray)
        {
                foreach(Foo f; array)
                {
                        if( /* whatever you need */ )
                        {
                                if(ret == null)
                                        ret = f;
                                break;
                        }
                }
        }
        return ret;
}


-- 

July 04, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=2192





------- Comment #2 from business3@twistedpairgaming.com  2008-07-04 02:25 -------
Erm...that should be "is null" in the workaround, not "== null". Must be tired...


-- 

July 09, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=2192





--- Comment #3 from Serg Kovrov <kovrov+puremagic@gmail.com>  2009-07-09 07:55:32 PDT ---
Same error is issued on returning from nested foreach over opApply/delegates.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 03, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=2192


yebblies <yebblies@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |yebblies@gmail.com
           Platform|x86                         |All
            Version|1.029                       |D1 & D2
            Summary|Returning element in an AA  |Returning from inside
                   |of AAs during nested        |foreach body delegate only
                   |foreach generates compiler  |returns from inner delegate
                   |error                       |
         OS/Version|Windows                     |All
           Severity|normal                      |enhancement


--- Comment #4 from yebblies <yebblies@gmail.com> 2012-02-03 22:23:44 EST ---
This is an issue with opApply foreach iteration, and is working as designed
(however unexpected).

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 03, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=2192



--- Comment #5 from Kenji Hara <k.hara.pg@gmail.com> 2012-02-03 04:48:55 PST ---
I think the original issue titled "Returning element in an AA of AAs during nested foreach generates compiler error" is a dup of bug 3187, and today it works as expected.

So, I can't understand why yebblies changed the title and switched the importance to an enhancement.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 03, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=2192


yebblies <yebblies@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |wrong-code
             Status|NEW                         |RESOLVED
         Resolution|                            |DUPLICATE
           Severity|enhancement                 |normal


--- Comment #6 from yebblies <yebblies@gmail.com> 2012-02-04 00:25:24 EST ---
(In reply to comment #5)
> I think the original issue titled "Returning element in an AA of AAs during nested foreach generates compiler error" is a dup of bug 3187, and today it works as expected.
> 
> So, I can't understand why yebblies changed the title and switched the importance to an enhancement.

I had no idea that it worked, or that it had been fixed!  My compiler's broken for the moment, so I can't test it, but I'll take your comment to mean this works and close it as a duplicate.

*** This issue has been marked as a duplicate of issue 3187 ***

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