Skip to content

Commit c18d25c

Browse files
committed
Use statics + clone instead of const until const can access statics
1 parent dd626e7 commit c18d25c

Some content is hidden

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

48 files changed

+393
-357
lines changed

crates/hir-def/src/attr.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use hir_expand::{
99
attrs::{collect_attrs, Attr, AttrId, RawAttrs},
1010
HirFileId, InFile,
1111
};
12-
use intern::sym;
12+
use intern::{sym, Symbol};
1313
use la_arena::{ArenaMap, Idx, RawIdx};
1414
use mbe::DelimiterKind;
1515
use syntax::{
@@ -153,7 +153,7 @@ impl Attrs {
153153
}
154154

155155
pub fn lang_item(&self) -> Option<LangItem> {
156-
self.by_key("lang").string_value().and_then(LangItem::from_str)
156+
self.by_key("lang").string_value().and_then(|it| LangItem::from_symbol(&Symbol::intern(it)))
157157
}
158158

159159
pub fn has_doc_hidden(&self) -> bool {
@@ -200,7 +200,11 @@ impl Attrs {
200200
.segments()
201201
.iter()
202202
.rev()
203-
.zip([sym::core, sym::prelude, sym::v1, sym::test].iter().rev())
203+
.zip(
204+
[sym::core.clone(), sym::prelude.clone(), sym::v1.clone(), sym::test.clone()]
205+
.iter()
206+
.rev(),
207+
)
204208
.all(|it| it.0 == it.1)
205209
})
206210
}

