@@ -11,7 +11,7 @@ use syntax::utils::is_raw_identifier;
1111/// and declarations. In theory, names should also carry hygiene info, but we are
1212/// not there yet!
1313///
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 .
1515/// This is because we want to show (in completions etc.) names as raw depending on the needs
1616/// of the current crate, for example if it is edition 2021 complete `gen` even if the defining
1717/// crate is in edition 2024 and wrote `r#gen`, and the opposite holds as well.
@@ -77,6 +77,7 @@ impl Name {
7777 /// Hopefully, this should allow us to integrate hygiene cleaner in the
7878 /// future, and to switch to interned representation of names.
7979 fn new_text ( text : & str ) -> Name {
80+ debug_assert ! ( !text. starts_with( "r#" ) ) ;
8081 Name { symbol : Symbol :: intern ( text) , ctx : ( ) }
8182 }
8283
@@ -91,15 +92,34 @@ impl Name {
9192
9293 pub fn new_root ( text : & str ) -> Name {
9394 // 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 ) )
9596 }
9697
9798 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 : ( ) }
99119 }
100120
101121 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#" ) )
103123 }
104124
105125 /// Resolve a name from the text of token.
@@ -142,15 +162,18 @@ impl Name {
142162 }
143163
144164 /// 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.
145167 pub fn as_str ( & self ) -> & str {
146168 self . symbol . as_str ( )
147169 }
148170
171+ // FIXME: Remove this
149172 pub fn unescaped ( & self ) -> UnescapedName < ' _ > {
150173 UnescapedName ( self )
151174 }
152175
153- pub fn is_escaped ( & self , edition : Edition ) -> bool {
176+ pub fn needs_escape ( & self , edition : Edition ) -> bool {
154177 is_raw_identifier ( self . symbol . as_str ( ) , edition)
155178 }
156179
@@ -173,16 +196,19 @@ impl Name {
173196 & self . symbol
174197 }
175198
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#" ) ) ;
177201 _ = ctx;
178202 Self { symbol, ctx : ( ) }
179203 }
180204
181205 // 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#" ) ) ;
183208 Self { symbol : sym, ctx : ( ) }
184209 }
185210
211+ // FIXME: Remove this
186212 #[ inline]
187213 pub fn eq_ident ( & self , ident : & str ) -> bool {
188214 self . as_str ( ) == ident. trim_start_matches ( "r#" )
0 commit comments