Bug 2706 – invalid template instantiation (and declaration?) is not rejected
Status
RESOLVED
Resolution
DUPLICATE
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
x86
OS
Linux
Creation time
2009-03-02T22:35:00Z
Last change time
2014-03-01T00:35:40Z
Keywords
accepts-invalid
Assigned to
nobody
Creator
tomas
Comments
Comment #0 by tomas — 2009-03-02T22:35:49Z
The following code doesn't make sense to me, yet DMD accepts it:
module k;
enum TYP { ch, c }
class TypeBasic {
int oo;
/// Allocates an instance of TypeBasic and assigns it to typeName.
template newTB(char[] typeName)
{
const newTB = mixin("new TypeBasic(TYP."~typeName~")");
}
/// Initializes predefined types.
static this()
{
newTB!("c");
newTB!("ch");
}
this(int inp)
{
oo=inp;
}
}
void main()
{
}
I got this from a LDC bug report in IRC, and TBH I can't think anything but WTF is going on here? DMD seems to emit two 'new' expressions in the static ctor.
I would think it should be:
error: 'new TypeBasic(TYP.c)' is not a constant expression
Comment #1 by tomas — 2009-03-02T22:37:34Z
Anoter test case:
template newTB(char[] a, char[] b)
{
const newTB = mixin(a~"+"~b);
}
void main()
{
int a, b;
auto c = newTB!("a", "b");
}
Here the bogus error message:
Error: Array operations not implemented
is produced when it should be:
Error: a + b is not a constant expression.
or am I completely off here ?
Comment #2 by tomas — 2009-03-02T22:45:24Z
Ok the second one makes a little sense. but what about this one:
template newTB(char[] c, char[] d)
{
const newTB = mixin(c~"+"~d);
}
int a=1, b=2;
void main()
{
auto c = newTB!("a", "b");
printf("%d\n", c);
a = 4;
b = 6;
c = newTB!("a", "b");
printf("%d\n", c);
}
extern(C) int printf(char*, ...);
/*
[tomas@myhost tests]$ dmd bar.d
[tomas@myhost tests]$ ./bar
3
10
*/
?
Comment #3 by clugdbug — 2011-04-14T15:31:39Z
Updated test case for D2.052:
template newTB(string c, string d)
{
enum newTB = mixin(c~"+"~d);
}
int a=1, b=2;
void main()
{
auto c = newTB!("a", "b");
a = 4;
b = 6;
c = newTB!("a", "b");
}
Comment #4 by yebblies — 2012-01-29T22:23:06Z
*** This issue has been marked as a duplicate of issue 2526 ***