Skip to content

Commit f9b39d2

Browse files
lucasly-baLucas Ly Ba
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-expand-visitor.cc(ExpandVisitor::expand_inner_items): Add check and error. gcc/testsuite/ChangeLog: * rust/compile/issue-3971.rs: New test. Signed-off-by: Lucas Ly Ba <[email protected]>
1 parent b92a684 commit f9b39d2

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

gcc/rust/expand/rust-expand-visitor.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include "rust-expand-visitor.h"
2020
#include "rust-ast-fragment.h"
21+
#include "rust-diagnostics.h"
2122
#include "rust-item.h"
2223
#include "rust-proc-macro.h"
2324
#include "rust-attributes.h"
@@ -180,6 +181,15 @@ ExpandVisitor::expand_inner_items (
180181

181182
if (current.is_derive ())
182183
{
184+
if (item.get_item_kind () != AST::Item::Kind::Enum
185+
&& item.get_item_kind () != AST::Item::Kind::Struct
186+
&& item.get_item_kind () != AST::Item::Kind::Union)
187+
{
188+
rust_error_at (current.get_locus (),
189+
"derive may only be applied to structs, "
190+
"enums and unions");
191+
return;
192+
}
183193
current.parse_attr_to_meta_item ();
184194
attr_it = attrs.erase (attr_it);
185195
// Get traits to derive in the current attribute
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#[derive(PartialOrd, PartialEq)]
2+
// { dg-error "derive may only be applied to structs, enums and unions" "" { target *-*-* } .-1 }
3+
// { dg-error "could not resolve trait .PartialOrd." "" { target *-*-* } .-2 }
4+
// { dg-error "could not resolve trait .PartialEq." "" { target *-*-* } .-3 }
5+
6+
pub fn check_ge(a: Foo, b: Foo) -> bool {
7+
// { dg-error "could not resolve type path .Foo." "" { target *-*-* } .-1 }
8+
// { dg-error "could not resolve type path .Foo." "" { target *-*-* } .-2 }
9+
a >= b
10+
}
11+

0 commit comments

Comments
 (0)