Skip to content

Commit f06a3c4

Browse files
lucasly-baphilberty
authored andcommitted
gccrs: add error check if derive has wrong item
Derive may only be applied to structs, enums and unions. gcc/rust/ChangeLog: * expand/rust-derive.cc (DeriveVisitor::derive): Add check and error. gcc/testsuite/ChangeLog: * rust/compile/issue-3971.rs: New test. Signed-off-by: Lucas Ly Ba <lucas.ly-ba@outlook.com>
1 parent 6e5929a commit f06a3c4

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

gcc/rust/expand/rust-derive.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "rust-derive-ord.h"
2626
#include "rust-derive-partial-eq.h"
2727
#include "rust-derive-hash.h"
28+
#include "rust-system.h"
2829

2930
namespace Rust {
3031
namespace AST {
@@ -39,6 +40,16 @@ DeriveVisitor::derive (Item &item, const Attribute &attr,
3940
{
4041
auto loc = attr.get_locus ();
4142

43+
using Kind = AST::Item::Kind;
44+
auto item_kind = item.get_item_kind ();
45+
if (item_kind != Kind::Enum && item_kind != Kind::Struct
46+
&& item_kind != Kind::Union)
47+
{
48+
rust_error_at (loc,
49+
"derive may only be applied to structs, enums and unions");
50+
return {};
51+
}
52+
4253
switch (to_derive)
4354
{
4455
case BuiltinMacro::Clone:
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#[lang = "copy"]
2+
trait Copy {}
3+
4+
// since the macro expansion fails, the current nameres fixpoint error is emitted - just accept it for now
5+
#[derive(Copy)]
6+
// { dg-error "derive may only be applied to structs, enums and unions" "" { target *-*-* } .-1 }
7+
// { dg-excess-errors "could not resolve trait" }
8+
9+
pub fn check_ge(a: i32, b: i32) -> bool {
10+
a >= b
11+
}

0 commit comments

Comments
 (0)