D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 2173 - foreach iteration not possible for associative array indexed with static array
Summary: foreach iteration not possible for associative array indexed with static array
Status: RESOLVED WORKSFORME
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D1 (retired)
Hardware: x86 Linux
: P3 normal
Assignee: Walter Bright
URL:
Keywords: patch, rejects-valid
Depends on:
Blocks:
 
Reported: 2008-06-25 11:38 UTC by Georg Lukas
Modified: 2014-02-24 16:00 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Georg Lukas 2008-06-25 11:38:19 UTC
Trying to foreach through an AA[SA] generates a compiler error:

import std.stdio;
import std.md5;
int main(char[][] args) {
	int[ubyte[16]] md5count;
	foreach (md5, count; md5count)
		writefln("%s: %d", std.md5.digestToString(md5), count);
	return 0;
}

Both gdc and dmd bail out on the foreach statement with:
"test.d(5): Error: cannot have out or ref parameter of type ubyte[16u]"

Possible workarounds:
 * use a wrapper struct for the static array (looks ugly)
 * use foreach (md5; md5count.keys) ... count = md5count[md5]
      (decreases performance almost by 50%)
Comment 1 david 2008-06-26 01:39:30 UTC
i bet the code actually meant as following:

mtype.c (2790):
	    if (arg->storageClass & (STCout | STCref | STClazy))
	    {
---		if (t->ty == Tsarray)
+++		if (t->ty != Tsarray)
		    error(loc, "cannot have out or ref parameter of type %s", t->toChars());
	    }

I hand-crafted a dmd which bypass this check, it can generate correct binary. 
Comment 2 david 2008-07-02 09:12:09 UTC
umm, the patch is incorrect.. 
bypassing the whole test should work.
/*
            if (arg->storageClass & (STCout | STCref | STClazy))
            {
                if (t->ty == Tsarray)
                if (t->ty != Tsarray)
                    error(loc, "cannot have out or ref parameter of type %s",
t->toChars());
            }
*/
Comment 3 bearophile_hugs 2010-02-18 11:09:11 UTC
I think this bug is now fixed, because fixed-sized arrays are managed by value.