1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
use std::fmt;
use ra_syntax::{ast, SmolStr};
/// `Name` is a wrapper around string, which is used in hir for both references
/// and declarations. In theory, names should also carry hygiene info, but we are
/// not there yet!
#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Name {
text: SmolStr,
}
impl fmt::Display for Name {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(&self.text, f)
}
}
impl fmt::Debug for Name {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(&self.text, f)
}
}
impl Name {
/// Note: this is private to make creating name from random string hard.
/// Hopefully, this should allow us to integrate hygiene cleaner in the
/// future, and to switch to interned representation of names.
fn new(text: SmolStr) -> Name {
Name { text }
}
pub(crate) fn missing() -> Name {
Name::new("[missing name]".into())
}
pub(crate) fn self_param() -> Name {
Name::new("self".into())
}
pub(crate) fn self_type() -> Name {
Name::new("Self".into())
}
pub(crate) fn tuple_field_name(idx: usize) -> Name {
Name::new(idx.to_string().into())
}
// Needed for Deref
pub(crate) fn target() -> Name {
Name::new("Target".into())
}
// There's should be no way to extract a string out of `Name`: `Name` in the
// future, `Name` will include hygiene information, and you can't encode
// hygiene into a String.
//
// If you need to compare something with `Name`, compare `Name`s directly.
//
// If you need to render `Name` for the user, use the `Display` impl, but be
// aware that it strips hygiene info.
#[deprecated(note = "use to_string instead")]
pub fn as_smolstr(&self) -> &SmolStr {
&self.text
}
pub(crate) fn as_known_name(&self) -> Option<KnownName> {
let name = match self.text.as_str() {
"isize" => KnownName::Isize,
"i8" => KnownName::I8,
"i16" => KnownName::I16,
"i32" => KnownName::I32,
"i64" => KnownName::I64,
"i128" => KnownName::I128,
"usize" => KnownName::Usize,
"u8" => KnownName::U8,
"u16" => KnownName::U16,
"u32" => KnownName::U32,
"u64" => KnownName::U64,
"u128" => KnownName::U128,
"f32" => KnownName::F32,
"f64" => KnownName::F64,
"bool" => KnownName::Bool,
"char" => KnownName::Char,
"str" => KnownName::Str,
"Self" => KnownName::SelfType,
"self" => KnownName::SelfParam,
"macro_rules" => KnownName::MacroRules,
"std" => KnownName::Std,
"iter" => KnownName::Iter,
"IntoIterator" => KnownName::IntoIterator,
"Item" => KnownName::Item,
_ => return None,
};
Some(name)
}
}
pub(crate) trait AsName {
fn as_name(&self) -> Name;
}
impl AsName for ast::NameRef {
fn as_name(&self) -> Name {
let name = resolve_name(self.text());
Name::new(name)
}
}
impl AsName for ast::Name {
fn as_name(&self) -> Name {
let name = resolve_name(self.text());
Name::new(name)
}
}
impl<'a> AsName for ast::FieldKind<'a> {
fn as_name(&self) -> Name {
match self {
ast::FieldKind::Name(nr) => nr.as_name(),
ast::FieldKind::Index(idx) => Name::new(idx.text().clone()),
}
}
}
impl AsName for ra_db::Dependency {
fn as_name(&self) -> Name {
Name::new(self.name.clone())
}
}
// Ideally, should be replaced with
// ```
// const ISIZE: Name = Name::new("isize")
// ```
// but const-fn is not that powerful yet.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum KnownName {
Isize,
I8,
I16,
I32,
I64,
I128,
Usize,
U8,
U16,
U32,
U64,
U128,
F32,
F64,
Bool,
Char,
Str,
SelfType,
SelfParam,
MacroRules,
Std,
Iter,
IntoIterator,
Item,
}
impl AsName for KnownName {
fn as_name(&self) -> Name {
let s = match self {
KnownName::Isize => "isize",
KnownName::I8 => "i8",
KnownName::I16 => "i16",
KnownName::I32 => "i32",
KnownName::I64 => "i64",
KnownName::I128 => "i128",
KnownName::Usize => "usize",
KnownName::U8 => "u8",
KnownName::U16 => "u16",
KnownName::U32 => "u32",
KnownName::U64 => "u64",
KnownName::U128 => "u128",
KnownName::F32 => "f32",
KnownName::F64 => "f64",
KnownName::Bool => "bool",
KnownName::Char => "char",
KnownName::Str => "str",
KnownName::SelfType => "Self",
KnownName::SelfParam => "self",
KnownName::MacroRules => "macro_rules",
KnownName::Std => "std",
KnownName::Iter => "iter",
KnownName::IntoIterator => "IntoIterator",
KnownName::Item => "Item",
};
Name::new(s.into())
}
}
fn resolve_name(text: &SmolStr) -> SmolStr {
let raw_start = "r#";
if text.as_str().starts_with(raw_start) {
SmolStr::new(&text[raw_start.len()..])
} else {
text.clone()
}
}
|