Skip to content

Commit

Permalink
rust: Add support for Clone and Copy derive on generic types
Browse files Browse the repository at this point in the history
When we generate derivations for Copy and Clone we need to make sure
the associated impl block sets up the generic parameters and arguments
correctly. This patch introduces the framework to copy chunks of the AST
because we need to make sure these new AST nodes have their own associated
id, calling clone on the nodes will just confuse name-resolution and
subsequent mappings.

Fixes #3139

gcc/rust/ChangeLog:

	* Make-lang.in: new objects
	* ast/rust-ast-builder.cc (Builder::generic_type_path_segment): new helper
	(Builder::single_generic_type_path): likewise
	(Builder::new_type): likewise
	(Builder::new_lifetime_param): likewise
	(Builder::new_type_param): likewise
	(Builder::new_lifetime): likewise
	(Builder::new_generic_args): likewise
	* ast/rust-ast-builder.h: new helper decls
	* ast/rust-ast.h: new const getters
	* ast/rust-path.h: likewise
	* ast/rust-type.h: likewise
	* expand/rust-derive-clone.cc (DeriveClone::clone_impl): take the types generics
	(DeriveClone::visit_tuple): likewise
	(DeriveClone::visit_struct): likewise
	(DeriveClone::visit_union): likewise
	* expand/rust-derive-clone.h: update header
	* expand/rust-derive-copy.cc (DeriveCopy::copy_impl): similarly take type generics
	(DeriveCopy::visit_struct): likewise
	(DeriveCopy::visit_tuple): likewise
	(DeriveCopy::visit_enum): likewise
	(DeriveCopy::visit_union): likewise
	* expand/rust-derive-copy.h: likewse
	* ast/rust-ast-builder-base.cc: New file.
	* ast/rust-ast-builder-base.h: New file.
	* ast/rust-ast-builder-type.cc: New file.
	* ast/rust-ast-builder-type.h: New file.

gcc/testsuite/ChangeLog:

	* rust/compile/issue-3139-1.rs: New test.
	* rust/compile/issue-3139-2.rs: New test.
	* rust/compile/issue-3139-3.rs: New test.
	* rust/compile/nr2/exclude: these all break nr2
  • Loading branch information
philberty committed Sep 20, 2024
1 parent 522576d commit b4ac492
Show file tree
Hide file tree
Showing 18 changed files with 1,560 additions and 22 deletions.
2 changes: 2 additions & 0 deletions gcc/rust/Make-lang.in
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ GRS_OBJS = \
rust/rust-cfg-strip.o \
rust/rust-expand-visitor.o \
rust/rust-ast-builder.o \
rust/rust-ast-builder-base.o \
rust/rust-ast-builder-type.o \
rust/rust-derive.o \
rust/rust-derive-clone.o \
rust/rust-derive-copy.o \
Expand Down
Loading

0 comments on commit b4ac492

Please sign in to comment.