@@ -4,8 +4,8 @@ use std::fmt;
44
55use intern:: { sym, Symbol } ;
66use span:: { Edition , SyntaxContextId } ;
7- use syntax:: ast;
87use syntax:: utils:: is_raw_identifier;
8+ use syntax:: { ast, format_smolstr} ;
99
1010/// `Name` is a wrapper around string, which is used in hir for both references
1111/// and declarations. In theory, names should also carry hygiene info, but we are
@@ -69,27 +69,8 @@ impl PartialEq<Name> for &Symbol {
6969 }
7070}
7171
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-
8772impl 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.
9173 fn new_text ( text : & str ) -> Name {
92- debug_assert ! ( !text. starts_with( "r#" ) ) ;
9374 Name { symbol : Symbol :: intern ( text) , ctx : ( ) }
9475 }
9576
@@ -99,12 +80,15 @@ impl Name {
9980 // Can't do that for all `SyntaxContextId`s because it breaks Salsa.
10081 ctx. remove_root_edition ( ) ;
10182 _ = 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+ }
10387 }
10488
10589 pub fn new_root ( text : & str ) -> Name {
10690 // 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 ) )
10892 }
10993
11094 pub fn new_tuple_field ( idx : usize ) -> Name {
@@ -131,12 +115,22 @@ impl Name {
131115 }
132116
133117 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+ }
135123 }
136124
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 ) )
140134 }
141135
142136 /// A fake name for things missing in the source code.
@@ -173,22 +167,19 @@ impl Name {
173167 self . symbol . as_str ( ) . parse ( ) . ok ( )
174168 }
175169
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+
176175 /// Returns the text this name represents if it isn't a tuple field.
177176 ///
178177 /// 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
179179 pub fn as_str ( & self ) -> & str {
180180 self . symbol . as_str ( )
181181 }
182182
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-
192183 pub fn display < ' a > (
193184 & ' a self ,
194185 db : & dyn crate :: db:: ExpandDatabase ,
@@ -198,7 +189,7 @@ impl Name {
198189 self . display_no_db ( edition)
199190 }
200191
201- // FIXME: Remove this
192+ // FIXME: Remove this in favor of `display`, see fixme on `as_str`
202193 #[ doc( hidden) ]
203194 pub fn display_no_db ( & self , edition : Edition ) -> impl fmt:: Display + ' _ {
204195 Display { name : self , needs_escaping : is_raw_identifier ( self . symbol . as_str ( ) , edition) }
@@ -207,24 +198,6 @@ impl Name {
207198 pub fn symbol ( & self ) -> & Symbol {
208199 & self . symbol
209200 }
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- }
228201}
229202
230203struct Display < ' a > {
@@ -241,17 +214,6 @@ impl fmt::Display for Display<'_> {
241214 }
242215}
243216
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-
255217pub trait AsName {
256218 fn as_name ( & self ) -> Name ;
257219}
@@ -260,14 +222,14 @@ impl AsName for ast::NameRef {
260222 fn as_name ( & self ) -> Name {
261223 match self . as_tuple_field ( ) {
262224 Some ( idx) => Name :: new_tuple_field ( idx) ,
263- None => Name :: resolve ( & self . text ( ) ) ,
225+ None => Name :: new_root ( & self . text ( ) ) ,
264226 }
265227 }
266228}
267229
268230impl AsName for ast:: Name {
269231 fn as_name ( & self ) -> Name {
270- Name :: resolve ( & self . text ( ) )
232+ Name :: new_root ( & self . text ( ) )
271233 }
272234}
273235
@@ -282,7 +244,7 @@ impl AsName for ast::NameOrNameRef {
282244
283245impl < Span > AsName for tt:: Ident < Span > {
284246 fn as_name ( & self ) -> Name {
285- Name :: resolve ( self . sym . as_str ( ) )
247+ Name :: new_root ( self . sym . as_str ( ) )
286248 }
287249}
288250
@@ -300,6 +262,6 @@ impl AsName for ast::FieldKind {
300262
301263impl AsName for base_db:: Dependency {
302264 fn as_name ( & self ) -> Name {
303- Name :: new_text ( & self . name )
265+ Name :: new_root ( & self . name )
304266 }
305267}
0 commit comments