DMD 2.059:
struct S{ template t(){ int t; } }
void main(){
S s,t;
writeln(t.t!()); // 0
s.t!()=256;
writeln(t.t!()," ",s.t!()); // 1 256
}
The code is illegal, but accepted.
I remember that such code used to be rejected.
Comment #1 by k.hara.pg — 2012-07-18T09:11:32Z
The code is not illegal, but generates wrong code.
See "Limitations" in http://dlang.org/template .
> Templates cannot be used to add non-static members or virtual functions to
> classes. For example:
>
> class Foo {
> template TBar(T) {
> T xx; // becomes a static member of Foo
> int func(T) { ... } // non-virtual
>
> static T yy; // Ok
> static int func(T t, int y) { ... } // Ok
> }
> }
In this case, variable t in template t should become static member of struct S.
Therefore following test case should pass, but fails with assertions.
extern(C) int printf(const char*, ...);
struct S{ template t(){ int t; } }
void main(){
S s1, s2;
printf("%p %p\n", &s1, &s1.t!());
printf("%p %p\n", &s2, &s2.t!());
assert(cast(void*)&s1 != cast(void*)&s2 );
assert(cast(void*)&s1 != cast(void*)&s1.t!()); // fails
assert(cast(void*)&s2 != cast(void*)&s2.t!()); // fails
assert(cast(void*)&s1.t!() == cast(void*)&s2.t!()); // fails
s1.t!() = 256;
assert(s2.t!() == 256); // fails
}
I couldn't find the dmd version "used to be rejected". But for the above reasons, This is not a regression, but just a wrong-code bug.