www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 20897] New: -betterC generates `try`/`catch` in the AST when

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

          Issue ID: 20897
           Summary: -betterC generates `try`/`catch` in the AST when using
                    struct destructors
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: assemblyislaw gmail.com

Both DMD 2.091.1 and LDC 10.0.0 when compiling the following code
```
import core.stdc.stdlib;
import core.volatile;

struct List(T) {
        T* a;
    this(T b) {
        a = cast(T*)malloc(b);
    }

    ~this() {
        free(a);
    }
}

uint ioAPICRead(size_t ioAPIC, uint reg) {
    auto list = List!uint(reg);

    uint* base = cast(uint*)(cast(size_t)list.a);
    volatileStore(base, reg);
    return volatileLoad(base + 4);
}
```

Generate a pair of `try`/`catch` in the AST that will generate references to
`_d_eh_personality`:
```
import object;
import core.stdc.stdlib;
import core.volatile;
struct List(T)
{
        T* a;
        this(T b)
        {
                a = cast(T*)malloc(b);
        }
        ~this()
        {
                free(a);
        }
}
uint ioAPICRead(ulong ioAPIC, uint reg)
{
        List!uint list = list = 0 , list.this(reg);
        try
        {
                uint* base = cast(uint*)cast(ulong)list.a;
                volatileStore(base, reg);
                return volatileLoad(base + 16L);
        }
        finally
                list.~this();
}
List!uint
{
        struct List
        {
                uint* a;
                nothrow  nogc  system this(uint b)
                {
                        this.a = cast(uint*)malloc(cast(ulong)b);
                        return this;
                }
                ~this()
                {
                        free(cast(void*)this.a);
                }
                alias __xdtor = ~this()
                {
                        free(cast(void*)this.a);
                }
                ;
                nothrow  nogc ref  system List!uint opAssign(List!uint p)
return
                {
                        (List!uint __swap2 = void;) , __swap2 = this , (this =
p , __swap2.~this());
                        return this;
                }
        }

}
RTInfo!(List!uint)
{
        enum immutable(ulong)* RTInfo = & RTInfoImpl;

}
NoPointersBitmapPayload!1LU
{
        enum ulong[1] NoPointersBitmapPayload = 0LU;

}
RTInfoImpl!([8LU, 1LU])
{
        immutable immutable(ulong[2]) RTInfoImpl = [8LU, 1LU];

}
```

It can also be checked in https://run.dlang.io/is/aewRQ1 .

--
Jun 05