Skip to content

Commit c246504

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. Signed-off-by: Lucas Ly Ba <lucas.ly-ba@outlook.com>
1 parent b92a684 commit c246504

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

gcc/rust/expand/rust-derive.cc

Lines changed: 9 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 {
@@ -38,6 +39,14 @@ DeriveVisitor::derive (Item &item, const Attribute &attr,
3839
BuiltinMacro to_derive)
3940
{
4041
auto loc = attr.get_locus ();
42+
if (item.get_item_kind () != AST::Item::Kind::Enum
43+
&& item.get_item_kind () != AST::Item::Kind::Struct
44+
&& item.get_item_kind () != AST::Item::Kind::Union)
45+
{
46+
rust_error_at (loc,
47+
"derive may only be applied to structs, enums and unions");
48+
return {};
49+
}
4150

4251
switch (to_derive)
4352
{

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)