www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Debug prints in nogc

reply =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
I'm struggling with debug printing in my  nogc-containers.

The alternatives:

     assert(false, "Fixed message with no parameters");

is not enough for my needs

     debug writeln("Fixed");

doesn't bypass  nogc checking. Why?

And temporary commenting out  nogc is cumbersome.

I'm aware of C-style printf but that is not as flexible as 
writeln.

Any advice?
Aug 30 2016
next sibling parent Seb <seb wilzba.ch> writes:
On Tuesday, 30 August 2016 at 10:26:28 UTC, Nordlöw wrote:
 I'm struggling with debug printing in my  nogc-containers.

 The alternatives:

     assert(false, "Fixed message with no parameters");

 is not enough for my needs

     debug writeln("Fixed");

 doesn't bypass  nogc checking. Why?

 And temporary commenting out  nogc is cumbersome.

 I'm aware of C-style printf but that is not as flexible as 
 writeln.

 Any advice?
I have experienced the same pain, but afaik there's no good solution except commenting or printf and I really wish the debug directive would ignore all attribute restrictions and not only pure. To my knowledge the sole good solution is to fix DMD / D.
Aug 30 2016
prev sibling next sibling parent reply rikki cattermole <rikki cattermole.co.nz> writes:
On 30/08/2016 10:26 PM, Nordlöw wrote:
 I'm struggling with debug printing in my  nogc-containers.

 The alternatives:

     assert(false, "Fixed message with no parameters");

 is not enough for my needs

     debug writeln("Fixed");

 doesn't bypass  nogc checking. Why?

 And temporary commenting out  nogc is cumbersome.

 I'm aware of C-style printf but that is not as flexible as writeln.

 Any advice?
Static array + formattedWrite, perhaps?
Aug 30 2016
parent reply =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
On Tuesday, 30 August 2016 at 10:36:13 UTC, rikki cattermole 
wrote:
 On 30/08/2016 10:26 PM, Nordlöw wrote:
 I'm struggling with debug printing in my  nogc-containers.

 The alternatives:

     assert(false, "Fixed message with no parameters");

 is not enough for my needs

     debug writeln("Fixed");

 doesn't bypass  nogc checking. Why?

 And temporary commenting out  nogc is cumbersome.

 I'm aware of C-style printf but that is not as flexible as 
 writeln.

 Any advice?
Static array + formattedWrite, perhaps?
I can't grasp how to use this from the docs at https://dlang.org/library/std/format/formatted_write.html uint fwrite(A...)(A a) { import std.stdio : LockingTextWriter; import std.stdio : stdout; import std.format : formattedWrite; return formattedWrite!(stdout.lockingTextWriter)(a); } LockingTextWriter is not publicly exported from std.stdio and stdout.lockingTextWriter can not be read at compile-time. Help please.
Aug 30 2016
parent rikki cattermole <rikki cattermole.co.nz> writes:
On 30/08/2016 11:10 PM, Nordlöw wrote:
 On Tuesday, 30 August 2016 at 10:36:13 UTC, rikki cattermole wrote:
 On 30/08/2016 10:26 PM, Nordlöw wrote:
 I'm struggling with debug printing in my  nogc-containers.

 The alternatives:

     assert(false, "Fixed message with no parameters");

 is not enough for my needs

     debug writeln("Fixed");

 doesn't bypass  nogc checking. Why?

 And temporary commenting out  nogc is cumbersome.

 I'm aware of C-style printf but that is not as flexible as writeln.

 Any advice?
Static array + formattedWrite, perhaps?
I can't grasp how to use this from the docs at https://dlang.org/library/std/format/formatted_write.html uint fwrite(A...)(A a) { import std.stdio : LockingTextWriter; import std.stdio : stdout; import std.format : formattedWrite; return formattedWrite!(stdout.lockingTextWriter)(a); } LockingTextWriter is not publicly exported from std.stdio and stdout.lockingTextWriter can not be read at compile-time. Help please.
I fixed your code but lockingTextWriter is not nogc. void main() nogc { import std.stdio : stdout; import std.format : formattedWrite; formattedWrite(stdout.lockingTextWriter, "%x", 7); } Okay looks like formattedWrite isn't nogc able. Oh wells thought it was. Please create an issue as this is quite an important feature.
Aug 30 2016
prev sibling parent reply Johannes Pfau <nospam example.com> writes:
Am Tue, 30 Aug 2016 10:26:28 +0000
schrieb Nordl=C3=B6w <per.nordlow gmail.com>:

 I'm struggling with debug printing in my  nogc-containers.
=20
 The alternatives:
=20
      assert(false, "Fixed message with no parameters");
