@@ -86,7 +86,7 @@ pub struct CrateId(pub u32);
8686pub struct CrateName ( SmolStr ) ;
8787
8888impl CrateName {
89- /// Crates a crate name, checking for dashes in the string provided.
89+ /// Creates a crate name, checking for dashes in the string provided.
9090 /// Dashes are not allowed in the crate names,
9191 /// hence the input string is returned as `Err` for those cases.
9292 pub fn new ( name : & str ) -> Result < CrateName , & str > {
@@ -97,7 +97,7 @@ impl CrateName {
9797 }
9898 }
9999
100- /// Crates a crate name, unconditionally replacing the dashes with underscores.
100+ /// Creates a crate name, unconditionally replacing the dashes with underscores.
101101 pub fn normalize_dashes ( name : & str ) -> CrateName {
102102 Self ( SmolStr :: new ( name. replace ( '-' , "_" ) ) )
103103 }
@@ -107,6 +107,7 @@ impl CrateName {
107107struct CrateData {
108108 file_id : FileId ,
109109 edition : Edition ,
110+ declaration_name : Option < String > ,
110111 cfg_options : CfgOptions ,
111112 env : Env ,
112113 dependencies : Vec < Dependency > ,
@@ -134,10 +135,11 @@ impl CrateGraph {
134135 & mut self ,
135136 file_id : FileId ,
136137 edition : Edition ,
138+ declaration_name : Option < String > ,
137139 cfg_options : CfgOptions ,
138140 env : Env ,
139141 ) -> CrateId {
140- let data = CrateData :: new ( file_id, edition, cfg_options, env) ;
142+ let data = CrateData :: new ( file_id, edition, declaration_name , cfg_options, env) ;
141143 let crate_id = CrateId ( self . arena . len ( ) as u32 ) ;
142144 let prev = self . arena . insert ( crate_id, data) ;
143145 assert ! ( prev. is_none( ) ) ;
@@ -177,6 +179,15 @@ impl CrateGraph {
177179 self . arena [ & crate_id] . edition
178180 }
179181
182+ /// Returns a name of a crate, declared in the root project.
183+ /// May be missing for some cases, such as when the crate definition was created for a code snippet.
184+ ///
185+ /// This should not be considered as a normal crate name, since the actual name can be different in
186+ /// a particular dependent crate, where it is specified.
187+ pub fn declaration_name ( & self , crate_id : & CrateId ) -> Option < & String > {
188+ self . arena [ crate_id] . declaration_name . as_ref ( )
189+ }
190+
180191 // FIXME: this only finds one crate with the given root; we could have multiple
181192 pub fn crate_id_for_crate_root ( & self , file_id : FileId ) -> Option < CrateId > {
182193 let ( & crate_id, _) = self . arena . iter ( ) . find ( |( _crate_id, data) | data. file_id == file_id) ?;
@@ -230,8 +241,14 @@ impl CrateId {
230241}
231242
232243impl CrateData {
233- fn new ( file_id : FileId , edition : Edition , cfg_options : CfgOptions , env : Env ) -> CrateData {
234- CrateData { file_id, edition, dependencies : Vec :: new ( ) , cfg_options, env }
244+ fn new (
245+ file_id : FileId ,
246+ edition : Edition ,
247+ declaration_name : Option < String > ,
248+ cfg_options : CfgOptions ,
249+ env : Env ,
250+ ) -> CrateData {
251+ CrateData { file_id, edition, declaration_name, dependencies : Vec :: new ( ) , cfg_options, env }
235252 }
236253
237254 fn add_dep ( & mut self , name : SmolStr , crate_id : CrateId ) {
@@ -290,12 +307,27 @@ mod tests {
290307 #[ test]
291308 fn it_should_panic_because_of_cycle_dependencies ( ) {
292309 let mut graph = CrateGraph :: default ( ) ;
293- let crate1 =
294- graph. add_crate_root ( FileId ( 1u32 ) , Edition2018 , CfgOptions :: default ( ) , Env :: default ( ) ) ;
295- let crate2 =
296- graph. add_crate_root ( FileId ( 2u32 ) , Edition2018 , CfgOptions :: default ( ) , Env :: default ( ) ) ;
297- let crate3 =
298- graph. add_crate_root ( FileId ( 3u32 ) , Edition2018 , CfgOptions :: default ( ) , Env :: default ( ) ) ;
310+ let crate1 = graph. add_crate_root (
311+ FileId ( 1u32 ) ,
312+ Edition2018 ,
313+ None ,
314+ CfgOptions :: default ( ) ,
315+ Env :: default ( ) ,
316+ ) ;
317+ let crate2 = graph. add_crate_root (
318+ FileId ( 2u32 ) ,
319+ Edition2018 ,
320+ None ,
321+ CfgOptions :: default ( ) ,
322+ Env :: default ( ) ,
323+ ) ;
324+ let crate3 = graph. add_crate_root (
325+ FileId ( 3u32 ) ,
326+ Edition2018 ,
327+ None ,
328+ CfgOptions :: default ( ) ,
329+ Env :: default ( ) ,
330+ ) ;
299331 assert ! ( graph. add_dep( crate1, CrateName :: new( "crate2" ) . unwrap( ) , crate2) . is_ok( ) ) ;
300332 assert ! ( graph. add_dep( crate2, CrateName :: new( "crate3" ) . unwrap( ) , crate3) . is_ok( ) ) ;
301333 assert ! ( graph. add_dep( crate3, CrateName :: new( "crate1" ) . unwrap( ) , crate1) . is_err( ) ) ;
@@ -304,23 +336,48 @@ mod tests {
304336 #[ test]
305337 fn it_works ( ) {
306338 let mut graph = CrateGraph :: default ( ) ;
307- let crate1 =
308- graph. add_crate_root ( FileId ( 1u32 ) , Edition2018 , CfgOptions :: default ( ) , Env :: default ( ) ) ;
309- let crate2 =
310- graph. add_crate_root ( FileId ( 2u32 ) , Edition2018 , CfgOptions :: default ( ) , Env :: default ( ) ) ;
311- let crate3 =
312- graph. add_crate_root ( FileId ( 3u32 ) , Edition2018 , CfgOptions :: default ( ) , Env :: default ( ) ) ;
339+ let crate1 = graph. add_crate_root (
340+ FileId ( 1u32 ) ,
341+ Edition2018 ,
342+ None ,
343+ CfgOptions :: default ( ) ,
344+ Env :: default ( ) ,
345+ ) ;
346+ let crate2 = graph. add_crate_root (
347+ FileId ( 2u32 ) ,
348+ Edition2018 ,
349+ None ,
350+ CfgOptions :: default ( ) ,
351+ Env :: default ( ) ,
352+ ) ;
353+ let crate3 = graph. add_crate_root (
354+ FileId ( 3u32 ) ,
355+ Edition2018 ,
356+ None ,
357+ CfgOptions :: default ( ) ,
358+ Env :: default ( ) ,
359+ ) ;
313360 assert ! ( graph. add_dep( crate1, CrateName :: new( "crate2" ) . unwrap( ) , crate2) . is_ok( ) ) ;
314361 assert ! ( graph. add_dep( crate2, CrateName :: new( "crate3" ) . unwrap( ) , crate3) . is_ok( ) ) ;
315362 }
316363
317364 #[ test]
318365 fn dashes_are_normalized ( ) {
319366 let mut graph = CrateGraph :: default ( ) ;
320- let crate1 =
321- graph. add_crate_root ( FileId ( 1u32 ) , Edition2018 , CfgOptions :: default ( ) , Env :: default ( ) ) ;
322- let crate2 =
323- graph. add_crate_root ( FileId ( 2u32 ) , Edition2018 , CfgOptions :: default ( ) , Env :: default ( ) ) ;
367+ let crate1 = graph. add_crate_root (
368+ FileId ( 1u32 ) ,
369+ Edition2018 ,
370+ None ,
371+ CfgOptions :: default ( ) ,
372+ Env :: default ( ) ,
373+ ) ;
374+ let crate2 = graph. add_crate_root (
375+ FileId ( 2u32 ) ,
376+ Edition2018 ,
377+ None ,
378+ CfgOptions :: default ( ) ,
379+ Env :: default ( ) ,
380+ ) ;
324381 assert ! ( graph
325382 . add_dep( crate1, CrateName :: normalize_dashes( "crate-name-with-dashes" ) , crate2)
326383 . is_ok( ) ) ;
0 commit comments