Skip to content

Commit cd72ca8

Browse files
Lucas Ly BaCohenArthur
authored andcommitted
gccrs: fix inner attr derive doesn't show error
gcc/rust/ChangeLog: * ast/rust-ast.cc (Attribute::is_derive): Change string derive to its definition. * util/rust-attribute-values.h: Add attribute definition derive. * util/rust-attributes.cc (AttributeChecker::visit): Add method check_inner_attributes. (AttributeChecker::check_inner_attributes): Check if there is a bad inner attribute. * util/rust-attributes.h: Add method check_inner_attributes in .h. gcc/testsuite/ChangeLog: * rust/compile/issue-4212.rs: New test. Signed-off-by: Lucas Ly Ba <[email protected]>
1 parent 0427d48 commit cd72ca8

File tree

5 files changed

+22
-1
lines changed

5 files changed

+22
-1
lines changed

gcc/rust/ast/rust-ast.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ Attribute::as_string () const
248248
bool
249249
Attribute::is_derive () const
250250
{
251-
return has_attr_input () && get_path () == "derive";
251+
return has_attr_input () && get_path () == Values::Attributes::DERIVE;
252252
}
253253

254254
/**

gcc/rust/util/rust-attribute-values.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ class Attributes
4949
static constexpr auto &PROC_MACRO_DERIVE = "proc_macro_derive";
5050
static constexpr auto &PROC_MACRO_ATTRIBUTE = "proc_macro_attribute";
5151

52+
static constexpr auto &DERIVE = "derive";
53+
5254
static constexpr auto &TARGET_FEATURE = "target_feature";
5355
// From now on, these are reserved by the compiler and gated through
5456
// #![feature(rustc_attrs)]

gcc/rust/util/rust-attributes.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ static const BuiltinAttrDefinition __definitions[]
9090
{Attrs::PROC_MACRO, EXPANSION},
9191
{Attrs::PROC_MACRO_DERIVE, EXPANSION},
9292
{Attrs::PROC_MACRO_ATTRIBUTE, EXPANSION},
93+
94+
{Attrs::DERIVE, EXPANSION},
9395
// FIXME: This is not implemented yet, see
9496
// https://github.com/Rust-GCC/gccrs/issues/1475
9597
{Attrs::TARGET_FEATURE, CODE_GENERATION},
@@ -170,6 +172,7 @@ AttributeChecker::go (AST::Crate &crate)
170172
void
171173
AttributeChecker::visit (AST::Crate &crate)
172174
{
175+
check_inner_attributes (crate.get_inner_attrs ());
173176
check_attributes (crate.get_inner_attrs ());
174177

175178
for (auto &item : crate.items)
@@ -353,6 +356,15 @@ AttributeChecker::check_attribute (const AST::Attribute &attribute)
353356
check_doc_attribute (attribute);
354357
}
355358

359+
void
360+
AttributeChecker::check_inner_attributes (const AST::AttrVec &attributes)
361+
{
362+
for (auto &attr : attributes)
363+
if (attr.is_derive ())
364+
rust_error_at (attr.get_locus (),
365+
"derive attribute cannot be used at crate level");
366+
}
367+
356368
void
357369
AttributeChecker::check_attributes (const AST::AttrVec &attributes)
358370
{

gcc/rust/util/rust-attributes.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ class AttributeChecker : public AST::DefaultASTVisitor
106106
void check_attribute (const AST::Attribute &attribute);
107107

108108
/* Check the validity of all given attributes */
109+
110+
void check_inner_attributes (const AST::AttrVec &attributes);
109111
void check_attributes (const AST::AttrVec &attributes);
110112

111113
// rust-ast.h
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#![derive(PartialOrd, PartialEq)]
2+
// { dg-error "derive attribute cannot be used at crate level" "" { target *-*-* } .-1 }
3+
pub fn check_ge(a: i32, b: i32) -> bool {
4+
a >= b
5+
}

0 commit comments

Comments
 (0)