crates/hir-def/src/body/lower.rs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ impl ExprCollector<'_> {
188188
let is_mutable =
189189
self_param.mut_token().is_some() && self_param.amp_token().is_none();
190190
let binding_id: la_arena::Idx<Binding> = self.alloc_binding(
191-
Name::new_symbol_root(sym::self_),
191+
Name::new_symbol_root(sym::self_.clone()),
192192
BindingAnnotation::new(is_mutable, false),
193193
);
194194
self.body.self_param = Some(binding_id);
@@ -1732,14 +1732,14 @@ impl ExprCollector<'_> {
17321732
let Some(new_v1_formatted) = LangItem::FormatArguments.ty_rel_path(
17331733
self.db,
17341734
self.krate,
1735-
Name::new_symbol_root(sym::new_v1_formatted),
1735+
Name::new_symbol_root(sym::new_v1_formatted.clone()),
17361736
) else {
17371737
return self.missing_expr();
17381738
};
17391739
let Some(unsafe_arg_new) = LangItem::FormatUnsafeArg.ty_rel_path(
17401740
self.db,
17411741
self.krate,
1742-
Name::new_symbol_root(sym::new),
1742+
Name::new_symbol_root(sym::new.clone()),
17431743
) else {
17441744
return self.missing_expr();
17451745
};
@@ -1822,10 +1822,10 @@ impl ExprCollector<'_> {
18221822
self.db,
18231823
self.krate,
18241824
match alignment {
1825-
Some(FormatAlignment::Left) => Name::new_symbol_root(sym::Left),
1826-
Some(FormatAlignment::Right) => Name::new_symbol_root(sym::Right),
1827-
Some(FormatAlignment::Center) => Name::new_symbol_root(sym::Center),
1828-
None => Name::new_symbol_root(sym::Unknown),
1825+
Some(FormatAlignment::Left) => Name::new_symbol_root(sym::Left.clone()),
1826+
Some(FormatAlignment::Right) => Name::new_symbol_root(sym::Right.clone()),
1827+
Some(FormatAlignment::Center) => Name::new_symbol_root(sym::Center.clone()),
1828+
None => Name::new_symbol_root(sym::Unknown.clone()),
18291829
},
18301830
);
18311831
match align {
@@ -1851,7 +1851,7 @@ impl ExprCollector<'_> {
18511851
let format_placeholder_new = LangItem::FormatPlaceholder.ty_rel_path(
18521852
self.db,
18531853
self.krate,
1854-
Name::new_symbol_root(sym::new),
1854+
Name::new_symbol_root(sym::new.clone()),
18551855
);
18561856
match format_placeholder_new {
18571857
Some(path) => self.alloc_expr_desugared(Expr::Path(path)),
@@ -1899,7 +1899,7 @@ impl ExprCollector<'_> {
18991899
let count_is = match LangItem::FormatCount.ty_rel_path(
19001900
self.db,
19011901
self.krate,
1902-
Name::new_symbol_root(sym::Is),
1902+
Name::new_symbol_root(sym::Is.clone()),
19031903
) {
19041904
Some(count_is) => self.alloc_expr_desugared(Expr::Path(count_is)),
19051905
None => self.missing_expr(),
@@ -1921,7 +1921,7 @@ impl ExprCollector<'_> {
19211921
let count_param = match LangItem::FormatCount.ty_rel_path(
19221922
self.db,
19231923
self.krate,
1924-
Name::new_symbol_root(sym::Param),
1924+
Name::new_symbol_root(sym::Param.clone()),
19251925
) {
19261926
Some(count_param) => self.alloc_expr_desugared(Expr::Path(count_param)),
19271927
None => self.missing_expr(),
@@ -1940,7 +1940,7 @@ impl ExprCollector<'_> {
19401940
None => match LangItem::FormatCount.ty_rel_path(
19411941
self.db,
19421942
self.krate,
1943-
Name::new_symbol_root(sym::Implied),
1943+
Name::new_symbol_root(sym::Implied.clone()),
19441944
) {
19451945
Some(count_param) => self.alloc_expr_desugared(Expr::Path(count_param)),
19461946
None => self.missing_expr(),
@@ -1963,16 +1963,16 @@ impl ExprCollector<'_> {
19631963
self.db,
19641964
self.krate,
19651965
Name::new_symbol_root(match ty {
1966-
Format(Display) => sym::new_display,
1967-
Format(Debug) => sym::new_debug,
1968-
Format(LowerExp) => sym::new_lower_exp,
1969-
Format(UpperExp) => sym::new_upper_exp,
1970-
Format(Octal) => sym::new_octal,
1971-
Format(Pointer) => sym::new_pointer,
1972-
Format(Binary) => sym::new_binary,
1973-
Format(LowerHex) => sym::new_lower_hex,
1974-
Format(UpperHex) => sym::new_upper_hex,
1975-
Usize => sym::from_usize,
1966+
Format(Display) => sym::new_display.clone(),
1967+
Format(Debug) => sym::new_debug.clone(),
1968+
Format(LowerExp) => sym::new_lower_exp.clone(),
1969+
Format(UpperExp) => sym::new_upper_exp.clone(),
1970+
Format(Octal) => sym::new_octal.clone(),
1971+
Format(Pointer) => sym::new_pointer.clone(),
1972+
Format(Binary) => sym::new_binary.clone(),
1973+
Format(LowerHex) => sym::new_lower_hex.clone(),
1974+
Format(UpperHex) => sym::new_upper_hex.clone(),
1975+
Usize => sym::from_usize.clone(),
19761976
}),
19771977
) {
19781978
Some(new_fn) => self.alloc_expr_desugared(Expr::Path(new_fn)),

crates/hir-def/src/builtin_type.rs

Lines changed: 48 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -49,63 +49,67 @@ pub enum BuiltinType {
4949

5050
impl BuiltinType {
5151
#[rustfmt::skip]
52-
pub const ALL: &'static [(Name, BuiltinType)] = &[
53-
(Name::new_symbol_root(sym::char), BuiltinType::Char),
54-
(Name::new_symbol_root(sym::bool), BuiltinType::Bool),
55-
(Name::new_symbol_root(sym::str), BuiltinType::Str),
56-
57-
(Name::new_symbol_root(sym::isize), BuiltinType::Int(BuiltinInt::Isize)),
58-
(Name::new_symbol_root(sym::i8), BuiltinType::Int(BuiltinInt::I8)),
59-
(Name::new_symbol_root(sym::i16), BuiltinType::Int(BuiltinInt::I16)),
60-
(Name::new_symbol_root(sym::i32), BuiltinType::Int(BuiltinInt::I32)),
61-
(Name::new_symbol_root(sym::i64), BuiltinType::Int(BuiltinInt::I64)),
62-
(Name::new_symbol_root(sym::i128), BuiltinType::Int(BuiltinInt::I128)),
63-
64-
(Name::new_symbol_root(sym::usize), BuiltinType::Uint(BuiltinUint::Usize)),
65-
(Name::new_symbol_root(sym::u8), BuiltinType::Uint(BuiltinUint::U8)),
66-
(Name::new_symbol_root(sym::u16), BuiltinType::Uint(BuiltinUint::U16)),
67-
(Name::new_symbol_root(sym::u32), BuiltinType::Uint(BuiltinUint::U32)),
68-
(Name::new_symbol_root(sym::u64), BuiltinType::Uint(BuiltinUint::U64)),
69-
(Name::new_symbol_root(sym::u128), BuiltinType::Uint(BuiltinUint::U128)),
70-
71-
(Name::new_symbol_root(sym::f16), BuiltinType::Float(BuiltinFloat::F16)),
72-
(Name::new_symbol_root(sym::f32), BuiltinType::Float(BuiltinFloat::F32)),
73-
(Name::new_symbol_root(sym::f64), BuiltinType::Float(BuiltinFloat::F64)),
74-
(Name::new_symbol_root(sym::f128), BuiltinType::Float(BuiltinFloat::F128)),
75-
];
52+
pub fn all_builtin_types() -> [(Name, BuiltinType); 19] {
53+
[
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)),
76+
]
77+
}
7678

7779
pub fn by_name(name: &Name) -> Option<Self> {
78-
Self::ALL.iter().find_map(|(n, ty)| if n == name { Some(*ty) } else { None })
80+
Self::all_builtin_types()
81+
.iter()
82+
.find_map(|(n, ty)| if n == name { Some(*ty) } else { None })
7983
}
8084
}
8185

8286
impl AsName for BuiltinType {
8387
fn as_name(&self) -> Name {
8488
match self {
85-
BuiltinType::Char => Name::new_symbol_root(sym::char),
86-
BuiltinType::Bool => Name::new_symbol_root(sym::bool),
87-
BuiltinType::Str => Name::new_symbol_root(sym::str),
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()),
8892
BuiltinType::Int(it) => match it {
89-
BuiltinInt::Isize => Name::new_symbol_root(sym::isize),
90-
BuiltinInt::I8 => Name::new_symbol_root(sym::i8),
91-
BuiltinInt::I16 => Name::new_symbol_root(sym::i16),
92-
BuiltinInt::I32 => Name::new_symbol_root(sym::i32),
93-
BuiltinInt::I64 => Name::new_symbol_root(sym::i64),
94-
BuiltinInt::I128 => Name::new_symbol_root(sym::i128),
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()),
9599
},
96100
BuiltinType::Uint(it) => match it {
97-
BuiltinUint::Usize => Name::new_symbol_root(sym::usize),
98-
BuiltinUint::U8 => Name::new_symbol_root(sym::u8),
99-
BuiltinUint::U16 => Name::new_symbol_root(sym::u16),
100-
BuiltinUint::U32 => Name::new_symbol_root(sym::u32),
101-
BuiltinUint::U64 => Name::new_symbol_root(sym::u64),
102-
BuiltinUint::U128 => Name::new_symbol_root(sym::u128),
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()),
103107
},
104108
BuiltinType::Float(it) => match it {
105-
BuiltinFloat::F16 => Name::new_symbol_root(sym::f16),
106-
BuiltinFloat::F32 => Name::new_symbol_root(sym::f32),
107-
BuiltinFloat::F64 => Name::new_symbol_root(sym::f64),
108-
BuiltinFloat::F128 => Name::new_symbol_root(sym::f128),
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()),
109113
},
110114
}
111115
}

