Skip to content

Commit 5b19825

Browse files
bors[bot]mominul
andcommitted
Merge #1507
1507: Constify KnownName's r=matklad a=mominul Closes #1503 Co-authored-by: Muhammad Mominul Huque <mominul2082@gmail.com>
2 parents a4316d6 + 09b7248 commit 5b19825

File tree

11 files changed

+85
-167
lines changed

11 files changed

+85
-167
lines changed

Cargo.lock

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/ra_hir/src/code_model.rs

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ use crate::{
1919
TypeAliasId,
2020
},
2121
impl_block::ImplBlock,
22+
name::{
23+
BOOL, CHAR, F32, F64, I128, I16, I32, I64, I8, ISIZE, SELF_TYPE, STR, U128, U16, U32, U64,
24+
U8, USIZE,
25+
},
2226
nameres::{CrateModuleId, ImportId, ModuleScope, Namespace},
2327
resolve::Resolver,
2428
traits::{TraitData, TraitItem},
@@ -28,7 +32,7 @@ use crate::{
2832
},
2933
type_ref::Mutability,
3034
type_ref::TypeRef,
31-
AsName, AstDatabase, AstId, DefDatabase, Either, HasSource, HirDatabase, KnownName, Name, Ty,
35+
AsName, AstDatabase, AstId, DefDatabase, Either, HasSource, HirDatabase, Name, Ty,
3236
};
3337

3438
/// hir::Crate describes a single crate. It's the main interface with which
@@ -96,27 +100,27 @@ pub enum BuiltinType {
96100

97101
impl BuiltinType {
98102
#[rustfmt::skip]
99-
pub(crate) const ALL: &'static [(KnownName, BuiltinType)] = &[
100-
(KnownName::Char, BuiltinType::Char),
101-
(KnownName::Bool, BuiltinType::Bool),
102-
(KnownName::Str, BuiltinType::Str),
103-
104-
(KnownName::Isize, BuiltinType::Int(IntTy { signedness: Signedness::Signed, bitness: IntBitness::Xsize })),
105-
(KnownName::I8, BuiltinType::Int(IntTy { signedness: Signedness::Signed, bitness: IntBitness::X8 })),
106-
(KnownName::I16, BuiltinType::Int(IntTy { signedness: Signedness::Signed, bitness: IntBitness::X16 })),
107-
(KnownName::I32, BuiltinType::Int(IntTy { signedness: Signedness::Signed, bitness: IntBitness::X32 })),
108-
(KnownName::I64, BuiltinType::Int(IntTy { signedness: Signedness::Signed, bitness: IntBitness::X64 })),
109-
(KnownName::I128, BuiltinType::Int(IntTy { signedness: Signedness::Signed, bitness: IntBitness::X128 })),
110-
111-
(KnownName::Usize, BuiltinType::Int(IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::Xsize })),
112-
(KnownName::U8, BuiltinType::Int(IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X8 })),
113-
(KnownName::U16, BuiltinType::Int(IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X16 })),
114-
(KnownName::U32, BuiltinType::Int(IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X32 })),
115-
(KnownName::U64, BuiltinType::Int(IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X64 })),
116-
(KnownName::U128, BuiltinType::Int(IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X128 })),
117-
118-
(KnownName::F32, BuiltinType::Float(FloatTy { bitness: FloatBitness::X32 })),
119-
(KnownName::F64, BuiltinType::Float(FloatTy { bitness: FloatBitness::X64 })),
103+
pub(crate) const ALL: &'static [(Name, BuiltinType)] = &[
104+
(CHAR, BuiltinType::Char),
105+
(BOOL, BuiltinType::Bool),
106+
(STR, BuiltinType::Str),
107+
108+
(ISIZE, BuiltinType::Int(IntTy { signedness: Signedness::Signed, bitness: IntBitness::Xsize })),
109+
(I8, BuiltinType::Int(IntTy { signedness: Signedness::Signed, bitness: IntBitness::X8 })),
110+
(I16, BuiltinType::Int(IntTy { signedness: Signedness::Signed, bitness: IntBitness::X16 })),
111+
(I32, BuiltinType::Int(IntTy { signedness: Signedness::Signed, bitness: IntBitness::X32 })),
112+
(I64, BuiltinType::Int(IntTy { signedness: Signedness::Signed, bitness: IntBitness::X64 })),
113+
(I128, BuiltinType::Int(IntTy { signedness: Signedness::Signed, bitness: IntBitness::X128 })),
114+
115+
(USIZE, BuiltinType::Int(IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::Xsize })),
116+
(U8, BuiltinType::Int(IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X8 })),
117+
(U16, BuiltinType::Int(IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X16 })),
118+
(U32, BuiltinType::Int(IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X32 })),
119+
(U64, BuiltinType::Int(IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X64 })),
120+
(U128, BuiltinType::Int(IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X128 })),
121+
122+
(F32, BuiltinType::Float(FloatTy { bitness: FloatBitness::X32 })),
123+
(F64, BuiltinType::Float(FloatTy { bitness: FloatBitness::X64 })),
120124
];
121125
}
122126

