From: Arthur Cohen <arthur.co...@embecosm.com> gcc/rust/ChangeLog:
* expand/rust-derive-debug.cc: New file. * expand/rust-derive-debug.h: New file. * Make-lang.in: Compile them. * expand/rust-derive.cc (DeriveVisitor::derive): Call into DeriveDebug. gcc/testsuite/ChangeLog: * rust/compile/derive-debug1.rs: New test. * rust/compile/nr2/exclude: Exclude it. --- gcc/rust/Make-lang.in | 1 + gcc/rust/expand/rust-derive-debug.cc | 130 ++++++++++++++++++++ gcc/rust/expand/rust-derive-debug.h | 55 +++++++++ gcc/rust/expand/rust-derive.cc | 6 + gcc/testsuite/rust/compile/derive-debug1.rs | 41 ++++++ gcc/testsuite/rust/compile/nr2/exclude | 1 + 6 files changed, 234 insertions(+) create mode 100644 gcc/rust/expand/rust-derive-debug.cc create mode 100644 gcc/rust/expand/rust-derive-debug.h create mode 100644 gcc/testsuite/rust/compile/derive-debug1.rs diff --git a/gcc/rust/Make-lang.in b/gcc/rust/Make-lang.in index f317c678359..bc58a341131 100644 --- a/gcc/rust/Make-lang.in +++ b/gcc/rust/Make-lang.in @@ -96,6 +96,7 @@ GRS_OBJS = \ rust/rust-derive.o \ rust/rust-derive-clone.o \ rust/rust-derive-copy.o \ + rust/rust-derive-debug.o \ rust/rust-proc-macro.o \ rust/rust-macro-invoc-lexer.o \ rust/rust-proc-macro-invoc-lexer.o \ diff --git a/gcc/rust/expand/rust-derive-debug.cc b/gcc/rust/expand/rust-derive-debug.cc new file mode 100644 index 00000000000..910f27c67b2 --- /dev/null +++ b/gcc/rust/expand/rust-derive-debug.cc @@ -0,0 +1,130 @@ +// Copyright (C) 2025 Free Software Foundation, Inc. + +// This file is part of GCC. + +// GCC is free software; you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free +// Software Foundation; either version 3, or (at your option) any later +// version. + +// GCC is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +// for more details. + +// You should have received a copy of the GNU General Public License +// along with GCC; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +#include "rust-derive-debug.h" +#include "rust-ast.h" +#include "rust-hir-map.h" +#include "rust-system.h" + +namespace Rust { +namespace AST { + +DeriveDebug::DeriveDebug (location_t loc) + : DeriveVisitor (loc), expanded (nullptr) +{} + +std::unique_ptr<Item> +DeriveDebug::go (Item &item) +{ + item.accept_vis (*this); + + rust_assert (expanded); + + return std::move (expanded); +} + +/* Pointer-ify something */ +template <typename T> +static std::unique_ptr<T> +ptrify (T value) +{ + return std::unique_ptr<T> (new T (value)); +} + +std::unique_ptr<AssociatedItem> +DeriveDebug::stub_debug_fn () +{ + auto unit_expr = builder.tuple (); + auto ok_expr + = ptrify (builder.path_in_expression (LangItem::Kind::RESULT_OK)); + + auto stub_return = builder.call (std::move (ok_expr), std::move (unit_expr)); + + // we can't use builder.block() here as it returns a unique_ptr<Expr> and + // Function's constructor expects a unique_ptr<BlockExpr> + auto block = std::unique_ptr<BlockExpr> ( + new BlockExpr ({}, std::move (stub_return), {}, {}, + AST::LoopLabel::error (), loc, loc)); + + auto self = builder.self_ref_param (); + + auto return_type + = ptrify (builder.type_path ({"core", "fmt", "Result"}, true)); + + auto mut_fmt_type_inner + = ptrify (builder.type_path ({"core", "fmt", "Formatter"}, true)); + + auto mut_fmt_type + = builder.reference_type (std::move (mut_fmt_type_inner), true); + + auto fmt = builder.function_param (builder.identifier_pattern ("_fmt"), + std::move (mut_fmt_type)); + + auto params = vec (std::move (self), std::move (fmt)); + + auto function = builder.function ({"fmt"}, std::move (params), + std::move (return_type), std::move (block)); + + return ptrify (function); +} + +std::unique_ptr<Item> +DeriveDebug::stub_derive_impl ( + std::string name, + const std::vector<std::unique_ptr<GenericParam>> &type_generics) +{ + auto trait_items = vec (stub_debug_fn ()); + + auto debug = builder.type_path ({"core", "fmt", "Debug"}, true); + auto generics + = setup_impl_generics (name, type_generics, builder.trait_bound (debug)); + + return builder.trait_impl (debug, std::move (generics.self_type), + std::move (trait_items), + std::move (generics.impl)); +} + +void +DeriveDebug::visit_struct (StructStruct &struct_item) +{ + expanded = stub_derive_impl (struct_item.get_identifier ().as_string (), + struct_item.get_generic_params ()); +} + +void +DeriveDebug::visit_tuple (TupleStruct &tuple_item) +{ + expanded = stub_derive_impl (tuple_item.get_identifier ().as_string (), + tuple_item.get_generic_params ()); +} + +void +DeriveDebug::visit_enum (Enum &enum_item) +{ + expanded = stub_derive_impl (enum_item.get_identifier ().as_string (), + enum_item.get_generic_params ()); +} + +void +DeriveDebug::visit_union (Union &enum_item) +{ + rust_error_at (loc, "derive(Debug) cannot be derived for unions"); +} + +} // namespace AST +} // namespace Rust diff --git a/gcc/rust/expand/rust-derive-debug.h b/gcc/rust/expand/rust-derive-debug.h new file mode 100644 index 00000000000..14af89e551c --- /dev/null +++ b/gcc/rust/expand/rust-derive-debug.h @@ -0,0 +1,55 @@ +// Copyright (C) 2025 Free Software Foundation, Inc. + +// This file is part of GCC. + +// GCC is free software; you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free +// Software Foundation; either version 3, or (at your option) any later +// version. + +// GCC is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +// for more details. + +// You should have received a copy of the GNU General Public License +// along with GCC; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +#ifndef RUST_DERIVE_DEBUG_H +#define RUST_DERIVE_DEBUG_H + +#include "rust-derive.h" +#include "rust-ast.h" + +namespace Rust { +namespace AST { + +// This derive is currently incomplete and only generate a stub implementation +// which does not do any debug formatting +class DeriveDebug : DeriveVisitor +{ +public: + DeriveDebug (location_t loc); + + std::unique_ptr<Item> go (Item &); + +private: + std::unique_ptr<Item> expanded; + + std::unique_ptr<AssociatedItem> stub_debug_fn (); + + std::unique_ptr<Item> stub_derive_impl ( + std::string name, + const std::vector<std::unique_ptr<GenericParam>> &type_generics); + + virtual void visit_struct (StructStruct &struct_item) override; + virtual void visit_tuple (TupleStruct &tuple_item) override; + virtual void visit_enum (Enum &enum_item) override; + virtual void visit_union (Union &enum_item) override; +}; + +} // namespace AST +} // namespace Rust + +#endif // ! RUST_DERIVE_DEBUG_H diff --git a/gcc/rust/expand/rust-derive.cc b/gcc/rust/expand/rust-derive.cc index 13675ba594c..2c5ccc3ce52 100644 --- a/gcc/rust/expand/rust-derive.cc +++ b/gcc/rust/expand/rust-derive.cc @@ -19,6 +19,7 @@ #include "rust-derive.h" #include "rust-derive-clone.h" #include "rust-derive-copy.h" +#include "rust-derive-debug.h" namespace Rust { namespace AST { @@ -38,6 +39,11 @@ DeriveVisitor::derive (Item &item, const Attribute &attr, case BuiltinMacro::Copy: return DeriveCopy (attr.get_locus ()).go (item); case BuiltinMacro::Debug: + rust_warning_at ( + attr.get_locus (), 0, + "derive(Debug) is not fully implemented yet and has no effect - only a " + "stub implementation will be generated"); + return DeriveDebug (attr.get_locus ()).go (item); case BuiltinMacro::Default: case BuiltinMacro::Eq: case BuiltinMacro::PartialEq: diff --git a/gcc/testsuite/rust/compile/derive-debug1.rs b/gcc/testsuite/rust/compile/derive-debug1.rs new file mode 100644 index 00000000000..2596a3792f1 --- /dev/null +++ b/gcc/testsuite/rust/compile/derive-debug1.rs @@ -0,0 +1,41 @@ +#[lang = "sized"] +trait Sized {} + +mod core { + pub mod result { + pub enum Result<T, E> { + #[lang = "Ok"] + Ok(T), + #[lang = "Err"] + Err(E), + } + } + + mod fmt { + struct Formatter; // { dg-warning "is never constructed" } + struct Error; // { dg-warning "is never constructed" } + + type Result = core::result::Result<(), Error>; + + trait Debug { + fn fmt(&self, fmt: &mut Formatter) -> Result; + } + } +} + +#[derive(Debug)] // { dg-warning "unused name" } +// { dg-warning "stub implementation" "" { target *-*-* } .-1 } +struct Foo { a: i32, b: i64 } // { dg-warning "is never constructed" } + +#[derive(Debug)] // { dg-warning "unused name" } +// { dg-warning "stub implementation" "" { target *-*-* } .-1 } +struct Bar(i32, i32); // { dg-warning "is never constructed" } + +#[derive(Debug)] // { dg-warning "unused name" } +// { dg-warning "stub implementation" "" { target *-*-* } .-1 } +enum Baz { + A, + B(i32), + C { a: i32 } +} + diff --git a/gcc/testsuite/rust/compile/nr2/exclude b/gcc/testsuite/rust/compile/nr2/exclude index e5e5c12a978..acb4334e867 100644 --- a/gcc/testsuite/rust/compile/nr2/exclude +++ b/gcc/testsuite/rust/compile/nr2/exclude @@ -151,4 +151,5 @@ issue-3030.rs traits12.rs try-trait.rs issue-3174.rs +derive-debug1.rs # please don't delete the trailing newline -- 2.45.2