Thread overview
Bug 20868 and dip1000
Jan 11
RazvanN
Jan 11
Meta
Jan 11
Meta
Jan 11
jmh530
Jan 11
Meta
Jan 11
jmh530
January 11
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
January 11
On Monday, 11 January 2021 at 13:20:12 UTC, RazvanN wrote:
> 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

You need to email Walter on this as he is the one spearheading the dip1000.

-Alex


January 11
On Monday, 11 January 2021 at 13:20:12 UTC, RazvanN wrote:
> 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

I've run into a similar problem. I never got a satisfactory answer from Walter, and the only solution I could come up with other than rewriting the code was to reach for @trusted.
January 11
On Monday, 11 January 2021 at 15:19:07 UTC, Meta wrote:
> I've run into a similar problem. I never got a satisfactory answer from Walter, and the only solution I could come up with other than rewriting the code was to reach for @trusted.

Whoops, forgot the link:
https://forum.dlang.org/post/laqjadtwrsdhdrqokryx@forum.dlang.org
January 11
On Monday, 11 January 2021 at 15:19:44 UTC, Meta wrote:
> On Monday, 11 January 2021 at 15:19:07 UTC, Meta wrote:
>> I've run into a similar problem. I never got a satisfactory answer from Walter, and the only solution I could come up with other than rewriting the code was to reach for @trusted.
>
> Whoops, forgot the link:
> https://forum.dlang.org/post/laqjadtwrsdhdrqokryx@forum.dlang.org

I suspect Walter would complain about a lack of bug report.
January 11
On Monday, 11 January 2021 at 15:27:50 UTC, jmh530 wrote:
> On Monday, 11 January 2021 at 15:19:44 UTC, Meta wrote:
>> On Monday, 11 January 2021 at 15:19:07 UTC, Meta wrote:
>>> I've run into a similar problem. I never got a satisfactory answer from Walter, and the only solution I could come up with other than rewriting the code was to reach for @trusted.
>>
>> Whoops, forgot the link:
>> https://forum.dlang.org/post/laqjadtwrsdhdrqokryx@forum.dlang.org
>
> I suspect Walter would complain about a lack of bug report.

I am not sure if it's a bug or working as intended (though I suspect the latter). I couldn't get an answer for that, either.
January 11
On Monday, 11 January 2021 at 16:09:29 UTC, Meta wrote:
> [snip]
>>
>> I suspect Walter would complain about a lack of bug report.
>
> I am not sure if it's a bug or working as intended (though I suspect the latter). I couldn't get an answer for that, either.

I'd say report it and if it's working as intended, then he can close himself.
January 14
On 1/11/2021 5:20 AM, RazvanN wrote:
> [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

Replied in bugzilla.