@@ -11,7 +11,7 @@ use syntax::utils::is_raw_identifier;
11
11
/// and declarations. In theory, names should also carry hygiene info, but we are
12
12
/// not there yet!
13
13
///
14
- /// Note that the rawness (`r#`) of names does not depend on whether they are written raw .
14
+ /// Note that the rawness (`r#`) of names is not preserved. Names are always stored without a `r#` prefix .
15
15
/// This is because we want to show (in completions etc.) names as raw depending on the needs
16
16
/// of the current crate, for example if it is edition 2021 complete `gen` even if the defining
17
17
/// crate is in edition 2024 and wrote `r#gen`, and the opposite holds as well.
@@ -77,6 +77,7 @@ impl Name {
77
77
/// Hopefully, this should allow us to integrate hygiene cleaner in the
78
78
/// future, and to switch to interned representation of names.
79
79
fn new_text ( text : & str ) -> Name {
80
+ debug_assert ! ( !text. starts_with( "r#" ) ) ;
80
81
Name { symbol : Symbol :: intern ( text) , ctx : ( ) }
81
82
}
82
83
@@ -91,15 +92,34 @@ impl Name {
91
92
92
93
pub fn new_root ( text : & str ) -> Name {
93
94
// The edition doesn't matter for hygiene.
94
- Self :: new ( text, SyntaxContextId :: root ( Edition :: Edition2015 ) )
95
+ Self :: new ( text. trim_start_matches ( "r#" ) , SyntaxContextId :: root ( Edition :: Edition2015 ) )
95
96
}
96
97
97
98
pub fn new_tuple_field ( idx : usize ) -> Name {
98
- Name { symbol : Symbol :: intern ( & idx. to_string ( ) ) , ctx : ( ) }
99
+ let symbol = match idx {
100
+ 0 => sym:: INTEGER_0 . clone ( ) ,
101
+ 1 => sym:: INTEGER_1 . clone ( ) ,
102
+ 2 => sym:: INTEGER_2 . clone ( ) ,
103
+ 3 => sym:: INTEGER_3 . clone ( ) ,
104
+ 4 => sym:: INTEGER_4 . clone ( ) ,
105
+ 5 => sym:: INTEGER_5 . clone ( ) ,
106
+ 6 => sym:: INTEGER_6 . clone ( ) ,
107
+ 7 => sym:: INTEGER_7 . clone ( ) ,
108
+ 8 => sym:: INTEGER_8 . clone ( ) ,
109
+ 9 => sym:: INTEGER_9 . clone ( ) ,
110
+ 10 => sym:: INTEGER_10 . clone ( ) ,
111
+ 11 => sym:: INTEGER_11 . clone ( ) ,
112
+ 12 => sym:: INTEGER_12 . clone ( ) ,
113
+ 13 => sym:: INTEGER_13 . clone ( ) ,
114
+ 14 => sym:: INTEGER_14 . clone ( ) ,
115
+ 15 => sym:: INTEGER_15 . clone ( ) ,
116
+ _ => Symbol :: intern ( & idx. to_string ( ) ) ,
117
+ } ;
118
+ Name { symbol, ctx : ( ) }
99
119
}
100
120
101
121
pub fn new_lifetime ( lt : & ast:: Lifetime ) -> Name {
102
- Name { symbol : Symbol :: intern ( lt. text ( ) . as_str ( ) ) , ctx : ( ) }
122
+ Self :: new_text ( lt. text ( ) . as_str ( ) . trim_start_matches ( "r#" ) )
103
123
}
104
124
105
125
/// Resolve a name from the text of token.
@@ -142,15 +162,18 @@ impl Name {
142
162
}
143
163
144
164
/// Returns the text this name represents if it isn't a tuple field.
165
+ ///
166
+ /// Do not use this for user-facing text, use `display` instead to handle editions properly.
145
167
pub fn as_str ( & self ) -> & str {
146
168
self . symbol . as_str ( )
147
169
}
148
170
171
+ // FIXME: Remove this
149
172
pub fn unescaped ( & self ) -> UnescapedName < ' _ > {
150
173
UnescapedName ( self )
151
174
}
152
175
153
- pub fn is_escaped ( & self , edition : Edition ) -> bool {
176
+ pub fn needs_escape ( & self , edition : Edition ) -> bool {
154
177
is_raw_identifier ( self . symbol . as_str ( ) , edition)
155
178
}
156
179
@@ -173,16 +196,19 @@ impl Name {
173
196
& self . symbol
174
197
}
175
198
176
- pub const fn new_symbol ( symbol : Symbol , ctx : SyntaxContextId ) -> Self {
199
+ pub fn new_symbol ( symbol : Symbol , ctx : SyntaxContextId ) -> Self {
200
+ debug_assert ! ( !symbol. as_str( ) . starts_with( "r#" ) ) ;
177
201
_ = ctx;
178
202
Self { symbol, ctx : ( ) }
179
203
}
180
204
181
205
// FIXME: This needs to go once we have hygiene
182
- pub const fn new_symbol_root ( sym : Symbol ) -> Self {
206
+ pub fn new_symbol_root ( sym : Symbol ) -> Self {
207
+ debug_assert ! ( !sym. as_str( ) . starts_with( "r#" ) ) ;
183
208
Self { symbol : sym, ctx : ( ) }
184
209
}
185
210
211
+ // FIXME: Remove this
186
212
#[ inline]
187
213
pub fn eq_ident ( & self , ident : & str ) -> bool {
188
214
self . as_str ( ) == ident. trim_start_matches ( "r#" )
0 commit comments