-
-
Notifications
You must be signed in to change notification settings - Fork 644
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
Ensure Move Constructor Requires Copy Constructor (#20942) #21041
base: master
Are you sure you want to change the base?
Conversation
Thanks for your pull request and interest in making D better, @khushishukla2813! 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#21041" |
I’ve added a check to ensure that a move constructor is only allowed if a copy constructor is also declared, as per the D language specification. I’ve verified the changes locally by creating test cases and updating the build makefile.... but I’m encountering CI failures. |
This is said to be a breaking change, and that's why it wasn't in the first round. My suggestion was to deprecate that case in the future, but move ctors should get some exercise before that change. |
@TurkeyMan Umm got it! but... maybe we could start with a warning instead of an immediate error.. That way existing code still runs but devs get a heads-up to fix things before it becomes a hard rule.. |
@rikkimax Thanks for pointing that out! I checked it but ran into a mess, so I ended up doing manual testing instead. What do you think would be the best approach here adjusting the check in my PR or updating the existing code to align with the spec! |
100% agree with you! I suggested a depreciation warning for the rogue move constructors. (I reckon they're probably actually just wild bugs) That said, I do see some sense in getting move semantics somewhat working and proven before nagging people to adapt their code. |
I agree with Manu about doing a deprecation. You will also need to fix phobos so it passes, in a separate PR. Until then we won't know how bad the situation is in the ecosystem since the buildkite tests are currently not running due to Phobos. |
This PR addresses the issue where a move constructor is silently ignored if no copy constructor is declared. According to the D specification, when a move constructor is declared, a copy constructor should also be present. However, this requirement was not enforced in the compiler, leading to unexpected behavior.
Changes & Fixes:
Added an explicit error message:
If a move constructor is declared without a corresponding copy constructor, the compiler now generates an error:
error(this.loc, "Move constructor declared without a corresponding copy constructor.");
This prevents silent failures and ensures the specification is followed.
Updated hasCopyConstruction function:
Previously, hasCopyConstruction() only checked for postblit || hasCopyCtor.
Now, it also considers hasMoveCtor, ensuring that the presence of a move constructor triggers the necessary checks.
Removed the commented-out condition that was preventing proper enforcement:
The check hasMoveCtorLocal && !hasCpCtorLocal was previously commented out to avoid build failures in vibe.d.
Instead of silently ignoring the move constructor, this PR explicitly enforces the requirement while preserving compatibility.
How This Fixes the Issue:
Ensures that a move constructor is not ignored without a copy constructor, aligning with the D language specification.
Provides a clear error message to guide developers in properly defining their struct constructors.
Improves compiler correctness and prevents unintended behavior when move semantics are used.
This PR ensures consistency in constructor handling and prevents silent issues in struct initialization.