Skip to content

Commit 561b83b

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

File tree

3 files changed

+22
-0
lines changed

3 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:

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

Lines changed: 1 addition & 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"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#[lang = "copy"]
2+
trait Copy {}
3+
4+
#[derive(Copy)]
5+
// { dg-error "derive may only be applied to structs, enums and unions" "" { target *-*-* } .-1 }
6+
7+
pub fn check_ge(a: i32, b: i32) -> bool {
8+
a >= b
9+
}
10+

0 commit comments

Comments
 (0)