www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - std.container.array.Array is not nogc?

reply drug007 <drug2004 bk.ru> writes:
Is there a way to use Array in  nogc code:
```
import std.container.array : Array;

 nogc:
void main(string[ ] args)
{
    Array!int ai;
    ai ~= 1;
    assert(ai[0] == 1);
}
```
fails:
```
main.d(8): Error:  nogc function 'D main' cannot call non- nogc function 
'std.container.array.Array!int.Array.opOpAssign!("~", int).opOpAssign' 
 

main.d(9): Error:  nogc function 'D main' cannot call non- nogc function 
'std.container.array.Array!int.Array.opIndex'
```
am I doing something wrong?
Jan 15 2017
parent reply Jack Stouffer <jack jackstouffer.com> writes:
On Sunday, 15 January 2017 at 11:47:06 UTC, drug007 wrote:
 Is there a way to use Array in  nogc code:
 ```
 import std.container.array : Array;

  nogc:
 void main(string[ ] args)
 {
    Array!int ai;
    ai ~= 1;
    assert(ai[0] == 1);
 }
 ```
 fails:
 ```
 main.d(8): Error:  nogc function 'D main' cannot call non- nogc 
 function 'std.container.array.Array!int.Array.opOpAssign!("~", 
 int).opOpAssign'


 main.d(9): Error:  nogc function 'D main' cannot call non- nogc 
 function 'std.container.array.Array!int.Array.opIndex'
 ```
 am I doing something wrong?
No you're not. Array was designed before the nogc attribute was created, so it wasn't coded with it's requirements in mind. Looking at the code, Array allocates GC memory for exception throwing in some cases. These can and should be changed to asserts. I am writing a PR now to fix this. There doesn't seem to be too many cases to fix.
Jan 15 2017
parent reply drug007 <drug2004 bk.ru> writes:
On 15.01.2017 15:49, Jack Stouffer wrote:
 No you're not.

 Array was designed before the  nogc attribute was created, so it wasn't
 coded with it's requirements in mind. Looking at the code, Array
 allocates GC memory for exception throwing in some cases. These can and
 should be changed to asserts.

 I am writing a PR now to fix this. There doesn't seem to be too many
 cases to fix.
Thanks for answer. Looking forward for your PR.
Jan 15 2017
parent Jack Stouffer <jack jackstouffer.com> writes:
On Sunday, 15 January 2017 at 13:08:52 UTC, drug007 wrote:
 Thanks for answer. Looking forward for your PR.
https://github.com/dlang/phobos/pull/5036
Jan 15 2017