digitalmars.D.learn - Problem with safe
- Atila Neves (27/27) Mar 25 2014 I just spent a lot of time trying to debug my code and it seems
- Meta (11/38) Mar 25 2014 I don't know the answer to your problem, but I wanted to mention
- Atila Neves (6/33) Mar 25 2014 I probably should have mentioned that the bug is introduced in
- Atila Neves (8/35) Mar 25 2014 Sigh. I figured out what was going on. The reason why the
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
Mar 25 2014
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! AtilaI 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"); }
Mar 25 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
Mar 25 2014
Sigh. I figured out what was going on. The reason why the compiler never complained and the intended function never got called is because I had a static if checking for __traits(compiles, ...). The attributes made sense, but _I_ was silencing the compiler. Ugh. Atila 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
Mar 25 2014