2192 – Returning from inside foreach body delegate only returns from inner delegate

D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 2192 - Returning from inside foreach body delegate only returns from inner delegate
Summary: Returning from inside foreach body delegate only returns from inner delegate
Status: RESOLVED DUPLICATE of issue 3187
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 normal
Assignee: No Owner
URL:
Keywords: rejects-valid, wrong-code
Depends on:
Blocks:
Reported: 2008年07月04日 01:42 UTC by Nick Sabalausky
Modified: 2015年06月09日 05:12 UTC (History)
2 users (show)

See Also:


Attachments
Add an attachment (proposed patch, testcase, etc.)

Note You need to log in before you can comment on or make changes to this issue.
Description Nick Sabalausky 2008年07月04日 01:42:09 UTC
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;
}
Comment 1 Nick Sabalausky 2008年07月04日 02:22:30 UTC
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;
}
Comment 2 Nick Sabalausky 2008年07月04日 02:25:22 UTC
Erm...that should be "is null" in the workaround, not "== null". Must be tired...
Comment 3 Serg Kovrov 2009年07月09日 07:55:32 UTC
Same error is issued on returning from nested foreach over opApply/delegates.
Comment 4 yebblies 2012年02月03日 03:23:44 UTC
This is an issue with opApply foreach iteration, and is working as designed (however unexpected).
Comment 5 Kenji Hara 2012年02月03日 04:48:55 UTC
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.
Comment 6 yebblies 2012年02月03日 05:25:24 UTC
(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 ***


AltStyle によって変換されたページ (->オリジナル) /