1
Fork 0

Avoid unnecessary MetaItem/Attribute conversions.

`check_builtin_attribute` calls `parse_meta` to convert an `Attribute`
to a `MetaItem`, which it then checks. However, many callers of
`check_builtin_attribute` start with a `MetaItem`, and then convert it
to an `Attribute` by calling `cx.attribute(meta_item)`. This `MetaItem`
to `Attribute` to `MetaItem` conversion is silly.

This commit adds a new function `check_builtin_meta_item`, which can be
called instead from these call sites. `check_builtin_attribute` also now
calls it. The commit also renames `check_meta` as `check_attr` to better
match its arguments.
This commit is contained in:
Nicholas Nethercote 2022-11-24 16:00:57 +11:00
parent 2585bcea0b
commit c9ae38c71e
6 changed files with 44 additions and 29 deletions

View file

@ -37,10 +37,10 @@ impl MultiItemModifier for Expander {
_is_derive_const: bool,
) -> ExpandResult<Vec<Annotatable>, Annotatable> {
let template = AttributeTemplate { list: Some("path"), ..Default::default() };
let attr = &ecx.attribute(meta_item.clone());
validate_attr::check_builtin_attribute(
validate_attr::check_builtin_meta_item(
&ecx.sess.parse_sess,
attr,
&meta_item,
ast::AttrStyle::Outer,
sym::cfg_accessible,
template,
);

View file

@ -33,14 +33,15 @@ impl MultiItemModifier for Expander {
ecx.resolver.resolve_derives(ecx.current_expansion.id, ecx.force_mode, &|| {
let template =
AttributeTemplate { list: Some("Trait1, Trait2, ..."), ..Default::default() };
let attr =
attr::mk_attr_outer(&sess.parse_sess.attr_id_generator, meta_item.clone());
validate_attr::check_builtin_attribute(
validate_attr::check_builtin_meta_item(
&sess.parse_sess,
&attr,
&meta_item,
ast::AttrStyle::Outer,
sym::derive,
template,
);
let attr =
attr::mk_attr_outer(&sess.parse_sess.attr_id_generator, meta_item.clone());
let mut resolutions: Vec<_> = attr
.meta_item_list()

View file

@ -1,4 +1,4 @@
use rustc_ast::{Attribute, MetaItem};
use rustc_ast::{AttrStyle, Attribute, MetaItem};
use rustc_expand::base::{Annotatable, ExtCtxt};
use rustc_feature::AttributeTemplate;
use rustc_lint_defs::builtin::DUPLICATE_MACRO_ATTRIBUTES;
@ -8,8 +8,13 @@ use rustc_span::Symbol;
pub fn check_builtin_macro_attribute(ecx: &ExtCtxt<'_>, meta_item: &MetaItem, name: Symbol) {
// All the built-in macro attributes are "words" at the moment.
let template = AttributeTemplate { word: true, ..Default::default() };
let attr = ecx.attribute(meta_item.clone());
validate_attr::check_builtin_attribute(&ecx.sess.parse_sess, &attr, name, template);
validate_attr::check_builtin_meta_item(
&ecx.sess.parse_sess,
&meta_item,
AttrStyle::Outer,
name,
template,
);
}
/// Emit a warning if the item is annotated with the given attribute. This is used to diagnose when