@@ -374,64 +374,24 @@ impl CrateGraph {
374374 self . arena . alloc ( data)
375375 }
376376
377- /// Remove the crate from crate graph. If any crates depend on this crate, the dependency would be replaced
378- /// with the second input.
379- pub fn remove_and_replace (
380- & mut self ,
381- id : CrateId ,
382- replace_with : CrateId ,
383- ) -> Result < ( ) , CyclicDependenciesError > {
384- for ( x, data) in self . arena . iter ( ) {
385- if x == id {
386- continue ;
387- }
388- for edge in & data. dependencies {
389- if edge. crate_id == id {
390- self . check_cycle_after_dependency ( edge. crate_id , replace_with) ?;
391- }
392- }
393- }
394- // if everything was ok, start to replace
395- for ( x, data) in self . arena . iter_mut ( ) {
396- if x == id {
397- continue ;
398- }
399- for edge in & mut data. dependencies {
400- if edge. crate_id == id {
401- edge. crate_id = replace_with;
402- }
403- }
404- }
405- Ok ( ( ) )
406- }
407-
408377 pub fn add_dep (
409378 & mut self ,
410379 from : CrateId ,
411380 dep : Dependency ,
412381 ) -> Result < ( ) , CyclicDependenciesError > {
413382 let _p = tracing:: info_span!( "add_dep" ) . entered ( ) ;
414383
415- self . check_cycle_after_dependency ( from, dep. crate_id ) ?;
416-
417- self . arena [ from] . add_dep ( dep) ;
418- Ok ( ( ) )
419- }
420-
421- /// Check if adding a dep from `from` to `to` creates a cycle. To figure
422- /// that out, look for a path in the *opposite* direction, from `to` to
423- /// `from`.
424- fn check_cycle_after_dependency (
425- & self ,
426- from : CrateId ,
427- to : CrateId ,
428- ) -> Result < ( ) , CyclicDependenciesError > {
429- if let Some ( path) = self . find_path ( & mut FxHashSet :: default ( ) , to, from) {
384+ // Check if adding a dep from `from` to `to` creates a cycle. To figure
385+ // that out, look for a path in the *opposite* direction, from `to` to
386+ // `from`.
387+ if let Some ( path) = self . find_path ( & mut FxHashSet :: default ( ) , dep. crate_id , from) {
430388 let path = path. into_iter ( ) . map ( |it| ( it, self [ it] . display_name . clone ( ) ) ) . collect ( ) ;
431389 let err = CyclicDependenciesError { path } ;
432- assert ! ( err. from( ) . 0 == from && err. to( ) . 0 == to ) ;
390+ assert ! ( err. from( ) . 0 == from && err. to( ) . 0 == dep . crate_id ) ;
433391 return Err ( err) ;
434392 }
393+
394+ self . arena [ from] . add_dep ( dep) ;
435395 Ok ( ( ) )
436396 }
437397
@@ -531,43 +491,29 @@ impl CrateGraph {
531491 . for_each ( |( _, data) | data. dependencies . sort_by_key ( |dep| dep. crate_id ) ) ;
532492 }
533493
534- /// Extends this crate graph by adding a complete disjoint second crate
494+ /// Extends this crate graph by adding a complete second crate
535495 /// graph and adjust the ids in the [`ProcMacroPaths`] accordingly.
536496 ///
537- /// This will deduplicate the crates of the graph where possible.
538- /// Note that for deduplication to fully work, `self`'s crate dependencies must be sorted by crate id.
539- /// If the crate dependencies were sorted, the resulting graph from this `extend` call will also
540- /// have the crate dependencies sorted.
541- ///
542- /// Returns a mapping from `other`'s crate ids to the new crate ids in `self`.
497+ /// Returns a map mapping `other`'s IDs to the new IDs in `self`.
543498 pub fn extend (
544499 & mut self ,
545500 mut other : CrateGraph ,
546501 proc_macros : & mut ProcMacroPaths ,
547- merge : impl Fn ( ( CrateId , & mut CrateData ) , ( CrateId , & CrateData ) ) -> bool ,
548502 ) -> FxHashMap < CrateId , CrateId > {
549- let m = self . len ( ) ;
550503 let topo = other. crates_in_topological_order ( ) ;
551504 let mut id_map: FxHashMap < CrateId , CrateId > = FxHashMap :: default ( ) ;
552505 for topo in topo {
553506 let crate_data = & mut other. arena [ topo] ;
554507
555508 crate_data. dependencies . iter_mut ( ) . for_each ( |dep| dep. crate_id = id_map[ & dep. crate_id ] ) ;
556509 crate_data. dependencies . sort_by_key ( |dep| dep. crate_id ) ;
557- let res = self
558- . arena
559- . iter_mut ( )
560- . take ( m)
561- . find_map ( |( id, data) | merge ( ( id, data) , ( topo, crate_data) ) . then_some ( id) ) ;
562-
563- let new_id =
564- if let Some ( res) = res { res } else { self . arena . alloc ( crate_data. clone ( ) ) } ;
510+
511+ let new_id = self . arena . alloc ( crate_data. clone ( ) ) ;
565512 id_map. insert ( topo, new_id) ;
566513 }
567514
568515 * proc_macros =
569516 mem:: take ( proc_macros) . into_iter ( ) . map ( |( id, macros) | ( id_map[ & id] , macros) ) . collect ( ) ;
570-
571517 id_map
572518 }
573519
0 commit comments