[Issue 12625] implicit slicing of RValue static array should be illegal

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Thu Mar 16 15:40:15 PDT 2017


https://issues.dlang.org/show_bug.cgi?id=12625

hsteoh at quickfur.ath.cx changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |accepts-invalid

--- Comment #18 from hsteoh at quickfur.ath.cx ---
Marking this as accepts-invalid, because this code is allowed even with
-dip1000:

------
int[16] func();
int[] a = func();
------

According to DIP 1000:

1) Lifetimes of statically-sized arrays T[n] is analyzed as if the array were a
struct with n fields, each of type T. [Under "Aggregates"]

2) For types without indirections such as int, reachability and lifetime are
equal for rvalues and lvalues. [Under "Definitions" > "Reachability vs.
lifetime"]

3) For an rvalue, the reachability is the expression within which it is used.
[Under "Definitions" > "Reachability vs. lifetime"]

4) The lifetime of e[] (implicit in this case) is lifetime(e) [Under
"Definitions" > "Algebra of lifetimes"]

5) A scope variable can only be initialized and assigned from values that have
lifetimes longer than the variable's lifetime. [Under "Fundamentals of scope"]

Point (1) indicates that static arrays should be analysed as structs with n
fields, and therefore int[n] is subject to (2): reachability and lifetime are
equal. Point (3) indicates that an rvalue has reachability equal to the
expression in which it is used, which, by point (2), implies that the lifetime
of the static array is also the expression in which it is used. Then the slice
produced by the implicit slicing, according to (4), must also have the same
lifetime, i.e., the expression in which the rvalue occurs.

Since the lifetime of `a` in the code above is longer than the lifetime of the
expression on the right-hand side, by (5) this assignment is illegal.

So, the fact that -dip1000 lets this code through is a bug in the
implementation of DIP 1000.

--


More information about the Digitalmars-d-bugs mailing list