Bug 8238 – templates can create ghost fields

Status
RESOLVED
Resolution
FIXED
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-06-14T07:36:00Z
Last change time
2013-02-18T19:52:12Z
Keywords
pull, wrong-code
Assigned to
nobody
Creator
timon.gehr

Comments

Comment #0 by timon.gehr — 2012-06-14T07:36:12Z
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.
Comment #2 by k.hara.pg — 2012-07-18T09:14:46Z
Comment #3 by github-bugzilla — 2013-02-18T18:46:43Z
Commits pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/b7f3f45b62541099bbff5e263a4da6f2b00c9023 fix Issue 8238 - templates can create ghost fields https://github.com/D-Programming-Language/dmd/commit/c20f46ca30b3a9ec8f4c3ab4e76397b0e691926f Merge pull request #1054 from 9rnsr/fix8238 Issue 8238 - templates can create ghost fields