Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions gcc/rust/expand/rust-derive.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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:
Expand Down
11 changes: 11 additions & 0 deletions gcc/testsuite/rust/compile/issue-3971.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#[lang = "copy"]
trait Copy {}

// since the macro expansion fails, the current nameres fixpoint error is emitted - just accept it for now
#[derive(Copy)]
// { dg-error "derive may only be applied to structs, enums and unions" "" { target *-*-* } .-1 }
// { dg-excess-errors "could not resolve trait" }

pub fn check_ge(a: i32, b: i32) -> bool {
a >= b
}