From: Arthur Cohen Date: Fri, 12 May 2023 13:14:49 +0000 (+0200) Subject: gccrs: derive: Add proper derive(Clone) for unions X-Git-Tag: basepoints/gcc-15~2499 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c566f9081067c9328a0f07bcc8bac46dc69d1599;p=thirdparty%2Fgcc.git gccrs: derive: Add proper derive(Clone) for unions gcc/rust/ChangeLog: * ast/rust-ast-builder.cc (AstBuilder::struct_expr_struct): New function. (AstBuilder::let): Likewise. (AstBuilder::struct_expr): Likewise. (AstBuilder::struct_expr_field): Likewise. (AstBuilder::field_access): Likewise. (AstBuilder::wildcard): Likewise. * ast/rust-ast-builder.h: Likewise. * expand/rust-derive-clone.cc (DeriveClone::visit_union): Implement properly. gcc/testsuite/ChangeLog: * rust/compile/derive_macro4.rs: New test. * rust/compile/derive_macro6.rs: New test. --- diff --git a/gcc/rust/ast/rust-ast-builder.cc b/gcc/rust/ast/rust-ast-builder.cc index d37f143ca34a..b630cfa53920 100644 --- a/gcc/rust/ast/rust-ast-builder.cc +++ b/gcc/rust/ast/rust-ast-builder.cc @@ -81,13 +81,6 @@ AstBuilder::path_in_expression (std::vector &&segments) return PathInExpression (std::move (path_segments), {}, loc); } -std::unique_ptr -AstBuilder::struct_expr_struct (std::string struct_name) -{ - return std::unique_ptr ( - new StructExprStruct (path_in_expression ({struct_name}), {}, {}, loc)); -} - std::unique_ptr AstBuilder::block (std::vector> &&stmts, std::unique_ptr &&tail_expr) @@ -100,9 +93,9 @@ std::unique_ptr AstBuilder::let (std::unique_ptr pattern, std::unique_ptr type, std::unique_ptr init) { - return std::unique_ptr ( - new LetStmt (/* needs a pattern here, not just a name */ nullptr, - std::move (init), std::move (type), {}, loc)); + return std::unique_ptr (new LetStmt (std::move (pattern), + std::move (init), std::move (type), + {}, loc)); } std::unique_ptr @@ -118,6 +111,13 @@ AstBuilder::deref (std::unique_ptr &&of) return std::unique_ptr (new DereferenceExpr (std::move (of), {}, loc)); } +std::unique_ptr +AstBuilder::struct_expr_struct (std::string struct_name) +{ + return std::unique_ptr ( + new StructExprStruct (path_in_expression ({struct_name}), {}, {}, loc)); +} + std::unique_ptr AstBuilder::struct_expr (std::string struct_name, std::vector> &&fields) @@ -142,5 +142,11 @@ AstBuilder::field_access (std::unique_ptr &&instance, std::string field) new FieldAccessExpr (std::move (instance), field, {}, loc)); } +std::unique_ptr +AstBuilder::wildcard () +{ + return std::unique_ptr (new WildcardPattern (loc)); +} + } // namespace AST } // namespace Rust diff --git a/gcc/rust/ast/rust-ast-builder.h b/gcc/rust/ast/rust-ast-builder.h index 1c524b438d83..524b3905bbda 100644 --- a/gcc/rust/ast/rust-ast-builder.h +++ b/gcc/rust/ast/rust-ast-builder.h @@ -100,6 +100,9 @@ public: std::unique_ptr field_access (std::unique_ptr &&instance, std::string field); + /* Create a wildcard pattern (`_`) */ + std::unique_ptr wildcard (); + private: /** * Location of the generated AST nodes diff --git a/gcc/rust/expand/rust-derive-clone.cc b/gcc/rust/expand/rust-derive-clone.cc index 176cee9b7497..8529bac50193 100644 --- a/gcc/rust/expand/rust-derive-clone.cc +++ b/gcc/rust/expand/rust-derive-clone.cc @@ -155,8 +155,29 @@ DeriveClone::visit_enum (Enum &item) void DeriveClone::visit_union (Union &item) { - rust_sorry_at (item.get_locus (), "cannot derive %qs for these items yet", - "Clone"); + // FIXME: Should be $crate::core::clone::AssertParamIsCopy (or similar) + + // + auto arg = GenericArg::create_type (builder.single_type_path ("Self")); + + // AssertParamIsCopy:: + auto type = std::unique_ptr ( + new TypePathSegmentGeneric (PathIdentSegment ("AssertParamIsCopy", loc), + false, GenericArgs ({}, {arg}, {}, loc), loc)); + auto type_paths = std::vector> (); + type_paths.emplace_back (std::move (type)); + + auto full_path + = std::unique_ptr (new TypePath ({std::move (type_paths)}, loc)); + + auto stmts = std::vector> (); + stmts.emplace_back ( + builder.let (builder.wildcard (), std::move (full_path), nullptr)); + auto tail_expr = builder.deref (builder.identifier ("self")); + + auto block = builder.block (std::move (stmts), std::move (tail_expr)); + + expanded = clone_impl (clone_fn (std::move (block)), item.get_identifier ()); } } // namespace AST diff --git a/gcc/testsuite/rust/compile/derive_macro4.rs b/gcc/testsuite/rust/compile/derive_macro4.rs new file mode 100644 index 000000000000..564555f896f2 --- /dev/null +++ b/gcc/testsuite/rust/compile/derive_macro4.rs @@ -0,0 +1,16 @@ +pub trait Copy {} +pub trait Clone { + fn clone(&self) -> Self; +} + +struct PhantomData; + +pub struct AssertParamIsCopy { + _field: PhantomData, +} + +#[derive(Clone)] // { dg-error "bounds not satisfied for U .Copy. is not satisfied" } +union U { + i: i32, + f: f64, +} diff --git a/gcc/testsuite/rust/compile/derive_macro6.rs b/gcc/testsuite/rust/compile/derive_macro6.rs new file mode 100644 index 000000000000..0254754f7aec --- /dev/null +++ b/gcc/testsuite/rust/compile/derive_macro6.rs @@ -0,0 +1,21 @@ +pub trait Copy {} +pub trait Clone { + fn clone(&self) -> Self; +} + +#[lang = "phantom_data"] +pub struct PhantomData; + +pub struct AssertParamIsCopy { + pub _field: PhantomData, +} + +impl Copy for i32 {} +impl Copy for i64 {} +impl Copy for U {} + +#[derive(Clone)] +union U { + i: i32, + f: f64, +}