www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 22528] New: Template breaks return annotation for class


          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 puremagic.com
          Reporter: thomas.bockman 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,
        } 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) {
                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:


Nov 20 2021