=20
 is not enough for my needs
=20
      debug writeln("Fixed");
=20
 doesn't bypass  nogc checking. Why?
=20
 And temporary commenting out  nogc is cumbersome.
=20
 I'm aware of C-style printf but that is not as flexible as=20
 writeln.
=20
 Any advice?
----------------------------------------------------- import std.stdio; debug { enum writelnPtr =3D &writeln!string; enum void function(string) nogc writelnNoGC =3D cast(void function(string) nogc)writelnPtr; } void main() nogc { debug writelnNoGC("foo"); } ----------------------------------------------------- As long as it's only for debugging, the extra indirection shouldn't matter for performance. Even for release builds the optimizer can probably remove the indirection. An alternative solution is using mangleof + pragma(mangle) to refer to the external function. In both cases this approach can be tedious for templated methods. You don't want to write that boilerplate for every possible type combination. However, it should be possible to refactor the code above into a template and automate the boilerplate generation in some way.
Aug 30 2016
parent reply =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
On Tuesday, 30 August 2016 at 11:52:21 UTC, Johannes Pfau wrote:
 import std.stdio;

 debug
 {
     enum writelnPtr = &writeln!string;
     enum void function(string)  nogc writelnNoGC =
         cast(void function(string)  nogc)writelnPtr;
 }

 void main()  nogc
 {
     debug writelnNoGC("foo");
 }
Just being able to print a string is not good enough. I want the variadic part writeln so I can debug-print values in my buggy code. Do you have a similar solution?
Aug 30 2016
parent reply Cauterite <cauterite gmail.com> writes:
On Tuesday, 30 August 2016 at 14:38:47 UTC, Nordlöw wrote:
 Just being able to print a string is not good enough. I want 
 the variadic part writeln so I can debug-print values in my 
 buggy code. Do you have a similar solution?
Take a look at the example here: http://dlang.org/phobos/std_traits#SetFunctionAttributes You could make a `assumeNogc` template similar to the example `assumePure`. Oh yeah, here's one I prepared earlier: https://dpaste.dzfl.pl/254a5c2697a7
Aug 30 2016
parent reply Johannes Pfau <nospam example.com> writes:
Am Tue, 30 Aug 2016 16:37:53 +0000
schrieb Cauterite <cauterite gmail.com>:

 On Tuesday, 30 August 2016 at 14:38:47 UTC, Nordl=C3=B6w wrote:
 Just being able to print a string is not good enough. I want=20
 the variadic part writeln so I can debug-print values in my=20
 buggy code. Do you have a similar solution? =20
=20 Take a look at the example here: http://dlang.org/phobos/std_traits#SetFunctionAttributes You could make a `assumeNogc` template similar to the example=20 `assumePure`. =20 Oh yeah, here's one I prepared earlier: https://dpaste.dzfl.pl/254a5c2697a7
Nice! Here's a slightly modified version: https://dpaste.dzfl.pl/8c5ec90c5b39 This version does not need an additional delegate. It can be used like this: assumeNogc!writefln("foo %s", 42); assumeNogc!writeln("foo", 42);
Aug 30 2016
parent reply =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
On Tuesday, 30 August 2016 at 17:11:48 UTC, Johannes Pfau wrote:
 Nice! Here's a slightly modified version: 
 https://dpaste.dzfl.pl/8c5ec90c5b39

 This version does not need an additional delegate. It can be 
 used like this:

 assumeNogc!writefln("foo %s", 42);
 assumeNogc!writeln("foo", 42);
Marvellous! I update my cryptic but short-and-sweet `dln` at https://github.com/nordlow/phobos-next/blob/master/src/dbg.d#L58 I would love to see this very useful little snippet make its way into Phobos somehow. Any suggestions on how?
Aug 30 2016
next sibling parent =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
On Tuesday, 30 August 2016 at 19:03:06 UTC, Nordlöw wrote:
 https://github.com/nordlow/phobos-next/blob/master/src/dbg.d#L58
Renamed to https://github.com/nordlow/phobos-next/blob/master/src/dbgio.d#L58
Aug 30 2016
prev sibling parent reply Seb <seb wilzba.ch> writes:
On Tuesday, 30 August 2016 at 19:03:06 UTC, Nordlöw wrote:
 On Tuesday, 30 August 2016 at 17:11:48 UTC, Johannes Pfau wrote:
 Nice! Here's a slightly modified version: 
 https://dpaste.dzfl.pl/8c5ec90c5b39

 This version does not need an additional delegate. It can be 
 used like this:

 assumeNogc!writefln("foo %s", 42);
 assumeNogc!writeln("foo", 42);
