[Issue 22528] New: Template breaks return annotation for class reference returned by struct method

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sat Nov 20 08:00:04 UTC 2021


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

          Issue ID: 22528
           Summary: Template breaks return annotation for class reference
                    returned by struct method
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: blocker
          Priority: P1
         Component: dmd
          Assignee: nobody at puremagic.com
          Reporter: thomas.bockman at gmail.com

In the program below, UniqueD.borrowA and uniqueD.borrowB differ only in that
borrowA is a template.

When compiled with dip1000 enabled, the return annotation works correctly for
the non-template borrowB, but does not work for the template borrowA.

//////////////////////////////////////////////
module app;

import core.stdc.stdlib : malloc, free;
import core.lifetime : emplace;

import std.traits;

class D { }

struct UniqueD {
    private D _target;
    inout(D) borrowA()() return inout pure @safe nothrow @nogc {
        return _target; }
    inout(D) borrowB() return inout pure @safe nothrow @nogc {
        return _target; }

    this(this This)(const(bool) value) @trusted {
        if(value) {
            _target = cast(typeof(_target)) malloc(__traits(classInstanceSize,
D));
            emplace(_target);
        } else
            _target = null;
    }

    @disable this(this);
    @disable this(ref typeof(this));
    @disable ref typeof(this) opAssign(ref typeof(this));

    ~this() @trusted {
        if(_target !is null) {
            destroy!false(_target);
                free(cast(void*) _target);
            _target = null;
        }
    }
}

void main() @safe {
    static D staticD = null;

    UniqueD uniqueD = true;
    staticD = uniqueD.borrowA; // Accepts invalid only if borrow is a template.
    staticD = uniqueD.borrowB; // Error: address of variable `uniqueD` assigned
to `staticD` with longer lifetime
}
//////////////////////////////////////////////

This bug is a blocker to achieving @safe C++ or Rust style memory management in
D for some very basic use cases, along with:

https://issues.dlang.org/show_bug.cgi?id=21981#add_comment

--


More information about the Digitalmars-d-bugs mailing list