@@ -560,7 +564,7 @@ impl FnData {
560564
let self_type = if let Some(type_ref) = self_param.ascribed_type() {
561565
TypeRef::from_ast(type_ref)
562566
} else {
563-
let self_type = TypeRef::Path(Name::self_type().into());
567+
let self_type = TypeRef::Path(SELF_TYPE.into());
564568
match self_param.kind() {
565569
ast::SelfParamKind::Owned => self_type,
566570
ast::SelfParamKind::Ref => {

crates/ra_hir/src/expr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use ra_syntax::{
1313
};
1414

1515
use crate::{
16-
name::AsName,
16+
name::{AsName, SELF_PARAM},
1717
type_ref::{Mutability, TypeRef},
1818
DefWithBody, Either, HasSource, HirDatabase, HirFileId, MacroCallLoc, MacroFileKind, Name,
1919
Path, Resolver,
@@ -981,7 +981,7 @@ where
981981
let ptr = AstPtr::new(self_param);
982982
let param_pat = self.alloc_pat(
983983
Pat::Bind {
984-
name: Name::self_param(),
984+
name: SELF_PARAM,
985985
mode: BindingAnnotation::Unannotated,
986986
subpat: None,
987987
},

crates/ra_hir/src/generics.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use ra_syntax::ast::{self, DefaultTypeParamOwner, NameOwner, TypeBoundsOwner, Ty
99

1010
use crate::{
1111
db::{AstDatabase, DefDatabase, HirDatabase},
12+
name::SELF_TYPE,
1213
path::Path,
1314
type_ref::TypeRef,
1415
AdtDef, AsName, Container, Enum, EnumVariant, Function, HasSource, ImplBlock, Name, Struct,
@@ -81,11 +82,7 @@ impl GenericParams {
8182
GenericDef::Enum(it) => generics.fill(&*it.source(db).ast, start),
8283
GenericDef::Trait(it) => {
8384
// traits get the Self type as an implicit first type parameter
84-
generics.params.push(GenericParam {
85-
idx: start,
86-
name: Name::self_type(),
87-
default: None,
88-
});
85+
generics.params.push(GenericParam { idx: start, name: SELF_TYPE, default: None });
8986
generics.fill(&*it.source(db).ast, start + 1);
9087
}
9188
GenericDef::TypeAlias(it) => generics.fill(&*it.source(db).ast, start),

crates/ra_hir/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ mod marks;
4949
use crate::{
5050
db::{AstDatabase, DefDatabase, HirDatabase, InternDatabase},
5151
ids::MacroFileKind,
52-
name::{AsName, KnownName},
52+
name::AsName,
5353
resolve::Resolver,
5454
source_id::{AstId, FileAstId},
5555
};

crates/ra_hir/src/name.rs

Lines changed: 26 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,14 @@ impl Name {
2626
/// Note: this is private to make creating name from random string hard.
2727
/// Hopefully, this should allow us to integrate hygiene cleaner in the
2828
/// future, and to switch to interned representation of names.
29-
fn new(text: SmolStr) -> Name {
29+
const fn new(text: SmolStr) -> Name {
3030
Name { text }
3131
}
3232

3333
pub(crate) fn missing() -> Name {
3434
Name::new("[missing name]".into())
3535
}
3636

37-
pub(crate) fn self_param() -> Name {
38-
Name::new("self".into())
39-
}
40-
41-
pub(crate) fn self_type() -> Name {
42-
Name::new("Self".into())
43-
}
44-
4537
pub(crate) fn tuple_field_name(idx: usize) -> Name {
4638
Name::new(idx.to_string().into())
4739
}
@@ -63,38 +55,6 @@ impl Name {
6355
pub fn as_smolstr(&self) -> &SmolStr {
6456
&self.text
6557
}
66-
67-
pub(crate) fn as_known_name(&self) -> Option<KnownName> {
68-
let name = match self.text.as_str() {
69-
"isize" => KnownName::Isize,
70-
"i8" => KnownName::I8,
71-
"i16" => KnownName::I16,
72-
"i32" => KnownName::I32,
73-
"i64" => KnownName::I64,
74-
"i128" => KnownName::I128,
75-
"usize" => KnownName::Usize,
76-
"u8" => KnownName::U8,
77-
"u16" => KnownName::U16,
78-
"u32" => KnownName::U32,
79-
"u64" => KnownName::U64,
80-
"u128" => KnownName::U128,
81-
"f32" => KnownName::F32,
82-
"f64" => KnownName::F64,
83-
"bool" => KnownName::Bool,
84-
"char" => KnownName::Char,
85-
"str" => KnownName::Str,
86-
"Self" => KnownName::SelfType,
87-
"self" => KnownName::SelfParam,
88-
"macro_rules" => KnownName::MacroRules,
89-
90-
"std" => KnownName::Std,
91-
"iter" => KnownName::Iter,
92-
"IntoIterator" => KnownName::IntoIterator,
93-
"Item" => KnownName::Item,
94-
_ => return None,
95-
};
96-
Some(name)
97-
}
9858
}
9959

10060
pub(crate) trait AsName {
@@ -130,76 +90,31 @@ impl AsName for ra_db::Dependency {
13090
}
13191
}
13292

133-
// Ideally, should be replaced with
134-
// ```
135-
// const ISIZE: Name = Name::new("isize")
136-
// ```
137-
// but const-fn is not that powerful yet.
138-
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
139-
pub(crate) enum KnownName {
140-
Isize,
141-
I8,
142-
I16,
143-
I32,
144-
I64,
145-
I128,
146-
147-
Usize,
148-
U8,
149-
U16,
150-
U32,
151-
U64,
152-
U128,
153-
154-
F32,
155-
F64,
156-
157-
Bool,
158-
Char,
159-
Str,
160-
161-
SelfType,
162-
SelfParam,
163-
164-
MacroRules,
165-
166-
Std,
167-
Iter,
168-
IntoIterator,
169-
Item,
170-
}
171-
172-
impl AsName for KnownName {
173-
fn as_name(&self) -> Name {
174-
let s = match self {
175-
KnownName::Isize => "isize",
176-
KnownName::I8 => "i8",
177-
KnownName::I16 => "i16",
178-
KnownName::I32 => "i32",
179-
KnownName::I64 => "i64",
180-
KnownName::I128 => "i128",
181-
KnownName::Usize => "usize",
182-
KnownName::U8 => "u8",
183-
KnownName::U16 => "u16",
184-
KnownName::U32 => "u32",
185-
KnownName::U64 => "u64",
186-
KnownName::U128 => "u128",
187-
KnownName::F32 => "f32",
188-
KnownName::F64 => "f64",
189-
KnownName::Bool => "bool",
190-
KnownName::Char => "char",
191-
KnownName::Str => "str",
192-
KnownName::SelfType => "Self",
193-
KnownName::SelfParam => "self",
194-
KnownName::MacroRules => "macro_rules",
195-
KnownName::Std => "std",
196-
KnownName::Iter => "iter",
197-
KnownName::IntoIterator => "IntoIterator",
198-
KnownName::Item => "Item",
199-
};
200-
Name::new(s.into())
201-
}
202-
}
93+
pub(crate) const ISIZE: Name = Name::new(SmolStr::new_inline_from_ascii(5, b"isize"));
94+
pub(crate) const I8: Name = Name::new(SmolStr::new_inline_from_ascii(2, b"i8"));
95+
pub(crate) const I16: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"i16"));
96+
pub(crate) const I32: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"i32"));
97+
pub(crate) const I64: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"i64"));
98+
pub(crate) const I128: Name = Name::new(SmolStr::new_inline_from_ascii(4, b"i128"));
99+
pub(crate) const USIZE: Name = Name::new(SmolStr::new_inline_from_ascii(5, b"usize"));
100+
pub(crate) const U8: Name = Name::new(SmolStr::new_inline_from_ascii(2, b"u8"));
101+
pub(crate) const U16: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"u16"));
102+
pub(crate) const U32: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"u32"));
103+
pub(crate) const U64: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"u64"));
104+
pub(crate) const U128: Name = Name::new(SmolStr::new_inline_from_ascii(4, b"u128"));
105+
pub(crate) const F32: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"f32"));
106+
pub(crate) const F64: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"f64"));
107+
pub(crate) const BOOL: Name = Name::new(SmolStr::new_inline_from_ascii(4, b"bool"));
108+
pub(crate) const CHAR: Name = Name::new(SmolStr::new_inline_from_ascii(4, b"char"));
109+
pub(crate) const STR: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"str"));
110+
pub(crate) const SELF_PARAM: Name = Name::new(SmolStr::new_inline_from_ascii(4, b"self"));
111+
pub(crate) const SELF_TYPE: Name = Name::new(SmolStr::new_inline_from_ascii(4, b"Self"));
112+
pub(crate) const MACRO_RULES: Name = Name::new(SmolStr::new_inline_from_ascii(11, b"macro_rules"));
113+
pub(crate) const STD: Name = Name::new(SmolStr::new_inline_from_ascii(3, b"std"));
114+
pub(crate) const ITER: Name = Name::new(SmolStr::new_inline_from_ascii(4, b"iter"));
115+
pub(crate) const INTO_ITERATOR: Name =
116+
Name::new(SmolStr::new_inline_from_ascii(12, b"IntoIterator"));
117+
pub(crate) const ITEM: Name = Name::new(SmolStr::new_inline_from_ascii(4, b"Item"));
203118

