How do you debug @safe @nogc code? Can't figure out how to print.
aliak
something at something.com
Fri Nov 16 12:59:22 UTC 2018
Hi, I previously had trouble trying to get print statements
working while debugging in nogc. And that was hackishly solved
[0], but I can't figure out how to do the same if I have @safe
involved. Here's a cut down sample:
// This is the function from the thread referenced
auto assumeNoGC(T)(T t) {
import std.traits;
enum attrs = functionAttributes!T | FunctionAttribute.nogc;
return cast(SetFunctionAttributes!(T, functionLinkage!T,
attrs)) t;
}
private struct Dispatcher {
public template opDispatch(string name){
auto ref opDispatch(Args...)(auto ref Args args) {
import std.stdio;
// Print args here for debugging purposes
assumeNoGC({ writeln(args); })();
return 3;
}
}
}
@safe @nogc void main() {
auto i = Dispatcher().getI(3);
}
So, basically with the above, using:
debug writeln(args);
Causes "Error: @nogc function D main cannot call non- at nogc
function"
Using:
debug assumeNoGC({ writeln(args); })();
Causes:
Error: @safe function D main cannot call @system function
Error: @nogc function D main cannot call non- at nogc function
Changing attrs in assumeNoGC
enum attrs = functionAttributes!T | FunctionAttribute.nogc |
FunctionAttribute.safe
Causes same as previous errors
Adding @trusted to declaration of opDispatch gets rid of @safe
error but I still get "Error: @nogc function D main cannot call
non- at nogc function". And going through the codebase and figuring
out where to add @trusted is *very* cumbersome.
I can't figure out how to make this work. The ideal way would be
just a debug_print() function that works in nogc and safe code.
I.e. does not affect the attribute inference of templates.
Cheers,
- Ali
[0] https://forum.dlang.org/post/pmf4dm$jmo$1@digitalmars.com
More information about the Digitalmars-d-learn
mailing list