Skip to content

Commit 73f7a4c

Browse files
author
Lucas Ly Ba
committed
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): Give can_be_inner a value for each atribute. (AttributeChecker::check_inner_attributes): Implement method for errors check. * util/rust-attributes.h (struct BuiltinAttrDefinition): Add can_be_inner object in struct. gcc/testsuite/ChangeLog: * rust/compile/issue-4212.rs: Signed-off-by: Lucas Ly Ba <[email protected]>
1 parent cd72ca8 commit 73f7a4c

File tree

5 files changed

+77
-74
lines changed

5 files changed

+77
-74
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: 68 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -67,69 +67,60 @@ using Attrs = Values::Attributes;
6767

6868
// https://doc.rust-lang.org/stable/nightly-rustc/src/rustc_feature/builtin_attrs.rs.html#248
6969
static const BuiltinAttrDefinition __definitions[]
70-
= {{Attrs::INLINE, CODE_GENERATION},
71-
{Attrs::COLD, CODE_GENERATION},
72-
{Attrs::CFG, EXPANSION},
73-
{Attrs::CFG_ATTR, EXPANSION},
74-
{Attrs::DERIVE_ATTR, EXPANSION},
75-
{Attrs::DEPRECATED, STATIC_ANALYSIS},
76-
{Attrs::ALLOW, STATIC_ANALYSIS},
77-
{Attrs::ALLOW_INTERNAL_UNSTABLE, STATIC_ANALYSIS},
78-
{Attrs::DOC, HIR_LOWERING},
79-
{Attrs::MUST_USE, STATIC_ANALYSIS},
80-
{Attrs::LANG, HIR_LOWERING},
81-
{Attrs::LINK_NAME, CODE_GENERATION},
82-
{Attrs::LINK_SECTION, CODE_GENERATION},
83-
{Attrs::NO_MANGLE, CODE_GENERATION},
84-
{Attrs::REPR, CODE_GENERATION},
85-
{Attrs::RUSTC_BUILTIN_MACRO, EXPANSION},
86-
{Attrs::RUSTC_MACRO_TRANSPARENCY, EXPANSION},
87-
{Attrs::PATH, EXPANSION},
88-
{Attrs::MACRO_USE, NAME_RESOLUTION},
89-
{Attrs::MACRO_EXPORT, NAME_RESOLUTION},
90-
{Attrs::PROC_MACRO, EXPANSION},
91-
{Attrs::PROC_MACRO_DERIVE, EXPANSION},
92-
{Attrs::PROC_MACRO_ATTRIBUTE, EXPANSION},
93-
94-
{Attrs::DERIVE, EXPANSION},
70+
= {{Attrs::INLINE, CODE_GENERATION, false},
71+
{Attrs::COLD, CODE_GENERATION, true},
72+
{Attrs::CFG, EXPANSION, true},
73+
{Attrs::CFG_ATTR, EXPANSION, true},
74+
{Attrs::DERIVE_ATTR, EXPANSION, false},
75+
{Attrs::DEPRECATED, STATIC_ANALYSIS, true},
76+
{Attrs::ALLOW, STATIC_ANALYSIS, true},
77+
{Attrs::ALLOW_INTERNAL_UNSTABLE, STATIC_ANALYSIS, false},
78+
{Attrs::DOC, HIR_LOWERING, true},
79+
{Attrs::MUST_USE, STATIC_ANALYSIS, true},
80+
{Attrs::LANG, HIR_LOWERING, false},
81+
{Attrs::LINK_NAME, CODE_GENERATION, true},
82+
{Attrs::LINK_SECTION, CODE_GENERATION, true},
83+
{Attrs::NO_MANGLE, CODE_GENERATION, true},
84+
{Attrs::REPR, CODE_GENERATION, false},
85+
{Attrs::RUSTC_BUILTIN_MACRO, EXPANSION, true},
86+
{Attrs::RUSTC_MACRO_TRANSPARENCY, EXPANSION, true},
87+
{Attrs::PATH, EXPANSION, false},
88+
{Attrs::MACRO_USE, NAME_RESOLUTION, true},
89+
{Attrs::MACRO_EXPORT, NAME_RESOLUTION, true},
90+
{Attrs::PROC_MACRO, EXPANSION, true},
91+
{Attrs::PROC_MACRO_DERIVE, EXPANSION, true},
92+
{Attrs::PROC_MACRO_ATTRIBUTE, EXPANSION, true},
9593
// FIXME: This is not implemented yet, see
9694
// https://github.com/Rust-GCC/gccrs/issues/1475
97-
{Attrs::TARGET_FEATURE, CODE_GENERATION},
95+
{Attrs::TARGET_FEATURE, CODE_GENERATION, false},
9896
// From now on, these are reserved by the compiler and gated through
9997
// #![feature(rustc_attrs)]
100-
{Attrs::RUSTC_DEPRECATED, STATIC_ANALYSIS},
101-
{Attrs::RUSTC_INHERIT_OVERFLOW_CHECKS, CODE_GENERATION},
102-
{Attrs::STABLE, STATIC_ANALYSIS},
103-
{Attrs::UNSTABLE, STATIC_ANALYSIS},
104-
98+
{Attrs::RUSTC_DEPRECATED, STATIC_ANALYSIS, true},
99+
{Attrs::RUSTC_INHERIT_OVERFLOW_CHECKS, CODE_GENERATION, true},
100+
{Attrs::STABLE, STATIC_ANALYSIS, true},
101+
{Attrs::UNSTABLE, STATIC_ANALYSIS, true},
105102
// assuming we keep these for static analysis
106-
{Attrs::RUSTC_PROMOTABLE, CODE_GENERATION},
107-
{Attrs::RUSTC_CONST_STABLE, STATIC_ANALYSIS},
108-
{Attrs::RUSTC_CONST_UNSTABLE, STATIC_ANALYSIS},
109-
{Attrs::RUSTC_ALLOW_CONST_FN_UNSTABLE, STATIC_ANALYSIS},
110-
{Attrs::PRELUDE_IMPORT, NAME_RESOLUTION},
111-
{Attrs::TRACK_CALLER, CODE_GENERATION},
112-
{Attrs::RUSTC_SPECIALIZATION_TRAIT, TYPE_CHECK},
113-
{Attrs::RUSTC_UNSAFE_SPECIALIZATION_MARKER, TYPE_CHECK},
114-
{Attrs::RUSTC_RESERVATION_IMPL, TYPE_CHECK},
115-
{Attrs::RUSTC_PAREN_SUGAR, TYPE_CHECK},
116-
{Attrs::RUSTC_NONNULL_OPTIMIZATION_GUARANTEED, TYPE_CHECK},
117-
118-
{Attrs::RUSTC_LAYOUT_SCALAR_VALID_RANGE_START, CODE_GENERATION},
119-
103+
{Attrs::RUSTC_PROMOTABLE, CODE_GENERATION, true},
104+
{Attrs::RUSTC_CONST_STABLE, STATIC_ANALYSIS, true},
105+
{Attrs::RUSTC_CONST_UNSTABLE, STATIC_ANALYSIS, true},
106+
{Attrs::RUSTC_ALLOW_CONST_FN_UNSTABLE, STATIC_ANALYSIS, true},
107+
{Attrs::PRELUDE_IMPORT, NAME_RESOLUTION, true},
108+
{Attrs::TRACK_CALLER, CODE_GENERATION, true},
109+
{Attrs::RUSTC_SPECIALIZATION_TRAIT, TYPE_CHECK, true},
110+
{Attrs::RUSTC_UNSAFE_SPECIALIZATION_MARKER, TYPE_CHECK, true},
111+
{Attrs::RUSTC_RESERVATION_IMPL, TYPE_CHECK, true},
112+
{Attrs::RUSTC_PAREN_SUGAR, TYPE_CHECK, true},
113+
{Attrs::RUSTC_NONNULL_OPTIMIZATION_GUARANTEED, TYPE_CHECK, true},
114+
{Attrs::RUSTC_LAYOUT_SCALAR_VALID_RANGE_START, CODE_GENERATION, true},
120115
// TODO: be careful about calling functions marked with this?
121-
{Attrs::RUSTC_ARGS_REQUIRED_CONST, CODE_GENERATION},
122-
123-
{Attrs::PRELUDE_IMPORT, NAME_RESOLUTION},
124-
125-
{Attrs::RUSTC_DIAGNOSTIC_ITEM, STATIC_ANALYSIS},
126-
{Attrs::RUSTC_ON_UNIMPLEMENTED, STATIC_ANALYSIS},
127-
128-
{Attrs::FUNDAMENTAL, TYPE_CHECK},
129-
{Attrs::NON_EXHAUSTIVE, TYPE_CHECK},
130-
{Attrs::RUSTFMT, EXTERNAL},
131-
132-
{Attrs::TEST, CODE_GENERATION}};
116+
{Attrs::RUSTC_ARGS_REQUIRED_CONST, CODE_GENERATION, true},
117+
{Attrs::PRELUDE_IMPORT, NAME_RESOLUTION, true},
118+
{Attrs::RUSTC_DIAGNOSTIC_ITEM, STATIC_ANALYSIS, true},
119+
{Attrs::RUSTC_ON_UNIMPLEMENTED, STATIC_ANALYSIS, true},
120+
{Attrs::FUNDAMENTAL, TYPE_CHECK, true},
121+
{Attrs::NON_EXHAUSTIVE, TYPE_CHECK, true},
122+
{Attrs::RUSTFMT, EXTERNAL, true},
123+
{Attrs::TEST, CODE_GENERATION, false}};
133124

