Skip to content

Commit b6eb883

Browse files
authored
Merge pull request rust-lang#19644 from ChayimFriedman2/const-syms
internal: Make predefined symbols `const` instead of `static`
2 parents d7ddec6 + 3b2bb61 commit b6eb883

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+438
-479
lines changed

src/tools/rust-analyzer/crates/cfg/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub struct CfgOptions {
3131

3232
impl Default for CfgOptions {
3333
fn default() -> Self {
34-
Self { enabled: FxHashSet::from_iter([CfgAtom::Flag(sym::true_.clone())]) }
34+
Self { enabled: FxHashSet::from_iter([CfgAtom::Flag(sym::true_)]) }
3535
}
3636
}
3737

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

Lines changed: 51 additions & 27 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,101 +141,115 @@ 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()
201217
.segments()
202218
.iter()
203219
.rev()
204-
.zip(
205-
[sym::core.clone(), sym::prelude.clone(), sym::v1.clone(), sym::test.clone()]
206-
.iter()
207-
.rev(),
208-
)
220+
.zip([sym::core, sym::prelude, sym::v1, sym::test].iter().rev())
209221
.all(|it| it.0 == it.1)
210222
})
211223
}
212224

225+
#[inline]
213226
pub fn is_ignore(&self) -> bool {
214-
self.by_key(&sym::ignore).exists()
227+
self.by_key(sym::ignore).exists()
215228
}
216229

230+
#[inline]
217231
pub fn is_bench(&self) -> bool {
218-
self.by_key(&sym::bench).exists()
232+
self.by_key(sym::bench).exists()
219233
}
220234

235+
#[inline]
221236
pub fn is_unstable(&self) -> bool {
222-
self.by_key(&sym::unstable).exists()
237+
self.by_key(sym::unstable).exists()
223238
}
224239

240+
#[inline]
225241
pub fn rustc_legacy_const_generics(&self) -> Option<Box<Box<[u32]>>> {
226-
self.by_key(&sym::rustc_legacy_const_generics)
242+
self.by_key(sym::rustc_legacy_const_generics)
227243
.tt_values()
228244
.next()
229245
.map(parse_rustc_legacy_const_generics)
230246
.filter(|it| !it.is_empty())
231247
.map(Box::new)
232248
}
233249

