Thread overview
[Issue 5357] New: mixin templates accept strings as struct name
Dec 19, 2010
Piotr Szturmaj
Dec 19, 2010
Jonathan M Davis
Dec 19, 2010
Piotr Szturmaj
Dec 19, 2010
nfxjfg@gmail.com
Dec 19, 2010
Piotr Szturmaj
Dec 19, 2010
Piotr Szturmaj
Dec 21, 2010
Piotr Szturmaj
December 19, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=5357

           Summary: mixin templates accept strings as struct name
           Product: D
           Version: D2
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Severity: minor
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: gacek999@tlen.pl


--- Comment #0 from Piotr Szturmaj <gacek999@tlen.pl> 2010-12-18 16:07:03 PST ---
Compiles on D2.050:

mixin template GenStruct1(string name)
{
    struct name
    {

    }
}

mixin template GenStruct2(alias name)
{
    struct name
    {

    }
}

mixin GenStruct1!("test");
mixin GenStruct2!("test2");

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 19, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=5357


Jonathan M Davis <jmdavisProg@gmx.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jmdavisProg@gmx.com


--- Comment #1 from Jonathan M Davis <jmdavisProg@gmx.com> 2010-12-18 16:22:16 PST ---
So, what's the problem? That seems perfectly okay to me. Templates generate code. Here, you're generating code, and as part of that, you're giving the name of the struct as a string. I don't see the problem.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 19, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=5357



--- Comment #2 from Piotr Szturmaj <gacek999@tlen.pl> 2010-12-18 16:27:01 PST ---
I forgot to add - try to use generated struct like this:

struct Other
{
    test t;
}

Error: identifier 'test' is not defined
Error: test is used as a type

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 19, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=5357


nfxjfg@gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |nfxjfg@gmail.com
         Resolution|                            |INVALID


--- Comment #3 from nfxjfg@gmail.com 2010-12-18 16:38:39 PST ---
Dear god... stuff doesn't work this way AT ALL.

The name of the struct the mixin generates is "name", not whatever you passed as the parameter named "name". What makes you think the compiler automatically replaces identifier with the same name as the parameter by the parameter's value?

Also, learn to write clear error reports.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 19, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=5357


Piotr Szturmaj <gacek999@tlen.pl> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |REOPENED
         Resolution|INVALID                     |


--- Comment #4 from Piotr Szturmaj <gacek999@tlen.pl> 2010-12-18 16:48:11 PST ---
> Dear god... stuff doesn't work this way AT ALL.

Oh, really?

> The name of the struct the mixin generates is "name", not whatever you passed as the parameter named "name".

string name is a value parameter, so it should be an error. It should not generate struct 'name'.

> What makes you think the compiler automatically
> replaces identifier with the same name as the parameter by the parameter's
> value?

I don't think so! I was just experimenting with mixin templates.

> Also, learn to write clear error reports.

You better learn how to reason.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 19, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=5357


Piotr Szturmaj <gacek999@tlen.pl> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|REOPENED                    |RESOLVED
         Resolution|                            |INVALID


--- Comment #5 from Piotr Szturmaj <gacek999@tlen.pl> 2010-12-18 16:51:38 PST ---
I'm sorry. You're right.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 21, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=5357


Lars T. Kyllingstad <bugzilla@kyllingen.net> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bugzilla@kyllingen.net


--- Comment #6 from Lars T. Kyllingstad <bugzilla@kyllingen.net> 2010-12-21 00:37:40 PST ---
I think this is a bug after all.  What does the symbol 'name' refer to inside the template?  The string or the struct?  Here's a similar example, which quite correctly fails to compile:

  void foo(string bar)
  {
      struct bar { }
  }

In this case, the compiler says "Error: declaration bar is already defined".

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 21, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=5357



--- Comment #7 from Piotr Szturmaj <gacek999@tlen.pl> 2010-12-21 04:47:38 PST ---
This is why I filled this bug report. I think it introduces a confusion, Jonathan also got caught on that.

Consider following code:

mixin template Test(string s)
{
    static string str = s;
    //struct s { }
}

mixin Test!("text");

it compiles flawlessly. Now uncomment line with struct:

mixin template Test(string s)
{
    static string str = s;
    struct s { }
}

mixin Test!("text");

This time compiler fails with:
Error: cannot implicitly convert expression (s) of type s to string

Now it is impossible to refer to value parameter s. I think there should be at least a warning which informs user that template declarations are hiding template parameters.

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