dmd icon indicating copy to clipboard operation
dmd copied to clipboard

Attempt to fix issue 23868

Open JohanEngelen opened this issue 1 year ago • 9 comments

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

Not sure if this is correct.

JohanEngelen avatar Apr 29 '23 22:04 JohanEngelen

Thanks for your pull request and interest in making D better, @JohanEngelen! We are looking forward to reviewing it, and you should be hearing from a maintainer soon. Please verify that your PR follows this checklist:

  • My PR is fully covered with tests (you can see the coverage diff by visiting the details link of the codecov check)
  • My PR is as minimal as possible (smaller, focused PRs are easier to review than big ones)
  • I have provided a detailed rationale explaining my changes
  • New or modified functions have Ddoc comments (with Params: and Returns:)

Please see CONTRIBUTING.md for more information.


If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment.

Bugzilla references

Auto-close Bugzilla Severity Description
23868 major Compiler-generated opAssign has very high stack frame usage

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub run digger -- build "master + dmd#15148"

dlang-bot avatar Apr 29 '23 22:04 dlang-bot

@WalterBright

maxhaton avatar Apr 30 '23 00:04 maxhaton

This probably needs some extra work to make it exception-safe (what if exception is thrown in the dtor)

JohanEngelen avatar Apr 30 '23 08:04 JohanEngelen

This probably needs some extra work to make it exception-safe (what if exception is thrown in the dtor)

This case wasn't treated previously either. Since we are not constructing anything I don't see why we would need to do anything extra.

RazvanN7 avatar May 01 '23 11:05 RazvanN7

This probably needs some extra work to make it exception-safe (what if exception is thrown in the dtor)

This case wasn't treated previously either. Since we are not constructing anything I don't see why we would need to do anything extra.

The state is different after exception is thrown in dtor: Before: this object contains new assigned object After: this object contains old value (on which the dtor failed). The assign object value is lost.

JohanEngelen avatar May 01 '23 17:05 JohanEngelen

Testcase whose behavior changes with this change (i.e. I think the PR is wrong currently) :

import std.stdio;
import std.exception;

struct S {
    int i;

    ~this() {
        writeln("~S(", i, ")");
        if (i == 1)
            throw new Exception("thorw");
    }
}

void main() {
    S s;
    s.i = 1;

    try {
        s = S(2);
    } catch (Throwable) {}

    writeln(s.i);
}

Without the change:

~S(1)
2
~S(2)

With this PR:

~S(1)
1
~S(1)
object.Exception@../dtor_throw.d(11): thorw
----------------
??:? void dtor_throw.S.__dtor() [0x100fd3689]
??:? _Dmain [0x100fd35ce]

JohanEngelen avatar May 02 '23 20:05 JohanEngelen

The deconstructor should be nogc and nothrow by default here.

12345swordy avatar May 02 '23 21:05 12345swordy

The deconstructor should be nogc and nothrow by default here.

Why? Is that a language requirement? (do you have a reference?)

JohanEngelen avatar May 02 '23 21:05 JohanEngelen

Please see https://issues.dlang.org/show_bug.cgi?id=23868 which explains that this solution is not correct, and offers suggestions on how to avoid the issue.

WalterBright avatar Jan 26 '24 23:01 WalterBright