D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 6382 - edge case with static foreach
Summary: edge case with static foreach
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-07-26 05:33 UTC by Andrej Mitrovic
Modified: 2013-11-22 02:52 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Andrej Mitrovic 2011-07-26 05:33:56 UTC
import std.typetuple;

struct Foo
{   
    this(void delegate() dg) { }
}

void test()
{
    Foo[] result;
    
    foreach (Type; TypeTuple!(int, int))
    {
        result ~= Foo( (){} );
    }
}

void main()
{
    test();
}

OPTLINK (R) for Win32  Release 8.00.12
Copyright (C) Digital Mars 1989-2010  All rights reserved.
http://www.digitalmars.com/ctg/optlink.html
C:\DOCUME~1\Andrej\LOCALS~1\Temp\.rdmd\rdmd-ctfe_bug22.d-97E30C11895AB443DA692FB4CE11A18B\ctfe_bug22-d-97E30C11895AB443DA692FB4CE11A18B.obj(ctfe_bug22-d-97E30C11895AB443DA692FB4CE11A18B)  Offset 00960H Record Type 00C3
 Error 1: Previous Definition Different : _D12createFields4testFZv12__dgliteral1MFZv

It seems as if the compiler generates two delegates with the same name inside of test(), and they end up clashing. Of course, this function should be called at compile time, not runtime. The fix is to use it as a template:

import std.typetuple;

struct Foo
{   
    this(void delegate() dg) { }
}

void test()()  // template func
{
    Foo[] result;
    
    foreach (Type; TypeTuple!(int, int))
    {
        result ~= Foo( (){} );
    }
}

void main()
{
    test!();  // call it as a template
}

I don't know if the first case is accepts-invalid or completely legal, but linker errors are never nice to see. Anyway I thought this was worth putting here.
Comment 1 Kenji Hara 2013-11-22 02:49:23 UTC
The problem is finally fixed in 2.064, by fixing bug 9571.