Skip to content

Translate _d_delstruct to template - #13398

Merged
RazvanN7 merged 10 commits into
dlang:masterfrom
teodutu:convert-_d_delstruct-hook
Dec 20, 2021
Merged

RazvanN7 merged 10 commits into
dlang:masterfrom
teodutu:convert-_d_delstruct-hook

Conversation

@teodutu

@teodutu teodutu commented Dec 7, 2021

Copy link
Copy Markdown
Member

This PR translates the lowering of expressions like delete p to a template version of _d_delstruct that no longer uses the second TypeInfo_Struct parameter. The PR where this new template hook was implemented is this: dlang/druntime#3639

@teodutu
teodutu requested a review from ibuclaw as a code owner December 7, 2021 16:30
@dlang-bot

Copy link
Copy Markdown

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

Comment thread src/dmd/errors.d Resolved
Comment thread src/dmd/expressionsem.d Resolved
Comment thread src/dmd/expressionsem.d Resolved
Comment thread src/dmd/e2ir.d Outdated
* expression. This is how e2ir.d gets here.
* Since the lowering has already been made, the DeleteExp can
* be ignored here.
*/

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.

How is this going to affect the traceGC hooks? If you look below, there is a call to toTraceGC which basically calls a wrapper of the hook if the gc_profile command line flag is passed. It seems that in this case, the wrapper call is ignored.

@teodutu
teodutu force-pushed the convert-_d_delstruct-hook branch from 3d0c446 to 94803a6 Compare December 8, 2021 09:14
@teodutu
teodutu force-pushed the convert-_d_delstruct-hook branch 4 times, most recently from 683ce63 to 32a4d4f Compare December 14, 2021 17:46

@RazvanN7 RazvanN7 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.

Looks good to me

Comment thread src/dmd/expressionsem.d Outdated
* `_d_delstruct` in order to keep checking for dtor errors as
* well as generate code for the call to `_d_delstruct`.
*/
e = Expression.combine(exp, e);

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.

Why do we still need the original expression and pass it all the way to e2ir? Can't we simply check here if the dtor is callable in the current context and output an error if not?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I tried to do this, but certain errors are output from different contexts, which are difficult to replicate here.

Here's an example: this snippet [1] may be output an error if the dtor throws. In order to verify this, canThrow is called here [2]. _d_delstruct(s) is nothrow so that it's not mentioned in errors such as this one [3].

I agree that returning both expressions is inelegant, but I don't see a way to avoid the situation above without duplicating the logic from semantic3.d in expressionsem.d.

[1]

dmd/src/dmd/semantic3.d

Lines 747 to 750 in 93108bb

// Check for errors related to 'nothrow'.
const blockexit = funcdecl.fbody.blockExit(funcdecl, f.isnothrow);
if (f.isnothrow && blockexit & BE.throw_)
error(funcdecl.loc, "`nothrow` %s `%s` may throw", funcdecl.kind(), funcdecl.toPrettyChars());

[2]

dmd/src/dmd/blockexit.d

Lines 113 to 114 in 93108bb

if (canThrow(s.exp, func, mustNotThrow))
result |= BE.throw_;

[3]

dmd/src/dmd/canthrow.d

Lines 58 to 64 in 93108bb

if (mustNotThrow)
{
e.error("%s `%s` is not `nothrow`",
f.kind(), f.toPrettyChars());
e.checkOverridenDtor(null, f, dd => dd.type.toTypeFunction().isnothrow, "not nothrow");
}

@teodutu
teodutu force-pushed the convert-_d_delstruct-hook branch from 32a4d4f to 9b25792 Compare December 16, 2021 16:47
Comment thread src/dmd/frontend.h Resolved
@teodutu
teodutu force-pushed the convert-_d_delstruct-hook branch from 9b25792 to 45e22ef Compare December 16, 2021 16:54

@RazvanN7 RazvanN7 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.

Does not need to recreate the DeleteExp.

Comment thread src/dmd/canthrow.d Outdated
// In expressionsem.d `delete s` was lowered to `_d_delstruct(s)`.
// The following code rewrites it back to the original expresion
// in order to properly output `nothrow` errors.
auto de = new DeleteExp(ce.loc, (*ce.arguments)[0], false);

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.

Creating a DeleteExp is superfluous here. This is wasteful because allocation is costly. You can simply check if the dtor of of the aggregate type (if it exists) throws: this is essentially what the DeleteExp visiting method does.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I was trying to not duplicate the code in visit(DeleteExp), but I see your point.

Comment thread src/dmd/dinterpret.d Outdated
// interpret that expression.
assert(e.arguments.dim == 1);

auto de = new DeleteExp(e.loc, (*e.arguments)[0], false);

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.

ditto.

Comment thread src/dmd/nogc.d Outdated
// In expressionsem.d `delete s` was lowered to `_d_delstruct(s)`.
// The following code rewrites it back to the original expresion in
// order to properly output `nogc` errors.
auto de = new DeleteExp(e.loc, (*e.arguments)[0], false);

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.

ditto

@RazvanN7 RazvanN7 added the Merge:72h no objection -> merge The PR will be merged if there are no objections raised. label Dec 17, 2021
@RazvanN7

Copy link
Copy Markdown
Contributor

Great work, @teodutu ! Thanks for this. Let's wait a bit to see if someone else has anything to comment and then we'll merge.

@RazvanN7
RazvanN7 merged commit 5e757c8 into dlang:master Dec 20, 2021
@teodutu
teodutu deleted the convert-_d_delstruct-hook branch December 20, 2021 16:22
teodutu added a commit to teodutu/druntime that referenced this pull request May 26, 2022
These hooks have been converted to templates and their lowerings changed
to use those templates:
- dlang/dmd#13116
- dlang/dmd#13495
- dlang/dmd#13398

Signed-off-by: Teodor Dutu <[email protected]>
teodutu added a commit to teodutu/druntime that referenced this pull request May 26, 2022
These hooks have been converted to templates and their lowerings changed
to use those templates:
- dlang/dmd#13116
- dlang/dmd#13495
- dlang/dmd#13398

Signed-off-by: Teodor Dutu <[email protected]>
thewilsonator pushed a commit to dlang/druntime that referenced this pull request May 27, 2022
These hooks have been converted to templates and their lowerings changed
to use those templates:
- dlang/dmd#13116
- dlang/dmd#13495
- dlang/dmd#13398

Signed-off-by: Teodor Dutu <[email protected]>
kinke pushed a commit to ldc-developers/ldc that referenced this pull request Nov 25, 2022
These hooks have been converted to templates and their lowerings changed
to use those templates:
- dlang/dmd#13116
- dlang/dmd#13495
- dlang/dmd#13398

Signed-off-by: Teodor Dutu <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Merge:72h no objection -> merge The PR will be merged if there are no objections raised.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants