www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 19183] New: DIP1000 defeated if auto used instead of scope in

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

          Issue ID: 19183
           Summary: DIP1000 defeated if auto used instead of scope in
                    variable declaration with template this member
                    function
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: major
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: atila.neves gmail.com

The code:


--------------------
 safe:

const(int)* gInts;

void main() {
    auto s = MyStruct(10);
    gInts = s.ints;
}

struct MyStruct
{
    import core.stdc.stdlib;
    int* ints;
    this(int size)  trusted { ints = cast(int*) malloc(size); }
    ~this() scope  trusted { free(ints); }
    scope ptr(this This)() { return ints; }
}
--------------------


This compiles with dip1000 and the code is allowed to escape the reference to
ints even though it shouldn't. Writing out 3 member functions by hand for
mutable, const and immutable works as intended.

Even more strangely, this code can be made to _not_ compile by changing `auto s
= MyStruct(10)` to `scope s = MyStruct(10)` _or_ by adding `scope` to the
destructor. In either of those cases, the code fails to compile with an error
message about escaping, which is the correct behaviour.

--
Aug 20 2018