Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Issue 23182 - Can't assign struct with opAssign to SumType in CTFE #8474

Merged
merged 1 commit into from Jun 14, 2022

Conversation

pbackus
Copy link
Contributor

@pbackus pbackus commented Jun 13, 2022

SumType.opAssign now avoids calling core.lifetime.move or
core.lifetime.forward during CTFE whenever possible.

SumType.opAssign now avoids calling core.lifetime.move or
core.lifetime.forward during CTFE whenever possible.
@dlang-bot
Copy link
Contributor

Thanks for your pull request and interest in making D better, @pbackus! 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
23182 normal Can't assign struct with opAssign to SumType in CTFE

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 + phobos#8474"

@dlang-bot dlang-bot merged commit a504a5e into dlang:master Jun 14, 2022
static if (isCopyable!(typeof(value)))
{
// Workaround for https://issues.dlang.org/show_bug.cgi?id=21542
this = __ctfe ? value : move(value);
Copy link
Contributor

@nordlow nordlow Jun 14, 2022

Choose a reason for hiding this comment

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

What about also adding a CTFE-version of move being executable at compile-time by replacing the memcpy with an void[]-copy for the __ctfe-case? Ping, @RazvanN7.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Doing a proper move in CTFE is impossible, because CTFE does not support reinterpreting casts (which includes casting to void[]). See PR #936 for related discussion. The best that can be done is to fall back to a copy, as is done here.

Copy link
Contributor

@nordlow nordlow Jun 14, 2022

Choose a reason for hiding this comment

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

Afaict, the core of this limitation is rather that the compiler is lacking the ability to automatically (internally) perform a move of value into this at

this = value;

as this statment is the last statement that references value before this is returned. The are more such cornercases where the compiler should move (but currently doesn't) thereby avoiding these static if-branchings on isCopyable!T. Afaict, if the compiler had that ability that would unlock being able to use non-copyable types at CTFE at least in situations like these. Ping, @andralex @RazvanN7 @WalterBright.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, if the compiler could automatically turn these last-use assignments into moves, the explicit calls to move and forward would no longer be necessary in the first place, so there would be no need to add workarounds to avoid calling them in CTFE.

Copy link
Contributor

@nordlow nordlow Jun 14, 2022

Choose a reason for hiding this comment

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

Indeed. That would be awesome. Moreover, there are many Phobos' ranges that would be able to operate on non-copyable source range types if that restriction was lifted.

pbackus added a commit to pbackus/sumtype that referenced this pull request Jun 21, 2022
SumType.opAssign now avoids calling core.lifetime.move or
core.lifetime.forward during CTFE whenever possible.

Dlang issue: https://issues.dlang.org/show_bug.cgi?id=23182
Phobos PR: dlang/phobos#8474
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants