Variables should have the ability to be @nogc

Basile B. via Digitalmars-d digitalmars-d at puremagic.com
Tue May 31 06:33:03 PDT 2016


std.experimental.make allows use to create instances of structs 
or instances of classes that are not known by the garbage 
collector.


However, I've recently observed that it could leads to severe 
bugs when the aggregate that's allocated with 'make()' contains 
at least one member that's managed by the GC. The issue is here: 
https://issues.dlang.org/show_bug.cgi?id=15790.


A solution would be, in 'make()', to statically determine if the 
aggregate contains arrays, pointers or classes. This is very 
simple, as seen for example in EMSI container library with the 
'shouldAddGCRange' template. 
(https://github.com/economicmodeling/containers/blob/master/src/containers/internal/node.d#L59).


The problem with this template is that it returns false 
positives. For example an array will be seen as a managed type 
while actually it may be only modified by 'expandArray()' and 
'shrinkArray()' with Mallocator.


The obvious solution is simply to allow to anotate variables with 
@nogc. But...if it's already accepted by the compiler, it cannot 
be detected by the built-in traits. Why ? Because '@nogc' is a 
function attribute and is detectable only with a trait that works 
on functions.


Finally my proposition is:
0. verify that front really take @nogc on a variable.
1. add a trait to get the non-UDA attributes of a variable.
2. add to phobos the 'shouldAddGCRange' template, but enhanced 
with the detection of the variables marked @nogc
3. update 'make()' so that it uses the enhanced 
'shouldAddGCRange' to declare the content of the aggragate to the 
GC
4. close issue 15790.


I need your point of view about 0. & 1. Also I'm not a dmd 
commiter so someone will have to do it (i can do point 2 and 3). 
If you don't understand well what I'm trying to do, look at this 
paste, it's more or less what I'd like to add to phobos (except 
that currently it uses a UDA) https://dpaste.dzfl.pl/25a32c5cf106.


More information about the Digitalmars-d mailing list