Skip to content

Commit b290843

Browse files
Get rid of static predefined symbols
Make them all `const`.
1 parent 42de0a5 commit b290843

File tree

24 files changed

+120
-112
lines changed

24 files changed

+120
-112
lines changed

src/tools/rust-analyzer/crates/hir-def/src/attr.rs

Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -117,17 +117,20 @@ impl Attrs {
117117
}
118118

119119
impl Attrs {
120-
pub fn by_key<'attrs>(&'attrs self, key: &'attrs Symbol) -> AttrQuery<'attrs> {
120+
#[inline]
121+
pub fn by_key(&self, key: Symbol) -> AttrQuery<'_> {
121122
AttrQuery { attrs: self, key }
122123
}
123124

125+
#[inline]
124126
pub fn rust_analyzer_tool(&self) -> impl Iterator<Item = &Attr> {
125127
self.iter()
126128
.filter(|&attr| attr.path.segments().first().is_some_and(|s| *s == sym::rust_analyzer))
127129
}
128130

131+
#[inline]
129132
pub fn cfg(&self) -> Option<CfgExpr> {
130-
let mut cfgs = self.by_key(&sym::cfg).tt_values().map(CfgExpr::parse);
133+
let mut cfgs = self.by_key(sym::cfg).tt_values().map(CfgExpr::parse);
131134
let first = cfgs.next()?;
132135
match cfgs.next() {
133136
Some(second) => {
@@ -138,63 +141,76 @@ impl Attrs {
138141
}
139142
}
140143

144+
#[inline]
141145
pub fn cfgs(&self) -> impl Iterator<Item = CfgExpr> + '_ {
142-
self.by_key(&sym::cfg).tt_values().map(CfgExpr::parse)
146+
self.by_key(sym::cfg).tt_values().map(CfgExpr::parse)
143147
}
144148

149+
#[inline]
145150
pub(crate) fn is_cfg_enabled(&self, cfg_options: &CfgOptions) -> bool {
146151
match self.cfg() {
147152
None => true,
148153
Some(cfg) => cfg_options.check(&cfg) != Some(false),
149154
}
150155
}
151156

157+
#[inline]
152158
pub fn lang(&self) -> Option<&Symbol> {
153-
self.by_key(&sym::lang).string_value()
159+
self.by_key(sym::lang).string_value()
154160
}
155161

162+
#[inline]
156163
pub fn lang_item(&self) -> Option<LangItem> {
157-
self.by_key(&sym::lang).string_value().and_then(LangItem::from_symbol)
164+
self.by_key(sym::lang).string_value().and_then(LangItem::from_symbol)
158165
}
159166

167+
#[inline]
160168
pub fn has_doc_hidden(&self) -> bool {
161-
self.by_key(&sym::doc).tt_values().any(|tt| {
169+
self.by_key(sym::doc).tt_values().any(|tt| {
162170
tt.top_subtree().delimiter.kind == DelimiterKind::Parenthesis &&
163171
matches!(tt.token_trees().flat_tokens(), [tt::TokenTree::Leaf(tt::Leaf::Ident(ident))] if ident.sym == sym::hidden)
164172
})
165173
}
166174

175+
#[inline]
167176
pub fn has_doc_notable_trait(&self) -> bool {
168-
self.by_key(&sym::doc).tt_values().any(|tt| {
177+
self.by_key(sym::doc).tt_values().any(|tt| {
169178
tt.top_subtree().delimiter.kind == DelimiterKind::Parenthesis &&
170179
matches!(tt.token_trees().flat_tokens(), [tt::TokenTree::Leaf(tt::Leaf::Ident(ident))] if ident.sym == sym::notable_trait)
171180
})
172181
}
173182

183+
#[inline]
174184
pub fn doc_exprs(&self) -> impl Iterator<Item = DocExpr> + '_ {
175-
self.by_key(&sym::doc).tt_values().map(DocExpr::parse)
185+
self.by_key(sym::doc).tt_values().map(DocExpr::parse)
176186
}
177187

188+
#[inline]
178189
pub fn doc_aliases(&self) -> impl Iterator<Item = Symbol> + '_ {
179190
self.doc_exprs().flat_map(|doc_expr| doc_expr.aliases().to_vec())
180191
}
181192

193+
#[inline]
182194
pub fn export_name(&self) -> Option<&Symbol> {
183-
self.by_key(&sym::export_name).string_value()
195+
self.by_key(sym::export_name).string_value()
184196
}
185197

198+
#[inline]
186199
pub fn is_proc_macro(&self) -> bool {
187-
self.by_key(&sym::proc_macro).exists()
200+
self.by_key(sym::proc_macro).exists()
188201
}
189202

203+
#[inline]
190204
pub fn is_proc_macro_attribute(&self) -> bool {
191-
self.by_key(&sym::proc_macro_attribute).exists()
205+
self.by_key(sym::proc_macro_attribute).exists()
192206
}
193207

208+
#[inline]
194209
pub fn is_proc_macro_derive(&self) -> bool {
195-
self.by_key(&sym::proc_macro_derive).exists()
210+
self.by_key(sym::proc_macro_derive).exists()
196211
}
197212

213+
#[inline]
198214
pub fn is_test(&self) -> bool {
199215
self.iter().any(|it| {
200216
it.path()
@@ -210,29 +226,34 @@ impl Attrs {
210226
})
211227
}
212228

229+
#[inline]
213230
pub fn is_ignore(&self) -> bool {
214-
self.by_key(&sym::ignore).exists()
231+
self.by_key(sym::ignore).exists()
215232
}
216233

234+
#[inline]
217235
pub fn is_bench(&self) -> bool {
218-
self.by_key(&sym::bench).exists()
236+
self.by_key(sym::bench).exists()
219237
}
220238

239+
#[inline]
221240
pub fn is_unstable(&self) -> bool {
222-
self.by_key(&sym::unstable).exists()
241+
self.by_key(sym::unstable).exists()
223242
}
224243

244+
#[inline]
225245
pub fn rustc_legacy_const_generics(&self) -> Option<Box<Box<[u32]>>> {
226-
self.by_key(&sym::rustc_legacy_const_generics)
246+
self.by_key(sym::rustc_legacy_const_generics)
227247
.tt_values()
228248
.next()
229249
.map(parse_rustc_legacy_const_generics)
230250
.filter(|it| !it.is_empty())
231251
.map(Box::new)
232252
}
233253

254+
#[inline]
234255
pub fn repr(&self) -> Option<ReprOptions> {
235-
self.by_key(&sym::repr).tt_values().filter_map(parse_repr_tt).fold(None, |acc, repr| {
256+
self.by_key(sym::repr).tt_values().filter_map(parse_repr_tt).fold(None, |acc, repr| {
236257
acc.map_or(Some(repr), |mut acc| {
237258
merge_repr(&mut acc, repr);
238259
Some(acc)
@@ -681,36 +702,42 @@ impl AttrSourceMap {
681702
}
682703
}
683704

684-
#[derive(Debug, Clone, Copy)]
705+
#[derive(Debug, Clone)]
685706
pub struct AttrQuery<'attr> {
686707
attrs: &'attr Attrs,
687-
key: &'attr Symbol,
708+
key: Symbol,
688709
}
689710

690711
impl<'attr> AttrQuery<'attr> {
712+
#[inline]
691713
pub fn tt_values(self) -> impl Iterator<Item = &'attr crate::tt::TopSubtree> {
692714
self.attrs().filter_map(|attr| attr.token_tree_value())
693715
}
694716

717+
#[inline]
695718
pub fn string_value(self) -> Option<&'attr Symbol> {
696719
self.attrs().find_map(|attr| attr.string_value())
697720
}
698721

722+
#[inline]
699723
pub fn string_value_with_span(self) -> Option<(&'attr Symbol, span::Span)> {
700724
self.attrs().find_map(|attr| attr.string_value_with_span())
701725
}
702726

727+
#[inline]
703728
pub fn string_value_unescape(self) -> Option<Cow<'attr, str>> {
704729
self.attrs().find_map(|attr| attr.string_value_unescape())
705730
}
706731

732+
#[inline]
707733
pub fn exists(self) -> bool {
708734
self.attrs().next().is_some()
709735
}
710736

737+
#[inline]
711738
pub fn attrs(self) -> impl Iterator<Item = &'attr Attr> + Clone {
712739
let key = self.key;
713-
self.attrs.iter().filter(move |attr| attr.path.as_ident().is_some_and(|s| *s == *key))
740+
self.attrs.iter().filter(move |attr| attr.path.as_ident().is_some_and(|s| *s == key))
714741
}
715742

716743
/// Find string value for a specific key inside token tree
@@ -719,10 +746,11 @@ impl<'attr> AttrQuery<'attr> {
719746
/// #[doc(html_root_url = "url")]
720747
/// ^^^^^^^^^^^^^ key
721748
/// ```
722-
pub fn find_string_value_in_tt(self, key: &'attr Symbol) -> Option<&'attr str> {
749+
#[inline]
750+
pub fn find_string_value_in_tt(self, key: Symbol) -> Option<&'attr str> {
723751
self.tt_values().find_map(|tt| {
724752
let name = tt.iter()
725-
.skip_while(|tt| !matches!(tt, TtElement::Leaf(tt::Leaf::Ident(tt::Ident { sym, ..} )) if *sym == *key))
753+
.skip_while(|tt| !matches!(tt, TtElement::Leaf(tt::Leaf::Ident(tt::Ident { sym, ..} )) if *sym == key))
726754
.nth(2);
727755

728756
match name {

src/tools/rust-analyzer/crates/hir-def/src/nameres/collector.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1712,7 +1712,7 @@ impl ModCollector<'_, '_> {
17121712
id: ItemTreeId::new(self.tree_id, item_tree_id),
17131713
}
17141714
.intern(db);
1715-
let is_prelude = attrs.by_key(&sym::prelude_import).exists();
1715+
let is_prelude = attrs.by_key(sym::prelude_import).exists();
17161716
Import::from_use(
17171717
self.item_tree,
17181718
ItemTreeId::new(self.tree_id, item_tree_id),
@@ -1770,7 +1770,7 @@ impl ModCollector<'_, '_> {
17701770
if !is_self {
17711771
self.process_macro_use_extern_crate(
17721772
id,
1773-
attrs.by_key(&sym::macro_use).attrs(),
1773+
attrs.by_key(sym::macro_use).attrs(),
17741774
resolved.krate,
17751775
);
17761776
}
@@ -2015,8 +2015,8 @@ impl ModCollector<'_, '_> {
20152015
}
20162016

20172017
fn collect_module(&mut self, module_id: FileItemTreeId<Mod>, attrs: &Attrs) {
2018-
let path_attr = attrs.by_key(&sym::path).string_value_unescape();
2019-
let is_macro_use = attrs.by_key(&sym::macro_use).exists();
2018+
let path_attr = attrs.by_key(sym::path).string_value_unescape();
2019+
let is_macro_use = attrs.by_key(sym::macro_use).exists();
20202020
let module = &self.item_tree[module_id];
20212021
match &module.kind {
20222022
// inline module, just recurse
@@ -2093,7 +2093,7 @@ impl ModCollector<'_, '_> {
20932093
let is_macro_use = is_macro_use
20942094
|| item_tree
20952095
.top_level_attrs(db, krate)
2096-
.by_key(&sym::macro_use)
2096+
.by_key(sym::macro_use)
20972097
.exists();
20982098
if is_macro_use {
20992099
self.import_all_legacy_macros(module_id);
@@ -2252,11 +2252,11 @@ impl ModCollector<'_, '_> {
22522252
let attrs = self.item_tree.attrs(self.def_collector.db, krate, ModItem::from(id).into());
22532253
let ast_id = InFile::new(self.file_id(), mac.ast_id.upcast());
22542254

2255-
let export_attr = attrs.by_key(&sym::macro_export);
2255+
let export_attr = || attrs.by_key(sym::macro_export);
22562256

2257-
let is_export = export_attr.exists();
2257+
let is_export = export_attr().exists();
22582258
let local_inner = if is_export {
2259-
export_attr.tt_values().flat_map(|it| it.iter()).any(|it| match it {
2259+
export_attr().tt_values().flat_map(|it| it.iter()).any(|it| match it {
22602260
tt::TtElement::Leaf(tt::Leaf::Ident(ident)) => ident.sym == sym::local_inner_macros,
22612261
_ => false,
22622262
})
@@ -2265,17 +2265,17 @@ impl ModCollector<'_, '_> {
22652265
};
22662266

22672267
// Case 1: builtin macros
2268-
let expander = if attrs.by_key(&sym::rustc_builtin_macro).exists() {
2268+
let expander = if attrs.by_key(sym::rustc_builtin_macro).exists() {
22692269
// `#[rustc_builtin_macro = "builtin_name"]` overrides the `macro_rules!` name.
22702270
let name;
2271-
let name = match attrs.by_key(&sym::rustc_builtin_macro).string_value_with_span() {
2271+
let name = match attrs.by_key(sym::rustc_builtin_macro).string_value_with_span() {
22722272
Some((it, span)) => {
22732273
name = Name::new_symbol(it.clone(), span.ctx);
22742274
&name
22752275
}
22762276
None => {
22772277
let explicit_name =
2278-
attrs.by_key(&sym::rustc_builtin_macro).tt_values().next().and_then(|tt| {
2278+
attrs.by_key(sym::rustc_builtin_macro).tt_values().next().and_then(|tt| {
22792279
match tt.token_trees().flat_tokens().first() {
22802280
Some(tt::TokenTree::Leaf(tt::Leaf::Ident(name))) => Some(name),
22812281
_ => None,
@@ -2305,7 +2305,7 @@ impl ModCollector<'_, '_> {
23052305
// Case 2: normal `macro_rules!` macro
23062306
MacroExpander::Declarative
23072307
};
2308-
let allow_internal_unsafe = attrs.by_key(&sym::allow_internal_unsafe).exists();
2308+
let allow_internal_unsafe = attrs.by_key(sym::allow_internal_unsafe).exists();
23092309

23102310
let mut flags = MacroRulesLocFlags::empty();
23112311
flags.set(MacroRulesLocFlags::LOCAL_INNER, local_inner);
@@ -2339,14 +2339,14 @@ impl ModCollector<'_, '_> {
23392339
// Case 1: builtin macros
23402340
let mut helpers_opt = None;
23412341
let attrs = self.item_tree.attrs(self.def_collector.db, krate, ModItem::from(id).into());
2342-
let expander = if attrs.by_key(&sym::rustc_builtin_macro).exists() {
2342+
let expander = if attrs.by_key(sym::rustc_builtin_macro).exists() {
23432343
if let Some(expander) = find_builtin_macro(&mac.name) {
23442344
match expander {
23452345
Either::Left(it) => MacroExpander::BuiltIn(it),
23462346
Either::Right(it) => MacroExpander::BuiltInEager(it),
23472347
}
23482348
} else if let Some(expander) = find_builtin_derive(&mac.name) {
2349-
if let Some(attr) = attrs.by_key(&sym::rustc_builtin_macro).tt_values().next() {
2349+
if let Some(attr) = attrs.by_key(sym::rustc_builtin_macro).tt_values().next() {
23502350
// NOTE: The item *may* have both `#[rustc_builtin_macro]` and `#[proc_macro_derive]`,
23512351
// in which case rustc ignores the helper attributes from the latter, but it
23522352
// "doesn't make sense in practice" (see rust-lang/rust#87027).
@@ -2377,7 +2377,7 @@ impl ModCollector<'_, '_> {
23772377
// Case 2: normal `macro`
23782378
MacroExpander::Declarative
23792379
};
2380-
let allow_internal_unsafe = attrs.by_key(&sym::allow_internal_unsafe).exists();
2380+
let allow_internal_unsafe = attrs.by_key(sym::allow_internal_unsafe).exists();
23812381

23822382
let macro_id = Macro2Loc {
23832383
container: module,

src/tools/rust-analyzer/crates/hir-def/src/nameres/proc_macro.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ impl Attrs {
3535
Some(ProcMacroDef { name: func_name.clone(), kind: ProcMacroKind::Bang })
3636
} else if self.is_proc_macro_attribute() {
3737
Some(ProcMacroDef { name: func_name.clone(), kind: ProcMacroKind::Attr })
38-
} else if self.by_key(&sym::proc_macro_derive).exists() {
38+
} else if self.by_key(sym::proc_macro_derive).exists() {
3939
let derive = self.parse_proc_macro_derive();
4040
Some(match derive {
4141
Some((name, helpers)) => {
@@ -52,12 +52,12 @@ impl Attrs {
5252
}
5353

5454
pub fn parse_proc_macro_derive(&self) -> Option<(Name, Box<[Name]>)> {
55-
let derive = self.by_key(&sym::proc_macro_derive).tt_values().next()?;
55+
let derive = self.by_key(sym::proc_macro_derive).tt_values().next()?;
5656
parse_macro_name_and_helper_attrs(derive)
5757
}
5858

5959
pub fn parse_rustc_builtin_macro(&self) -> Option<(Name, Box<[Name]>)> {
60-
let derive = self.by_key(&sym::rustc_builtin_macro).tt_values().next()?;
60+
let derive = self.by_key(sym::rustc_builtin_macro).tt_values().next()?;
6161
parse_macro_name_and_helper_attrs(derive)
6262
}
6363
}

0 commit comments

Comments
 (0)