134125
BuiltinAttributeMappings *
135126
BuiltinAttributeMappings::get ()
@@ -326,6 +317,26 @@ check_proc_macro_non_root (AST::AttrVec attributes, location_t loc)
326317
}
327318
}
328319

320+
void
321+
AttributeChecker::check_inner_attribute (const AST::Attribute &attribute)
322+
{
323+
BuiltinAttrDefinition result;
324+
325+
if (!is_builtin (attribute, result))
326+
return;
327+
328+
if (!result.can_be_inner)
329+
rust_error_at (attribute.get_locus (),
330+
"attribute cannot be used at crate level");
331+
}
332+
333+
void
334+
AttributeChecker::check_inner_attributes (const AST::AttrVec &attributes)
335+
{
336+
for (auto &attr : attributes)
337+
check_inner_attribute (attr);
338+
}
339+
329340
void
330341
AttributeChecker::check_attribute (const AST::Attribute &attribute)
331342
{
@@ -356,15 +367,6 @@ AttributeChecker::check_attribute (const AST::Attribute &attribute)
356367
check_doc_attribute (attribute);
357368
}
358369

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-
368370
void
369371
AttributeChecker::check_attributes (const AST::AttrVec &attributes)
370372
{

gcc/rust/util/rust-attributes.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,11 @@ struct BuiltinAttrDefinition
5454
{
5555
std::string name;
5656
CompilerPass handler;
57+
bool can_be_inner;
5758

5859
static BuiltinAttrDefinition get_error ()
5960
{
60-
return BuiltinAttrDefinition{"", UNKNOWN};
61+
return BuiltinAttrDefinition{"", UNKNOWN, false};
6162
}
6263

6364
static BuiltinAttrDefinition &error_node ()
@@ -102,12 +103,14 @@ class AttributeChecker : public AST::DefaultASTVisitor
102103

103104
private:
104105
using AST::DefaultASTVisitor::visit;
106+
107+
/* Check the validity of an inner attribute */
108+
void check_inner_attribute (const AST::Attribute &attribute);
109+
/* Check the validy of all inner attributes */
110+
void check_inner_attributes (const AST::AttrVec &attributes);
105111
/* Check the validity of a given attribute */
106112
void check_attribute (const AST::Attribute &attribute);
107-
108113
/* Check the validity of all given attributes */
109-
110-
void check_inner_attributes (const AST::AttrVec &attributes);
111114
void check_attributes (const AST::AttrVec &attributes);
112115

113116
// 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
}

0 commit comments

Comments
 (0)