assert(false) and GC

Steven Schveighoffer schveiguy at gmail.com
Sat Jul 10 01:23:26 UTC 2021


On 7/9/21 8:44 PM, russhy wrote:
> On Friday, 9 July 2021 at 23:34:25 UTC, Ali Çehreli wrote:
>> On 7/9/21 4:12 PM, russhy wrote:
>>
>> >> One way of forcing compile-time evaluation in D is to define
>> an enum
>> >> (which means "manifest constant" in that use).
>>
>> That's all I meant. It was a general comment.
>>
>> > this is very bad, assert are good because they are one liner,
>> making it
>> > 2 line to avoid GC is just poor design, compiler should be
>> smarter
>>
>> There must be a misunderstanding. The one-liner does not allocate 
>> either (or @nogc is broken).
>>
> 
> https://run.dlang.io/is/HJVSo0
> 
> it allocates

That test is possibly showing the wrong thing. You are capturing all 
allocations, not just the concatenation.

Change the assert line to:

```d
string s = __FUNCTION__ ~ "This is an error message";
```

And no GC allocations occur. Even without optimizations turned on.

I think it's the throwing/catching of the `Throwable` that is 
allocating. But I don't know from where the allocation happens.

-Steve


More information about the Digitalmars-d-learn mailing list