www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 22635] New: opCast prevent calling destructor for const this.

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

          Issue ID: 22635
           Summary: opCast prevent calling destructor for const this.
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: major
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: submada gmail.com

This code doesn't compile:

```d
struct Foo{
    bool opCast(T : bool)()const{
        assert(0);
    }

    ~this(){}
}

struct Bar{
    const Foo foo;
}

void main(){


}
```

Error: template instance `opCast!(Foo)` does not match template declaration
`opCast(T : bool)()`


Generated constructor Bar.~dtor is using Foo.opCast to remove const from foo
before calling Foo.~this.
Right way is to take address of foo and cast pointer without involvement of
Foo.opCast.

This bug prevent emplacing qualified struct with opCast + ~this(). Example:

```d
struct Foo{
    bool opCast(T : bool)()const{
        assert(0);
    }

    this(int i){}

    ~this(){}
}


void main(){
    import core.lifetime : emplace;

    void[Foo.sizeof] buffer = void;
    emplace(cast(const Foo*)&buffer, 42);
}

```
Error: template instance `opCast!(Foo)` does not match template declaration
`opCast(T : bool)()`

--
Dec 29 2021