Thread overview
option -ignore_pure for temporary debugging (or how to wrap an unpure function inside a pure one)?
Feb 08, 2018
Timothee Cour
Feb 08, 2018
Adam D. Ruppe
Feb 08, 2018
Seb
February 07, 2018
while hacking into druntime and adding temporary debug information (eg with custom logging etc) I had a hard time making things compile because lots of functions are pure nothrow safe, resulting in compile errors when my custom debugging functions are not pure nothrow safe.


How about adding flags ` -ignore_pure` (and perhaps -ignore_safe -ignore_nothrow) to allow code to compile ignoring safe, pure, nothrow mismatches?

This would be meant for temporary debugging obviously, production code would not enable these flags.

my workaround for nothrow and safe attributes is to call via wrapNothrow!fun:

@trusted
nothrow auto wrapNothrow(alias fun, T...)(T a){
  import std.exception;
  try{
    return fun(a);
  }
  catch(Exception t){
    assert(0, t.msg);
  }
}

What would be a workaround to wrap a non-pure function?
February 08, 2018
On Thursday, 8 February 2018 at 03:29:54 UTC, Timothee Cour wrote:
> How about adding flags ` -ignore_pure` (and perhaps -ignore_safe -ignore_nothrow) to allow code to compile ignoring safe, pure, nothrow mismatches?

We already have that for pure. Just write `debug your_function` and it will work.

void foo() {}

pure void main() {
        debug foo();
}

Note you must use the -debug switch to dmd to compile such statements in, but they are exempt from the pure check.

Then for safe and nothrow, a @trusted wrapper that catches Exceptions and rethrows them as Errors will handle that, like you are basically already doing.

@nogc is the exception... I think you can cast. So this:

---
void foo() {}

@trusted nothrow @nogc void da(scope void delegate() a) {
        auto hack = cast(void delegate() @nogc) a;
        try
        hack();
        catch(Exception e)
                assert(0, e.msg);
}

@safe nothrow @nogc pure void main() {
        debug da({foo();});
}
---


cheats the system entirely.
February 08, 2018
On Thursday, 8 February 2018 at 03:29:54 UTC, Timothee Cour wrote:
> while hacking into druntime and adding temporary debug information (eg with custom logging etc) I had a hard time making things compile because lots of functions are pure nothrow safe, resulting in compile errors when my custom debugging functions are not pure nothrow safe.
>
>
> How about adding flags ` -ignore_pure` (and perhaps -ignore_safe -ignore_nothrow) to allow code to compile ignoring safe, pure, nothrow mismatches?
>
> This would be meant for temporary debugging obviously, production code would not enable these flags.
>
> my workaround for nothrow and safe attributes is to call via wrapNothrow!fun:
>
> @trusted
> nothrow auto wrapNothrow(alias fun, T...)(T a){
>   import std.exception;
>   try{
>     return fun(a);
>   }
>   catch(Exception t){
>     assert(0, t.msg);
>   }
> }
>
> What would be a workaround to wrap a non-pure function?


This comes up every 2-3 months - we really need a better solution.

-> assumeNogc: https://forum.dlang.org/post/kvacxfalxqtoumatxtsx@forum.dlang.org


Other threads: https://forum.dlang.org/post/eiifgqvoimbtkgcwfiiy@forum.dlang.org

We really need better out-of-the-box solutions for this. There's debug, which already allows impure code in pure code.
It should be possible to use `debug` in same way for `@nogc`, `@safe` and `nothrow`.