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

Finish derive(Clone) for enums #3343

Merged
merged 15 commits into from
Jan 20, 2025

Conversation

CohenArthur
Copy link
Member

@CohenArthur CohenArthur commented Jan 3, 2025

This PR changes how lang item paths are handled and subsequently finishes the implementation of derive(Clone) for enums and their variants. Putting it as a draft since it's a mess and needs cleaning up. Addresses #2996, but we still need to properly implement generic Clone I think?

I might instead split this up in multiple PRs to first refactor the lang item paths and then finish implementing derive(Clone) on top of it - next week.

Needs #3366

@CohenArthur CohenArthur force-pushed the finish-derive-clone branch 2 times, most recently from dc29897 to d953618 Compare January 3, 2025 16:32
@CohenArthur CohenArthur force-pushed the finish-derive-clone branch 2 times, most recently from 2087be9 to f9a15c5 Compare January 15, 2025 12:29
@CohenArthur CohenArthur force-pushed the finish-derive-clone branch 2 times, most recently from d1f38dd to b6104c1 Compare January 20, 2025 11:05
gcc/testsuite/ChangeLog:

	* rust/compile/derive_macro1.rs: Add #[lang = "clone"] to Clone trait.
	* rust/compile/derive_macro3.rs: Likewise.
	* rust/compile/derive_macro6.rs: Likewise.
	* rust/execute/torture/derive_macro3.rs: Likewise.
gcc/rust/ChangeLog:

	* ast/rust-ast-builder.cc (Builder::struct_struct): New function.
	* ast/rust-ast-builder.h (vec): New function.
gcc/rust/ChangeLog:

	* expand/rust-derive-clone.cc (DeriveClone::visit_union): Manually generate
	the struct used for asserting a union implements Copy.
gcc/testsuite/ChangeLog:

	* rust/compile/derive_macro4.rs: Make PhantomData a lang item.
gcc/rust/ChangeLog:

	* expand/rust-derive-copy.cc: Use lang item path.
gcc/rust/ChangeLog:

	* ast/rust-ast-builder.cc: New functions.
	* ast/rust-ast-builder.h: Declare them.
gcc/rust/ChangeLog:

	* expand/rust-derive-clone.cc (DeriveClone::visit_union): Create a lang item path
	instead of a regular path.
gcc/rust/ChangeLog:

	* expand/rust-derive-clone.cc (DeriveClone::clone_call): Mention using `clone_fn`
	lang item in the future.
gcc/testsuite/ChangeLog:

	* rust/compile/derive_macro4.rs: Mark Copy and Clone as lang items.
gcc/rust/ChangeLog:

	* expand/rust-derive-clone.cc: Clone enum identifier variants properly
	* expand/rust-derive-clone.h: Declare new functions used.
gcc/rust/ChangeLog:

	* ast/rust-ast-builder.cc: Add new methods for constructing struct exprs.
	* ast/rust-ast-builder.h: Mention how to build tuple expressions.
gcc/rust/ChangeLog:

	* expand/rust-derive-clone.cc (DeriveClone::variant_match_path): New function.
	(DeriveClone::clone_enum_identifier): Rename.
	(DeriveClone::clone_enum_tuple): New function.
	(DeriveClone::visit_enum): Visit tuple variants properly.
	* expand/rust-derive-clone.h: Declare new functions.
gcc/rust/ChangeLog:

	* expand/rust-derive-clone.cc (DeriveClone::clone_enum_struct): New function for deriving
	enum struct variants.
	(DeriveClone::visit_enum): Call into the new function.

gcc/testsuite/ChangeLog:

	* rust/compile/nr2/exclude:
	* rust/compile/derive_clone_enum1.rs: New test.
	* rust/compile/derive_clone_enum2.rs: New test.
	* rust/compile/derive_clone_enum3.rs: New test.
	* rust/execute/torture/derive_clone_enum1.rs: New test.
gcc/testsuite/ChangeLog:

	* rust/compile/nr2/exclude: Add failing lang item typepaths tests.
	* rust/execute/torture/derive_macro4.rs: Mark Clone as lang item.
gcc/rust/ChangeLog:

	* resolve/rust-late-name-resolver-2.0.cc (Late::visit): Improve formatting.
@CohenArthur CohenArthur force-pushed the finish-derive-clone branch 2 times, most recently from b01380b to 49f5d2d Compare January 20, 2025 11:18
gcc/rust/expand/rust-derive-clone.cc Show resolved Hide resolved
gcc/rust/expand/rust-derive-clone.cc Show resolved Hide resolved
gcc/rust/expand/rust-derive-clone.cc Show resolved Hide resolved
gcc/rust/expand/rust-derive-clone.cc Show resolved Hide resolved
Comment on lines +30 to +51
template <typename T>
std::vector<std::unique_ptr<T>>
vec (std::unique_ptr<T> &&t)
{
auto v = std::vector<std::unique_ptr<T>> ();

v.emplace_back (std::move (t));

return v;
}

template <typename T>
std::vector<std::unique_ptr<T>>
vec (std::unique_ptr<T> &&t1, std::unique_ptr<T> &&t2)
{
auto v = std::vector<std::unique_ptr<T>> ();

v.emplace_back (std::move (t1));
v.emplace_back (std::move (t2));

return v;
}
Copy link
Member Author

Choose a reason for hiding this comment

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

this should probably be just one variadic function?

@CohenArthur CohenArthur marked this pull request as ready for review January 20, 2025 11:31
@CohenArthur CohenArthur enabled auto-merge January 20, 2025 11:42
@CohenArthur CohenArthur added this pull request to the merge queue Jan 20, 2025
Merged via the queue into Rust-GCC:master with commit 9c6e09e Jan 20, 2025
12 checks passed
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.

1 participant