about summary refs log tree commit diff
diff options
context:
space:
mode:
authorlucasholten <lucasholten@dreamsolution.nl>2025-01-02 15:50:51 +0100
committerlucasholten <lucasholten@dreamsolution.nl>2025-01-02 15:50:51 +0100
commit4485cbb0d42de605fd09a922d1a3a8b174af9839 (patch)
tree95c66f4ffbd0cd190f1b3070202e69a864423fba
parent71d996f34e2c9f73b16905dc3ae91a70358ee52f (diff)
downloadrust-4485cbb0d42de605fd09a922d1a3a8b174af9839.tar.gz
rust-4485cbb0d42de605fd09a922d1a3a8b174af9839.zip
Automatically sort crate graph
-rw-r--r--src/tools/rust-analyzer/crates/base-db/src/input.rs16
-rw-r--r--src/tools/rust-analyzer/crates/project-model/src/tests.rs2
2 files changed, 7 insertions, 11 deletions
diff --git a/src/tools/rust-analyzer/crates/base-db/src/input.rs b/src/tools/rust-analyzer/crates/base-db/src/input.rs
index b263e7382d8..a0fc8c31eaf 100644
--- a/src/tools/rust-analyzer/crates/base-db/src/input.rs
+++ b/src/tools/rust-analyzer/crates/base-db/src/input.rs
@@ -490,19 +490,11 @@ impl CrateGraph {
         }
     }
 
-    pub fn sort_deps(&mut self) {
-        self.arena
-            .iter_mut()
-            .for_each(|(_, data)| data.dependencies.sort_by_key(|dep| dep.crate_id));
-    }
-
     /// Extends this crate graph by adding a complete second crate
     /// graph and adjust the ids in the [`ProcMacroPaths`] accordingly.
     ///
     /// This will deduplicate the crates of the graph where possible.
-    /// Note that for deduplication to fully work, `self`'s crate dependencies must be sorted by crate id.
-    /// If the crate dependencies were sorted, the resulting graph from this `extend` call will also
-    /// have the crate dependencies sorted.
+    /// Furthermore dependencies are sorted by crate id to make deduplication easier.
     ///
     /// Returns a map mapping `other`'s IDs to the new IDs in `self`.
     pub fn extend(
@@ -510,6 +502,12 @@ impl CrateGraph {
         mut other: CrateGraph,
         proc_macros: &mut ProcMacroPaths,
     ) -> FxHashMap<CrateId, CrateId> {
+        // Sorting here is a bit pointless because the input is likely already sorted.
+        // However, the overhead is small and it makes the `extend` method harder to misuse.
+        self.arena
+            .iter_mut()
+            .for_each(|(_, data)| data.dependencies.sort_by_key(|dep| dep.crate_id));
+
         let m = self.len();
         let topo = other.crates_in_topological_order();
         let mut id_map: FxHashMap<CrateId, CrateId> = FxHashMap::default();
diff --git a/src/tools/rust-analyzer/crates/project-model/src/tests.rs b/src/tools/rust-analyzer/crates/project-model/src/tests.rs
index 74d41436055..8bb130433a1 100644
--- a/src/tools/rust-analyzer/crates/project-model/src/tests.rs
+++ b/src/tools/rust-analyzer/crates/project-model/src/tests.rs
@@ -232,7 +232,6 @@ fn rust_project_is_proc_macro_has_proc_macro_dep() {
 #[test]
 fn crate_graph_dedup_identical() {
     let (mut crate_graph, proc_macros) = load_cargo("regex-metadata.json");
-    crate_graph.sort_deps();
 
     let (d_crate_graph, mut d_proc_macros) = (crate_graph.clone(), proc_macros.clone());
 
@@ -253,7 +252,6 @@ fn crate_graph_dedup() {
     let (regex_crate_graph, mut regex_proc_macros) = to_crate_graph(regex_workspace, &mut file_map);
     assert_eq!(regex_crate_graph.iter().count(), 50);
 
-    crate_graph.sort_deps();
     crate_graph.extend(regex_crate_graph, &mut regex_proc_macros);
     assert_eq!(crate_graph.iter().count(), 108);
 }