<div dir="ltr">If you want to opt-out safety from template function foo, you can just add @system attribute to it.<div><br></div><div>int[] a;</div><div>@system int foo()() { return a[0]; }</div><div>void main() { foo!()(); }</div>
<div style><br></div><div style><div>$ dmd -run test</div><div>core.exception.RangeError@test(3): Range violation<br></div></div><div style><br></div><div style><div>$ dmd -release -run test</div><div>object.Error: Access Violation<br>
</div></div><div style><br></div><div style>Kenji Hara</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">2013/3/11 monarch_dodra <span dir="ltr"><<a href="mailto:monarchdodra@gmail.com" target="_blank">monarchdodra@gmail.com</a>></span><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I was on vacation last week, mostly without internet and even less D, which gave me the time to have existential thoughts (about D ^^).<br>

<br>
The issue I'm thinking about is @safe. @safe has a double function:<br>
1) Restrict any any usage of "unsafe" code.<br>
2) Bounds check slices, even in release mode.<br>
<br>
I think that when you ask for safe code, then 1) is always desired. 2), on the other hand, and not always desired. In particular, the only way to turn off 2) is with the global "-noboundscheck" hook.<br>
<br>
...<br>
<br>
Things get fun with D's attribute inference. As you know, D is able to infer if a function is nothrow... or safe!<br>
<br>
So here's the kicker: You wrote a template that is potentially safe. The D compiler tags it as safe. Awesome. BUT, because it's safe, it also means it is bounds checked, even in release. Not only is this (probably) not desired, but it is also *very* not-obvious.<br>

<br>
Take this "dumb" program:<br>
<br>
//----<br>
int[] a;<br>
<br>
int foo()()<br>
{<br>
    return a[4];<br>
}<br>
void main()<br>
{<br>
  a = [1, 2, 3];<br>
  foo!()();<br>
}<br>
//----<br>
<br>
You know what you get in release? A range error because foo!() is safe, and the compiler generated the associated bounds checking. To override this, foo must be explicitly tagged as @system.<br>
<br>
FACT: I have NEVER seen ANYBODY use @system, be it in phobos, or user code.<br>
<br>
This is counter intuitive, because for normal functions, @safe is "opt-in". For templates though, when possible, safety becomes opt-*out*. Weird 0_o.<br>
<br>
Has anybody else though about these kinds of issues before? I'm wondering how many of our algorithm benchmarks this has clobbered, and in which ratio this could be a factor of D's "bad" performance relative to C++?.<br>

<br>
<br>
------------------------------<u></u>----------------------------<br>
------------------------------<u></u>----------------------------<br>
<br>
<br>
Solution?<br>
<br>
This may be crazy, but given that "safe" is not free, I think "default" safety inference should be dropped.<br>
<br>
However, templates would have an opt-in "safe if possible" keyword "@optsafe":<br>
<br>
A template marked "@optsafe" would ban any unsafe code that does not depend on parametrization. It would always check bounds of arrays (provided no -noboundscheck).<br>
<br>
However, if at the end of the day, there is unsafe parametrized usage, then the final template just won't be marked as @safe.<br>
<br>
Thoughts?<br>
</blockquote></div><br></div>