Problem with @safe

Atila Neves atila.neves at gmail.com
Tue Mar 25 11:18:44 PDT 2014


I probably should have mentioned that the bug is introduced in 
the parent commit. The code I originally wrote that triggered the 
bug was larger, of course.

Replacing that parent commit with a call to writeln does the same 
thing. I would expect a compiler error instead.

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



More information about the Digitalmars-d-learn mailing list