Skip to content

Commit c9cf9c6

Browse files
committed
Auto merge of rust-lang#92294 - Kobzol:rustdoc-meta-kind, r=GuillaumeGomez
Add Attribute::meta_kind The `AttrItem::meta` function is being called on a lot of places, however almost always the caller is only interested in the `kind` of the result `MetaItem`. Before, the `path` had to be cloned in order to get the kind, now it does not have to be. There is a larger related "problem". In a lot of places, something wants to know contents of attributes. This is accessed through `Attribute::meta_item_list`, which calls `AttrItem::meta` (now `AttrItem::meta_kind`), among other methods. When this function is called, the meta item list has to be recreated from scratch. Everytime something asks a simple question (like is this item/list of attributes `#[doc(hidden)]`?), the tokens of the attribute(s) are cloned, parsed and the results are allocated on the heap. That seems really unnecessary. What would be the best way to cache this? Turn `meta_item_list` into a query perhaps? Related PR: rust-lang#92227 r? rust-lang/rustdoc
2 parents 4d2e0fd + 047275a commit c9cf9c6

File tree

4 files changed

+32
-11
lines changed

4 files changed

+32
-11
lines changed

compiler/rustc_ast/src/attr/mod.rs

+25-4
Original file line numberDiff line numberDiff line change
@@ -136,15 +136,15 @@ impl Attribute {
136136

137137
pub fn value_str(&self) -> Option<Symbol> {
138138
match self.kind {
139-
AttrKind::Normal(ref item, _) => item.meta(self.span).and_then(|meta| meta.value_str()),
139+
AttrKind::Normal(ref item, _) => item.meta_kind().and_then(|kind| kind.value_str()),
140140
AttrKind::DocComment(..) => None,
141141
}
142142
}
143143

144144
pub fn meta_item_list(&self) -> Option<Vec<NestedMetaItem>> {
145145
match self.kind {
146-
AttrKind::Normal(ref item, _) => match item.meta(self.span) {
147-
Some(MetaItem { kind: MetaItemKind::List(list), .. }) => Some(list),
146+
AttrKind::Normal(ref item, _) => match item.meta_kind() {
147+
Some(MetaItemKind::List(list)) => Some(list),
148148
_ => None,
149149
},
150150
AttrKind::DocComment(..) => None,
@@ -228,6 +228,10 @@ impl AttrItem {
228228
span,
229229
})
230230
}
231+
232+
pub fn meta_kind(&self) -> Option<MetaItemKind> {
233+
Some(MetaItemKind::from_mac_args(&self.args)?)
234+
}
231235
}
232236

233237
impl Attribute {
@@ -242,7 +246,7 @@ impl Attribute {
242246
match self.kind {
243247
AttrKind::DocComment(.., data) => Some(data),
244248
AttrKind::Normal(ref item, _) if item.path == sym::doc => {
245-
item.meta(self.span).and_then(|meta| meta.value_str())
249+
item.meta_kind().and_then(|kind| kind.value_str())
246250
}
247251
_ => None,
248252
}
@@ -270,6 +274,13 @@ impl Attribute {
270274
}
271275
}
272276

277+
pub fn meta_kind(&self) -> Option<MetaItemKind> {
278+
match self.kind {
279+
AttrKind::Normal(ref item, _) => item.meta_kind(),
280+
AttrKind::DocComment(..) => None,
281+
}
282+
}
283+
273284
pub fn tokens(&self) -> AttrAnnotatedTokenStream {
274285
match self.kind {
275286
AttrKind::Normal(_, ref tokens) => tokens
@@ -436,6 +447,16 @@ impl MetaItem {
436447
}
437448

438449
impl MetaItemKind {
450+
pub fn value_str(&self) -> Option<Symbol> {
451+
match self {
452+
MetaItemKind::NameValue(ref v) => match v.kind {
453+
LitKind::Str(ref s, _) => Some(*s),
454+
_ => None,
455+
},
456+
_ => None,
457+
}
458+
}
459+
439460
pub fn mac_args(&self, span: Span) -> MacArgs {
440461
match self {
441462
MetaItemKind::Word => MacArgs::Empty,

compiler/rustc_interface/src/util.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ pub(crate) fn check_attr_crate_type(
484484
return;
485485
}
486486

487-
if let ast::MetaItemKind::NameValue(spanned) = a.meta().unwrap().kind {
487+
if let ast::MetaItemKind::NameValue(spanned) = a.meta_kind().unwrap() {
488488
let span = spanned.span;
489489
let lev_candidate = find_best_match_for_name(
490490
&CRATE_TYPES.iter().map(|(k, _)| *k).collect::<Vec<_>>(),

compiler/rustc_passes/src/lib_features.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// and `#[unstable (..)]`), but are not declared in one single location
55
// (unlike lang features), which means we need to collect them instead.
66

7-
use rustc_ast::{Attribute, MetaItem, MetaItemKind};
7+
use rustc_ast::{Attribute, MetaItemKind};
88
use rustc_errors::struct_span_err;
99
use rustc_hir::intravisit::{NestedVisitorMap, Visitor};
1010
use rustc_middle::hir::map::Map;
@@ -34,8 +34,8 @@ impl<'tcx> LibFeatureCollector<'tcx> {
3434
// Find a stability attribute (i.e., `#[stable (..)]`, `#[unstable (..)]`,
3535
// `#[rustc_const_unstable (..)]`).
3636
if let Some(stab_attr) = stab_attrs.iter().find(|stab_attr| attr.has_name(**stab_attr)) {
37-
let meta_item = attr.meta();
38-
if let Some(MetaItem { kind: MetaItemKind::List(ref metas), .. }) = meta_item {
37+
let meta_kind = attr.meta_kind();
38+
if let Some(MetaItemKind::List(ref metas)) = meta_kind {
3939
let mut feature = None;
4040
let mut since = None;
4141
for meta in metas {

compiler/rustc_typeck/src/collect.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2894,7 +2894,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs {
28942894
}
28952895
}
28962896
} else if attr.has_name(sym::instruction_set) {
2897-
codegen_fn_attrs.instruction_set = match attr.meta().map(|i| i.kind) {
2897+
codegen_fn_attrs.instruction_set = match attr.meta_kind() {
28982898
Some(MetaItemKind::List(ref items)) => match items.as_slice() {
28992899
[NestedMetaItem::MetaItem(set)] => {
29002900
let segments =
@@ -2999,7 +2999,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs {
29992999
if !attr.has_name(sym::inline) {
30003000
return ia;
30013001
}
3002-
match attr.meta().map(|i| i.kind) {
3002+
match attr.meta_kind() {
30033003
Some(MetaItemKind::Word) => InlineAttr::Hint,
30043004
Some(MetaItemKind::List(ref items)) => {
30053005
inline_span = Some(attr.span);
@@ -3038,7 +3038,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs {
30383038
return ia;
30393039
}
30403040
let err = |sp, s| struct_span_err!(tcx.sess.diagnostic(), sp, E0722, "{}", s).emit();
3041-
match attr.meta().map(|i| i.kind) {
3041+
match attr.meta_kind() {
30423042
Some(MetaItemKind::Word) => {
30433043
err(attr.span, "expected one argument");
30443044
ia

0 commit comments

Comments
 (0)