Thread overview
DMD 0.132: Bizarre template interaction with aliases and dependent instantiation
Sep 25, 2005
Burton Radons
Sep 26, 2005
Regan Heath
Sep 26, 2005
Burton Radons
Sep 26, 2005
Burton Radons
Sep 26, 2005
Thomas Kühne
September 25, 2005
This code fails compilation with the error message "f.d(10): this for data needs to be type B not type B *".  It's valid code and should work.

    struct A (uint D)
    {
        alias B! (D - 1) b;
    }

    struct B (uint D)
    {
        uint [D] data;

        uint foo () { return data [0]; }
    }

    alias B! (3) b3;
    alias B! (4) b4;

The error goes away if the "alias B! (3) b3;" line is removed, the "alias B! (D - 1) b;" line is removed or changed to "alias B! (3) b;", or the "uint foo () { return data [0]; }" line is changed to "uint foo () { return this.data [0]; }".

Another variant that works is to change the "alias B! (3) b3;" line to "alias B! (4) b4;" and to change the "alias B! (D - 1) b;" line to "alias B! (D) b;".  So it seems to be an interaction between constant folding and aliasing.
September 26, 2005
On Sun, 25 Sep 2005 12:49:23 -0700, Burton Radons <burton-radons@smocky.com> wrote:
> This code fails compilation with the error message "f.d(10): this for data needs to be type B not type B *".  It's valid code and should work.
>
>      struct A (uint D)
>      {
>          alias B! (D - 1) b;
>      }
>
>      struct B (uint D)
>      {
>          uint [D] data;
>
>          uint foo () { return data [0]; }
>      }
>
>      alias B! (3) b3;
>      alias B! (4) b4;
>
> The error goes away if the "alias B! (3) b3;" line is removed, the "alias B! (D - 1) b;" line is removed or changed to "alias B! (3) b;", or the "uint foo () { return data [0]; }" line is changed to "uint foo () { return this.data [0]; }".
>
> Another variant that works is to change the "alias B! (3) b3;" line to "alias B! (4) b4;" and to change the "alias B! (D - 1) b;" line to "alias B! (D) b;".  So it seems to be an interaction between constant folding and aliasing.

I tried it with 0.133 on Windows...

C:\Library\D\src\temp>dmd
Digital Mars D Compiler v0.133
Copyright (c) 1999-2005 by Digital Mars written by Walter Bright
..

C:\Library\D\src\temp>dmd bug8.d
C:\Library\D\dmd\bin\..\..\dm\bin\link.exe bug8,,,user32+kernel32/noi;

<compiles fine>

When run my version outputs 0 twice (expected), full source below.

[bug8.d]
import std.stdio;

struct A (uint D)
{
	alias B! (D - 1) b;
}

struct B (uint D)
{
	uint [D] data;

	uint foo () { return data [0]; }
}

alias B! (3) b3;
alias B! (4) b4;

void main()
{
	b3 a;	
	b4 b;
	writefln(a.foo());
	writefln(b.foo());
}

Regan
September 26, 2005
Regan Heath wrote:
> On Sun, 25 Sep 2005 12:49:23 -0700, Burton Radons  <burton-radons@smocky.com> wrote:
> 
>> This code fails compilation with the error message "f.d(10): this for  data needs to be type B not type B *".  It's valid code and should work.
>>
>>      struct A (uint D)
>>      {
>>          alias B! (D - 1) b;
>>      }
>>
>>      struct B (uint D)
>>      {
>>          uint [D] data;
>>
>>          uint foo () { return data [0]; }
>>      }
>>
>>      alias B! (3) b3;
>>      alias B! (4) b4;
>>
>> The error goes away if the "alias B! (3) b3;" line is removed, the  "alias B! (D - 1) b;" line is removed or changed to "alias B! (3) b;",  or the "uint foo () { return data [0]; }" line is changed to "uint foo  () { return this.data [0]; }".
>>
>> Another variant that works is to change the "alias B! (3) b3;" line to  "alias B! (4) b4;" and to change the "alias B! (D - 1) b;" line to  "alias B! (D) b;".  So it seems to be an interaction between constant  folding and aliasing.
> 
> 
> I tried it with 0.133 on Windows...
> 
> C:\Library\D\src\temp>dmd
> Digital Mars D Compiler v0.133
> Copyright (c) 1999-2005 by Digital Mars written by Walter Bright
> ..
> 
> C:\Library\D\src\temp>dmd bug8.d
> C:\Library\D\dmd\bin\..\..\dm\bin\link.exe bug8,,,user32+kernel32/noi;
> 
> <compiles fine>
> 
> When run my version outputs 0 twice (expected), full source below.

Confirmed fixed on Linux in 0.133.
September 26, 2005
Burton Radons wrote:
> This code fails compilation with the error message "f.d(10): this for data needs to be type B not type B *".  It's valid code and should work.
> 
>     struct A (uint D)
>     {
>         alias B! (D - 1) b;
>     }
> 
>     struct B (uint D)
>     {
>         uint [D] data;
> 
>         uint foo () { return data [0]; }
>     }
> 
>     alias B! (3) b3;
>     alias B! (4) b4;
> 
> The error goes away if the "alias B! (3) b3;" line is removed, the "alias B! (D - 1) b;" line is removed or changed to "alias B! (3) b;", or the "uint foo () { return data [0]; }" line is changed to "uint foo () { return this.data [0]; }".
> 
> Another variant that works is to change the "alias B! (3) b3;" line to "alias B! (4) b4;" and to change the "alias B! (D - 1) b;" line to "alias B! (D) b;".  So it seems to be an interaction between constant folding and aliasing.

Whoops, scratch that.  The problem is NOT fixed in 0.133 (I tested a fixed copy by mistake), AND I posted the wrong example.  It should instead be:

    struct A (uint D)
    {
        alias B! (D - 1) b;
    }

    struct B (uint D)
    {
        uint [D] data;

        uint foo () { return data [0]; }
    }

    alias B! (3) b3;
    alias A! (4) a4;

One little letter at the end.
September 26, 2005
Burton Radons schrieb:

> Whoops, scratch that.  The problem is NOT fixed in 0.133 (I tested a fixed copy by mistake), AND I posted the wrong example.  It should instead be:
> 
>     struct A (uint D)
>     {
>         alias B! (D - 1) b;
>     }
> 
>     struct B (uint D)
>     {
>         uint [D] data;
> 
>         uint foo () { return data [0]; }
>     }
> 
>     alias B! (3) b3;
>     alias A! (4) a4;
> 
> One little letter at the end.

Added to DStress as http://dstress.kuehne.cn/run/t/template_class_10_A.d http://dstress.kuehne.cn/run/t/template_class_10_B.d http://dstress.kuehne.cn/run/t/template_class_10_C.d http://dstress.kuehne.cn/run/t/template_class_10_D.d http://dstress.kuehne.cn/run/t/template_class_10_E.d http://dstress.kuehne.cn/run/t/template_struct_03_A.d http://dstress.kuehne.cn/run/t/template_struct_03_B.d http://dstress.kuehne.cn/run/t/template_struct_03_C.d http://dstress.kuehne.cn/run/t/template_struct_03_D.d http://dstress.kuehne.cn/run/t/template_struct_03_E.d

Thomas