Thread overview
[Issue 15100] @nogc should ignore allocations on assertion fail
Sep 23, 2015
Jonathan M Davis
Dec 17, 2022
Iain Buclaw
September 22, 2015
https://issues.dlang.org/show_bug.cgi?id=15100

Steven Schveighoffer <schveiguy@yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |schveiguy@yahoo.com

--- Comment #1 from Steven Schveighoffer <schveiguy@yahoo.com> ---
Note that assert already is allocating if it fails.

I don't see why:

new Error("msg");

should be @nogc, but:

new Error("msg".idup);

isn't.

--
September 23, 2015
https://issues.dlang.org/show_bug.cgi?id=15100

Jonathan M Davis <issues.dlang@jmdavisProg.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |issues.dlang@jmdavisProg.co
                   |                            |m

--- Comment #2 from Jonathan M Davis <issues.dlang@jmdavisProg.com> ---
Whether it even makes sense for new Error to be @nogc depends on whether the GC is available or not. If it is, then the Error will presumably kill the program, so the allocation isn't a problem. However, if the GC isn't available, and the programmer is relying on @nogc to guarantee that the GC isn't actually used, then there's definitely a problem.

But maybe that can all be cleaned up with the reference counted Object hierarchy  that's been talked about previously, since Exceptions and Errors would presumably be part of that - though unless they're malloced, they'd still be using the GC, and presumably, using new wouldn't use malloc, so if we wanted a ref-counted Throwable to be on the malloced heap, something other than new would have to be used.

idup will always use the GC regardless though. I don't see how it could be otherwise. So, even if it were fixed so that Error was ref-counted and malloced, idup still wouldn't work without the GC. We really need some other way to handle the strings for the exception messages if we want to avoid the GC.

This whole situation is a bit sticky though, because we're basically trying to ban the GC in @nogc code while still using for stuff related to exceptions, and that's obviously problematic.

Maybe if allocating for an assertion fails, it should result in a message being printed (if possible) and then a HLT instruction? Even if Errors in general have a problem, stuff like assertions are controlled by druntime where we can have some control over what's going on without requiring a full-blown solution for dealing with the @nogc problem with exceptions. If we absolutely had to, we could use a function pointer to a @nogc function and using casting to make it point to one that isn't actually @nogc, though I really think that we should find a more principled solution to this that doesn't require lying about being @nogc.

Regardless of the exact solution here, I suspect that if we want to fully sort out the various issues with @nogc and exceptions (be they Errors or Exceptions), we need to sort out the ref-counting Object hierarchy stuff and whatever that's supposed to do and look like.

--
September 23, 2015
https://issues.dlang.org/show_bug.cgi?id=15100

--- Comment #3 from Steven Schveighoffer <schveiguy@yahoo.com> ---
Jonathan, I think you misunderstand.

This compiles today, and uses the gc (using a user-supplied variable to prevent
folding into assert(0)):

void main(string[] args) @nogc
{
   assert(args.length > 100, "hi");
}

But this doesn't:

assert(args.length > 100, "hi".idup);

Note that the exception isn't constructed (or allocated), and the "hi".idup isn't run unless something fails. This code is truly @nogc unless something goes horribly wrong, in which case the program will exit anyway, who cares if the gc is used?

Note, I'm not advocating that the expression 'new Error("msg")' be considered @nogc. My point in saying that is this is effectively what an assert does, and it's considered @nogc. All I'm saying is that asserts should be treated fully as @nogc, not just the error construction part.

--
December 17, 2022
https://issues.dlang.org/show_bug.cgi?id=15100

Iain Buclaw <ibuclaw@gdcproject.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P1                          |P4

--
December 13
https://issues.dlang.org/show_bug.cgi?id=15100

--- Comment #4 from dlangBugzillaToGithub <robert.schadek@posteo.de> ---
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/dmd/issues/19048

DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB

--