diff --git a/gcc/rust/expand/rust-derive.cc b/gcc/rust/expand/rust-derive.cc index 55147df26f2..0e985ee3806 100644 --- a/gcc/rust/expand/rust-derive.cc +++ b/gcc/rust/expand/rust-derive.cc @@ -25,6 +25,7 @@ #include "rust-derive-ord.h" #include "rust-derive-partial-eq.h" #include "rust-derive-hash.h" +#include "rust-system.h" namespace Rust { namespace AST { @@ -39,6 +40,16 @@ DeriveVisitor::derive (Item &item, const Attribute &attr, { auto loc = attr.get_locus (); + using kind = AST::Item::Kind; + auto item_kind = item.get_item_kind (); + if (item_kind != kind::Enum && item_kind != kind::Struct + && item_kind != kind::Union) + { + rust_error_at (loc, + "derive may only be applied to structs, enums and unions"); + return {}; + } + switch (to_derive) { case BuiltinMacro::Clone: diff --git a/gcc/rust/expand/rust-expand-visitor.cc b/gcc/rust/expand/rust-expand-visitor.cc index 4593cc3f930..1ad395a9ea8 100644 --- a/gcc/rust/expand/rust-expand-visitor.cc +++ b/gcc/rust/expand/rust-expand-visitor.cc @@ -18,6 +18,7 @@ #include "rust-expand-visitor.h" #include "rust-ast-fragment.h" +#include "rust-diagnostics.h" #include "rust-item.h" #include "rust-proc-macro.h" #include "rust-attributes.h" diff --git a/gcc/testsuite/rust/compile/issue-3971.rs b/gcc/testsuite/rust/compile/issue-3971.rs new file mode 100644 index 00000000000..f3dbb801f9f --- /dev/null +++ b/gcc/testsuite/rust/compile/issue-3971.rs @@ -0,0 +1,10 @@ +#[lang = "copy"] +trait Copy {} + +#[derive(Copy)] +// { dg-error "derive may only be applied to structs, enums and unions" "" { target *-*-* } .-1 } + +pub fn check_ge(a: i32, b: i32) -> bool { + a >= b +} +