99use std:: { fmt, ops, panic:: RefUnwindSafe , str:: FromStr , sync:: Arc } ;
1010
1111use cfg:: CfgOptions ;
12- use rustc_hash:: { FxHashMap , FxHashSet } ;
12+ use rustc_hash:: FxHashMap ;
13+ use stdx:: hash:: { NoHashHashMap , NoHashHashSet } ;
1314use syntax:: SmolStr ;
1415use tt:: Subtree ;
15- use vfs:: { file_set:: FileSet , FileId , VfsPath } ;
16+ use vfs:: { file_set:: FileSet , AnchoredPath , FileId , VfsPath } ;
1617
1718/// Files are grouped into source roots. A source root is a directory on the
1819/// file systems which is watched for changes. Typically it corresponds to a
@@ -31,22 +32,30 @@ pub struct SourceRoot {
3132 /// Libraries are considered mostly immutable, this assumption is used to
3233 /// optimize salsa's query structure
3334 pub is_library : bool ,
34- pub ( crate ) file_set : FileSet ,
35+ file_set : FileSet ,
3536}
3637
3738impl SourceRoot {
3839 pub fn new_local ( file_set : FileSet ) -> SourceRoot {
3940 SourceRoot { is_library : false , file_set }
4041 }
42+
4143 pub fn new_library ( file_set : FileSet ) -> SourceRoot {
4244 SourceRoot { is_library : true , file_set }
4345 }
46+
4447 pub fn path_for_file ( & self , file : & FileId ) -> Option < & VfsPath > {
4548 self . file_set . path_for_file ( file)
4649 }
50+
4751 pub fn file_for_path ( & self , path : & VfsPath ) -> Option < & FileId > {
4852 self . file_set . file_for_path ( path)
4953 }
54+
55+ pub fn resolve_path ( & self , path : AnchoredPath < ' _ > ) -> Option < FileId > {
56+ self . file_set . resolve_path ( path)
57+ }
58+
5059 pub fn iter ( & self ) -> impl Iterator < Item = FileId > + ' _ {
5160 self . file_set . iter ( )
5261 }
@@ -72,12 +81,19 @@ impl SourceRoot {
7281/// <https://github.com/rust-lang/rust-analyzer/blob/master/docs/dev/architecture.md#serialization>
7382#[ derive( Debug , Clone , Default /* Serialize, Deserialize */ ) ]
7483pub struct CrateGraph {
75- arena : FxHashMap < CrateId , CrateData > ,
84+ arena : NoHashHashMap < CrateId , CrateData > ,
7685}
7786
78- #[ derive( Debug , Clone , Copy , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
87+ #[ derive( Debug , Clone , Copy , PartialEq , Eq , PartialOrd , Ord ) ]
7988pub struct CrateId ( pub u32 ) ;
8089
90+ impl stdx:: hash:: NoHashHashable for CrateId { }
91+ impl std:: hash:: Hash for CrateId {
92+ fn hash < H : std:: hash:: Hasher > ( & self , state : & mut H ) {
93+ self . 0 . hash ( state) ;
94+ }
95+ }
96+
8197#[ derive( Debug , Clone , PartialEq , Eq , Hash ) ]
8298pub struct CrateName ( SmolStr ) ;
8399
@@ -342,7 +358,7 @@ impl CrateGraph {
342358 // Check if adding a dep from `from` to `to` creates a cycle. To figure
343359 // that out, look for a path in the *opposite* direction, from `to` to
344360 // `from`.
345- if let Some ( path) = self . find_path ( & mut FxHashSet :: default ( ) , dep. crate_id , from) {
361+ if let Some ( path) = self . find_path ( & mut NoHashHashSet :: default ( ) , dep. crate_id , from) {
346362 let path = path. into_iter ( ) . map ( |it| ( it, self [ it] . display_name . clone ( ) ) ) . collect ( ) ;
347363 let err = CyclicDependenciesError { path } ;
348364 assert ! ( err. from( ) . 0 == from && err. to( ) . 0 == dep. crate_id) ;
@@ -365,7 +381,7 @@ impl CrateGraph {
365381 /// including the crate itself.
366382 pub fn transitive_deps ( & self , of : CrateId ) -> impl Iterator < Item = CrateId > {
367383 let mut worklist = vec ! [ of] ;
368- let mut deps = FxHashSet :: default ( ) ;
384+ let mut deps = NoHashHashSet :: default ( ) ;
369385
370386 while let Some ( krate) = worklist. pop ( ) {
371387 if !deps. insert ( krate) {
@@ -382,10 +398,10 @@ impl CrateGraph {
382398 /// including the crate itself.
383399 pub fn transitive_rev_deps ( & self , of : CrateId ) -> impl Iterator < Item = CrateId > {
384400 let mut worklist = vec ! [ of] ;
385- let mut rev_deps = FxHashSet :: default ( ) ;
401+ let mut rev_deps = NoHashHashSet :: default ( ) ;
386402 rev_deps. insert ( of) ;
387403
388- let mut inverted_graph = FxHashMap :: < _ , Vec < _ > > :: default ( ) ;
404+ let mut inverted_graph = NoHashHashMap :: < _ , Vec < _ > > :: default ( ) ;
389405 self . arena . iter ( ) . for_each ( |( & krate, data) | {
390406 data. dependencies
391407 . iter ( )
@@ -409,7 +425,7 @@ impl CrateGraph {
409425 /// come before the crate itself).
410426 pub fn crates_in_topological_order ( & self ) -> Vec < CrateId > {
411427 let mut res = Vec :: new ( ) ;
412- let mut visited = FxHashSet :: default ( ) ;
428+ let mut visited = NoHashHashSet :: default ( ) ;
413429
414430 for krate in self . arena . keys ( ) . copied ( ) {
415431 go ( self , & mut visited, & mut res, krate) ;
@@ -419,7 +435,7 @@ impl CrateGraph {
419435
420436 fn go (
421437 graph : & CrateGraph ,
422- visited : & mut FxHashSet < CrateId > ,
438+ visited : & mut NoHashHashSet < CrateId > ,
423439 res : & mut Vec < CrateId > ,
424440 source : CrateId ,
425441 ) {
@@ -459,7 +475,7 @@ impl CrateGraph {
459475
460476 fn find_path (
461477 & self ,
462- visited : & mut FxHashSet < CrateId > ,
478+ visited : & mut NoHashHashSet < CrateId > ,
463479 from : CrateId ,
464480 to : CrateId ,
465481 ) -> Option < Vec < CrateId > > {
0 commit comments