[Issue 18265] `scope` storage class w/ -dip1000 and `scope` type modifier behavior inconsistent

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Jan 19 14:06:48 UTC 2018


https://issues.dlang.org/show_bug.cgi?id=18265

--- Comment #1 from Mike Franklin <slavo5150 at yahoo.com> ---
GRRR!!! Copied the wrong error.  Please disregard the first comment.   Instead
see the following.


The following code uses `scope` as a type modifier and is compiled WITHOUT
-dip1000

---
import std.stdio;

scope class A 
{
    this() { writeln("A.this()"); }
    ~this() { writeln("A.~this()"); }
    void method() { writeln("A.method()"); }
}

A fun()  // onlineapp.d(10): Error: functions cannot return scope onlineapp.A
{
    writeln("entering fun()");
    auto a = new A;

    writeln("leaving fun()");
    return a;

    // a.~this() called here
}

void main(string[] args)
{
    auto a = fun();

    // a.~this() has already been called, so the 
    // call to a.method() is undefined behavior
    a.method();
}
---
Run online at https://run.dlang.io/is/s9oozy

The compiler emits the following error:
onlineapp.d(10): Error: functions cannot return scope onlineapp.A


Now consider the following using `scope` as a storage class.
---
import std.stdio;

class A 
{
    this() { writeln("A.this()"); }
    ~this() { writeln("A.~this()"); }
    void method() { writeln("A.method()"); }
}

A fun() 
{
    writeln("entering fun()");
    scope a = new A;

    writeln("leaving fun()");
    return a;  // onlineapp.d(16): Error: scope variable a may not be returned

    // a.~this() called here
}

void main(string[] args)
{
    auto a = fun();

    // a.~this() has already been called, so the 
    // call to a.method() is undefined behavior
    a.method();
}
---
Run online at https://run.dlang.io/is/AUwGEb

Compiling WITH -dip1000 results in the following error:
onlineapp.d(16): Error: scope variable a may not be returned

These are essentially two ways of implementing the same program, yet the
compiler emits different errors at different locations.  The error messages
should, at least be at the same location.

--


More information about the Digitalmars-d-bugs mailing list