[Issue 8238] regression: templates can create ghost fields

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Jul 18 09:11:36 PDT 2012


http://d.puremagic.com/issues/show_bug.cgi?id=8238


Kenji Hara <k.hara.pg at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|accepts-invalid             |wrong-code
           Severity|regression                  |major


--- Comment #1 from Kenji Hara <k.hara.pg at gmail.com> 2012-07-18 09:11:32 PDT ---
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.

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


More information about the Digitalmars-d-bugs mailing list