@@ -4,8 +4,8 @@ use std::fmt;
4
4
5
5
use intern:: { sym, Symbol } ;
6
6
use span:: { Edition , SyntaxContextId } ;
7
- use syntax:: ast;
8
7
use syntax:: utils:: is_raw_identifier;
8
+ use syntax:: { ast, format_smolstr} ;
9
9
10
10
/// `Name` is a wrapper around string, which is used in hir for both references
11
11
/// and declarations. In theory, names should also carry hygiene info, but we are
@@ -69,27 +69,8 @@ impl PartialEq<Name> for &Symbol {
69
69
}
70
70
}
71
71
72
- /// Wrapper of `Name` to print the name without "r#" even when it is a raw identifier.
73
- #[ derive( Debug , Clone , Copy , PartialEq , Eq , Hash ) ]
74
- pub struct UnescapedName < ' a > ( & ' a Name ) ;
75
-
76
- impl < ' a > UnescapedName < ' a > {
77
- pub fn display ( self , db : & dyn crate :: db:: ExpandDatabase ) -> impl fmt:: Display + ' a {
78
- _ = db;
79
- UnescapedDisplay { name : self }
80
- }
81
- #[ doc( hidden) ]
82
- pub fn display_no_db ( self ) -> impl fmt:: Display + ' a {
83
- UnescapedDisplay { name : self }
84
- }
85
- }
86
-
87
72
impl Name {
88
- /// Note: this is private to make creating name from random string hard.
89
- /// Hopefully, this should allow us to integrate hygiene cleaner in the
90
- /// future, and to switch to interned representation of names.
91
73
fn new_text ( text : & str ) -> Name {
92
- debug_assert ! ( !text. starts_with( "r#" ) ) ;
93
74
Name { symbol : Symbol :: intern ( text) , ctx : ( ) }
94
75
}
95
76
@@ -99,12 +80,15 @@ impl Name {
99
80
// Can't do that for all `SyntaxContextId`s because it breaks Salsa.
100
81
ctx. remove_root_edition ( ) ;
101
82
_ = ctx;
102
- Self :: new_text ( text)
83
+ match text. strip_prefix ( "r#" ) {
84
+ Some ( text) => Self :: new_text ( text) ,
85
+ None => Self :: new_text ( text) ,
86
+ }
103
87
}
104
88
105
89
pub fn new_root ( text : & str ) -> Name {
106
90
// The edition doesn't matter for hygiene.
107
- Self :: new ( text. trim_start_matches ( "r#" ) , SyntaxContextId :: root ( Edition :: Edition2015 ) )
91
+ Self :: new ( text, SyntaxContextId :: root ( Edition :: Edition2015 ) )
108
92
}
109
93
110
94
pub fn new_tuple_field ( idx : usize ) -> Name {
@@ -131,12 +115,22 @@ impl Name {
131
115
}
132
116
133
117
pub fn new_lifetime ( lt : & ast:: Lifetime ) -> Name {
134
- Self :: new_text ( lt. text ( ) . as_str ( ) . trim_start_matches ( "r#" ) )
118
+ let text = lt. text ( ) ;
119
+ match text. strip_prefix ( "'r#" ) {
120
+ Some ( text) => Self :: new_text ( & format_smolstr ! ( "'{text}" ) ) ,
121
+ None => Self :: new_text ( text. as_str ( ) ) ,
122
+ }
135
123
}
136
124
137
- /// Resolve a name from the text of token.
138
- fn resolve ( raw_text : & str ) -> Name {
139
- Name :: new_text ( raw_text. trim_start_matches ( "r#" ) )
125
+ pub fn new_symbol ( symbol : Symbol , ctx : SyntaxContextId ) -> Self {
126
+ debug_assert ! ( !symbol. as_str( ) . starts_with( "r#" ) ) ;
127
+ _ = ctx;
128
+ Self { symbol, ctx : ( ) }
129
+ }
130
+
131
+ // FIXME: This needs to go once we have hygiene
132
+ pub fn new_symbol_root ( sym : Symbol ) -> Self {
133
+ Self :: new_symbol ( sym, SyntaxContextId :: root ( Edition :: Edition2015 ) )
140
134
}
141
135
142
136
/// A fake name for things missing in the source code.
@@ -173,22 +167,19 @@ impl Name {
173
167
self . symbol . as_str ( ) . parse ( ) . ok ( )
174
168
}
175
169
170
+ /// Whether this name needs to be escaped in the given edition via `r#`.
171
+ pub fn needs_escape ( & self , edition : Edition ) -> bool {
172
+ is_raw_identifier ( self . symbol . as_str ( ) , edition)
173
+ }
174
+
176
175
/// Returns the text this name represents if it isn't a tuple field.
177
176
///
178
177
/// Do not use this for user-facing text, use `display` instead to handle editions properly.
178
+ // FIXME: This should take a database argument to hide the interning
179
179
pub fn as_str ( & self ) -> & str {
180
180
self . symbol . as_str ( )
181
181
}
182
182
183
- // FIXME: Remove this
184
- pub fn unescaped ( & self ) -> UnescapedName < ' _ > {
185
- UnescapedName ( self )
186
- }
187
-
188
- pub fn needs_escape ( & self , edition : Edition ) -> bool {
189
- is_raw_identifier ( self . symbol . as_str ( ) , edition)
190
- }
191
-
192
183
pub fn display < ' a > (
193
184
& ' a self ,
194
185
db : & dyn crate :: db:: ExpandDatabase ,
@@ -198,7 +189,7 @@ impl Name {
198
189
self . display_no_db ( edition)
199
190
}
200
191
201
- // FIXME: Remove this
192
+ // FIXME: Remove this in favor of `display`, see fixme on `as_str`
202
193
#[ doc( hidden) ]
203
194
pub fn display_no_db ( & self , edition : Edition ) -> impl fmt:: Display + ' _ {
204
195
Display { name : self , needs_escaping : is_raw_identifier ( self . symbol . as_str ( ) , edition) }
@@ -207,24 +198,6 @@ impl Name {
207
198
pub fn symbol ( & self ) -> & Symbol {
208
199
& self . symbol
209
200
}
210
-
211
- pub fn new_symbol ( symbol : Symbol , ctx : SyntaxContextId ) -> Self {
212
- debug_assert ! ( !symbol. as_str( ) . starts_with( "r#" ) ) ;
213
- _ = ctx;
214
- Self { symbol, ctx : ( ) }
215
- }
216
-
217
- // FIXME: This needs to go once we have hygiene
218
- pub fn new_symbol_root ( sym : Symbol ) -> Self {
219
- debug_assert ! ( !sym. as_str( ) . starts_with( "r#" ) ) ;
220
- Self { symbol : sym, ctx : ( ) }
221
- }
222
-
223
- // FIXME: Remove this
224
- #[ inline]
225
- pub fn eq_ident ( & self , ident : & str ) -> bool {
226
- self . as_str ( ) == ident. trim_start_matches ( "r#" )
227
- }
228
201
}
229
202
230
203
struct Display < ' a > {
@@ -241,17 +214,6 @@ impl fmt::Display for Display<'_> {
241
214
}
242
215
}
243
216
244
- struct UnescapedDisplay < ' a > {
245
- name : UnescapedName < ' a > ,
246
- }
247
-
248
- impl fmt:: Display for UnescapedDisplay < ' _ > {
249
- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
250
- let symbol = self . name . 0 . symbol . as_str ( ) ;
251
- fmt:: Display :: fmt ( symbol, f)
252
- }
253
- }
254
-
255
217
pub trait AsName {
256
218
fn as_name ( & self ) -> Name ;
257
219
}
@@ -260,14 +222,14 @@ impl AsName for ast::NameRef {
260
222
fn as_name ( & self ) -> Name {
261
223
match self . as_tuple_field ( ) {
262
224
Some ( idx) => Name :: new_tuple_field ( idx) ,
263
- None => Name :: resolve ( & self . text ( ) ) ,
225
+ None => Name :: new_root ( & self . text ( ) ) ,
264
226
}
265
227
}
266
228
}
267
229
268
230
impl AsName for ast:: Name {
269
231
fn as_name ( & self ) -> Name {
270
- Name :: resolve ( & self . text ( ) )
232
+ Name :: new_root ( & self . text ( ) )
271
233
}
272
234
}
273
235
@@ -282,7 +244,7 @@ impl AsName for ast::NameOrNameRef {
282
244
283
245
impl < Span > AsName for tt:: Ident < Span > {
284
246
fn as_name ( & self ) -> Name {
285
- Name :: resolve ( self . sym . as_str ( ) )
247
+ Name :: new_root ( self . sym . as_str ( ) )
286
248
}
287
249
}
288
250
@@ -300,6 +262,6 @@ impl AsName for ast::FieldKind {
300
262
301
263
impl AsName for base_db:: Dependency {
302
264
fn as_name ( & self ) -> Name {
303
- Name :: new_text ( & self . name )
265
+ Name :: new_root ( & self . name )
304
266
}
305
267
}
0 commit comments