D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 21225 - preview=dtorfields inserts unnecessary dtor call in nothrow ctors
Summary: preview=dtorfields inserts unnecessary dtor call in nothrow ctors
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 normal
Assignee: No Owner
URL:
Keywords: pull
Depends on:
Blocks:
 
Reported: 2020-09-04 23:46 UTC by moonlightsentinel
Modified: 2020-09-05 19:04 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description moonlightsentinel 2020-09-04 23:46:02 UTC
The following code fails to compile with `-preview=dtorfields`:

======================================
struct Nothrow
{
    ~this() {}
}

struct NothrowConstructor
{
    Nothrow member;
    this(int) pure nothrow {}
}

======================================

 Error: `pure` constructor `NothrowConstructor.this` cannot call impure destructor `NothrowConstructor.~this`

The code currently fails to compile because the compiler rewrites the user-defined constructor as:

try
    <Old constructor>
catch (Exception e)
{
    <Destructor>
    throw e;
}

This causes the attribute missmatch (NothrowConstructor.~this not pure) albeit the catch block is unreachable (unless the old constructor violates it's `nothrow guarantee - which is UB anyway)
Comment 1 Dlang Bot 2020-09-04 23:50:07 UTC
@MoonlightSentinel created dlang/dmd pull request #11692 "Fix 21225 - preview=dtorfields inserts unnecessary dtor calls..." fixing this issue:

- Fix 21225 - preview=dtorfields inserts unnecessary dtor calls...
  
  ... in nothrow ctors
  
  Fixed by not inserting the destructor call if the constructor is
  `nothrow` - which makes the generated `catch`-block unreachable
  (unless the ctor violates it's `nothrow` guarantee - which is UB).
  
  This also saves time & memory spent on generating unused AST elements.

https://github.com/dlang/dmd/pull/11692
Comment 2 Dlang Bot 2020-09-05 19:04:09 UTC
dlang/dmd pull request #11692 "Fix 21225 - preview=dtorfields inserts unnecessary dtor calls..." was merged into master:

- 15caafef20af92469b11cfaaeaf16b4e28e2b1d3 by MoonlightSentinel:
  Fix 21225 - preview=dtorfields inserts unnecessary dtor calls...
  
  ... in nothrow ctors
  
  Fixed by not inserting the destructor call if the constructor is
  `nothrow` - which makes the generated `catch`-block unreachable
  (unless the ctor violates it's `nothrow` guarantee - which is UB).
  
  This also saves time & memory spent on generating unused AST elements.

https://github.com/dlang/dmd/pull/11692