DIP 1028---Make @safe the Default---Community Review Round 1

Dominikus Dittes Scherkl dominikus at scherkl.de
Thu Jan 2 15:08:41 UTC 2020


On Thursday, 2 January 2020 at 15:02:07 UTC, jmh530 wrote:
> On Thursday, 2 January 2020 at 14:37:02 UTC, Dominikus Dittes 
> Scherkl wrote:
>> [snip]
>>
>> Yes, yes!
>> But this tool should not slap @system on every function, but 
>> only on those that don't compile with the new default - the 
>> rest was already @safe but the proper annotation was missing, 
>> so this is the benefit we get. Don't waste it!
>
> You should probably also check that the unittests still pass 
> too...especially if the behavior of the function can change 
> depending on whether something is @safe or not. For instance, 
> in the code below, if you add in @system for foo and baz, then 
> the unittests still compile, but if you make foo and baz @safe, 
> then the unittests no longer pass.
>
> import std;
>
> int foo(int x) {
>     static if (isSafe!baz) {
>         return baz(x);
>     } else {
>         return bar(x);
>     }
> }
>
> @safe int bar(int x) {
>     return x + 1;
> }
>
> int baz(int x) {
>     return x + 2;
> }
>
> unittest {
>     int x = 5;
>     int y = foo(x);
>     assert(y == 6);
> }

No, this should not be necessary. baz() compiles with @safe as 
default, so it should not be annotated @system automatically. And 
if it doesn't compile, that would be a bug in the original code, 
so it is a good thing that the test now fails.


More information about the Digitalmars-d mailing list