@@ -102,11 +102,46 @@ impl fmt::Display for CrateName {
102102
103103impl ops:: Deref for CrateName {
104104 type Target = str ;
105- fn deref ( & self ) -> & Self :: Target {
105+ fn deref ( & self ) -> & str {
106106 & * self . 0
107107 }
108108}
109109
110+ #[ derive( Debug , Clone , PartialEq , Eq , Hash ) ]
111+ pub struct CrateDisplayName {
112+ // The name we use to display various paths (with `_`).
113+ crate_name : CrateName ,
114+ // The name as specified in Cargo.toml (with `-`).
115+ canonical_name : String ,
116+ }
117+
118+ impl From < CrateName > for CrateDisplayName {
119+ fn from ( crate_name : CrateName ) -> CrateDisplayName {
120+ let canonical_name = crate_name. to_string ( ) ;
121+ CrateDisplayName { crate_name, canonical_name }
122+ }
123+ }
124+
125+ impl fmt:: Display for CrateDisplayName {
126+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
127+ write ! ( f, "{}" , self . crate_name)
128+ }
129+ }
130+
131+ impl ops:: Deref for CrateDisplayName {
132+ type Target = str ;
133+ fn deref ( & self ) -> & str {
134+ & * self . crate_name
135+ }
136+ }
137+
138+ impl CrateDisplayName {
139+ pub fn from_canonical_name ( canonical_name : String ) -> CrateDisplayName {
140+ let crate_name = CrateName :: normalize_dashes ( & canonical_name) ;
141+ CrateDisplayName { crate_name, canonical_name }
142+ }
143+ }
144+
110145#[ derive( Debug , Copy , Clone , PartialEq , Eq , Hash ) ]
111146pub struct ProcMacroId ( pub u32 ) ;
112147
@@ -127,11 +162,13 @@ impl PartialEq for ProcMacro {
127162pub struct CrateData {
128163 pub root_file_id : FileId ,
129164 pub edition : Edition ,
130- /// A name used in the package's project declaration: for Cargo projects, it's [package].name,
131- /// can be different for other project types or even absent (a dummy crate for the code snippet, for example).
132- /// NOTE: The crate can be referenced as a dependency under a different name,
133- /// this one should be used when working with crate hierarchies.
134- pub declaration_name : Option < CrateName > ,
165+ /// A name used in the package's project declaration: for Cargo projects,
166+ /// it's [package].name, can be different for other project types or even
167+ /// absent (a dummy crate for the code snippet, for example).
168+ ///
169+ /// For purposes of analysis, crates are anonymous (only names in
170+ /// `Dependency` matters), this name should only be used for UI.
171+ pub display_name : Option < CrateDisplayName > ,
135172 pub cfg_options : CfgOptions ,
136173 pub env : Env ,
137174 pub dependencies : Vec < Dependency > ,
@@ -160,7 +197,7 @@ impl CrateGraph {
160197 & mut self ,
161198 file_id : FileId ,
162199 edition : Edition ,
163- declaration_name : Option < CrateName > ,
200+ display_name : Option < CrateDisplayName > ,
164201 cfg_options : CfgOptions ,
165202 env : Env ,
166203 proc_macro : Vec < ( SmolStr , Arc < dyn tt:: TokenExpander > ) > ,
@@ -171,7 +208,7 @@ impl CrateGraph {
171208 let data = CrateData {
172209 root_file_id : file_id,
173210 edition,
174- declaration_name ,
211+ display_name ,
175212 cfg_options,
176213 env,
177214 proc_macro,
@@ -310,8 +347,8 @@ impl CrateGraph {
310347 }
311348 }
312349
313- fn hacky_find_crate ( & self , declaration_name : & str ) -> Option < CrateId > {
314- self . iter ( ) . find ( |it| self [ * it] . declaration_name . as_deref ( ) == Some ( declaration_name ) )
350+ fn hacky_find_crate ( & self , display_name : & str ) -> Option < CrateId > {
351+ self . iter ( ) . find ( |it| self [ * it] . display_name . as_deref ( ) == Some ( display_name ) )
315352 }
316353}
317354
0 commit comments