250+
#[inline]
234251
pub fn repr(&self) -> Option<ReprOptions> {
235-
self.by_key(&sym::repr).tt_values().filter_map(parse_repr_tt).fold(None, |acc, repr| {
252+
self.by_key(sym::repr).tt_values().filter_map(parse_repr_tt).fold(None, |acc, repr| {
236253
acc.map_or(Some(repr), |mut acc| {
237254
merge_repr(&mut acc, repr);
238255
Some(acc)
@@ -681,36 +698,42 @@ impl AttrSourceMap {
681698
}
682699
}
683700

684-
#[derive(Debug, Clone, Copy)]
701+
#[derive(Debug, Clone)]
685702
pub struct AttrQuery<'attr> {
686703
attrs: &'attr Attrs,
687-
key: &'attr Symbol,
704+
key: Symbol,
688705
}
689706

690707
impl<'attr> AttrQuery<'attr> {
708+
#[inline]
691709
pub fn tt_values(self) -> impl Iterator<Item = &'attr crate::tt::TopSubtree> {
692710
self.attrs().filter_map(|attr| attr.token_tree_value())
693711
}
694712

713+
#[inline]
695714
pub fn string_value(self) -> Option<&'attr Symbol> {
696715
self.attrs().find_map(|attr| attr.string_value())
697716
}
698717

718+
#[inline]
699719
pub fn string_value_with_span(self) -> Option<(&'attr Symbol, span::Span)> {
700720
self.attrs().find_map(|attr| attr.string_value_with_span())
701721
}
702722

723+
#[inline]
703724
pub fn string_value_unescape(self) -> Option<Cow<'attr, str>> {
704725
self.attrs().find_map(|attr| attr.string_value_unescape())
705726
}
706727

728+
#[inline]
707729
pub fn exists(self) -> bool {
708730
self.attrs().next().is_some()
709731
}
710732

733+
#[inline]
711734
pub fn attrs(self) -> impl Iterator<Item = &'attr Attr> + Clone {
712735
let key = self.key;
713-
self.attrs.iter().filter(move |attr| attr.path.as_ident().is_some_and(|s| *s == *key))
736+
self.attrs.iter().filter(move |attr| attr.path.as_ident().is_some_and(|s| *s == key))
714737
}
715738

716739
/// Find string value for a specific key inside token tree
@@ -719,10 +742,11 @@ impl<'attr> AttrQuery<'attr> {
719742
/// #[doc(html_root_url = "url")]
720743
/// ^^^^^^^^^^^^^ key
721744
/// ```
722-
pub fn find_string_value_in_tt(self, key: &'attr Symbol) -> Option<&'attr str> {
745+
#[inline]
746+
pub fn find_string_value_in_tt(self, key: Symbol) -> Option<&'attr str> {
723747
self.tt_values().find_map(|tt| {
724748
let name = tt.iter()
725-
.skip_while(|tt| !matches!(tt, TtElement::Leaf(tt::Leaf::Ident(tt::Ident { sym, ..} )) if *sym == *key))
749+
.skip_while(|tt| !matches!(tt, TtElement::Leaf(tt::Leaf::Ident(tt::Ident { sym, ..} )) if *sym == key))
726750
.nth(2);
727751

728752
match name {

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

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -51,28 +51,28 @@ impl BuiltinType {
5151
#[rustfmt::skip]
5252
pub fn all_builtin_types() -> [(Name, BuiltinType); 19] {
5353
[
54-
(Name::new_symbol_root(sym::char.clone()), BuiltinType::Char),
55-
(Name::new_symbol_root(sym::bool.clone()), BuiltinType::Bool),
56-
(Name::new_symbol_root(sym::str.clone()), BuiltinType::Str),
57-
58-
(Name::new_symbol_root(sym::isize.clone()), BuiltinType::Int(BuiltinInt::Isize)),
59-
(Name::new_symbol_root(sym::i8.clone()), BuiltinType::Int(BuiltinInt::I8)),
60-
(Name::new_symbol_root(sym::i16.clone()), BuiltinType::Int(BuiltinInt::I16)),
61-
(Name::new_symbol_root(sym::i32.clone()), BuiltinType::Int(BuiltinInt::I32)),
62-
(Name::new_symbol_root(sym::i64.clone()), BuiltinType::Int(BuiltinInt::I64)),
63-
(Name::new_symbol_root(sym::i128.clone()), BuiltinType::Int(BuiltinInt::I128)),
64-
65-
(Name::new_symbol_root(sym::usize.clone()), BuiltinType::Uint(BuiltinUint::Usize)),
66-
(Name::new_symbol_root(sym::u8.clone()), BuiltinType::Uint(BuiltinUint::U8)),
67-
(Name::new_symbol_root(sym::u16.clone()), BuiltinType::Uint(BuiltinUint::U16)),
68-
(Name::new_symbol_root(sym::u32.clone()), BuiltinType::Uint(BuiltinUint::U32)),
69-
(Name::new_symbol_root(sym::u64.clone()), BuiltinType::Uint(BuiltinUint::U64)),
70-
(Name::new_symbol_root(sym::u128.clone()), BuiltinType::Uint(BuiltinUint::U128)),
71-
72-
(Name::new_symbol_root(sym::f16.clone()), BuiltinType::Float(BuiltinFloat::F16)),
73-
(Name::new_symbol_root(sym::f32.clone()), BuiltinType::Float(BuiltinFloat::F32)),
74-
(Name::new_symbol_root(sym::f64.clone()), BuiltinType::Float(BuiltinFloat::F64)),
75-
(Name::new_symbol_root(sym::f128.clone()), BuiltinType::Float(BuiltinFloat::F128)),
54+
(Name::new_symbol_root(sym::char), BuiltinType::Char),
55+
(Name::new_symbol_root(sym::bool), BuiltinType::Bool),
56+
(Name::new_symbol_root(sym::str), BuiltinType::Str),
57+
58+
(Name::new_symbol_root(sym::isize), BuiltinType::Int(BuiltinInt::Isize)),
59+
(Name::new_symbol_root(sym::i8), BuiltinType::Int(BuiltinInt::I8)),
60+
(Name::new_symbol_root(sym::i16), BuiltinType::Int(BuiltinInt::I16)),
61+
(Name::new_symbol_root(sym::i32), BuiltinType::Int(BuiltinInt::I32)),
62+
(Name::new_symbol_root(sym::i64), BuiltinType::Int(BuiltinInt::I64)),
63+
(Name::new_symbol_root(sym::i128), BuiltinType::Int(BuiltinInt::I128)),
64+
65+
(Name::new_symbol_root(sym::usize), BuiltinType::Uint(BuiltinUint::Usize)),
66+
(Name::new_symbol_root(sym::u8), BuiltinType::Uint(BuiltinUint::U8)),
67+
(Name::new_symbol_root(sym::u16), BuiltinType::Uint(BuiltinUint::U16)),
68+
(Name::new_symbol_root(sym::u32), BuiltinType::Uint(BuiltinUint::U32)),
69+
(Name::new_symbol_root(sym::u64), BuiltinType::Uint(BuiltinUint::U64)),
70+
(Name::new_symbol_root(sym::u128), BuiltinType::Uint(BuiltinUint::U128)),
71+
72+
(Name::new_symbol_root(sym::f16), BuiltinType::Float(BuiltinFloat::F16)),
73+
(Name::new_symbol_root(sym::f32), BuiltinType::Float(BuiltinFloat::F32)),
74+
(Name::new_symbol_root(sym::f64), BuiltinType::Float(BuiltinFloat::F64)),
75+
(Name::new_symbol_root(sym::f128), BuiltinType::Float(BuiltinFloat::F128)),
7676
]
7777
}
7878

@@ -86,30 +86,30 @@ impl BuiltinType {
8686
impl AsName for BuiltinType {
8787
fn as_name(&self) -> Name {
8888
match self {
89-
BuiltinType::Char => Name::new_symbol_root(sym::char.clone()),
90-
BuiltinType::Bool => Name::new_symbol_root(sym::bool.clone()),
91-
BuiltinType::Str => Name::new_symbol_root(sym::str.clone()),
89+
BuiltinType::Char => Name::new_symbol_root(sym::char),
90+
BuiltinType::Bool => Name::new_symbol_root(sym::bool),
91+
BuiltinType::Str => Name::new_symbol_root(sym::str),
9292
BuiltinType::Int(it) => match it {
93-
BuiltinInt::Isize => Name::new_symbol_root(sym::isize.clone()),
94-
BuiltinInt::I8 => Name::new_symbol_root(sym::i8.clone()),
95-
BuiltinInt::I16 => Name::new_symbol_root(sym::i16.clone()),
96-
BuiltinInt::I32 => Name::new_symbol_root(sym::i32.clone()),
97-
BuiltinInt::I64 => Name::new_symbol_root(sym::i64.clone()),
98-
BuiltinInt::I128 => Name::new_symbol_root(sym::i128.clone()),
93+
BuiltinInt::Isize => Name::new_symbol_root(sym::isize),
94+
BuiltinInt::I8 => Name::new_symbol_root(sym::i8),
95+
BuiltinInt::I16 => Name::new_symbol_root(sym::i16),
96+
BuiltinInt::I32 => Name::new_symbol_root(sym::i32),
97+
BuiltinInt::I64 => Name::new_symbol_root(sym::i64),
98+
BuiltinInt::I128 => Name::new_symbol_root(sym::i128),
9999
},
100100
BuiltinType::Uint(it) => match it {
101-
BuiltinUint::Usize => Name::new_symbol_root(sym::usize.clone()),
102-
BuiltinUint::U8 => Name::new_symbol_root(sym::u8.clone()),
103-
BuiltinUint::U16 => Name::new_symbol_root(sym::u16.clone()),
104-
BuiltinUint::U32 => Name::new_symbol_root(sym::u32.clone()),
105-
BuiltinUint::U64 => Name::new_symbol_root(sym::u64.clone()),
106-
BuiltinUint::U128 => Name::new_symbol_root(sym::u128.clone()),
101+
BuiltinUint::Usize => Name::new_symbol_root(sym::usize),
102+
BuiltinUint::U8 => Name::new_symbol_root(sym::u8),
103+
BuiltinUint::U16 => Name::new_symbol_root(sym::u16),
104+
BuiltinUint::U32 => Name::new_symbol_root(sym::u32),
105+
BuiltinUint::U64 => Name::new_symbol_root(sym::u64),
106+
BuiltinUint::U128 => Name::new_symbol_root(sym::u128),
107107
},
108108
BuiltinType::Float(it) => match it {
109-
BuiltinFloat::F16 => Name::new_symbol_root(sym::f16.clone()),
110-
BuiltinFloat::F32 => Name::new_symbol_root(sym::f32.clone()),
111-
BuiltinFloat::F64 => Name::new_symbol_root(sym::f64.clone()),
112-
BuiltinFloat::F128 => Name::new_symbol_root(sym::f128.clone()),
109+
BuiltinFloat::F16 => Name::new_symbol_root(sym::f16),
110+
BuiltinFloat::F32 => Name::new_symbol_root(sym::f32),
111+
BuiltinFloat::F64 => Name::new_symbol_root(sym::f64),
112+
BuiltinFloat::F128 => Name::new_symbol_root(sym::f128),
113113
},
114114
}
115115
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,8 +387,8 @@ fn crate_supports_no_std(db: &dyn DefDatabase, crate_id: Crate) -> bool {
387387
let attrs = item_tree.raw_attrs(AttrOwner::TopLevel);
388388
for attr in &**attrs {
389389
match attr.path().as_ident() {
390-
Some(ident) if *ident == sym::no_std.clone() => return true,
391-
Some(ident) if *ident == sym::cfg_attr.clone() => {}
390+
Some(ident) if *ident == sym::no_std => return true,
391+
Some(ident) if *ident == sym::cfg_attr => {}
392392
_ => continue,
393393
}
394394

0 commit comments

Comments
 (0)