On Tuesday, 25 March 2014 at 17:57:33 UTC, Atila Neves wrote:
I just spent a lot of time trying to debug my code and it seems to have to do with @safe. I have some template functions in a class, and one of them, marked as @safe, ended up making a call to an abstract function with no attribute. My unit test started failing and I tried writeln-debugging but now my function never seemed to get called.

I tried gdb but the code jumped around weirdly (even though it was a debug build) and some stack frame information was off so I didn't trust it.

But stepping through the code gave me the idea to put writelns earlier in the call stack, and as I did so I ran into a compiler error I hadn't gotten before: @safe functions can't call writeln. "Fair enough", I said, and ended up with a chain of functions that had to be changed.

And then my code worked again. So I removed the writelns, changed everything to @trusted, and... the code worked again. I narrowed the fix down to this:

https://github.com/atilaneves/cerealed/commit/09d278638394185339a6115e7093b8aab2fac480

So, because an abstract function wasn't @safe, the compiler emitted code to... I'm not quite sure to do what. All I know is it didn't call the right function.

Is there something about @safe, @trusted, etc. that causes the compiler to disregard template functions from instantiation??

The worst part of all this is the compiler said not one thing. And it took a while to track down. I nearly wish I hadn't bothered with @safe and friends!

Atila

I don't know the answer to your problem, but I wanted to mention that you *can* call @system functions in an @safe function if you use debug. It's extremely handy so you DON'T have to change a huge chain of functions like you did.

@safe void main()
{
    import std.stdio;
    //Okay, writeln is in a debug block
    debug writeln("Test");
}

Reply via email to