Skip to content

Add __traits(needsDestruction, T) - #22616

Merged
thewilsonator merged 9 commits into
dlang:masterfrom
ntrel:traits-dtor
Feb 28, 2026
Merged

thewilsonator merged 9 commits into
dlang:masterfrom
ntrel:traits-dtor

Conversation

@ntrel

@ntrel ntrel commented Feb 22, 2026

Copy link
Copy Markdown
Contributor

Fixes #22609.

Code adapted from isCopyable trait - @kinke

@dlang-bot

Copy link
Copy Markdown

Thanks for your pull request and interest in making D better, @ntrel! 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

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

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#22616"

Comment thread compiler/src/dmd/traits.d Outdated
t = tsa.next;
continue;
}
}

@kinke kinke Feb 22, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The static arrays and toBasetype() are all handled by baseElemOf(t), then you don't need the loop either, just e.g. if (auto ts = baseElemOf(t).isTypeStruct()). Edit: Nope, still need to check for for emptiness (could be t.size()).

I didn't realize the existing trait just handles structs with dtor (incl. static arrays thereof) - so has needsDestruction semantics. Based on its name, I'd have assumed it handles classes too, to see if there's an explicit or auto-generated finalizer.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So IMO the existing druntime/Phobos trait is a misnomer. A static array doesn't have a destructor. And what is an 'elaborate' destructor? Either there is one or there isn't.

So for a new builtin trait, we can/should get the name right. The compiler has Type.needsDestruction(), which is true for structs with some destructor, static arrays thereof (without special case for empty static arrays with size 0 AFAICT), and enums with a base type that needs destruction. So those semantics seem very close to the existing hasElaborateDestructor library trait (AFAICT, doesn't handle enums, and checks for empty static arrays).

We could also go with different semantics, only handling aggregates (so incl. classes). Then library traits could use it in hasElaborateDestructor etc.

@thewilsonator thewilsonator left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see the circleCI failure

Comment thread compiler/src/dmd/id.d Resolved
ntrel added a commit to ntrel/dlang.org that referenced this pull request Feb 23, 2026
@ntrel
ntrel marked this pull request as ready for review February 23, 2026 10:36
@ntrel
ntrel requested a review from ibuclaw as a code owner February 23, 2026 10:36
}
}
// Used by std.traits.hasElaborateDestructor
// TODO inline this in druntime

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this mean?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It means to remove the internal template and replace all uses of it with the new __traits expression.

@kinke kinke Feb 27, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[A quick glance at existing hasElaborateDestructor usages in druntime is scary - mostly only handling structs, not static arrays.]

Edit: We should probably have a core.lifetime.destruct() helper or so.

Comment thread changelog/dmd.hasElaborateDestructor.dd Resolved
Comment thread compiler/src/dmd/traits.d
Comment on lines +614 to +627
// T[0]
if (!t.size())
return False();

// get the base in case `t` is an `enum`
// handle static arrays
t = t.baseElemOf();
if (auto ts = t.isTypeStruct())
{
ts.sym.dsymbolSemantic(sc);
if (ts.sym.dtor)
return True();
}
return False();

@kinke kinke Feb 25, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// T[0]
if (!t.size())
return False();
// get the base in case `t` is an `enum`
// handle static arrays
t = t.baseElemOf();
if (auto ts = t.isTypeStruct())
{
ts.sym.dsymbolSemantic(sc);
if (ts.sym.dtor)
return True();
}
return False();
return t.needsDestruction() ? True() : False();

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That does not work, it doesn't call dsymbolSemantic first.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's why I suggested doing it in Type.needsDestruction() first, to make that actually work without having to think about whether sema already happened (could cause existing bugs).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So if you don't wanna touch that function, please add a comment at least why this trait's implementation isn't the expected one-liner.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, added comment. It's not easy to call dsymbolSemantic in Type.needsDestruction because it doesn't have the Scope.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right...

return tsa.next.needsDestruction();
return _this.size() && tsa.next.needsDestruction();
else if (auto ts = _this.isTypeStruct())
return ts.sym.dtor !is null;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the semantic call should be inserted here, otherwise needsDestruction() might silently return false before the struct gets dsymbolsema.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to undo this change. A refactor could be done after this PR.

Comment thread compiler/src/dmd/typesem.d Outdated
{
if (auto tsa = _this.isTypeSArray())
return tsa.next.needsDestruction();
return _this.size() && tsa.next.needsDestruction();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Computing the size might involve extra sema IIRC, so I'd probably swap the operands, to only add a later check which holds in 99.999% of cases.

@kinke kinke Feb 25, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, and as this is a recursive check similar to your first version, we could simply check the tsa.dim for 0.

@kinke kinke changed the title Add __traits(hasElaborateDestructor, T) Add __traits(needsDestruction, T) Feb 25, 2026

@thewilsonator thewilsonator left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this iPR good to go?

@ntrel

ntrel commented Feb 28, 2026

Copy link
Copy Markdown
Contributor Author

@thewilsonator I think so, I added a comment that Martin asked for.

@thewilsonator
thewilsonator merged commit a118d9c into dlang:master Feb 28, 2026
31 of 38 checks passed
thewilsonator pushed a commit to dlang/dlang.org that referenced this pull request Mar 2, 2026
gulugulubing pushed a commit to gulugulubing/dmd that referenced this pull request Mar 15, 2026
ntrel added a commit to ntrel/phobos that referenced this pull request Jul 4, 2026
thewilsonator pushed a commit to dlang/phobos that referenced this pull request Jul 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[REG master] hasElaborateDestructor fails with private destructor

5 participants