Repository navigation
Add __traits(needsDestruction, T) - #22616
Conversation
|
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 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 referencesYour 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 locallyIf you don't have a local development environment setup, you can use Digger to test this PR: dub run digger -- build "master + dmd#22616" |
| t = tsa.next; | ||
| continue; | ||
| } | ||
| } |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
see the circleCI failure
| } | ||
| } | ||
| // Used by std.traits.hasElaborateDestructor | ||
| // TODO inline this in druntime |
There was a problem hiding this comment.
It means to remove the internal template and replace all uses of it with the new __traits expression.
There was a problem hiding this comment.
[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.
| // 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(); |
There was a problem hiding this comment.
| // 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(); |
There was a problem hiding this comment.
That does not work, it doesn't call dsymbolSemantic first.
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
OK, added comment. It's not easy to call dsymbolSemantic in Type.needsDestruction because it doesn't have the Scope.
| return tsa.next.needsDestruction(); | ||
| return _this.size() && tsa.next.needsDestruction(); | ||
| else if (auto ts = _this.isTypeStruct()) | ||
| return ts.sym.dtor !is null; |
There was a problem hiding this comment.
I guess the semantic call should be inserted here, otherwise needsDestruction() might silently return false before the struct gets dsymbolsema.
There was a problem hiding this comment.
I'm going to undo this change. A refactor could be done after this PR.
| { | ||
| if (auto tsa = _this.isTypeSArray()) | ||
| return tsa.next.needsDestruction(); | ||
| return _this.size() && tsa.next.needsDestruction(); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Oh, and as this is a recursive check similar to your first version, we could simply check the tsa.dim for 0.
__traits(hasElaborateDestructor, T)__traits(needsDestruction, T)
Co-authored-by: Martin Kinkelin <[email protected]>
This reverts commit ae38c94.
thewilsonator
left a comment
There was a problem hiding this comment.
Is this iPR good to go?
|
@thewilsonator I think so, I added a comment that Martin asked for. |
Trait introduced in dlang/dmd#22616.
Trait introduced in dlang/dmd#22616.
Fixes #22609.
Code adapted from
isCopyabletrait - @kinke