Marvellous!
+1
 I update my cryptic but short-and-sweet `dln` at

 https://github.com/nordlow/phobos-next/blob/master/src/dbg.d#L58

 I would love to see this very useful little snippet make its 
 way into Phobos somehow. Any suggestions on how?
I am trying to push for a dump method in Phobos, we could try to combine it: https://github.com/dlang/phobos/pull/4318 AssumeNogc is potentially dangerous, so I don't know whether it can make it directly, but only if you try you know ;-)
Aug 31 2016
parent reply Cauterite <cauterite gmail.com> writes:
On Wednesday, 31 August 2016 at 15:10:11 UTC, Seb wrote:
 AssumeNogc is potentially dangerous, so I don't know whether it 
 can make it directly, but only if you try you know ;-)
So is assumeUnique
Aug 31 2016
parent reply Yuxuan Shui <yshuiv7 gmail.com> writes:
On Wednesday, 31 August 2016 at 15:52:18 UTC, Cauterite wrote:
 On Wednesday, 31 August 2016 at 15:10:11 UTC, Seb wrote:
 AssumeNogc is potentially dangerous, so I don't know whether 
 it can make it directly, but only if you try you know ;-)
So is assumeUnique
No. When you use assumeUnique, you know something the compiler does know, and have to use assumeUnique to tell the compiler that (at least when you use it correctly). But when you use assumeNogc, it's always because you want to bypass compiler checks.
Aug 31 2016
parent reply Cauterite <cauterite gmail.com> writes:
On Wednesday, 31 August 2016 at 16:17:51 UTC, Yuxuan Shui wrote:
 No. When you use assumeUnique, you know something the compiler 
 does know, and have to use assumeUnique to tell the compiler 
 that (at least when you use it correctly). But when you use 
 assumeNogc, it's always because you want to bypass compiler 
 checks.
assumeNogc works the same way, you're telling the compiler something it doesn't know — that the function should be treated as nogc. Using assumeNogc on a function that calls the GC is as unsafe as using assumeUnique on a reference that is not unique.
Aug 31 2016
parent reply Yuxuan Shui <yshuiv7 gmail.com> writes:
On Wednesday, 31 August 2016 at 18:07:46 UTC, Cauterite wrote:
 On Wednesday, 31 August 2016 at 16:17:51 UTC, Yuxuan Shui wrote:
 No. When you use assumeUnique, you know something the compiler 
 does know, and have to use assumeUnique to tell the compiler 
 that (at least when you use it correctly). But when you use 
 assumeNogc, it's always because you want to bypass compiler 
 checks.
assumeNogc works the same way, you're telling the compiler something it doesn't know — that the function should be treated as nogc. Using assumeNogc on a function that calls the GC is as unsafe as using assumeUnique on a reference that is not unique.
Correct me if I'm wrong. But I believe this is only true when the source code of function is not available. Otherwise the compiler should always know if a function is actually nogc or not.
Aug 31 2016
parent reply ag0aep6g <anonymous example.com> writes:
On 08/31/2016 09:23 PM, Yuxuan Shui wrote:
 Correct me if I'm wrong. But I believe this is only true when the source
 code of function is not available. Otherwise the compiler should always
 know if a function is actually  nogc or not.
Attributes are only inferred in certain cases where the source code must be available anyway (templates, `auto` return value). For ordinary functions, the compiler only considers them nogc when they're explicitly marked. For example, the compiler won't let you do this, even though f's source code is available and it's obviously de-facto nogc: ---- void f() {} void main() nogc { f(); } ----
Aug 31 2016
parent Yuxuan Shui <yshuiv7 gmail.com> writes:
On Wednesday, 31 August 2016 at 19:39:36 UTC, ag0aep6g wrote:
 On 08/31/2016 09:23 PM, Yuxuan Shui wrote:
 Correct me if I'm wrong. But I believe this is only true when 
 the source
 code of function is not available. Otherwise the compiler 
 should always
 know if a function is actually  nogc or not.
Attributes are only inferred in certain cases where the source code must be available anyway (templates, `auto` return value). For ordinary functions, the compiler only considers them nogc when they're explicitly marked. For example, the compiler won't let you do this, even though f's source code is available and it's obviously de-facto nogc: ---- void f() {} void main() nogc { f(); } ----
That's a good point. By IMHO, all the (non-template) function in Phobos that can be marked nogc, should be marked nogc, otherwise it's a bug. If the function is in your own code, you should use nogc, not assumeNogc. After a second thought, the real use case of assumeNogc is probably virtual functions. But then again, it is kind of dangerous to use it even in this case.
Aug 31 2016