Copy Constructor DIP

Timon Gehr timon.gehr at gmx.ch
Thu Jul 12 15:22:32 UTC 2018


On 12.07.2018 15:29, Andrei Alexandrescu wrote:
> On 07/11/2018 05:55 AM, Nick Treleaven wrote:
>> ...
>>
>> Removing `static` works. Otherwise I tried changing `ref` to `alias`:
>>
>> Error: variable src cannot be read at compile time
>>
>> But this shorter code seems to work fine:
>>
>> this.tupleof = src.tupleof;
> 
> Odd. Timon, what would be the reason for that error? Razvan, can you 
> please look into removing "static" for now. Thanks!

The reason for this specific error is that `src.i` is neither a symbol 
nor a constant value. tupleof is a case of built-in compiler magic, 
because it can produce an expression tuple that contains run-time values 
that are not symbols. The following (manually unrolled) code also does 
not work:

alias field0 = s.tupleof[0];
t.tupleof[0] = field0;
alias field1 = s.tupleof[1];
t.tupleof[1] = field1;
alias field2 = s.tupleof[2];
t.tupleof[2] = field2;

Error: alias `a` cannot alias an expression `tuple(s.a, s.b, s.c)[0]`
Error: alias `b` cannot alias an expression `tuple(s.a, s.b, s.c)[1]`
Error: alias `c` cannot alias an expression `tuple(s.a, s.b, s.c)[2]`

It could be argued that the `static foreach` implementation should 
produce the same error message. (The fact that the AliasDecl constructor 
takes a Dsymbol instead of an Expression has led to multiple 
reimplementations of parts of the aliasing logic in different parts of 
the DMD code, `static foreach` is using the same implementation also 
used by unrolled foreach, and it requires that all loop variables are 
either symbols or constant values, while unrolled foreach can fall back 
to introducing runtime variables for this special case.)

One way to fix is to lift all the unnecessary limitations that are 
introduced by the fact that the AliasDecl constructor takes a Dsymbol. 
I.e. that "you can only alias symbols".

Alternatively, it would be possible to redefine `static foreach` 
statements such that they work for any aggregate with statically known 
length and element types, and to allow run-time loop variables to be 
generated when iterating over run-time values.


More information about the Digitalmars-d mailing list