mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-11-10 07:04:22 +00:00
Remove crate graph deduplication logic
This commit is contained in:
parent
dd026ce6a4
commit
8905f86d8a
9 changed files with 5 additions and 19657 deletions
|
@ -491,22 +491,15 @@ impl CrateGraph {
|
|||
.for_each(|(_, data)| data.dependencies.sort_by_key(|dep| dep.crate_id));
|
||||
}
|
||||
|
||||
/// Extends this crate graph by adding a complete disjoint second crate
|
||||
/// 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.
|
||||
///
|
||||
/// Returns a mapping from `other`'s crate ids to the new crate ids in `self`.
|
||||
/// Returns a map mapping `other`'s IDs to the new IDs in `self`.
|
||||
pub fn extend(
|
||||
&mut self,
|
||||
mut other: CrateGraph,
|
||||
proc_macros: &mut ProcMacroPaths,
|
||||
merge: impl Fn((CrateId, &mut CrateData), (CrateId, &CrateData)) -> bool,
|
||||
) -> FxHashMap<CrateId, CrateId> {
|
||||
let m = self.len();
|
||||
let topo = other.crates_in_topological_order();
|
||||
let mut id_map: FxHashMap<CrateId, CrateId> = FxHashMap::default();
|
||||
for topo in topo {
|
||||
|
@ -514,20 +507,13 @@ impl CrateGraph {
|
|||
|
||||
crate_data.dependencies.iter_mut().for_each(|dep| dep.crate_id = id_map[&dep.crate_id]);
|
||||
crate_data.dependencies.sort_by_key(|dep| dep.crate_id);
|
||||
let res = self
|
||||
.arena
|
||||
.iter_mut()
|
||||
.take(m)
|
||||
.find_map(|(id, data)| merge((id, data), (topo, crate_data)).then_some(id));
|
||||
|
||||
let new_id =
|
||||
if let Some(res) = res { res } else { self.arena.alloc(crate_data.clone()) };
|
||||
let new_id = self.arena.alloc(crate_data.clone());
|
||||
id_map.insert(topo, new_id);
|
||||
}
|
||||
|
||||
*proc_macros =
|
||||
mem::take(proc_macros).into_iter().map(|(id, macros)| (id_map[&id], macros)).collect();
|
||||
|
||||
id_map
|
||||
}
|
||||
|
||||
|
|
|
@ -45,39 +45,6 @@ fn load_cargo_with_overrides(
|
|||
to_crate_graph(project_workspace)
|
||||
}
|
||||
|
||||
fn load_cargo_with_fake_sysroot(
|
||||
file_map: &mut FxHashMap<AbsPathBuf, FileId>,
|
||||
file: &str,
|
||||
) -> (CrateGraph, ProcMacroPaths) {
|
||||
let meta: Metadata = get_test_json_file(file);
|
||||
let manifest_path =
|
||||
ManifestPath::try_from(AbsPathBuf::try_from(meta.workspace_root.clone()).unwrap()).unwrap();
|
||||
let cargo_workspace = CargoWorkspace::new(meta, manifest_path);
|
||||
let project_workspace = ProjectWorkspace {
|
||||
kind: ProjectWorkspaceKind::Cargo {
|
||||
cargo: cargo_workspace,
|
||||
build_scripts: WorkspaceBuildScripts::default(),
|
||||
rustc: Err(None),
|
||||
cargo_config_extra_env: Default::default(),
|
||||
error: None,
|
||||
},
|
||||
sysroot: get_fake_sysroot(),
|
||||
rustc_cfg: Vec::new(),
|
||||
cfg_overrides: Default::default(),
|
||||
toolchain: None,
|
||||
target_layout: Err("target_data_layout not loaded".into()),
|
||||
};
|
||||
project_workspace.to_crate_graph(
|
||||
&mut {
|
||||
|path| {
|
||||
let len = file_map.len();
|
||||
Some(*file_map.entry(path.to_path_buf()).or_insert(FileId::from_raw(len as u32)))
|
||||
}
|
||||
},
|
||||
&Default::default(),
|
||||
)
|
||||
}
|
||||
|
||||
fn load_rust_project(file: &str) -> (CrateGraph, ProcMacroPaths) {
|
||||
let data = get_test_json_file(file);
|
||||
let project = rooted_project_json(data);
|
||||
|
@ -253,34 +220,6 @@ fn rust_project_is_proc_macro_has_proc_macro_dep() {
|
|||
crate_data.dependencies.iter().find(|&dep| dep.name.deref() == "proc_macro").unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn crate_graph_dedup_identical() {
|
||||
let (mut crate_graph, proc_macros) =
|
||||
load_cargo_with_fake_sysroot(&mut Default::default(), "regex-metadata.json");
|
||||
crate_graph.sort_deps();
|
||||
|
||||
let (d_crate_graph, mut d_proc_macros) = (crate_graph.clone(), proc_macros.clone());
|
||||
|
||||
crate_graph.extend(d_crate_graph.clone(), &mut d_proc_macros, |(_, a), (_, b)| a == b);
|
||||
assert!(crate_graph.iter().eq(d_crate_graph.iter()));
|
||||
assert_eq!(proc_macros, d_proc_macros);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn crate_graph_dedup() {
|
||||
let path_map = &mut Default::default();
|
||||
let (mut crate_graph, _proc_macros) =
|
||||
load_cargo_with_fake_sysroot(path_map, "ripgrep-metadata.json");
|
||||
assert_eq!(crate_graph.iter().count(), 81);
|
||||
crate_graph.sort_deps();
|
||||
let (regex_crate_graph, mut regex_proc_macros) =
|
||||
load_cargo_with_fake_sysroot(path_map, "regex-metadata.json");
|
||||
assert_eq!(regex_crate_graph.iter().count(), 60);
|
||||
|
||||
crate_graph.extend(regex_crate_graph, &mut regex_proc_macros, |(_, a), (_, b)| a == b);
|
||||
assert_eq!(crate_graph.iter().count(), 118);
|
||||
}
|
||||
|
||||
#[test]
|
||||
// FIXME Remove the ignore
|
||||
#[ignore = "requires nightly until the sysroot ships a cargo workspace for library on stable"]
|
||||
|
|
|
@ -1456,7 +1456,7 @@ fn sysroot_to_crate_graph(
|
|||
|
||||
// Remove all crates except the ones we are interested in to keep the sysroot graph small.
|
||||
let removed_mapping = cg.remove_crates_except(&marker_set);
|
||||
let mapping = crate_graph.extend(cg, &mut pm, |(_, a), (_, b)| a == b);
|
||||
let mapping = crate_graph.extend(cg, &mut pm);
|
||||
|
||||
// Map the id through the removal mapping first, then through the crate graph extension mapping.
|
||||
pub_deps.iter_mut().for_each(|(_, cid, _)| {
|
||||
|
|
File diff suppressed because it is too large
Load diff
File diff suppressed because it is too large
Load diff
|
@ -875,16 +875,7 @@ pub fn ws_to_crate_graph(
|
|||
let num_toolchains = toolchains.len();
|
||||
let ProjectWorkspace { toolchain, target_layout, .. } = ws;
|
||||
|
||||
let mapping = crate_graph.extend(
|
||||
other,
|
||||
&mut crate_proc_macros,
|
||||
|(cg_id, cg_data), (_o_id, o_data)| {
|
||||
// if the newly created crate graph's layout is equal to the crate of the merged graph, then
|
||||
// we can merge the crates.
|
||||
let id = cg_id.into_raw().into_u32() as usize;
|
||||
layouts[id] == *target_layout && toolchains[id] == *toolchain && cg_data == o_data
|
||||
},
|
||||
);
|
||||
let mapping = crate_graph.extend(other, &mut crate_proc_macros);
|
||||
// Populate the side tables for the newly merged crates
|
||||
mapping.values().for_each(|val| {
|
||||
let idx = val.into_raw().into_u32() as usize;
|
||||
|
|
|
@ -1,126 +0,0 @@
|
|||
use std::path::PathBuf;
|
||||
|
||||
use project_model::{
|
||||
CargoWorkspace, ManifestPath, Metadata, ProjectWorkspace, ProjectWorkspaceKind, Sysroot,
|
||||
WorkspaceBuildScripts,
|
||||
};
|
||||
use rust_analyzer::ws_to_crate_graph;
|
||||
use rustc_hash::FxHashMap;
|
||||
use serde::de::DeserializeOwned;
|
||||
use vfs::{AbsPathBuf, FileId};
|
||||
|
||||
fn load_cargo_with_fake_sysroot(file: &str) -> ProjectWorkspace {
|
||||
let meta: Metadata = get_test_json_file(file);
|
||||
let manifest_path =
|
||||
ManifestPath::try_from(AbsPathBuf::try_from(meta.workspace_root.clone()).unwrap()).unwrap();
|
||||
let cargo_workspace = CargoWorkspace::new(meta, manifest_path);
|
||||
ProjectWorkspace {
|
||||
kind: ProjectWorkspaceKind::Cargo {
|
||||
cargo: cargo_workspace,
|
||||
build_scripts: WorkspaceBuildScripts::default(),
|
||||
rustc: Err(None),
|
||||
cargo_config_extra_env: Default::default(),
|
||||
error: None,
|
||||
},
|
||||
sysroot: get_fake_sysroot(),
|
||||
rustc_cfg: Vec::new(),
|
||||
cfg_overrides: Default::default(),
|
||||
toolchain: None,
|
||||
target_layout: Err("target_data_layout not loaded".into()),
|
||||
}
|
||||
}
|
||||
|
||||
fn get_test_json_file<T: DeserializeOwned>(file: &str) -> T {
|
||||
let base = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
||||
let file = base.join("tests/test_data").join(file);
|
||||
let data = std::fs::read_to_string(file).unwrap();
|
||||
let mut json = data.parse::<serde_json::Value>().unwrap();
|
||||
fixup_paths(&mut json);
|
||||
return serde_json::from_value(json).unwrap();
|
||||
|
||||
fn fixup_paths(val: &mut serde_json::Value) {
|
||||
match val {
|
||||
serde_json::Value::String(s) => replace_root(s, true),
|
||||
serde_json::Value::Array(vals) => vals.iter_mut().for_each(fixup_paths),
|
||||
serde_json::Value::Object(kvals) => kvals.values_mut().for_each(fixup_paths),
|
||||
serde_json::Value::Null | serde_json::Value::Bool(_) | serde_json::Value::Number(_) => {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn replace_root(s: &mut String, direction: bool) {
|
||||
if direction {
|
||||
let root = if cfg!(windows) { r#"C:\\ROOT\"# } else { "/ROOT/" };
|
||||
*s = s.replace("$ROOT$", root)
|
||||
} else {
|
||||
let root = if cfg!(windows) { r#"C:\\\\ROOT\\"# } else { "/ROOT/" };
|
||||
*s = s.replace(root, "$ROOT$")
|
||||
}
|
||||
}
|
||||
|
||||
fn get_fake_sysroot_path() -> PathBuf {
|
||||
let base = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
||||
base.join("../project-model/test_data/fake-sysroot")
|
||||
}
|
||||
|
||||
fn get_fake_sysroot() -> Sysroot {
|
||||
let sysroot_path = get_fake_sysroot_path();
|
||||
// there's no `libexec/` directory with a `proc-macro-srv` binary in that
|
||||
// fake sysroot, so we give them both the same path:
|
||||
let sysroot_dir = AbsPathBuf::assert_utf8(sysroot_path);
|
||||
let sysroot_src_dir = sysroot_dir.clone();
|
||||
Sysroot::load(Some(sysroot_dir), Some(sysroot_src_dir))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_deduplicate_origin_dev() {
|
||||
let path_map = &mut FxHashMap::default();
|
||||
let ws = load_cargo_with_fake_sysroot("deduplication_crate_graph_A.json");
|
||||
let ws2 = load_cargo_with_fake_sysroot("deduplication_crate_graph_B.json");
|
||||
|
||||
let (crate_graph, ..) = ws_to_crate_graph(&[ws, ws2], &Default::default(), |path| {
|
||||
let len = path_map.len();
|
||||
Some(*path_map.entry(path.to_path_buf()).or_insert(FileId::from_raw(len as u32)))
|
||||
});
|
||||
|
||||
let mut crates_named_p2 = vec![];
|
||||
for id in crate_graph.iter() {
|
||||
let krate = &crate_graph[id];
|
||||
if let Some(name) = krate.display_name.as_ref() {
|
||||
if name.to_string() == "p2" {
|
||||
crates_named_p2.push(krate);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
assert_eq!(crates_named_p2.len(), 1);
|
||||
let p2 = crates_named_p2[0];
|
||||
assert!(p2.origin.is_local());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_deduplicate_origin_dev_rev() {
|
||||
let path_map = &mut FxHashMap::default();
|
||||
let ws = load_cargo_with_fake_sysroot("deduplication_crate_graph_B.json");
|
||||
let ws2 = load_cargo_with_fake_sysroot("deduplication_crate_graph_A.json");
|
||||
|
||||
let (crate_graph, ..) = ws_to_crate_graph(&[ws, ws2], &Default::default(), |path| {
|
||||
let len = path_map.len();
|
||||
Some(*path_map.entry(path.to_path_buf()).or_insert(FileId::from_raw(len as u32)))
|
||||
});
|
||||
|
||||
let mut crates_named_p2 = vec![];
|
||||
for id in crate_graph.iter() {
|
||||
let krate = &crate_graph[id];
|
||||
if let Some(name) = krate.display_name.as_ref() {
|
||||
if name.to_string() == "p2" {
|
||||
crates_named_p2.push(krate);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
assert_eq!(crates_named_p2.len(), 1);
|
||||
let p2 = crates_named_p2[0];
|
||||
assert!(p2.origin.is_local());
|
||||
}
|
|
@ -1,140 +0,0 @@
|
|||
{
|
||||
"packages": [
|
||||
{
|
||||
"name": "p1",
|
||||
"version": "0.1.0",
|
||||
"id": "p1 0.1.0 (path+file:///example_project/p1)",
|
||||
"license": null,
|
||||
"license_file": null,
|
||||
"description": null,
|
||||
"source": null,
|
||||
"dependencies": [
|
||||
{
|
||||
"name": "p2",
|
||||
"source": null,
|
||||
"req": "*",
|
||||
"kind": null,
|
||||
"rename": null,
|
||||
"optional": false,
|
||||
"uses_default_features": true,
|
||||
"features": [],
|
||||
"target": null,
|
||||
"registry": null,
|
||||
"path": "$ROOT$example_project/p2"
|
||||
}
|
||||
],
|
||||
"targets": [
|
||||
{
|
||||
"kind": [
|
||||
"lib"
|
||||
],
|
||||
"crate_types": [
|
||||
"lib"
|
||||
],
|
||||
"name": "p1",
|
||||
"src_path": "$ROOT$example_project/p1/src/lib.rs",
|
||||
"edition": "2021",
|
||||
"doc": true,
|
||||
"doctest": true,
|
||||
"test": true
|
||||
}
|
||||
],
|
||||
"features": {},
|
||||
"manifest_path": "$ROOT$example_project/p1/Cargo.toml",
|
||||
"metadata": null,
|
||||
"publish": null,
|
||||
"authors": [],
|
||||
"categories": [],
|
||||
"keywords": [],
|
||||
"readme": null,
|
||||
"repository": null,
|
||||
"homepage": null,
|
||||
"documentation": null,
|
||||
"edition": "2021",
|
||||
"links": null,
|
||||
"default_run": null,
|
||||
"rust_version": null
|
||||
},
|
||||
{
|
||||
"name": "p2",
|
||||
"version": "0.1.0",
|
||||
"id": "p2 0.1.0 (path+file:///example_project/p2)",
|
||||
"license": null,
|
||||
"license_file": null,
|
||||
"description": null,
|
||||
"source": null,
|
||||
"dependencies": [],
|
||||
"targets": [
|
||||
{
|
||||
"kind": [
|
||||
"lib"
|
||||
],
|
||||
"crate_types": [
|
||||
"lib"
|
||||
],
|
||||
"name": "p2",
|
||||
"src_path": "$ROOT$example_project/p2/src/lib.rs",
|
||||
"edition": "2021",
|
||||
"doc": true,
|
||||
"doctest": true,
|
||||
"test": true
|
||||
}
|
||||
],
|
||||
"features": {},
|
||||
"manifest_path": "$ROOT$example_project/p2/Cargo.toml",
|
||||
"metadata": null,
|
||||
"publish": null,
|
||||
"authors": [],
|
||||
"categories": [],
|
||||
"keywords": [],
|
||||
"readme": null,
|
||||
"repository": null,
|
||||
"homepage": null,
|
||||
"documentation": null,
|
||||
"edition": "2021",
|
||||
"links": null,
|
||||
"default_run": null,
|
||||
"rust_version": null
|
||||
}
|
||||
],
|
||||
"workspace_members": [
|
||||
"p1 0.1.0 (path+file:///example_project/p1)"
|
||||
],
|
||||
"workspace_default_members": [
|
||||
"p1 0.1.0 (path+file:///example_project/p1)"
|
||||
],
|
||||
"resolve": {
|
||||
"nodes": [
|
||||
{
|
||||
"id": "p1 0.1.0 (path+file:///example_project/p1)",
|
||||
"dependencies": [
|
||||
"p2 0.1.0 (path+file:///example_project/p2)"
|
||||
],
|
||||
"deps": [
|
||||
{
|
||||
"name": "p2",
|
||||
"pkg": "p2 0.1.0 (path+file:///example_project/p2)",
|
||||
"dep_kinds": [
|
||||
{
|
||||
"kind": null,
|
||||
"target": null
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"features": []
|
||||
},
|
||||
{
|
||||
"id": "p2 0.1.0 (path+file:///example_project/p2)",
|
||||
"dependencies": [],
|
||||
"deps": [],
|
||||
"features": []
|
||||
}
|
||||
],
|
||||
"root": "p1 0.1.0 (path+file:///example_project/p1)"
|
||||
},
|
||||
"target_directory": "$ROOT$example_project/p1/target",
|
||||
"version": 1,
|
||||
"workspace_root": "$ROOT$example_project/p1",
|
||||
"metadata": null
|
||||
}
|
|
@ -1,66 +0,0 @@
|
|||
{
|
||||
"packages": [
|
||||
{
|
||||
"name": "p2",
|
||||
"version": "0.1.0",
|
||||
"id": "p2 0.1.0 (path+file:///example_project/p2)",
|
||||
"license": null,
|
||||
"license_file": null,
|
||||
"description": null,
|
||||
"source": null,
|
||||
"dependencies": [],
|
||||
"targets": [
|
||||
{
|
||||
"kind": [
|
||||
"lib"
|
||||
],
|
||||
"crate_types": [
|
||||
"lib"
|
||||
],
|
||||
"name": "p2",
|
||||
"src_path": "$ROOT$example_project/p2/src/lib.rs",
|
||||
"edition": "2021",
|
||||
"doc": true,
|
||||
"doctest": true,
|
||||
"test": true
|
||||
}
|
||||
],
|
||||
"features": {},
|
||||
"manifest_path": "$ROOT$example_project/p2/Cargo.toml",
|
||||
"metadata": null,
|
||||
"publish": null,
|
||||
"authors": [],
|
||||
"categories": [],
|
||||
"keywords": [],
|
||||
"readme": null,
|
||||
"repository": null,
|
||||
"homepage": null,
|
||||
"documentation": null,
|
||||
"edition": "2021",
|
||||
"links": null,
|
||||
"default_run": null,
|
||||
"rust_version": null
|
||||
}
|
||||
],
|
||||
"workspace_members": [
|
||||
"p2 0.1.0 (path+file:///example_project/p2)"
|
||||
],
|
||||
"workspace_default_members": [
|
||||
"p2 0.1.0 (path+file:///example_project/p2)"
|
||||
],
|
||||
"resolve": {
|
||||
"nodes": [
|
||||
{
|
||||
"id": "p2 0.1.0 (path+file:///example_project/p2)",
|
||||
"dependencies": [],
|
||||
"deps": [],
|
||||
"features": []
|
||||
}
|
||||
],
|
||||
"root": "p2 0.1.0 (path+file:///example_project/p2)"
|
||||
},
|
||||
"target_directory": "$ROOT$example_project/p2/target",
|
||||
"version": 1,
|
||||
"workspace_root": "$ROOT$example_project/p2",
|
||||
"metadata": null
|
||||
}
|
Loading…
Reference in a new issue