December 14, 2021

On Thursday, 11 November 2021 at 09:24:17 UTC, Atila Neves wrote:

>

On Wednesday, 10 November 2021 at 13:52:26 UTC, deadalnix wrote:

>

On Tuesday, 9 November 2021 at 18:33:01 UTC, Atila Neves wrote:

>

I think that C++'s greatest gift to the world was the destructor. We have those too! Let's use them.

Indeed, but I have unfortunate news. We broke the gift. In D, object can be destroyed without being constructed first.

Consider: https://godbolt.org/z/EdW75jWGn

This is the first problem we need to fix here, if we don't want to have to plaster our code with runtime checks.

@disable this();, but you knew that. It's true that requiring a programmer to do something to prevent bugs is a terrible idea. Sigh.

Sorry for reviving this thread, was just sifting through...
The following code also outputs dtor!, unfortunately :(


import std.stdio:writeln;

struct S{
	@disable this();
	~this(){
		writeln("dtor!");
	}
}	

void main(){
	S s = void;
}

Compiler :
LDC - the LLVM D compiler (1.25.0):
based on DMD v2.095.1 and LLVM 11.1.0

December 14, 2021

On Tuesday, 14 December 2021 at 12:14:52 UTC, Tejas wrote:

>

On Thursday, 11 November 2021 at 09:24:17 UTC, Atila Neves wrote:

> >

@disable this();, but you knew that. It's true that requiring a programmer to do something to prevent bugs is a terrible idea. Sigh.

Sorry for reviving this thread, was just sifting through...
The following code also outputs dtor!, unfortunately :(


import std.stdio:writeln;

struct S{
	@disable this();
	~this(){
		writeln("dtor!");
	}
}	

void main(){
	S s = void;
}

Why is this unfortunate? You void-initialize, it's on you now to ensure valid state, i.e. it's exactly the tongue-in-cheek "terrible idea" of Atila's ;)

Slap a @safe on that and it won't compile.

December 14, 2021

On Tuesday, 14 December 2021 at 12:25:30 UTC, Stanislav Blinov wrote:

>

On Tuesday, 14 December 2021 at 12:14:52 UTC, Tejas wrote:

>

On Thursday, 11 November 2021 at 09:24:17 UTC, Atila Neves wrote:

> >

@disable this();, but you knew that. It's true that requiring a programmer to do something to prevent bugs is a terrible idea. Sigh.

Sorry for reviving this thread, was just sifting through...
The following code also outputs dtor!, unfortunately :(


import std.stdio:writeln;

struct S{
	@disable this();
	~this(){
		writeln("dtor!");
	}
}	

void main(){
	S s = void;
}

Why is this unfortunate? You void-initialize, it's on you now to ensure valid state, i.e. it's exactly the tongue-in-cheek "terrible idea" of Atila's ;)

Slap a @safe on that and it won't compile.

That's what I did after reading your message, with the bonus of slapping @safe on S.__dtor as well ;)

import std.stdio:writeln;

struct S{
	@disable this();
	~this()@safe/*now @safe*/{
		writeln("dtor!");
	}
}	

void main()@safe/* now @safe*/{
	S s = void;
}

It defeated @safe ;_;

December 14, 2021

On Tuesday, 14 December 2021 at 12:41:23 UTC, Tejas wrote:

>

On Tuesday, 14 December 2021 at 12:25:30 UTC, Stanislav Blinov wrote:

> >

Slap a @safe on that and it won't compile.

That's what I did after reading your message, with the bonus of slapping @safe on S.__dtor as well ;)

import std.stdio:writeln;

struct S{
	@disable this();
	~this()@safe/*now @safe*/{
		writeln("dtor!");
	}
}	

void main()@safe/* now @safe*/{
	S s = void;
}

It defeated @safe ;_;

Oh, right, S doesn't have any pointers. @safe only cares about void-initializing those.

December 14, 2021

On Tuesday, 14 December 2021 at 12:51:23 UTC, Stanislav Blinov wrote:

>

On Tuesday, 14 December 2021 at 12:41:23 UTC, Tejas wrote:

>

On Tuesday, 14 December 2021 at 12:25:30 UTC, Stanislav Blinov wrote:

> >

Slap a @safe on that and it won't compile.

That's what I did after reading your message, with the bonus of slapping @safe on S.__dtor as well ;)

import std.stdio:writeln;

struct S{
	@disable this();
	~this()@safe/*now @safe*/{
		writeln("dtor!");
	}
}	

void main()@safe/* now @safe*/{
	S s = void;
}

It defeated @safe ;_;

Oh, right, S doesn't have any pointers. @safe only cares about void-initializing those.

Should it be posted on bug tracker ?

December 14, 2021

On Tuesday, 14 December 2021 at 12:53:29 UTC, Tejas wrote:

>

On Tuesday, 14 December 2021 at 12:51:23 UTC, Stanislav Blinov wrote:

>

On Tuesday, 14 December 2021 at 12:41:23 UTC, Tejas wrote:

>

On Tuesday, 14 December 2021 at 12:25:30 UTC, Stanislav Blinov wrote:

> >

Slap a @safe on that and it won't compile.

That's what I did after reading your message, with the bonus of slapping @safe on S.__dtor as well ;)

import std.stdio:writeln;

struct S{
	@disable this();
	~this()@safe/*now @safe*/{
		writeln("dtor!");
	}
}	

void main()@safe/* now @safe*/{
	S s = void;
}

It defeated @safe ;_;

Oh, right, S doesn't have any pointers. @safe only cares about void-initializing those.

Should it be posted on bug tracker ?

It could do with an enhancement IMO. Your code specifically isn't causing any UB, but it's easy to see how practical code would. E.g. plain integers may refer to memory as well as any pointer.

December 14, 2021

On Tuesday, 14 December 2021 at 12:53:29 UTC, Tejas wrote:

>

On Tuesday, 14 December 2021 at 12:51:23 UTC, Stanislav Blinov wrote:

>

On Tuesday, 14 December 2021 at 12:41:23 UTC, Tejas wrote:

>

On Tuesday, 14 December 2021 at 12:25:30 UTC, Stanislav Blinov wrote:

> >

Slap a @safe on that and it won't compile.

That's what I did after reading your message, with the bonus of slapping @safe on S.__dtor as well ;)

import std.stdio:writeln;

struct S{
	@disable this();
	~this()@safe/*now @safe*/{
		writeln("dtor!");
	}
}	

void main()@safe/* now @safe*/{
	S s = void;
}

It defeated @safe ;_;

Oh, right, S doesn't have any pointers. @safe only cares about void-initializing those.

Should it be posted on bug tracker ?

If you can find a way to actually cause UB in @safe code, you should post it on the bug tracker. However, this example does not qualify.

December 14, 2021

On Tuesday, 14 December 2021 at 12:14:52 UTC, Tejas wrote:

>

On Thursday, 11 November 2021 at 09:24:17 UTC, Atila Neves wrote:

>

On Wednesday, 10 November 2021 at 13:52:26 UTC, deadalnix wrote:

>

[...]

@disable this();, but you knew that. It's true that requiring a programmer to do something to prevent bugs is a terrible idea. Sigh.

Sorry for reviving this thread, was just sifting through...
The following code also outputs dtor!, unfortunately :(


import std.stdio:writeln;

struct S{
	@disable this();
	~this(){
		writeln("dtor!");
	}
}	

void main(){
	S s = void;
}

Compiler :
LDC - the LLVM D compiler (1.25.0):
based on DMD v2.095.1 and LLVM 11.1.0

Is there an issue for this?

December 14, 2021

On Tuesday, 14 December 2021 at 15:51:58 UTC, Atila Neves wrote:

>

On Tuesday, 14 December 2021 at 12:14:52 UTC, Tejas wrote:

>

On Thursday, 11 November 2021 at 09:24:17 UTC, Atila Neves wrote:

>

On Wednesday, 10 November 2021 at 13:52:26 UTC, deadalnix wrote:

>

[...]

@disable this();, but you knew that. It's true that requiring a programmer to do something to prevent bugs is a terrible idea. Sigh.

Sorry for reviving this thread, was just sifting through...
The following code also outputs dtor!, unfortunately :(


import std.stdio:writeln;

struct S{
	@disable this();
	~this(){
		writeln("dtor!");
	}
}	

void main(){
	S s = void;
}

Compiler :
LDC - the LLVM D compiler (1.25.0):
based on DMD v2.095.1 and LLVM 11.1.0

Is there an issue for this?

There no issue there, this not used at all. Segfaults will start to happen, as expected, when trying to access a member variable.

December 14, 2021

On Tuesday, 14 December 2021 at 16:17:30 UTC, user1234 wrote:

>

On Tuesday, 14 December 2021 at 15:51:58 UTC, Atila Neves wrote:

>

On Tuesday, 14 December 2021 at 12:14:52 UTC, Tejas wrote:

>

On Thursday, 11 November 2021 at 09:24:17 UTC, Atila Neves wrote:

>

On Wednesday, 10 November 2021 at 13:52:26 UTC, deadalnix wrote:

>

[...]

@disable this();, but you knew that. It's true that requiring a programmer to do something to prevent bugs is a terrible idea. Sigh.

Sorry for reviving this thread, was just sifting through...
The following code also outputs dtor!, unfortunately :(


import std.stdio:writeln;

struct S{
	@disable this();
	~this(){
		writeln("dtor!");
	}
}	

void main(){
	S s = void;
}

Compiler :
LDC - the LLVM D compiler (1.25.0):
based on DMD v2.095.1 and LLVM 11.1.0

Is there an issue for this?

There no issue there, this not used at all. Segfaults will start to happen, as expected, when trying to access a member variable.

No, they wouldn't, per current spec.

https://dlang.org/spec/declaration.html#void_init
https://dlang.org/spec/function.html#safe-functions

Per that, this is @safe:

import core.sys.linux.fcntl;
import core.sys.linux.unistd;

struct MMap
{
    private int fd;
    @disable this();
    @disable this(this);
    // ...
    ~this() @trusted {
        if (isValid) {
            auto msg = "closed";
            size_t len = msg.length;
            write(fd, &len, len.sizeof);
            write(fd, msg.ptr, len);
            close(fd);
        }
    }

    private bool isValid() const @trusted {
        import core.stdc.errno;
        return fcntl(fd, F_GETFD) != -1 || errno != EBADF;
    }
}

void main() @safe
{
    // ...
    MMap mm = void; // currently allowed because MMap doesn't contain indirections
    // ...

} // nothing may happen, or may crash, or may write into someone else's memory, or to stdout...

Prolly should make an enhancement request for spec of @safe to disallow void initialization altogether.