2
2
3
3
use std:: fmt;
4
4
5
+ use intern:: Symbol ;
5
6
use syntax:: { ast, format_smolstr, utils:: is_raw_identifier, SmolStr } ;
6
7
7
8
/// `Name` is a wrapper around string, which is used in hir for both references
@@ -12,7 +13,10 @@ use syntax::{ast, format_smolstr, utils::is_raw_identifier, SmolStr};
12
13
/// is a raw identifier. Use [`unescaped()`][Name::unescaped] when you need the
13
14
/// name without "r#".
14
15
#[ derive( Debug , Clone , PartialEq , Eq , Hash , PartialOrd , Ord ) ]
15
- pub struct Name ( Repr ) ;
16
+ pub struct Name {
17
+ symbol : Symbol ,
18
+ span : ( ) ,
19
+ }
16
20
17
21
/// Wrapper of `Name` to print the name without "r#" even when it is a raw identifier.
18
22
#[ derive( Debug , Clone , PartialEq , Eq , Hash , PartialOrd , Ord ) ]
@@ -28,15 +32,11 @@ impl UnescapedName<'_> {
28
32
/// Returns the textual representation of this name as a [`SmolStr`]. Prefer using this over
29
33
/// [`ToString::to_string`] if possible as this conversion is cheaper in the general case.
30
34
pub fn to_smol_str ( & self ) -> SmolStr {
31
- match & self . 0 . 0 {
32
- Repr :: Text ( it) => {
33
- if let Some ( stripped) = it. strip_prefix ( "r#" ) {
34
- SmolStr :: new ( stripped)
35
- } else {
36
- it. clone ( )
37
- }
38
- }
39
- Repr :: TupleField ( it) => SmolStr :: new ( it. to_string ( ) ) ,
35
+ let it = self . 0 . symbol . as_str ( ) ;
36
+ if let Some ( stripped) = it. strip_prefix ( "r#" ) {
37
+ SmolStr :: new ( stripped)
38
+ } else {
39
+ it. into ( )
40
40
}
41
41
}
42
42
@@ -51,34 +51,39 @@ impl Name {
51
51
/// Hopefully, this should allow us to integrate hygiene cleaner in the
52
52
/// future, and to switch to interned representation of names.
53
53
const fn new_text ( text : SmolStr ) -> Name {
54
- Name ( Repr :: Text ( text) )
54
+ Name { symbol : Symbol :: intern ( text. as_str ( ) ) , span : ( ) }
55
55
}
56
56
57
57
// FIXME: See above, unfortunately some places really need this right now
58
58
#[ doc( hidden) ]
59
59
pub const fn new_text_dont_use ( text : SmolStr ) -> Name {
60
- Name ( Repr :: Text ( text) )
60
+ Name { symbol : Symbol :: intern ( text. as_str ( ) ) , span : ( ) }
61
61
}
62
62
63
63
pub fn new_tuple_field ( idx : usize ) -> Name {
64
- Name ( Repr :: TupleField ( idx) )
64
+ Name { symbol : Symbol :: intern ( & idx. to_string ( ) ) , span : ( ) }
65
65
}
66
66
67
67
pub fn new_lifetime ( lt : & ast:: Lifetime ) -> Name {
68
- Self :: new_text ( lt. text ( ) . into ( ) )
68
+ Name { symbol : Symbol :: intern ( lt. text ( ) . as_str ( ) ) , span : ( ) }
69
69
}
70
70
71
71
/// Shortcut to create a name from a string literal.
72
72
const fn new_static ( text : & ' static str ) -> Name {
73
- Name :: new_text ( SmolStr :: new_static ( text) )
73
+ todo ! ( )
74
+ }
75
+
76
+ /// Shortcut to create a name from a string literal.
77
+ fn new_ref ( text : & str ) -> Name {
78
+ Name { symbol : Symbol :: intern ( text) , span : ( ) }
74
79
}
75
80
76
81
/// Resolve a name from the text of token.
77
82
fn resolve ( raw_text : & str ) -> Name {
78
83
match raw_text. strip_prefix ( "r#" ) {
79
84
// When `raw_text` starts with "r#" but the name does not coincide with any
80
85
// keyword, we never need the prefix so we strip it.
81
- Some ( text) if !is_raw_identifier ( text) => Name :: new_text ( SmolStr :: new ( text) ) ,
86
+ Some ( text) if !is_raw_identifier ( text) => Name :: new_ref ( text) ,
82
87
// Keywords (in the current edition) *can* be used as a name in earlier editions of
83
88
// Rust, e.g. "try" in Rust 2015. Even in such cases, we keep track of them in their
84
89
// escaped form.
@@ -120,47 +125,32 @@ impl Name {
120
125
121
126
/// Returns the tuple index this name represents if it is a tuple field.
122
127
pub fn as_tuple_index ( & self ) -> Option < usize > {
123
- match self . 0 {
124
- Repr :: TupleField ( idx) => Some ( idx) ,
125
- _ => None ,
126
- }
128
+ self . symbol . as_str ( ) . parse ( ) . ok ( )
127
129
}
128
130
129
131
/// Returns the text this name represents if it isn't a tuple field.
130
- pub fn as_text ( & self ) -> Option < SmolStr > {
131
- match & self . 0 {
132
- Repr :: Text ( it) => Some ( it. clone ( ) ) ,
133
- _ => None ,
134
- }
132
+ pub fn as_text ( & self ) -> SmolStr {
133
+ self . symbol . as_str ( ) . into ( )
135
134
}
136
135
137
136
/// Returns the text this name represents if it isn't a tuple field.
138
- pub fn as_str ( & self ) -> Option < & str > {
139
- match & self . 0 {
140
- Repr :: Text ( it) => Some ( it) ,
141
- _ => None ,
142
- }
137
+ pub fn as_str ( & self ) -> & str {
138
+ self . symbol . as_str ( )
143
139
}
144
140
145
141
/// Returns the textual representation of this name as a [`SmolStr`].
146
142
/// Prefer using this over [`ToString::to_string`] if possible as this conversion is cheaper in
147
143
/// the general case.
148
144
pub fn to_smol_str ( & self ) -> SmolStr {
149
- match & self . 0 {
150
- Repr :: Text ( it) => it. clone ( ) ,
151
- Repr :: TupleField ( it) => SmolStr :: new ( it. to_string ( ) ) ,
152
- }
145
+ self . as_text ( )
153
146
}
154
147
155
148
pub fn unescaped ( & self ) -> UnescapedName < ' _ > {
156
149
UnescapedName ( self )
157
150
}
158
151
159
152
pub fn is_escaped ( & self ) -> bool {
160
- match & self . 0 {
161
- Repr :: Text ( it) => it. starts_with ( "r#" ) ,
162
- Repr :: TupleField ( _) => false ,
163
- }
153
+ self . symbol . as_str ( ) . starts_with ( "r#" )
164
154
}
165
155
166
156
pub fn display < ' a > ( & ' a self , db : & dyn crate :: db:: ExpandDatabase ) -> impl fmt:: Display + ' a {
@@ -175,10 +165,7 @@ struct Display<'a> {
175
165
176
166
impl fmt:: Display for Display < ' _ > {
177
167
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
178
- match & self . name . 0 {
179
- Repr :: Text ( text) => fmt:: Display :: fmt ( & text, f) ,
180
- Repr :: TupleField ( idx) => fmt:: Display :: fmt ( & idx, f) ,
181
- }
168
+ fmt:: Display :: fmt ( self . name . symbol . as_str ( ) , f)
182
169
}
183
170
}
184
171
@@ -188,13 +175,9 @@ struct UnescapedDisplay<'a> {
188
175
189
176
impl fmt:: Display for UnescapedDisplay < ' _ > {
190
177
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
191
- match & self . name . 0 . 0 {
192
- Repr :: Text ( text) => {
193
- let text = text. strip_prefix ( "r#" ) . unwrap_or ( text) ;
194
- fmt:: Display :: fmt ( & text, f)
195
- }
196
- Repr :: TupleField ( idx) => fmt:: Display :: fmt ( & idx, f) ,
197
- }
178
+ let symbol = & self . name . 0 . symbol . as_str ( ) ;
179
+ let text = symbol. strip_prefix ( "r#" ) . unwrap_or ( symbol) ;
180
+ fmt:: Display :: fmt ( & text, f)
198
181
}
199
182
}
200
183
0 commit comments