Avoid more MetaItem
-to-Attribute
conversions.
There is code for converting `Attribute` (syntactic) to `MetaItem` (semantic). There is also code for the reverse direction. The reverse direction isn't really necessary; it's currently only used when generating attributes, e.g. in `derive` code. This commit adds some new functions for creating `Attributes`s directly, without involving `MetaItem`s: `mk_attr_word`, `mk_attr_name_value_str`, `mk_attr_nested_word`, and `ExtCtxt::attr_{word,name_value_str,nested_word}`. These new methods replace the old functions for creating `Attribute`s: `mk_attr_inner`, `mk_attr_outer`, and `ExtCtxt::attribute`. Those functions took `MetaItem`s as input, and relied on many other functions that created `MetaItems`, which are also removed: `mk_name_value_item`, `mk_list_item`, `mk_word_item`, `mk_nested_word_item`, `{MetaItem,MetaItemKind,NestedMetaItem}::token_trees`, `MetaItemKind::attr_args`, `MetaItemLit::{from_lit_kind,to_token}`, `ExtCtxt::meta_word`. Overall this cuts more than 100 lines of code and makes thing simpler.
This commit is contained in:
parent
d1b61a31c5
commit
ba1751a201
20 changed files with 116 additions and 211 deletions
|
@ -95,9 +95,7 @@ fn generate_handler(cx: &ExtCtxt<'_>, handler: Ident, span: Span, sig_span: Span
|
|||
body,
|
||||
}));
|
||||
|
||||
let special = sym::rustc_std_internal_symbol;
|
||||
let special = cx.meta_word(span, special);
|
||||
let attrs = thin_vec![cx.attribute(special)];
|
||||
let attrs = thin_vec![cx.attr_word(sym::rustc_std_internal_symbol, span)];
|
||||
|
||||
let item = cx.item(span, Ident::from_str_and_span("__rg_oom", span), attrs, kind);
|
||||
cx.stmt_item(sig_span, item)
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
use rustc_ast::{
|
||||
attr,
|
||||
ptr::P,
|
||||
token,
|
||||
tokenstream::{DelimSpan, TokenStream, TokenTree},
|
||||
|
@ -118,10 +117,7 @@ impl<'cx, 'a> Context<'cx, 'a> {
|
|||
self.cx.item(
|
||||
self.span,
|
||||
Ident::empty(),
|
||||
thin_vec![self.cx.attribute(attr::mk_list_item(
|
||||
Ident::new(sym::allow, self.span),
|
||||
vec![attr::mk_nested_word_item(Ident::new(sym::unused_imports, self.span))],
|
||||
))],
|
||||
thin_vec![self.cx.attr_nested_word(sym::allow, sym::unused_imports, self.span)],
|
||||
ItemKind::Use(UseTree {
|
||||
prefix: self.cx.path(self.span, self.cx.std_path(&[sym::asserting])),
|
||||
kind: UseTreeKind::Nested(vec![
|
||||
|
|
|
@ -68,8 +68,7 @@ pub fn expand_deriving_clone(
|
|||
_ => cx.span_bug(span, "`#[derive(Clone)]` on trait item or impl item"),
|
||||
}
|
||||
|
||||
let inline = cx.meta_word(span, sym::inline);
|
||||
let attrs = thin_vec![cx.attribute(inline)];
|
||||
let attrs = thin_vec![cx.attr_word(sym::inline, span)];
|
||||
let trait_def = TraitDef {
|
||||
span,
|
||||
path: path_std!(clone::Clone),
|
||||
|
|
|
@ -5,7 +5,7 @@ use crate::deriving::path_std;
|
|||
use rustc_ast::{self as ast, MetaItem};
|
||||
use rustc_data_structures::fx::FxHashSet;
|
||||
use rustc_expand::base::{Annotatable, ExtCtxt};
|
||||
use rustc_span::symbol::{sym, Ident};
|
||||
use rustc_span::symbol::sym;
|
||||
use rustc_span::Span;
|
||||
use thin_vec::thin_vec;
|
||||
|
||||
|
@ -18,11 +18,11 @@ pub fn expand_deriving_eq(
|
|||
is_const: bool,
|
||||
) {
|
||||
let span = cx.with_def_site_ctxt(span);
|
||||
let inline = cx.meta_word(span, sym::inline);
|
||||
let hidden = rustc_ast::attr::mk_nested_word_item(Ident::new(sym::hidden, span));
|
||||
let doc = rustc_ast::attr::mk_list_item(Ident::new(sym::doc, span), vec![hidden]);
|
||||
let no_coverage = cx.meta_word(span, sym::no_coverage);
|
||||
let attrs = thin_vec![cx.attribute(inline), cx.attribute(doc), cx.attribute(no_coverage)];
|
||||
let attrs = thin_vec![
|
||||
cx.attr_word(sym::inline, span),
|
||||
cx.attr_nested_word(sym::doc, sym::hidden, span),
|
||||
cx.attr_word(sym::no_coverage, span)
|
||||
];
|
||||
let trait_def = TraitDef {
|
||||
span,
|
||||
path: path_std!(cmp::Eq),
|
||||
|
|
|
@ -15,8 +15,7 @@ pub fn expand_deriving_ord(
|
|||
push: &mut dyn FnMut(Annotatable),
|
||||
is_const: bool,
|
||||
) {
|
||||
let inline = cx.meta_word(span, sym::inline);
|
||||
let attrs = thin_vec![cx.attribute(inline)];
|
||||
let attrs = thin_vec![cx.attr_word(sym::inline, span)];
|
||||
let trait_def = TraitDef {
|
||||
span,
|
||||
path: path_std!(cmp::Ord),
|
||||
|
|
|
@ -68,8 +68,7 @@ pub fn expand_deriving_partial_eq(
|
|||
|
||||
// No need to generate `ne`, the default suffices, and not generating it is
|
||||
// faster.
|
||||
let inline = cx.meta_word(span, sym::inline);
|
||||
let attrs = thin_vec![cx.attribute(inline)];
|
||||
let attrs = thin_vec![cx.attr_word(sym::inline, span)];
|
||||
let methods = vec![MethodDef {
|
||||
name: sym::eq,
|
||||
generics: Bounds::empty(),
|
||||
|
|
|
@ -19,8 +19,7 @@ pub fn expand_deriving_partial_ord(
|
|||
let ret_ty =
|
||||
Path(Path::new_(pathvec_std!(option::Option), vec![Box::new(ordering_ty)], PathKind::Std));
|
||||
|
||||
let inline = cx.meta_word(span, sym::inline);
|
||||
let attrs = thin_vec![cx.attribute(inline)];
|
||||
let attrs = thin_vec![cx.attr_word(sym::inline, span)];
|
||||
|
||||
let partial_cmp_def = MethodDef {
|
||||
name: sym::partial_cmp,
|
||||
|
|
|
@ -20,8 +20,7 @@ pub fn expand_deriving_default(
|
|||
) {
|
||||
item.visit_with(&mut DetectNonVariantDefaultAttr { cx });
|
||||
|
||||
let inline = cx.meta_word(span, sym::inline);
|
||||
let attrs = thin_vec![cx.attribute(inline)];
|
||||
let attrs = thin_vec![cx.attr_word(sym::inline, span)];
|
||||
let trait_def = TraitDef {
|
||||
span,
|
||||
path: Path::new(vec![kw::Default, sym::Default]),
|
||||
|
|
|
@ -718,7 +718,7 @@ impl<'a> TraitDef<'a> {
|
|||
let path = cx.path_all(self.span, false, vec![type_ident], self_params);
|
||||
let self_type = cx.ty_path(path);
|
||||
|
||||
let attr = cx.attribute(cx.meta_word(self.span, sym::automatically_derived));
|
||||
let attr = cx.attr_word(sym::automatically_derived, self.span);
|
||||
let attrs = thin_vec![attr];
|
||||
let opt_trait_ref = Some(trait_ref);
|
||||
|
||||
|
|
|
@ -188,7 +188,7 @@ fn inject_impl_of_structural_trait(
|
|||
.cloned(),
|
||||
);
|
||||
// Mark as `automatically_derived` to avoid some silly lints.
|
||||
attrs.push(cx.attribute(cx.meta_word(span, sym::automatically_derived)));
|
||||
attrs.push(cx.attr_word(sym::automatically_derived, span));
|
||||
|
||||
let newitem = cx.item(
|
||||
span,
|
||||
|
|
|
@ -115,9 +115,7 @@ impl AllocFnFactory<'_, '_> {
|
|||
}
|
||||
|
||||
fn attrs(&self) -> AttrVec {
|
||||
let special = sym::rustc_std_internal_symbol;
|
||||
let special = self.cx.meta_word(self.span, special);
|
||||
thin_vec![self.cx.attribute(special)]
|
||||
thin_vec![self.cx.attr_word(sym::rustc_std_internal_symbol, self.span)]
|
||||
}
|
||||
|
||||
fn arg_ty(
|
||||
|
|
|
@ -1,6 +1,3 @@
|
|||
use std::mem;
|
||||
|
||||
use rustc_ast::attr;
|
||||
use rustc_ast::ptr::P;
|
||||
use rustc_ast::visit::{self, Visitor};
|
||||
use rustc_ast::{self as ast, NodeId};
|
||||
|
@ -13,6 +10,7 @@ use rustc_span::source_map::SourceMap;
|
|||
use rustc_span::symbol::{kw, sym, Ident, Symbol};
|
||||
use rustc_span::{Span, DUMMY_SP};
|
||||
use smallvec::smallvec;
|
||||
use std::mem;
|
||||
|
||||
struct ProcMacroDerive {
|
||||
id: NodeId,
|
||||
|
@ -365,14 +363,8 @@ fn mk_decls(cx: &mut ExtCtxt<'_>, macros: &[ProcMacro]) -> P<ast::Item> {
|
|||
cx.expr_array_ref(span, decls),
|
||||
)
|
||||
.map(|mut i| {
|
||||
let attr = cx.meta_word(span, sym::rustc_proc_macro_decls);
|
||||
i.attrs.push(cx.attribute(attr));
|
||||
|
||||
let deprecated_attr = attr::mk_nested_word_item(Ident::new(sym::deprecated, span));
|
||||
let allow_deprecated_attr =
|
||||
attr::mk_list_item(Ident::new(sym::allow, span), vec![deprecated_attr]);
|
||||
i.attrs.push(cx.attribute(allow_deprecated_attr));
|
||||
|
||||
i.attrs.push(cx.attr_word(sym::rustc_proc_macro_decls, span));
|
||||
i.attrs.push(cx.attr_nested_word(sym::allow, sym::deprecated, span));
|
||||
i
|
||||
});
|
||||
|
||||
|
|
|
@ -52,7 +52,7 @@ pub fn inject(
|
|||
cx.item(
|
||||
span,
|
||||
ident,
|
||||
thin_vec![cx.attribute(cx.meta_word(span, sym::macro_use))],
|
||||
thin_vec![cx.attr_word(sym::macro_use, span)],
|
||||
ast::ItemKind::ExternCrate(None),
|
||||
),
|
||||
);
|
||||
|
@ -79,7 +79,7 @@ pub fn inject(
|
|||
let use_item = cx.item(
|
||||
span,
|
||||
Ident::empty(),
|
||||
thin_vec![cx.attribute(cx.meta_word(span, sym::prelude_import))],
|
||||
thin_vec![cx.attr_word(sym::prelude_import, span)],
|
||||
ast::ItemKind::Use(ast::UseTree {
|
||||
prefix: cx.path(span, import_path),
|
||||
kind: ast::UseTreeKind::Glob,
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
/// Ideally, this code would be in libtest but for efficiency and error messages it lives here.
|
||||
use crate::util::{check_builtin_macro_attribute, warn_on_duplicate_attribute};
|
||||
use rustc_ast as ast;
|
||||
use rustc_ast::attr;
|
||||
use rustc_ast::ptr::P;
|
||||
use rustc_ast_pretty::pprust;
|
||||
use rustc_errors::Applicability;
|
||||
|
@ -47,11 +46,7 @@ pub fn expand_test_case(
|
|||
tokens: None,
|
||||
};
|
||||
item.ident.span = item.ident.span.with_ctxt(sp.ctxt());
|
||||
item.attrs.push(ecx.attribute(attr::mk_name_value_item_str(
|
||||
Ident::new(sym::rustc_test_marker, sp),
|
||||
test_path_symbol,
|
||||
sp,
|
||||
)));
|
||||
item.attrs.push(ecx.attr_name_value_str(sym::rustc_test_marker, test_path_symbol, sp));
|
||||
item
|
||||
});
|
||||
|
||||
|
@ -241,16 +236,9 @@ pub fn expand_test_or_bench(
|
|||
Ident::new(item.ident.name, sp),
|
||||
thin_vec![
|
||||
// #[cfg(test)]
|
||||
cx.attribute(attr::mk_list_item(
|
||||
Ident::new(sym::cfg, attr_sp),
|
||||
vec![attr::mk_nested_word_item(Ident::new(sym::test, attr_sp))],
|
||||
)),
|
||||
cx.attr_nested_word(sym::cfg, sym::test, attr_sp),
|
||||
// #[rustc_test_marker = "test_case_sort_key"]
|
||||
cx.attribute(attr::mk_name_value_item_str(
|
||||
Ident::new(sym::rustc_test_marker, attr_sp),
|
||||
test_path_symbol,
|
||||
attr_sp,
|
||||
)),
|
||||
cx.attr_name_value_str(sym::rustc_test_marker, test_path_symbol, attr_sp),
|
||||
]
|
||||
.into(),
|
||||
// const $ident: test::TestDescAndFn =
|
||||
|
|
|
@ -185,13 +185,12 @@ impl<'a> MutVisitor for EntryPointCleaner<'a> {
|
|||
let item = match entry_point_type(self.sess, &item, self.depth) {
|
||||
EntryPointType::MainNamed | EntryPointType::RustcMainAttr | EntryPointType::Start => {
|
||||
item.map(|ast::Item { id, ident, attrs, kind, vis, span, tokens }| {
|
||||
let allow_ident = Ident::new(sym::allow, self.def_site);
|
||||
let dc_nested =
|
||||
attr::mk_nested_word_item(Ident::new(sym::dead_code, self.def_site));
|
||||
let allow_dead_code_item = attr::mk_list_item(allow_ident, vec![dc_nested]);
|
||||
let allow_dead_code = attr::mk_attr_outer(
|
||||
let allow_dead_code = attr::mk_attr_nested_word(
|
||||
&self.sess.parse_sess.attr_id_generator,
|
||||
allow_dead_code_item,
|
||||
ast::AttrStyle::Outer,
|
||||
sym::allow,
|
||||
sym::dead_code,
|
||||
self.def_site,
|
||||
);
|
||||
let attrs = attrs
|
||||
.into_iter()
|
||||
|
@ -309,8 +308,7 @@ fn mk_main(cx: &mut TestCtxt<'_>) -> P<ast::Item> {
|
|||
);
|
||||
|
||||
// #[rustc_main]
|
||||
let main_meta = ecx.meta_word(sp, sym::rustc_main);
|
||||
let main_attr = ecx.attribute(main_meta);
|
||||
let main_attr = ecx.attr_word(sym::rustc_main, sp);
|
||||
|
||||
// pub fn main() { ... }
|
||||
let main_ret_ty = ecx.ty(sp, ast::TyKind::Tup(vec![]));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue