Bug 5357 – mixin templates accept strings as struct name
Status
RESOLVED
Resolution
INVALID
Severity
minor
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2010-12-18T16:07:00Z
Last change time
2010-12-21T04:47:38Z
Assigned to
nobody
Creator
pszturmaj
Comments
Comment #0 by pszturmaj — 2010-12-18T16:07:03Z
Compiles on D2.050:
mixin template GenStruct1(string name)
{
struct name
{
}
}
mixin template GenStruct2(alias name)
{
struct name
{
}
}
mixin GenStruct1!("test");
mixin GenStruct2!("test2");
Comment #1 by issues.dlang — 2010-12-18T16:22:16Z
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.
Comment #2 by pszturmaj — 2010-12-18T16:27:01Z
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
Comment #3 by nfxjfg — 2010-12-18T16:38:39Z
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.
Comment #4 by pszturmaj — 2010-12-18T16:48:11Z
> 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.
Comment #5 by pszturmaj — 2010-12-18T16:51:38Z
I'm sorry. You're right.
Comment #6 by bugzilla — 2010-12-21T00:37:40Z
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".
Comment #7 by pszturmaj — 2010-12-21T04:47:38Z
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.