crates/hir-def/src/data.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ impl ExternCrateDeclData {
485485

486486
let name = extern_crate.name.clone();
487487
let krate = loc.container.krate();
488-
let crate_id = if name == sym::self_ {
488+
let crate_id = if name == sym::self_.clone() {
489489
Some(krate)
490490
} else {
491491
db.crate_def_map(krate)

crates/hir-def/src/db.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,8 @@ fn crate_supports_no_std(db: &dyn DefDatabase, crate_id: CrateId) -> bool {
262262
let attrs = item_tree.raw_attrs(AttrOwner::TopLevel);
263263
for attr in &**attrs {
264264
match attr.path().as_ident() {
265-
Some(ident) if *ident == sym::no_std => return true,
266-
Some(ident) if *ident == sym::cfg_attr => {}
265+
Some(ident) if *ident == sym::no_std.clone() => return true,
266+
Some(ident) if *ident == sym::cfg_attr.clone() => {}
267267
_ => continue,
268268
}
269269

crates/hir-def/src/find_path.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -415,13 +415,13 @@ fn select_best_path(
415415
(Unstable, Stable) => return new_path,
416416
_ => {}
417417
}
418-
const STD_CRATES: [Symbol; 3] = [sym::std, sym::core, sym::alloc];
418+
let std_crates: [Symbol; 3] = [sym::std.clone(), sym::core.clone(), sym::alloc.clone()];
419419

420420
let choose = |new: (ModPath, _), old: (ModPath, _)| {
421421
let (new_path, _) = &new;
422422
let (old_path, _) = &old;
423-
let new_has_prelude = new_path.segments().iter().any(|seg| *seg == sym::prelude);
424-
let old_has_prelude = old_path.segments().iter().any(|seg| *seg == sym::prelude);
423+
let new_has_prelude = new_path.segments().iter().any(|seg| *seg == sym::prelude.clone());
424+
let old_has_prelude = old_path.segments().iter().any(|seg| *seg == sym::prelude.clone());
425425
match (new_has_prelude, old_has_prelude, cfg.prefer_prelude) {
426426
(true, false, true) | (false, true, false) => new,
427427
(true, false, false) | (false, true, true) => old,
@@ -443,19 +443,19 @@ fn select_best_path(
443443

444444
match (old_path.0.segments().first(), new_path.0.segments().first()) {
445445
(Some(old), Some(new))
446-
if STD_CRATES.contains(old.symbol()) && STD_CRATES.contains(new.symbol()) =>
446+
if std_crates.contains(old.symbol()) && std_crates.contains(new.symbol()) =>
447447
{
448448
let rank = match cfg.prefer_no_std {
449449
false => |name: &Name| match name {
450-
name if *name == sym::core => 0,
451-
name if *name == sym::alloc => 1,
452-
name if *name == sym::std => 2,
450+
name if *name == sym::core.clone() => 0,
451+
name if *name == sym::alloc.clone() => 1,
452+
name if *name == sym::std.clone() => 2,
453453
_ => unreachable!(),
454454
},
455455
true => |name: &Name| match name {
456-
name if *name == sym::core => 2,
457-
name if *name == sym::alloc => 1,
458-
name if *name == sym::std => 0,
456+
name if *name == sym::core.clone() => 2,
457+
name if *name == sym::alloc.clone() => 1,
458+
name if *name == sym::std.clone() => 0,
459459
_ => unreachable!(),
460460
},
461461
};

crates/hir-def/src/item_scope.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ struct DeriveMacroInvocation {
119119
}
120120

121121
pub(crate) static BUILTIN_SCOPE: Lazy<FxHashMap<Name, PerNs>> = Lazy::new(|| {
122-
BuiltinType::ALL
122+
BuiltinType::all_builtin_types()
123123
.iter()
124124
.map(|(name, ty)| (name.clone(), PerNs::types((*ty).into(), Visibility::Public, None)))
125125
.collect()

crates/hir-def/src/item_tree/lower.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,8 @@ impl<'a> Ctx<'a> {
324324
let self_type = match self_param.ty() {
325325
Some(type_ref) => TypeRef::from_ast(&self.body_ctx, type_ref),
326326
None => {
327-
let self_type = TypeRef::Path(Name::new_symbol_root(sym::Self_).into());
327+
let self_type =
328+
TypeRef::Path(Name::new_symbol_root(sym::Self_.clone()).into());
328329
match self_param.kind() {
329330
ast::SelfParamKind::Owned => self_type,
330331
ast::SelfParamKind::Ref => TypeRef::Reference(
@@ -670,7 +671,7 @@ impl<'a> Ctx<'a> {
670671
// Traits and trait aliases get the Self type as an implicit first type parameter.
671672
generics.type_or_consts.alloc(
672673
TypeParamData {
673-
name: Some(Name::new_symbol_root(sym::Self_)),
674+
name: Some(Name::new_symbol_root(sym::Self_.clone())),
674675
default: None,
675676
provenance: TypeParamProvenance::TraitSelf,
676677
}
@@ -681,7 +682,7 @@ impl<'a> Ctx<'a> {
681682
generics.fill_bounds(
682683
&self.body_ctx,
683684
bounds,
684-
Either::Left(TypeRef::Path(Name::new_symbol_root(sym::Self_).into())),
685+
Either::Left(TypeRef::Path(Name::new_symbol_root(sym::Self_.clone()).into())),
685686
);
686687
}
687688

@@ -746,7 +747,7 @@ fn desugar_future_path(orig: TypeRef) -> Path {
746747
let mut generic_args: Vec<_> =
747748
std::iter::repeat(None).take(path.segments().len() - 1).collect();
748749
let binding = AssociatedTypeBinding {
749-
name: Name::new_symbol_root(sym::Output),
750+
name: Name::new_symbol_root(sym::Output.clone()),
750751
args: None,
751752
type_ref: Some(orig),
752753
bounds: Box::default(),

crates/hir-def/src/lang_item.rs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,7 @@ impl LangItems {
192192
}
193193

194194
pub(crate) fn lang_attr(db: &dyn DefDatabase, item: AttrDefId) -> Option<LangItem> {
195-
let attrs = db.attrs(item);
196-
attrs.by_key("lang").string_value().and_then(LangItem::from_str)
195+
db.attrs(item).lang_item()
197196
}
198197

199198
pub(crate) fn notable_traits_in_deps(
@@ -261,18 +260,9 @@ macro_rules! language_item_table {
261260
}
262261

263262
/// Opposite of [`LangItem::name`]
264-
#[allow(clippy::should_implement_trait)]
265-
pub fn from_str(name: &str) -> Option<Self> {
266-
match name {
267-
$( stringify!($name) => Some(LangItem::$variant), )*
268-
_ => None,
269-
}
270-
}
271-
272-
/// Opposite of [`LangItem::name`]
273-
pub fn from_symbol(sym: Symbol) -> Option<Self> {
263+
pub fn from_symbol(sym: &Symbol) -> Option<Self> {
274264
match sym {
275-
$(sym if sym == $module::$name => Some(LangItem::$variant), )*
265+
$(sym if *sym == $module::$name => Some(LangItem::$variant), )*
276266
_ => None,
277267
}
278268
}
@@ -283,7 +273,7 @@ macro_rules! language_item_table {
283273
impl LangItem {
284274
/// Opposite of [`LangItem::name`]
285275
pub fn from_name(name: &hir_expand::name::Name) -> Option<Self> {
286-
Self::from_str(name.as_str())
276+
Self::from_symbol(name.symbol())
287277
}
288278

289279
pub fn path(&self, db: &dyn DefDatabase, start_crate: CrateId) -> Option<Path> {

0 commit comments

Comments
 (0)