digitalmars.D.bugs - [Issue 2192] New: Returning element in an AA of AAs during nested foreach generates compiler error
- d-bugmail puremagic.com Jul 03 2008
- d-bugmail puremagic.com Jul 04 2008
- d-bugmail puremagic.com Jul 04 2008
- d-bugmail puremagic.com Jul 09 2009
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; } --
Jul 03 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; } --
Jul 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... --
Jul 04 2008
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: -------
Jul 09 2009









d-bugmail puremagic.com 