www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 18265] New: `scope` storage class w/ -dip1000 and `scope`

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

          Issue ID: 18265
           Summary: `scope` storage class w/ -dip1000  and `scope` type
                    modifier behavior inconsistent
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: slavo5150 yahoo.com

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(10): Error: functions cannot return scope
onlineapp.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/AUwGEb

Compiling WITH -dip1000 results in the following error:
onlineapp.d(10): Error: functions cannot return scope onlineapp.A

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

--
Jan 19 2018