Skip to content

Commit 8098c3b

Browse files
lucasly-baLucas Ly Ba
authored andcommitted
gccrs: make invalid inner attributes show error
gcc/rust/ChangeLog: * ast/rust-ast.cc (Attribute::is_derive): Change is_derive method with its valid path. * util/rust-attribute-values.h: Delete redudant derive attribute. * util/rust-attributes.cc (AttributeChecker::check_inner_attribute): Helper method for check_inner_attributes (AttributeChecker::check_inner_attributes): Implement method for errors check. * util/rust-attributes.h: Add methods above in header. gcc/testsuite/ChangeLog: * rust/compile/issue-4212.rs: * rust/compile/issue-4219.rs: New test. Signed-off-by: Lucas Ly Ba <[email protected]>
1 parent 9b2f159 commit 8098c3b

File tree

6 files changed

+37
-25
lines changed

6 files changed

+37
-25
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 () == Values::Attributes::DERIVE;
251+
return has_attr_input () && get_path () == Values::Attributes::DERIVE_ATTR;
252252
}
253253

254254
/**

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ 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-
5452
static constexpr auto &TARGET_FEATURE = "target_feature";
5553
// From now on, these are reserved by the compiler and gated through
5654
// #![feature(rustc_attrs)]

gcc/rust/util/rust-attributes.cc

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,6 @@ 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},
9593
// FIXME: This is not implemented yet, see
9694
// https://github.com/Rust-GCC/gccrs/issues/1475
9795
{Attrs::TARGET_FEATURE, CODE_GENERATION},
@@ -101,7 +99,6 @@ static const BuiltinAttrDefinition __definitions[]
10199
{Attrs::RUSTC_INHERIT_OVERFLOW_CHECKS, CODE_GENERATION},
102100
{Attrs::STABLE, STATIC_ANALYSIS},
103101
{Attrs::UNSTABLE, STATIC_ANALYSIS},
104-
105102
// assuming we keep these for static analysis
106103
{Attrs::RUSTC_PROMOTABLE, CODE_GENERATION},
107104
{Attrs::RUSTC_CONST_STABLE, STATIC_ANALYSIS},
@@ -114,23 +111,22 @@ static const BuiltinAttrDefinition __definitions[]
114111
{Attrs::RUSTC_RESERVATION_IMPL, TYPE_CHECK},
115112
{Attrs::RUSTC_PAREN_SUGAR, TYPE_CHECK},
116113
{Attrs::RUSTC_NONNULL_OPTIMIZATION_GUARANTEED, TYPE_CHECK},
117-
118114
{Attrs::RUSTC_LAYOUT_SCALAR_VALID_RANGE_START, CODE_GENERATION},
119-
120115
// TODO: be careful about calling functions marked with this?
121116
{Attrs::RUSTC_ARGS_REQUIRED_CONST, CODE_GENERATION},
122-
123117
{Attrs::PRELUDE_IMPORT, NAME_RESOLUTION},
124-
125118
{Attrs::RUSTC_DIAGNOSTIC_ITEM, STATIC_ANALYSIS},
126119
{Attrs::RUSTC_ON_UNIMPLEMENTED, STATIC_ANALYSIS},
127-
128120
{Attrs::FUNDAMENTAL, TYPE_CHECK},
129121
{Attrs::NON_EXHAUSTIVE, TYPE_CHECK},
130122
{Attrs::RUSTFMT, EXTERNAL},
131-
132123
{Attrs::TEST, CODE_GENERATION}};
133124

125+
static const std::set<std::string> __outer_attributes
126+
= {Attrs::INLINE, Attrs::DERIVE_ATTR, Attrs::ALLOW_INTERNAL_UNSTABLE,
127+
Attrs::LANG, Attrs::REPR, Attrs::PATH,
128+
Attrs::TARGET_FEATURE, Attrs::TEST};
129+
134130
BuiltinAttributeMappings *
135131
BuiltinAttributeMappings::get ()
136132
{
@@ -326,6 +322,26 @@ check_proc_macro_non_root (AST::AttrVec attributes, location_t loc)
326322
}
327323
}
328324

325+
void
326+
AttributeChecker::check_inner_attribute (const AST::Attribute &attribute)
327+
{
328+
BuiltinAttrDefinition result;
329+
330+
if (!is_builtin (attribute, result))
331+
return;
332+
333+
if (__outer_attributes.find (result.name) != __outer_attributes.end ())
334+
rust_error_at (attribute.get_locus (),
335+
"attribute cannot be used at crate level");
336+
}
337+
338+
void
339+
AttributeChecker::check_inner_attributes (const AST::AttrVec &attributes)
340+
{
341+
for (auto &attr : attributes)
342+
check_inner_attribute (attr);
343+
}
344+
329345
void
330346
AttributeChecker::check_attribute (const AST::Attribute &attribute)
331347
{
@@ -356,15 +372,6 @@ AttributeChecker::check_attribute (const AST::Attribute &attribute)
356372
check_doc_attribute (attribute);
357373
}
358374

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-
368375
void
369376
AttributeChecker::check_attributes (const AST::AttrVec &attributes)
370377
{

gcc/rust/util/rust-attributes.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,14 @@ class AttributeChecker : public AST::DefaultASTVisitor
102102

103103
private:
104104
using AST::DefaultASTVisitor::visit;
105+
106+
/* Check the validity of an inner attribute */
107+
void check_inner_attribute (const AST::Attribute &attribute);
108+
/* Check the validy of all inner attributes */
109+
void check_inner_attributes (const AST::AttrVec &attributes);
105110
/* Check the validity of a given attribute */
106111
void check_attribute (const AST::Attribute &attribute);
107-
108112
/* Check the validity of all given attributes */
109-
110-
void check_inner_attributes (const AST::AttrVec &attributes);
111113
void check_attributes (const AST::AttrVec &attributes);
112114

113115
// rust-ast.h
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#![derive(PartialOrd, PartialEq)]
2-
// { dg-error "derive attribute cannot be used at crate level" "" { target *-*-* } .-1 }
2+
// { dg-error "attribute cannot be used at crate level" "" { target *-*-* } .-1 }
33
pub fn check_ge(a: i32, b: i32) -> bool {
44
a >= b
55
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#![inline]
2+
// { dg-error "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)