Skip to content

Commit 52517ad

Browse files
committed
Rename AttrItem as Meta.
I have long found the `Attribute`/`NormalAttr`/`AttrItem` naming confusing. This commit renames `AttrItem` as `Meta` which helps a lot. - Because `Meta` isn't the whole attribute, just the meta part, e.g. the `foo` within `#[foo]`. - This also helps with remembering the distinction between `NormalAttr::tokens` and (the newly renamed) `Meta::tokens`. - The name `Meta` also now matches `NtMeta`/`NonterminalKind::Meta`. - The name `Meta` is similar to the existing `MetaItem` type. This is reasonble because they're very similar types! - The variable name `meta` is much better than `item`, which can be easily confused with other language items.
1 parent dd790ab commit 52517ad

File tree

36 files changed

+142
-151
lines changed

36 files changed

+142
-151
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2828,15 +2828,15 @@ pub enum AttrKind {
28282828

28292829
#[derive(Clone, Encodable, Decodable, Debug)]
28302830
pub struct NormalAttr {
2831-
pub item: AttrItem,
2831+
pub meta: Meta,
28322832
// Tokens for the full attribute, e.g. `#[foo]`, `#![bar]`.
28332833
pub tokens: Option<LazyAttrTokenStream>,
28342834
}
28352835

28362836
impl NormalAttr {
28372837
pub fn from_ident(ident: Ident) -> Self {
28382838
Self {
2839-
item: AttrItem {
2839+
meta: Meta {
28402840
unsafety: Safety::Default,
28412841
path: Path::from_ident(ident),
28422842
args: AttrArgs::Empty,
@@ -2848,11 +2848,11 @@ impl NormalAttr {
28482848
}
28492849

28502850
#[derive(Clone, Encodable, Decodable, Debug, HashStable_Generic)]
2851-
pub struct AttrItem {
2851+
pub struct Meta {
28522852
pub unsafety: Safety,
28532853
pub path: Path,
28542854
pub args: AttrArgs,
2855-
// Tokens for the meta item, e.g. just the `foo` within `#[foo]` or `#![foo]`.
2855+
// Tokens for the meta part, e.g. just the `foo` within `#[foo]` or `#![foo]`.
28562856
pub tokens: Option<LazyAttrTokenStream>,
28572857
}
28582858

compiler/rustc_ast/src/ast_traits.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::token::Nonterminal;
77
use crate::tokenstream::LazyAttrTokenStream;
88
use crate::{Arm, Crate, ExprField, FieldDef, GenericParam, Param, PatField, Variant};
99
use crate::{AssocItem, Expr, ForeignItem, Item, NodeId};
10-
use crate::{AttrItem, AttrKind, Block, Pat, Path, Ty, Visibility};
10+
use crate::{AttrKind, Block, Meta, Pat, Path, Ty, Visibility};
1111
use crate::{AttrVec, Attribute, Stmt, StmtKind};
1212

1313
use rustc_span::Span;
@@ -116,7 +116,7 @@ impl<T: AstDeref<Target: HasSpan>> HasSpan for T {
116116
}
117117
}
118118

119-
impl HasSpan for AttrItem {
119+
impl HasSpan for Meta {
120120
fn span(&self) -> Span {
121121
self.span()
122122
}
@@ -158,7 +158,7 @@ macro_rules! impl_has_tokens_none {
158158
};
159159
}
160160

161-
impl_has_tokens!(AssocItem, AttrItem, Block, Expr, ForeignItem, Item, Pat, Path, Ty, Visibility);
161+
impl_has_tokens!(AssocItem, Block, Expr, ForeignItem, Item, Meta, Pat, Path, Ty, Visibility);
162162
impl_has_tokens_none!(Arm, ExprField, FieldDef, GenericParam, Param, PatField, Variant);
163163

164164
impl<T: AstDeref<Target: HasTokens>> HasTokens for T {
@@ -236,7 +236,7 @@ impl HasTokens for Nonterminal {
236236
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => expr.tokens(),
237237
Nonterminal::NtPat(pat) => pat.tokens(),
238238
Nonterminal::NtTy(ty) => ty.tokens(),
239-
Nonterminal::NtMeta(attr_item) => attr_item.tokens(),
239+
Nonterminal::NtMeta(meta) => meta.tokens(),
240240
Nonterminal::NtPath(path) => path.tokens(),
241241
Nonterminal::NtVis(vis) => vis.tokens(),
242242
Nonterminal::NtBlock(block) => block.tokens(),
@@ -249,7 +249,7 @@ impl HasTokens for Nonterminal {
249249
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => expr.tokens_mut(),
250250
Nonterminal::NtPat(pat) => pat.tokens_mut(),
251251
Nonterminal::NtTy(ty) => ty.tokens_mut(),
252-
Nonterminal::NtMeta(attr_item) => attr_item.tokens_mut(),
252+
Nonterminal::NtMeta(meta) => meta.tokens_mut(),
253253
Nonterminal::NtPath(path) => path.tokens_mut(),
254254
Nonterminal::NtVis(vis) => vis.tokens_mut(),
255255
Nonterminal::NtBlock(block) => block.tokens_mut(),
@@ -322,7 +322,7 @@ impl_has_attrs!(
322322
PatField,
323323
Variant,
324324
);
325-
impl_has_attrs_none!(Attribute, AttrItem, Block, Pat, Path, Ty, Visibility);
325+
impl_has_attrs_none!(Attribute, Block, Meta, Pat, Path, Ty, Visibility);
326326

327327
impl<T: AstDeref<Target: HasAttrs>> HasAttrs for T {
328328
const SUPPORTS_CUSTOM_INNER_ATTRS: bool = T::Target::SUPPORTS_CUSTOM_INNER_ATTRS;

compiler/rustc_ast/src/attr/mod.rs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Functions dealing with attributes and meta items.
22
33
use crate::ast::{
4-
AttrArgs, AttrArgsEq, AttrId, AttrItem, AttrKind, AttrStyle, AttrVec, Attribute, Safety,
4+
AttrArgs, AttrArgsEq, AttrId, AttrKind, AttrStyle, AttrVec, Attribute, Meta, Safety,
55
};
66
use crate::ast::{DelimArgs, Expr, ExprKind, LitKind, MetaItemLit};
77
use crate::ast::{MetaItem, MetaItemKind, NestedMetaItem, NormalAttr};
@@ -53,16 +53,16 @@ impl AttrIdGenerator {
5353
}
5454

5555
impl Attribute {
56-
pub fn get_normal_item(&self) -> &AttrItem {
56+
pub fn get_normal_meta(&self) -> &Meta {
5757
match &self.kind {
58-
AttrKind::Normal(normal) => &normal.item,
58+
AttrKind::Normal(normal) => &normal.meta,
5959
AttrKind::DocComment(..) => panic!("unexpected doc comment"),
6060
}
6161
}
6262

63-
pub fn unwrap_normal_item(self) -> AttrItem {
63+
pub fn unwrap_normal_meta(self) -> Meta {
6464
match self.kind {
65-
AttrKind::Normal(normal) => normal.into_inner().item,
65+
AttrKind::Normal(normal) => normal.into_inner().meta,
6666
AttrKind::DocComment(..) => panic!("unexpected doc comment"),
6767
}
6868
}
@@ -81,7 +81,7 @@ impl Attribute {
8181
pub fn ident(&self) -> Option<Ident> {
8282
match &self.kind {
8383
AttrKind::Normal(normal) => {
84-
if let [ident] = &*normal.item.path.segments {
84+
if let [ident] = &*normal.meta.path.segments {
8585
Some(ident.ident)
8686
} else {
8787
None
@@ -98,7 +98,7 @@ impl Attribute {
9898
pub fn path(&self) -> SmallVec<[Symbol; 1]> {
9999
match &self.kind {
100100
AttrKind::Normal(normal) => {
101-
normal.item.path.segments.iter().map(|s| s.ident.name).collect()
101+
normal.meta.path.segments.iter().map(|s| s.ident.name).collect()
102102
}
103103
AttrKind::DocComment(..) => smallvec![sym::doc],
104104
}
@@ -107,17 +107,17 @@ impl Attribute {
107107
#[inline]
108108
pub fn has_name(&self, name: Symbol) -> bool {
109109
match &self.kind {
110-
AttrKind::Normal(normal) => normal.item.path == name,
110+
AttrKind::Normal(normal) => normal.meta.path == name,
111111
AttrKind::DocComment(..) => false,
112112
}
113113
}
114114

115115
pub fn path_matches(&self, name: &[Symbol]) -> bool {
116116
match &self.kind {
117117
AttrKind::Normal(normal) => {
118-
normal.item.path.segments.len() == name.len()
118+
normal.meta.path.segments.len() == name.len()
119119
&& normal
120-
.item
120+
.meta
121121
.path
122122
.segments
123123
.iter()
@@ -130,22 +130,22 @@ impl Attribute {
130130

131131
pub fn is_word(&self) -> bool {
132132
if let AttrKind::Normal(normal) = &self.kind {
133-
matches!(normal.item.args, AttrArgs::Empty)
133+
matches!(normal.meta.args, AttrArgs::Empty)
134134
} else {
135135
false
136136
}
137137
}
138138

139139
pub fn meta_item_list(&self) -> Option<ThinVec<NestedMetaItem>> {
140140
match &self.kind {
141-
AttrKind::Normal(normal) => normal.item.meta_item_list(),
141+
AttrKind::Normal(normal) => normal.meta.meta_item_list(),
142142
AttrKind::DocComment(..) => None,
143143
}
144144
}
145145

146146
pub fn value_str(&self) -> Option<Symbol> {
147147
match &self.kind {
148-
AttrKind::Normal(normal) => normal.item.value_str(),
148+
AttrKind::Normal(normal) => normal.meta.value_str(),
149149
AttrKind::DocComment(..) => None,
150150
}
151151
}
@@ -158,8 +158,8 @@ impl Attribute {
158158
pub fn doc_str_and_comment_kind(&self) -> Option<(Symbol, CommentKind)> {
159159
match &self.kind {
160160
AttrKind::DocComment(kind, data) => Some((*data, *kind)),
161-
AttrKind::Normal(normal) if normal.item.path == sym::doc => {
162-
normal.item.value_str().map(|s| (s, CommentKind::Line))
161+
AttrKind::Normal(normal) if normal.meta.path == sym::doc => {
162+
normal.meta.value_str().map(|s| (s, CommentKind::Line))
163163
}
164164
_ => None,
165165
}
@@ -172,7 +172,7 @@ impl Attribute {
172172
pub fn doc_str(&self) -> Option<Symbol> {
173173
match &self.kind {
174174
AttrKind::DocComment(.., data) => Some(*data),
175-
AttrKind::Normal(normal) if normal.item.path == sym::doc => normal.item.value_str(),
175+
AttrKind::Normal(normal) if normal.meta.path == sym::doc => normal.meta.value_str(),
176176
_ => None,
177177
}
178178
}
@@ -190,14 +190,14 @@ impl Attribute {
190190
/// Extracts the MetaItem from inside this Attribute.
191191
pub fn meta(&self) -> Option<MetaItem> {
192192
match &self.kind {
193-
AttrKind::Normal(normal) => normal.item.meta(self.span),
193+
AttrKind::Normal(normal) => normal.meta.meta(self.span),
194194
AttrKind::DocComment(..) => None,
195195
}
196196
}
197197

198198
pub fn meta_kind(&self) -> Option<MetaItemKind> {
199199
match &self.kind {
200-
AttrKind::Normal(normal) => normal.item.meta_kind(),
200+
AttrKind::Normal(normal) => normal.meta.meta_kind(),
201201
AttrKind::DocComment(..) => None,
202202
}
203203
}
@@ -220,7 +220,7 @@ impl Attribute {
220220
}
221221
}
222222

223-
impl AttrItem {
223+
impl Meta {
224224
pub fn span(&self) -> Span {
225225
self.args.span().map_or(self.path.span, |args_span| self.path.span.to(args_span))
226226
}
@@ -579,18 +579,18 @@ pub fn mk_attr(
579579
args: AttrArgs,
580580
span: Span,
581581
) -> Attribute {
582-
mk_attr_from_item(g, AttrItem { unsafety, path, args, tokens: None }, None, style, span)
582+
mk_attr_from_meta(g, Meta { unsafety, path, args, tokens: None }, None, style, span)
583583
}
584584

585-
pub fn mk_attr_from_item(
585+
pub fn mk_attr_from_meta(
586586
g: &AttrIdGenerator,
587-
item: AttrItem,
587+
meta: Meta,
588588
tokens: Option<LazyAttrTokenStream>,
589589
style: AttrStyle,
590590
span: Span,
591591
) -> Attribute {
592592
Attribute {
593-
kind: AttrKind::Normal(P(NormalAttr { item, tokens })),
593+
kind: AttrKind::Normal(P(NormalAttr { meta, tokens })),
594594
id: g.mk_attr_id(),
595595
style,
596596
span,

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -642,10 +642,8 @@ fn noop_visit_attribute<T: MutVisitor>(attr: &mut Attribute, vis: &mut T) {
642642
let Attribute { kind, id: _, style: _, span } = attr;
643643
match kind {
644644
AttrKind::Normal(normal) => {
645-
let NormalAttr {
646-
item: AttrItem { unsafety: _, path, args, tokens },
647-
tokens: attr_tokens,
648-
} = &mut **normal;
645+
let NormalAttr { meta: Meta { unsafety: _, path, args, tokens }, tokens: attr_tokens } =
646+
&mut **normal;
649647
vis.visit_path(path);
650648
visit_attr_args(args, vis);
651649
visit_lazy_tts(tokens, vis);
@@ -825,8 +823,8 @@ fn visit_nonterminal<T: MutVisitor>(nt: &mut token::Nonterminal, vis: &mut T) {
825823
token::NtExpr(expr) => vis.visit_expr(expr),
826824
token::NtTy(ty) => vis.visit_ty(ty),
827825
token::NtLiteral(expr) => vis.visit_expr(expr),
828-
token::NtMeta(item) => {
829-
let AttrItem { unsafety: _, path, args, tokens } = item.deref_mut();
826+
token::NtMeta(meta) => {
827+
let Meta { unsafety: _, path, args, tokens } = meta.deref_mut();
830828
vis.visit_path(path);
831829
visit_attr_args(args, vis);
832830
visit_lazy_tts(tokens, vis);

compiler/rustc_ast/src/token.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -905,7 +905,7 @@ pub enum Nonterminal {
905905
NtTy(P<ast::Ty>),
906906
NtLiteral(P<ast::Expr>),
907907
/// Stuff inside brackets for attributes
908-
NtMeta(P<ast::AttrItem>),
908+
NtMeta(P<ast::Meta>),
909909
NtPath(P<ast::Path>),
910910
NtVis(P<ast::Visibility>),
911911
}
@@ -1002,7 +1002,7 @@ impl Nonterminal {
10021002
NtPat(pat) => pat.span,
10031003
NtExpr(expr) | NtLiteral(expr) => expr.span,
10041004
NtTy(ty) => ty.span,
1005-
NtMeta(attr_item) => attr_item.span(),
1005+
NtMeta(meta) => meta.span(),
10061006
NtPath(path) => path.span,
10071007
NtVis(vis) => vis.span,
10081008
}

compiler/rustc_ast/src/tokenstream.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ impl TokenStream {
462462
Nonterminal::NtStmt(stmt) => TokenStream::from_ast(stmt),
463463
Nonterminal::NtPat(pat) => TokenStream::from_ast(pat),
464464
Nonterminal::NtTy(ty) => TokenStream::from_ast(ty),
465-
Nonterminal::NtMeta(attr) => TokenStream::from_ast(attr),
465+
Nonterminal::NtMeta(meta) => TokenStream::from_ast(meta),
466466
Nonterminal::NtPath(path) => TokenStream::from_ast(path),
467467
Nonterminal::NtVis(vis) => TokenStream::from_ast(vis),
468468
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => TokenStream::from_ast(expr),

compiler/rustc_ast/src/visit.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,8 +1218,8 @@ pub fn walk_attribute<'a, V: Visitor<'a>>(visitor: &mut V, attr: &'a Attribute)
12181218
let Attribute { kind, id: _, style: _, span: _ } = attr;
12191219
match kind {
12201220
AttrKind::Normal(normal) => {
1221-
let NormalAttr { item, tokens: _ } = &**normal;
1222-
let AttrItem { unsafety: _, path, args, tokens: _ } = item;
1221+
let NormalAttr { meta, tokens: _ } = &**normal;
1222+
let Meta { unsafety: _, path, args, tokens: _ } = meta;
12231223
try_visit!(visitor.visit_path(path, DUMMY_NODE_ID));
12241224
try_visit!(walk_attr_args(visitor, args));
12251225
}

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -912,10 +912,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
912912
// Tokens are also not needed after macro expansion and parsing.
913913
let kind = match attr.kind {
914914
AttrKind::Normal(ref normal) => AttrKind::Normal(P(NormalAttr {
915-
item: AttrItem {
916-
unsafety: normal.item.unsafety,
917-
path: normal.item.path.clone(),
918-
args: self.lower_attr_args(&normal.item.args),
915+
meta: Meta {
916+
unsafety: normal.meta.unsafety,
917+
path: normal.meta.path.clone(),
918+
args: self.lower_attr_args(&normal.meta.args),
919919
tokens: None,
920920
},
921921
tokens: None,

compiler/rustc_ast_pretty/src/pprust/state.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
613613
ast::AttrStyle::Inner => self.word("#!["),
614614
ast::AttrStyle::Outer => self.word("#["),
615615
}
616-
self.print_attr_item(&normal.item, attr.span);
616+
self.print_meta(&normal.meta, attr.span);
617617
self.word("]");
618618
}
619619
ast::AttrKind::DocComment(comment_kind, data) => {
@@ -623,7 +623,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
623623
}
624624
}
625625

626-
fn print_attr_item(&mut self, item: &ast::AttrItem, span: Span) {
626+
fn print_meta(&mut self, item: &ast::Meta, span: Span) {
627627
self.ibox(0);
628628
match &item.args {
629629
AttrArgs::Delimited(DelimArgs { dspan: _, delim, tokens }) => self.print_mac_common(
@@ -1011,8 +1011,8 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
10111011
})
10121012
}
10131013

1014-
fn attr_item_to_string(&self, ai: &ast::AttrItem) -> String {
1015-
Self::to_string(|s| s.print_attr_item(ai, ai.path.span))
1014+
fn meta_to_string(&self, meta: &ast::Meta) -> String {
1015+
Self::to_string(|s| s.print_meta(meta, meta.path.span))
10161016
}
10171017

10181018
fn tts_to_string(&self, tokens: &TokenStream) -> String {

compiler/rustc_builtin_macros/src/cmdline_attrs.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use crate::errors;
44
use rustc_ast::attr::mk_attr;
55
use rustc_ast::token;
6-
use rustc_ast::{self as ast, AttrItem, AttrStyle};
6+
use rustc_ast::{self as ast, AttrStyle, Meta};
77
use rustc_parse::{new_parser_from_source_str, unwrap_or_emit_fatal};
88
use rustc_session::parse::ParseSess;
99
use rustc_span::FileName;
@@ -17,8 +17,8 @@ pub fn inject(krate: &mut ast::Crate, psess: &ParseSess, attrs: &[String]) {
1717
));
1818

1919
let start_span = parser.token.span;
20-
let AttrItem { unsafety, path, args, tokens: _ } = match parser.parse_attr_item(false) {
21-
Ok(ai) => ai,
20+
let Meta { unsafety, path, args, tokens: _ } = match parser.parse_meta(false) {
21+
Ok(meta) => meta,
2222
Err(err) => {
2323
err.emit();
2424
continue;

0 commit comments

Comments
 (0)