[Issue 21762] New: object.destroy may silently fail depending on whether a member function is a template

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Mar 24 22:14:54 UTC 2021


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

          Issue ID: 21762
           Summary: object.destroy may silently fail depending on whether
                    a member function is a template
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody at puremagic.com
          Reporter: thomas.bockman at gmail.com

Since 2.092, the program below calls B's destructor, but not C's:

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

import core.stdc.stdlib : free, malloc;
import std.stdio : writeln;

struct A(T) {
    T* address;
    this(const(bool) create) {
        if(create)
            this.address = cast(T*) malloc(T.sizeof);
    }
    ~this() {
        if(address !is null) {
            destroy(*address);
            free(address);
            address = null;
                        writeln("A destroyed");
        }
    }
}

void main() {
    static struct B {
        @property ref A!B next()();
        ~this() {
            writeln("B destroyed"); }
    }
    static struct C {
        @property ref A!C next();
        ~this() {
            writeln("C destroyed"); }
    }

    A!B b = true;
    A!C c = true;
}
///////////////////////////////////////

Apparently this is hard to get right:

///////////////////////////////////////
Up to      2.067.1: Success with output:
-----
C destroyed
A destroyed
B destroyed
A destroyed
-----

2.068.2 to 2.083.1: Success with output:
-----
A destroyed
B destroyed
A destroyed
-----

2.084.1 to 2.091.1: Failure with output similar to:
-----
Error: union `object.destroy!(true, C).destroy.UntypedInit` has forward
references
onlineapp.d(14): Error: template instance `object.destroy!(true, C)` error
instantiating
onlineapp.d(29):        instantiated from here: `A!(C)`
-----

Since      2.092.1: Success with output:
-----
A destroyed
B destroyed
A destroyed
-----

--


More information about the Digitalmars-d-bugs mailing list