Bug 20868 and dip1000

RazvanN razvan.nitu1305 at gmail.com
Mon Jan 11 13:20:12 UTC 2021


Hello everyone,

Bug 20868 [3] reports the following code:

void scoped (scope void delegate() dg)
{
     static void delegate()[] dgs;
     // dgs ~= dg;  // error

     nonScoped(dg);  // not an error???
}

void nonScoped (void delegate() dg)
{
     static void delegate()[] dgs;
     dgs ~= dg;
}

void main ()
{
     int x;
     scoped({x = 100;});
}

The above code, compiled with -dip1000, compiles succesfully.

If the scoped and nonScoped functions are annotated with @safe, 
the nonScoped call will be reported as an error (as it should).

Spec [1] says: "scope escape analysis is only done for @safe 
functions. For other functions scope semantics must be manually 
enforced.". Dip1000 [2] says: "Errors for scope violations are 
only reported in @safe code.".

My understanding is that scope checks are performed only when 
dip1000 is enabled and in @safe code. However, that is not 
entirely the case. For example:

int* scoped(scope int* p) {
     return p;
}

void main() {
     int i;
     int* p = &i;
     scoped(p);
}

Compiled with dip1000, yields: "onlineapp.d(2): Error: scope 
variable `p` may not be returned".

I want to fix the issue, but I don't know what the official 
specification is. Should we disable any dip1000 checks in @system 
functions or is there any other piece of spec that I am missing?

Cheers,
RazvanN

[1] https://dlang.org/spec/function.html#scope-parameters
[2] 
https://github.com/dlang/DIPs/blob/master/DIPs/other/DIP1000.md
[3] https://issues.dlang.org/show_bug.cgi?id=20868


More information about the Digitalmars-d mailing list