digitalmars.D.learn - Static Array Idiom not working anymore.
- SrMordred (17/17) Jun 12 2018 this idiom for creating static array used to work, but they are
- Steven Schveighoffer (11/33) Jun 12 2018 auto myStaticArray = [1,2,3].s;
- Guillaume Piolat (5/10) Jun 12 2018 I thought it was clear enough because the comment said
- Steven Schveighoffer (10/23) Jun 12 2018 No, that's not what I mean. What I mean is:
- Guillaume Piolat (4/11) Jun 14 2018 https://github.com/p0nce/d-idioms/issues/150
- Steven Schveighoffer (9/24) Jun 14 2018 The Phobos PR isn't merged yet, so I think it's still valid to have this...
- Jonathan M Davis (8/30) Jun 14 2018 Yeah. The problem is not the function to create a static array. The prob...
- Guillaume Piolat (7/11) Jun 12 2018 And a reminder that this idiom exist because _you can't have
- Seb (3/21) Jun 12 2018 FYI: There's a Phobos PR for such an idiom
- Steven Schveighoffer (8/35) Jun 12 2018 Note, this only will add the 's' function (named staticArray) to phobos,...
this idiom for creating static array used to work, but they are failing now. What changed and whats the alternative? (from https://p0nce.github.io/d-idioms/# nogc-Array-Literals:-Breaking-the-Limits) T[n] s(T, size_t n)(auto ref T[n] array) pure nothrow nogc safe { return array; } void main() nogc { int[] myDynamicArray = [1, 2, 3].s; // Slice that static array which is on stack } //output: Deprecation: slice of static array temporary returned by s([1, 2, 3]) assigned to longer lived variable myDynamicArray
Jun 12 2018
On 6/12/18 10:35 AM, SrMordred wrote:this idiom for creating static array used to work, but they are failing now. What changed and whats the alternative? (from https://p0nce.github.io/d-idioms/# nogc-Array-Literals:-Breaking-the-Limits) T[n] s(T, size_t n)(auto ref T[n] array) pure nothrow nogc safe { return array; } void main() nogc { int[] myDynamicArray = [1, 2, 3].s; // Slice that static array which is on stack } //output: Deprecation: slice of static array temporary returned by s([1, 2, 3]) assigned to longer lived variable myDynamicArrayauto myStaticArray = [1,2,3].s; Now use it anywhere you need dynamic arrays as myStaticArray[]. A lot of times, it will work without having to slice it. What you are being told is that your memory is not being kept around. Essentially what you had originally was a memory corruption bug (yes, even before the deprecation happened). Don't do that anymore! Note to ponce, please update your idioms, this is NOT safe, even within the same function. Just because it does work, doesn't mean it will always work. The language makes no guarantees once the lifetime is over. -Steve
Jun 12 2018
On Tuesday, 12 June 2018 at 14:44:12 UTC, Steven Schveighoffer wrote:Note to ponce, please update your idioms, this is NOT safe, even within the same function. Just because it does work, doesn't mean it will always work. The language makes no guarantees once the lifetime is over. -SteveI thought it was clear enough because the comment said // Slice that static array __which is on stack__ but now I see how it can be hard to see the unsafety.
Jun 12 2018
On 6/12/18 10:57 AM, Guillaume Piolat wrote:On Tuesday, 12 June 2018 at 14:44:12 UTC, Steven Schveighoffer wrote:No, that's not what I mean. What I mean is: int[] arr = [1,2,3].s; int[] arr2 = [4,5,6].s; Legally, the compiler is allowed to reuse the stack memory allocated for arr for arr2. The lifetime of the arr data is over. In other words, you should NEVER do this. You should ALWAYS create a static array on the stack with a variable name, and then slice that if you need to. -SteveNote to ponce, please update your idioms, this is NOT safe, even within the same function. Just because it does work, doesn't mean it will always work. The language makes no guarantees once the lifetime is over.I thought it was clear enough because the comment said // Slice that static array __which is on stack__ but now I see how it can be hard to see the unsafety.
Jun 12 2018
On Tuesday, 12 June 2018 at 15:35:42 UTC, Steven Schveighoffer wrote:No, that's not what I mean. What I mean is: int[] arr = [1,2,3].s; int[] arr2 = [4,5,6].s; Legally, the compiler is allowed to reuse the stack memory allocated for arr for arr2. The lifetime of the arr data is over. -Stevehttps://github.com/p0nce/d-idioms/issues/150 Especially if the stdlib has a way to do this now.
Jun 14 2018
On 6/14/18 7:07 AM, Guillaume Piolat wrote:On Tuesday, 12 June 2018 at 15:35:42 UTC, Steven Schveighoffer wrote:The Phobos PR isn't merged yet, so I think it's still valid to have this idiom, but it needs to be changed such that the implicit slicing doesn't happen. To be clear, I think the static array idiom is useful for many reasons (my favorite is avoiding issues with literal and type size mismatch), it just that the showcase usage leads to an instant dangling pointer and should be altered. -SteveNo, that's not what I mean. What I mean is: int[] arr = [1,2,3].s; int[] arr2 = [4,5,6].s; Legally, the compiler is allowed to reuse the stack memory allocated for arr for arr2. The lifetime of the arr data is over. -Stevehttps://github.com/p0nce/d-idioms/issues/150 Especially if the stdlib has a way to do this now.
Jun 14 2018
On Thursday, June 14, 2018 08:40:10 Steven Schveighoffer via Digitalmars-d- learn wrote:On 6/14/18 7:07 AM, Guillaume Piolat wrote:Yeah. The problem is not the function to create a static array. The problem is that the return value is being sliced rather than storing it on the stack and then slicing the local variable. Slicing the return value was never a safe thing to do, and if the compiler is now giving an error when you try to do it, that's actually a really good thing. - Jonathan M DavisOn Tuesday, 12 June 2018 at 15:35:42 UTC, Steven Schveighoffer wrote:The Phobos PR isn't merged yet, so I think it's still valid to have this idiom, but it needs to be changed such that the implicit slicing doesn't happen. To be clear, I think the static array idiom is useful for many reasons (my favorite is avoiding issues with literal and type size mismatch), it just that the showcase usage leads to an instant dangling pointer and should be altered.No, that's not what I mean. What I mean is: int[] arr = [1,2,3].s; int[] arr2 = [4,5,6].s; Legally, the compiler is allowed to reuse the stack memory allocated for arr for arr2. The lifetime of the arr data is over. -Stevehttps://github.com/p0nce/d-idioms/issues/150 Especially if the stdlib has a way to do this now.
Jun 14 2018
On Tuesday, 12 June 2018 at 14:44:12 UTC, Steven Schveighoffer wrote:What you are being told is that your memory is not being kept around. Essentially what you had originally was a memory corruption bug (yes, even before the deprecation happened). Don't do that anymore!And a reminder that this idiom exist because _you can't have static array literals under nogc_, which is just strange (I know there are reasons, this was debated to death at the time). When D makes a decision that isn't practical, the d-idioms page get one additional entry.
Jun 12 2018
On Tuesday, 12 June 2018 at 14:35:33 UTC, SrMordred wrote:this idiom for creating static array used to work, but they are failing now. What changed and whats the alternative? (from https://p0nce.github.io/d-idioms/# nogc-Array-Literals:-Breaking-the-Limits) T[n] s(T, size_t n)(auto ref T[n] array) pure nothrow nogc safe { return array; } void main() nogc { int[] myDynamicArray = [1, 2, 3].s; // Slice that static array which is on stack } //output: Deprecation: slice of static array temporary returned by s([1, 2, 3]) assigned to longer lived variable myDynamicArrayFYI: There's a Phobos PR for such an idiom https://github.com/dlang/phobos/pull/6489
Jun 12 2018
On 6/12/18 10:54 AM, Seb wrote:On Tuesday, 12 June 2018 at 14:35:33 UTC, SrMordred wrote:Note, this only will add the 's' function (named staticArray) to phobos, it still does not allow you to implicitly slice the temporary on return. In other words, even with this change, you STILL need to do: auto myStaticArray = [1,2,3].staticArray; auto myDynamicArray = myStaticArray[]; To not have memory corruption (and avoid the deprecation message). -Stevethis idiom for creating static array used to work, but they are failing now. What changed and whats the alternative? (from https://p0nce.github.io/d-idioms/# nogc-Array-Literals:-Breaking-the-Limits) T[n] s(T, size_t n)(auto ref T[n] array) pure nothrow nogc safe { return array; } void main() nogc { int[] myDynamicArray = [1, 2, 3].s; // Slice that static array which is on stack } //output: Deprecation: slice of static array temporary returned by s([1, 2, 3]) assigned to longer lived variable myDynamicArrayFYI: There's a Phobos PR for such an idiom https://github.com/dlang/phobos/pull/6489
Jun 12 2018
On Tuesday, 12 June 2018 at 15:39:10 UTC, Steven Schveighoffer wrote:Essentially what you had originally was a memory corruption bug (yes, even before the deprecation happened).Oops , that bad.
Jun 12 2018
On Tuesday, 12 June 2018 at 16:27:50 UTC, SrMordred wrote:On Tuesday, 12 June 2018 at 15:39:10 UTC, Steven Schveighoffer wrote:Well, not anymore ;)Essentially what you had originally was a memory corruption bug (yes, even before the deprecation happened).Oops , that bad.
Jun 12 2018