Skip to content

Commit 1d268fc

Browse files
committed
v2
1 parent d7b72ce commit 1d268fc

File tree

6 files changed

+282
-238
lines changed

6 files changed

+282
-238
lines changed

crates/base-db/src/input.rs

+22-30
Original file line numberDiff line numberDiff line change
@@ -329,12 +329,10 @@ pub struct CrateData {
329329
}
330330

331331
impl CrateData {
332-
/**
333-
Check if [`other`] is almost equal to [`self`].
334-
This method has some obscure bits. These are mostly there to be compliant with
335-
some patches. References to the patches are given.
336-
*/
337-
pub fn almost_eq(&self, other: &CrateData) -> bool {
332+
/// Check if [`other`] is almost equal to [`self`] ignoring `CrateOrigin` value.
333+
pub fn eq_ignoring_origin(&self, other: &CrateData) -> bool {
334+
// This method has some obscure bits. These are mostly there to be compliant with
335+
// some patches. References to the patches are given.
338336
if self.root_file_id != other.root_file_id {
339337
return false;
340338
}
@@ -356,16 +354,16 @@ impl CrateData {
356354
}
357355

358356
let mut opts = self.cfg_options.clone();
359-
opts.apply_diff(CfgDiff {
360-
disable: other.cfg_options.clone().into_iter().collect(),
361-
enable: vec![],
362-
});
357+
opts.apply_diff(
358+
CfgDiff::new(vec![], other.cfg_options.clone().into_iter().collect())
359+
.expect("CfgOptions were expected to contain no duplicates."),
360+
);
363361

364362
let mut cfgs = opts.into_iter();
365363
if let Some(cfg) = cfgs.next() {
366364
// Don't care if rust_analyzer CfgAtom is the only cfg in the difference set of self's and other's cfgs.
367365
// https://github.com/rust-lang/rust-analyzer/blob/0840038f02daec6ba3238f05d8caa037d28701a0/crates/project-model/src/workspace.rs#L894
368-
if !cfgs.next().is_none() || cfg.to_string() != "rust_analyzer" {
366+
if cfgs.next().is_some() || cfg.to_string() != "rust_analyzer" {
369367
return false;
370368
}
371369
}
@@ -686,41 +684,35 @@ impl CrateGraph {
686684
/// Note that for deduplication to fully work, `self`'s crate dependencies must be sorted by crate id.
687685
/// If the crate dependencies were sorted, the resulting graph from this `extend` call will also have the crate dependencies sorted.
688686
pub fn extend(&mut self, mut other: CrateGraph, proc_macros: &mut ProcMacroPaths) {
689-
enum ExtendStrategy {
690-
Dedup(CrateId),
691-
Replace(CrateId),
692-
}
693-
694687
let topo = other.crates_in_topological_order();
695688
let mut id_map: FxHashMap<CrateId, CrateId> = FxHashMap::default();
696689
for topo in topo {
697690
let crate_data = &mut other.arena[topo];
698691

699-
crate_data.dependencies.iter_mut().for_each(|dep| {
700-
dep.crate_id = id_map[&dep.crate_id];
701-
});
692+
crate_data.dependencies.iter_mut().for_each(|dep| dep.crate_id = id_map[&dep.crate_id]);
702693
crate_data.dependencies.sort_by_key(|dep| dep.crate_id);
703694
let res = self.arena.iter().find_map(|(id, data)| {
704-
if data.almost_eq(crate_data) {
695+
if data.eq_ignoring_origin(crate_data) {
705696
if data.origin.is_lib() && crate_data.origin.is_local() {
706697
// See #15656 for a relevant example.
707-
return Some(ExtendStrategy::Replace(id));
698+
return Some((id, true));
708699
}
709700

710-
return Some(ExtendStrategy::Dedup(id));
701+
return Some((id, false));
711702
}
712703
None
713704
});
714705

715-
if let Some(res) = res {
716-
match res {
717-
ExtendStrategy::Dedup(res) => id_map.insert(topo, res),
718-
ExtendStrategy::Replace(res) => {
719-
let id = self.arena.alloc(crate_data.clone());
720-
let _ = self.remove_and_replace(res, id);
721-
id_map.insert(topo, id)
706+
if let Some((res, should_update_lib_to_local)) = res {
707+
id_map.insert(topo, res);
708+
if should_update_lib_to_local {
709+
let origin_old = self.arena[res].origin.clone();
710+
assert!(origin_old.is_lib());
711+
712+
if let CrateOrigin::Library { repo, name } = origin_old {
713+
self.arena[res].origin = CrateOrigin::Local { repo, name: Some(name) };
722714
}
723-
};
715+
}
724716
} else {
725717
let id = self.arena.alloc(crate_data.clone());
726718
id_map.insert(topo, id);

crates/cfg/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ impl<'a> IntoIterator for &'a CfgOptions {
115115
#[derive(Default, Clone, Debug, PartialEq, Eq)]
116116
pub struct CfgDiff {
117117
// Invariants: No duplicates, no atom that's both in `enable` and `disable`.
118-
pub enable: Vec<CfgAtom>,
119-
pub disable: Vec<CfgAtom>,
118+
enable: Vec<CfgAtom>,
119+
disable: Vec<CfgAtom>,
120120
}
121121

122122
impl CfgDiff {

crates/project-model/src/tests.rs

+50
Original file line numberDiff line numberDiff line change
@@ -249,3 +249,53 @@ fn crate_graph_dedup() {
249249
crate_graph.extend(regex_crate_graph, &mut regex_proc_macros);
250250
assert_eq!(crate_graph.iter().count(), 118);
251251
}
252+
253+
#[test]
254+
fn test_deduplicate_crate_differing_in_origin() {
255+
let path_map = &mut Default::default();
256+
let (mut crate_graph, _proc_macros) =
257+
load_cargo_with_sysroot(path_map, "deduplication_crate_graph_A.json");
258+
crate_graph.sort_deps();
259+
let (crate_graph_1, mut _proc_macros_2) =
260+
load_cargo_with_sysroot(path_map, "deduplication_crate_graph_B.json");
261+
262+
crate_graph.extend(crate_graph_1, &mut _proc_macros_2);
263+
264+
let mut crates_named_p1 = vec![];
265+
for id in crate_graph.iter() {
266+
let krate = &crate_graph[id];
267+
if let Some(name) = krate.display_name.as_ref() {
268+
if name.to_string() == "p1" {
269+
crates_named_p1.push(krate);
270+
}
271+
}
272+
}
273+
274+
assert!(crates_named_p1.len() == 1);
275+
assert!(crates_named_p1[0].origin.is_local());
276+
}
277+
278+
#[test]
279+
fn test_deduplicate_crate_differing_in_origin_in_rev_resolution_order() {
280+
let path_map = &mut Default::default();
281+
let (mut crate_graph, _proc_macros) =
282+
load_cargo_with_sysroot(path_map, "deduplication_crate_graph_B.json");
283+
crate_graph.sort_deps();
284+
let (crate_graph_1, mut _proc_macros_2) =
285+
load_cargo_with_sysroot(path_map, "deduplication_crate_graph_A.json");
286+
287+
crate_graph.extend(crate_graph_1, &mut _proc_macros_2);
288+
289+
let mut crates_named_p1 = vec![];
290+
for id in crate_graph.iter() {
291+
let krate = &crate_graph[id];
292+
if let Some(name) = krate.display_name.as_ref() {
293+
if name.to_string() == "p1" {
294+
crates_named_p1.push(krate);
295+
}
296+
}
297+
}
298+
299+
assert!(crates_named_p1.len() == 1);
300+
assert!(crates_named_p1[0].origin.is_local());
301+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
{
2+
"packages": [
3+
{
4+
"name": "p1",
5+
"version": "0.1.0",
6+
"id": "p1 0.1.0 (path+file:///path/to/project/projects/p1)",
7+
"license": null,
8+
"license_file": null,
9+
"description": null,
10+
"source": null,
11+
"dependencies": [],
12+
"targets": [
13+
{
14+
"kind": [
15+
"lib"
16+
],
17+
"crate_types": [
18+
"lib"
19+
],
20+
"name": "p1",
21+
"src_path": "/path/to/project/projects/p1/src/lib.rs",
22+
"edition": "2021",
23+
"doc": true,
24+
"doctest": true,
25+
"test": true
26+
}
27+
],
28+
"features": {},
29+
"manifest_path": "/path/to/project/projects/p1/Cargo.toml",
30+
"metadata": null,
31+
"publish": null,
32+
"authors": [],
33+
"categories": [],
34+
"keywords": [],
35+
"readme": null,
36+
"repository": null,
37+
"homepage": null,
38+
"documentation": null,
39+
"edition": "2021",
40+
"links": null,
41+
"default_run": null,
42+
"rust_version": null
43+
}
44+
],
45+
"workspace_members": [
46+
"p1 0.1.0 (path+file:///path/to/project/projects/p1)"
47+
],
48+
"workspace_default_members": [
49+
"p1 0.1.0 (path+file:///path/to/project/projects/p1)"
50+
],
51+
"resolve": {
52+
"nodes": [
53+
{
54+
"id": "p1 0.1.0 (path+file:///path/to/project/projects/p1)",
55+
"dependencies": [],
56+
"deps": [],
57+
"features": []
58+
}
59+
],
60+
"root": "p1 0.1.0 (path+file:///path/to/project/projects/p1)"
61+
},
62+
"target_directory": "/path/to/project/projects/p1/target",
63+
"version": 1,
64+
"workspace_root": "/path/to/project/projects/p1",
65+
"metadata": null
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
{
2+
"packages": [
3+
{
4+
"name": "p1",
5+
"version": "0.1.0",
6+
"id": "p1 0.1.0 (path+file:///path/to/project/projects/p1)",
7+
"license": null,
8+
"license_file": null,
9+
"description": null,
10+
"source": null,
11+
"dependencies": [],
12+
"targets": [
13+
{
14+
"kind": [
15+
"lib"
16+
],
17+
"crate_types": [
18+
"lib"
19+
],
20+
"name": "p1",
21+
"src_path": "/path/to/project/projects/p1/src/lib.rs",
22+
"edition": "2021",
23+
"doc": true,
24+
"doctest": true,
25+
"test": true
26+
}
27+
],
28+
"features": {},
29+
"manifest_path": "/path/to/project/projects/p1/Cargo.toml",
30+
"metadata": null,
31+
"publish": null,
32+
"authors": [],
33+
"categories": [],
34+
"keywords": [],
35+
"readme": null,
36+
"repository": null,
37+
"homepage": null,
38+
"documentation": null,
39+
"edition": "2021",
40+
"links": null,
41+
"default_run": null,
42+
"rust_version": null
43+
},
44+
{
45+
"name": "p2",
46+
"version": "0.1.0",
47+
"id": "p2 0.1.0 (path+file:///path/to/project/projects/p2)",
48+
"license": null,
49+
"license_file": null,
50+
"description": null,
51+
"source": null,
52+
"dependencies": [
53+
{
54+
"name": "p1",
55+
"source": null,
56+
"req": "*",
57+
"kind": null,
58+
"rename": null,
59+
"optional": false,
60+
"uses_default_features": true,
61+
"features": [],
62+
"target": null,
63+
"registry": null,
64+
"path": "/path/to/project/projects/p1"
65+
}
66+
],
67+
"targets": [
68+
{
69+
"kind": [
70+
"lib"
71+
],
72+
"crate_types": [
73+
"lib"
74+
],
75+
"name": "p2",
76+
"src_path": "/path/to/project/projects/p2/src/lib.rs",
77+
"edition": "2021",
78+
"doc": true,
79+
"doctest": true,
80+
"test": true
81+
}
82+
],
83+
"features": {},
84+
"manifest_path": "/path/to/project/projects/p2/Cargo.toml",
85+
"metadata": null,
86+
"publish": null,
87+
"authors": [],
88+
"categories": [],
89+
"keywords": [],
90+
"readme": null,
91+
"repository": null,
92+
"homepage": null,
93+
"documentation": null,
94+
"edition": "2021",
95+
"links": null,
96+
"default_run": null,
97+
"rust_version": null
98+
}
99+
],
100+
"workspace_members": [
101+
"p2 0.1.0 (path+file:///path/to/project/projects/p2)"
102+
],
103+
"workspace_default_members": [
104+
"p2 0.1.0 (path+file:///path/to/project/projects/p2)"
105+
],
106+
"resolve": {
107+
"nodes": [
108+
{
109+
"id": "p1 0.1.0 (path+file:///path/to/project/projects/p1)",
110+
"dependencies": [],
111+
"deps": [],
112+
"features": []
113+
},
114+
{
115+
"id": "p2 0.1.0 (path+file:///path/to/project/projects/p2)",
116+
"dependencies": [
117+
"p1 0.1.0 (path+file:///path/to/project/projects/p1)"
118+
],
119+
"deps": [
120+
{
121+
"name": "p1",
122+
"pkg": "p1 0.1.0 (path+file:///path/to/project/projects/p1)",
123+
"dep_kinds": [
124+
{
125+
"kind": null,
126+
"target": null
127+
}
128+
]
129+
}
130+
],
131+
"features": []
132+
}
133+
],
134+
"root": "p2 0.1.0 (path+file:///path/to/project/projects/p2)"
135+
},
136+
"target_directory": "/path/to/project/projects/p2/target",
137+
"version": 1,
138+
"workspace_root": "/path/to/project/projects/p2",
139+
"metadata": null
140+
}

0 commit comments

Comments
 (0)