204119
fn resolve_name(text: &SmolStr) -> SmolStr {
205120
let raw_start = "r#";

crates/ra_hir/src/nameres.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ use test_utils::tested_by;
6565

6666
use crate::{
6767
diagnostics::DiagnosticSink, either::Either, ids::MacroDefId,
68-
nameres::diagnostics::DefDiagnostic, AsName, AstDatabase, AstId, BuiltinType, Crate,
69-
DefDatabase, HirFileId, MacroDef, Module, ModuleDef, Name, Path, PathKind, Trait,
68+
nameres::diagnostics::DefDiagnostic, AstDatabase, AstId, BuiltinType, Crate, DefDatabase,
69+
HirFileId, MacroDef, Module, ModuleDef, Name, Path, PathKind, Trait,
7070
};
7171

7272
pub(crate) use self::raw::{ImportSourceMap, RawItems};
@@ -138,8 +138,8 @@ pub struct ModuleScope {
138138
static BUILTIN_SCOPE: Lazy<FxHashMap<Name, Resolution>> = Lazy::new(|| {
139139
BuiltinType::ALL
140140
.iter()
141-
.map(|&(known_name, ty)| {
142-
(known_name.as_name(), Resolution { def: PerNs::types(ty.into()), import: None })
141+
.map(|(name, ty)| {
142+
(name.clone(), Resolution { def: PerNs::types(ty.clone().into()), import: None })
143143
})
144144
.collect()
145145
});

crates/ra_hir/src/nameres/collector.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ use test_utils::tested_by;
88
use crate::{
99
either::Either,
1010
ids::{AstItemDef, LocationCtx, MacroCallId, MacroCallLoc, MacroDefId, MacroFileKind},
11+
name::MACRO_RULES,
1112
nameres::{
1213
diagnostics::DefDiagnostic, raw, CrateDefMap, CrateModuleId, ItemOrMacro, ModuleData,
1314
ModuleDef, PerNs, ReachedFixedPoint, Resolution, ResolveMode,
1415
},
15-
AstId, Const, DefDatabase, Enum, Function, HirFileId, KnownName, MacroDef, Module, Name, Path,
16-
Static, Struct, Trait, TypeAlias, Union,
16+
AstId, Const, DefDatabase, Enum, Function, HirFileId, MacroDef, Module, Name, Path, Static,
17+
Struct, Trait, TypeAlias, Union,
1718
};
1819

1920
pub(super) fn collect_defs(db: &impl DefDatabase, mut def_map: CrateDefMap) -> CrateDefMap {
@@ -624,7 +625,7 @@ where
624625
}
625626

626627
fn is_macro_rules(path: &Path) -> bool {
627-
path.as_ident().and_then(Name::as_known_name) == Some(KnownName::MacroRules)
628+
path.as_ident() == Some(&MACRO_RULES)
628629
}
629630

630631
fn resolve_submodule(

0 commit comments

Comments
 (0)