Skip to content

Conversation

@powerboat9
Copy link
Collaborator

This patch adds a wrapper class called Cloneable, which can be used to automatically perform polymorphic copying. This should greatly reduce the need for error-prone user defined copy constructors and assignment operators.

As a demonstration/first step, this patch also uses Cloneable to simplify the pattern nodes described in ast/rust-pattern.h.

@powerboat9
Copy link
Collaborator Author

powerboat9 commented Jan 3, 2026

(this is partially yak shaving related to #4323, partially a follow up on #1914)

This patch adds a wrapper class called Cloneable, which can be used to
automatically perform polymorphic copying. This should greatly reduce
the need for error-prone user defined copy constructors and assignment
operators.

As a demonstration/first step, this patch also uses Cloneable to
simplify the pattern nodes described in gcc/rust/ast/rust-pattern.h.

gcc/rust/ChangeLog:

	* ast/rust-ast.h: Include "rust-cloneable.h".
	(struct CloneableDelegate<std::unique_ptr<AST::Pattern>>): Add
	specialization.
	* ast/rust-pattern.cc (IdentifierPattern::as_string): Handle
	field type changes.
	(RangePattern::as_string): Likewise.
	(ReferencePattern::as_string): Likewise.
	(StructPatternFieldTuplePat::as_string): Likewise.
	(StructPatternFieldIdentPat::as_string): Likewise.
	(StructPatternElements::as_string): Likewise.
	(TupleStructItemsNoRest::as_string): Likewise.
	(TupleStructItemsHasRest::as_string): Likewise.
	(TupleStructPattern::as_string): Likewise.
	(TuplePatternItemsNoRest::as_string): Likewise.
	(TuplePatternItemsHasRest::as_string): Likewise.
	(TuplePattern::as_string): Likewise.
	(SlicePatternItemsNoRest::as_string): Likewise.
	(SlicePatternItemsHasRest::as_string): Likewise.
	(SlicePattern::as_string): Likewise.
	(AltPattern::as_string): Likewise.
	* ast/rust-pattern.h: Include "rust-cloneable.h".
	(class IdentifierPattern): Use Cloneable to simplify copying.
	(class RangePattern): Likewise.
	(class ReferencePattern): Likewise.
	(class StructPatternFieldTuplePat): Likewise.
	(class StructPatternFieldIdentPat): Likewise.
	(class StructPatternElements): Likewise.
	(class TupleStructItemsNoRest): Likewise.
	(class TupleStructItemsHasRest): Likewise.
	(class TupleStructPattern): Likewise.
	(class TuplePatternItemsNoRest): Likewise.
	(class TuplePatternItemsHasRest): Likewise.
	(class TuplePattern): Likewise.
	(class GroupedPattern): Likewise.
	(class SlicePatternItemsNoRest): Likewise.
	(class SlicePatternItemsHasRest): Likewise.
	(class SlicePattern): Likewise.
	(class AltPattern): Likewise.
	(struct
	CloneableDelegate<std::unique_ptr<AST::RangePatternBound>>): Add
	specialization.
	(struct
	CloneableDelegate<std::unique_ptr<AST::TupleStructItems>>):
	Likewise.
	(struct
	CloneableDelegate<std::unique_ptr<AST::TuplePatternItems>>):
	Likewise.
	(struct
	CloneableDelegate<std::unique_ptr<AST::SlicePatternItems>>):
	Likewise.
	(struct
	CloneableDelegate<std::unique_ptr<AST::StructPatternField>>):
	Likewise.
	* util/rust-cloneable.h: New file.

Signed-off-by: Owen Avery <[email protected]>
@CohenArthur
Copy link
Member

I haven't really looked at the PR yet @powerboat9, does it tie in with something like #3799?

@powerboat9
Copy link
Collaborator Author

Not really, though it would be compatible. However, on a side note, #4323 does tie in/replace the reconstruct mechanism. This PR should provide a way to remove virtually all user defined copy constructors/assignment operators.

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.

2 participants