@@ -11,7 +11,6 @@ use std::{fmt, mem, ops, str::FromStr};
1111use cfg:: CfgOptions ;
1212use la_arena:: { Arena , Idx , RawIdx } ;
1313use rustc_hash:: { FxHashMap , FxHashSet } ;
14- use semver:: Version ;
1514use syntax:: SmolStr ;
1615use triomphe:: Arc ;
1716use vfs:: { file_set:: FileSet , AbsPathBuf , AnchoredPath , FileId , VfsPath } ;
@@ -292,16 +291,11 @@ pub struct CrateData {
292291 pub dependencies : Vec < Dependency > ,
293292 pub origin : CrateOrigin ,
294293 pub is_proc_macro : bool ,
295- // FIXME: These things should not be per crate! These are more per workspace crate graph level
296- // things. This info does need to be somewhat present though as to prevent deduplication from
297- // happening across different workspaces with different layouts.
298- pub target_layout : TargetLayoutLoadResult ,
299- pub toolchain : Option < Version > ,
300294}
301295
302296impl CrateData {
303297 /// Check if [`other`] is almost equal to [`self`] ignoring `CrateOrigin` value.
304- pub fn eq_ignoring_origin_and_deps ( & self , other : & CrateData , ignore_dev_deps : bool ) -> bool {
298+ fn eq_ignoring_origin_and_deps ( & self , other : & CrateData , ignore_dev_deps : bool ) -> bool {
305299 // This method has some obscure bits. These are mostly there to be compliant with
306300 // some patches. References to the patches are given.
307301 if self . root_file_id != other. root_file_id {
@@ -353,10 +347,6 @@ impl CrateData {
353347
354348 slf_deps. eq ( other_deps)
355349 }
356-
357- pub fn channel ( & self ) -> Option < ReleaseChannel > {
358- self . toolchain . as_ref ( ) . and_then ( |v| ReleaseChannel :: from_str ( & v. pre ) )
359- }
360350}
361351
362352#[ derive( Debug , Clone , Copy , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
@@ -439,8 +429,6 @@ impl CrateGraph {
439429 env : Env ,
440430 is_proc_macro : bool ,
441431 origin : CrateOrigin ,
442- target_layout : Result < Arc < str > , Arc < str > > ,
443- toolchain : Option < Version > ,
444432 ) -> CrateId {
445433 let data = CrateData {
446434 root_file_id,
@@ -452,9 +440,7 @@ impl CrateGraph {
452440 env,
453441 dependencies : Vec :: new ( ) ,
454442 origin,
455- target_layout,
456443 is_proc_macro,
457- toolchain,
458444 } ;
459445 self . arena . alloc ( data)
460446 }
@@ -524,6 +510,10 @@ impl CrateGraph {
524510 self . arena . is_empty ( )
525511 }
526512
513+ pub fn len ( & self ) -> usize {
514+ self . arena . len ( )
515+ }
516+
527517 pub fn iter ( & self ) -> impl Iterator < Item = CrateId > + ' _ {
528518 self . arena . iter ( ) . map ( |( idx, _) | idx)
529519 }
@@ -624,13 +614,16 @@ impl CrateGraph {
624614 ///
625615 /// This will deduplicate the crates of the graph where possible.
626616 /// Note that for deduplication to fully work, `self`'s crate dependencies must be sorted by crate id.
627- /// If the crate dependencies were sorted, the resulting graph from this `extend` call will also have the crate dependencies sorted.
617+ /// If the crate dependencies were sorted, the resulting graph from this `extend` call will also
618+ /// have the crate dependencies sorted.
619+ ///
620+ /// Returns a mapping from `other`'s crate ids to the new crate ids in `self`.
628621 pub fn extend (
629622 & mut self ,
630623 mut other : CrateGraph ,
631624 proc_macros : & mut ProcMacroPaths ,
632- on_finished : impl FnOnce ( & FxHashMap < CrateId , CrateId > ) ,
633- ) {
625+ may_merge : impl Fn ( ( CrateId , & CrateData ) , ( CrateId , & CrateData ) ) -> bool ,
626+ ) -> FxHashMap < CrateId , CrateId > {
634627 let topo = other. crates_in_topological_order ( ) ;
635628 let mut id_map: FxHashMap < CrateId , CrateId > = FxHashMap :: default ( ) ;
636629 for topo in topo {
@@ -639,6 +632,10 @@ impl CrateGraph {
639632 crate_data. dependencies . iter_mut ( ) . for_each ( |dep| dep. crate_id = id_map[ & dep. crate_id ] ) ;
640633 crate_data. dependencies . sort_by_key ( |dep| dep. crate_id ) ;
641634 let res = self . arena . iter ( ) . find_map ( |( id, data) | {
635+ if !may_merge ( ( id, & data) , ( topo, & crate_data) ) {
636+ return None ;
637+ }
638+
642639 match ( & data. origin , & crate_data. origin ) {
643640 ( a, b) if a == b => {
644641 if data. eq_ignoring_origin_and_deps ( crate_data, false ) {
@@ -663,8 +660,7 @@ impl CrateGraph {
663660 None
664661 } ) ;
665662
666- if let Some ( ( res, should_update_lib_to_local) ) = res {
667- id_map. insert ( topo, res) ;
663+ let new_id = if let Some ( ( res, should_update_lib_to_local) ) = res {
668664 if should_update_lib_to_local {
669665 assert ! ( self . arena[ res] . origin. is_lib( ) ) ;
670666 assert ! ( crate_data. origin. is_local( ) ) ;
@@ -673,16 +669,17 @@ impl CrateGraph {
673669 // Move local's dev dependencies into the newly-local-formerly-lib crate.
674670 self . arena [ res] . dependencies = crate_data. dependencies . clone ( ) ;
675671 }
672+ res
676673 } else {
677- let id = self . arena . alloc ( crate_data. clone ( ) ) ;
678- id_map . insert ( topo , id ) ;
679- }
674+ self . arena . alloc ( crate_data. clone ( ) )
675+ } ;
676+ id_map . insert ( topo , new_id ) ;
680677 }
681678
682679 * proc_macros =
683680 mem:: take ( proc_macros) . into_iter ( ) . map ( |( id, macros) | ( id_map[ & id] , macros) ) . collect ( ) ;
684681
685- on_finished ( & id_map) ;
682+ id_map
686683 }
687684
688685 fn find_path (
@@ -889,8 +886,6 @@ mod tests {
889886 Env :: default ( ) ,
890887 false ,
891888 CrateOrigin :: Local { repo : None , name : None } ,
892- Err ( "" . into ( ) ) ,
893- None ,
894889 ) ;
895890 let crate2 = graph. add_crate_root (
896891 FileId :: from_raw ( 2u32 ) ,
@@ -902,8 +897,6 @@ mod tests {
902897 Env :: default ( ) ,
903898 false ,
904899 CrateOrigin :: Local { repo : None , name : None } ,
905- Err ( "" . into ( ) ) ,
906- None ,
907900 ) ;
908901 let crate3 = graph. add_crate_root (
909902 FileId :: from_raw ( 3u32 ) ,
@@ -915,8 +908,6 @@ mod tests {
915908 Env :: default ( ) ,
916909 false ,
917910 CrateOrigin :: Local { repo : None , name : None } ,
918- Err ( "" . into ( ) ) ,
919- None ,
920911 ) ;
921912 assert ! ( graph
922913 . add_dep(
@@ -951,8 +942,6 @@ mod tests {
951942 Env :: default ( ) ,
952943 false ,
953944 CrateOrigin :: Local { repo : None , name : None } ,
954- Err ( "" . into ( ) ) ,
955- None ,
956945 ) ;
957946 let crate2 = graph. add_crate_root (
958947 FileId :: from_raw ( 2u32 ) ,
@@ -964,8 +953,6 @@ mod tests {
964953 Env :: default ( ) ,
965954 false ,
966955 CrateOrigin :: Local { repo : None , name : None } ,
967- Err ( "" . into ( ) ) ,
968- None ,
969956 ) ;
970957 assert ! ( graph
971958 . add_dep(
@@ -994,8 +981,6 @@ mod tests {
994981 Env :: default ( ) ,
995982 false ,
996983 CrateOrigin :: Local { repo : None , name : None } ,
997- Err ( "" . into ( ) ) ,
998- None ,
999984 ) ;
1000985 let crate2 = graph. add_crate_root (
1001986 FileId :: from_raw ( 2u32 ) ,
@@ -1007,8 +992,6 @@ mod tests {
1007992 Env :: default ( ) ,
1008993 false ,
1009994 CrateOrigin :: Local { repo : None , name : None } ,
1010- Err ( "" . into ( ) ) ,
1011- None ,
1012995 ) ;
1013996 let crate3 = graph. add_crate_root (
1014997 FileId :: from_raw ( 3u32 ) ,
@@ -1020,8 +1003,6 @@ mod tests {
10201003 Env :: default ( ) ,
10211004 false ,
10221005 CrateOrigin :: Local { repo : None , name : None } ,
1023- Err ( "" . into ( ) ) ,
1024- None ,
10251006 ) ;
10261007 assert ! ( graph
10271008 . add_dep(
@@ -1050,8 +1031,6 @@ mod tests {
10501031 Env :: default ( ) ,
10511032 false ,
10521033 CrateOrigin :: Local { repo : None , name : None } ,
1053- Err ( "" . into ( ) ) ,
1054- None ,
10551034 ) ;
10561035 let crate2 = graph. add_crate_root (
10571036 FileId :: from_raw ( 2u32 ) ,
@@ -1063,8 +1042,6 @@ mod tests {
10631042 Env :: default ( ) ,
10641043 false ,
10651044 CrateOrigin :: Local { repo : None , name : None } ,
1066- Err ( "" . into ( ) ) ,
1067- None ,
10681045 ) ;
10691046 assert ! ( graph
10701047 . add_dep(
0 commit comments