Warn about using the GC

Richard (Rikki) Andrew Cattermole richard at cattermole.co.nz
Sun Jan 14 09:45:49 UTC 2024


On 14/01/2024 10:25 PM, Walter Bright wrote:
> Instead of using @nogc, we could have a compiler switch "warn on use of 
> GC" which you can turn on if you like, or not, or turn on the -w switch 
> to treat the gc warnings as errors.

We already have a way of finding out where all the GC allocations is as 
a compiler switch.

This does not need to be a warning.

You want the guarantee in hot loops that a GC allocation will not take 
place there accidentally.

The primary source of this is closure creation. Where it is being stored 
is not scope (such as a function parameter).

I.e. ``@localnogc`` or ``@nogc(scope)``

```d
void hot(float[] a, float b) /* @localnogc */ {
	size_t last;
	accept(() { // error: will allocate closure context with GC
		if (last > a.length)
			return float.init;
		return a[last++] + b;
	});// ok, accept uses GC, this call is not checked.
}

void accept(float delegate() del) {

}
```

The transitive nature of ``@nogc`` where it checks function calls is the 
only behavior that needs to be disabled. Everything else is good for 
such an addition. Although AA mutation and ``.length =`` should probably 
be kept. Too easy to do them without realizing it.

The goal here is to prevent unnecessary work, that is almost certain to 
have been done by accident.



More information about the Digitalmars-d mailing list