Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a96bb45

Browse files
committedApr 25, 2023
Auto merge of rust-lang#14475 - Veykril:crate-origins, r=Veykril
Handle dev-dependency cycles cc rust-lang/rust-analyzer#14167 This should fix cycles errors mostly (it fixes the one on rome/tools at least, but not on rustc. Though there it might just be because of rustc workspace being rustc workspace). Unfortunately this will effectively duplicate all crates currently, since if we want to be completely correct we'd need to set the test cfg for all dev dependencies and the transitive dependencies of those, something I worry we should try to avoid.
2 parents e46d7a0 + e205af2 commit a96bb45

File tree

13 files changed

+1798
-52
lines changed

13 files changed

+1798
-52
lines changed
 

‎Cargo.lock

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎crates/base-db/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ doctest = false
1414
[dependencies]
1515
salsa = "0.17.0-pre.2"
1616
rustc-hash = "1.1.0"
17+
indexmap = "1.6.0"
1718

1819
la-arena = { version = "0.3.0", path = "../../lib/la-arena" }
1920

‎crates/base-db/src/input.rs

+11
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,11 @@ impl CrateGraph {
417417
Ok(())
418418
}
419419

420+
pub fn duplicate(&mut self, id: CrateId) -> CrateId {
421+
let data = self[id].clone();
422+
self.arena.alloc(data)
423+
}
424+
420425
pub fn add_dep(
421426
&mut self,
422427
from: CrateId,
@@ -612,6 +617,12 @@ impl ops::Index<CrateId> for CrateGraph {
612617
}
613618
}
614619

620+
impl ops::IndexMut<CrateId> for CrateGraph {
621+
fn index_mut(&mut self, crate_id: CrateId) -> &mut CrateData {
622+
&mut self.arena[crate_id]
623+
}
624+
}
625+
615626
impl CrateData {
616627
fn add_dep(&mut self, dep: Dependency) {
617628
self.dependencies.push(dep)

‎crates/base-db/src/lib.rs

+19-6
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ mod input;
66
mod change;
77
pub mod fixture;
88

9-
use std::{panic, sync::Arc};
9+
use std::{hash::BuildHasherDefault, panic, sync::Arc};
1010

11-
use rustc_hash::FxHashSet;
11+
use indexmap::IndexSet;
12+
use rustc_hash::FxHasher;
1213
use syntax::{ast, Parse, SourceFile, TextRange, TextSize};
1314

1415
pub use crate::{
@@ -59,7 +60,10 @@ pub trait FileLoader {
5960
/// Text of the file.
6061
fn file_text(&self, file_id: FileId) -> Arc<str>;
6162
fn resolve_path(&self, path: AnchoredPath<'_>) -> Option<FileId>;
62-
fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>>;
63+
fn relevant_crates(
64+
&self,
65+
file_id: FileId,
66+
) -> Arc<IndexSet<CrateId, BuildHasherDefault<FxHasher>>>;
6367
}
6468

6569
/// Database which stores all significant input facts: source code and project
@@ -99,10 +103,16 @@ pub trait SourceDatabaseExt: SourceDatabase {
99103
#[salsa::input]
100104
fn source_root(&self, id: SourceRootId) -> Arc<SourceRoot>;
101105

102-
fn source_root_crates(&self, id: SourceRootId) -> Arc<FxHashSet<CrateId>>;
106+
fn source_root_crates(
107+
&self,
108+
id: SourceRootId,
109+
) -> Arc<IndexSet<CrateId, BuildHasherDefault<FxHasher>>>;
103110
}
104111

105-
fn source_root_crates(db: &dyn SourceDatabaseExt, id: SourceRootId) -> Arc<FxHashSet<CrateId>> {
112+
fn source_root_crates(
113+
db: &dyn SourceDatabaseExt,
114+
id: SourceRootId,
115+
) -> Arc<IndexSet<CrateId, BuildHasherDefault<FxHasher>>> {
106116
let graph = db.crate_graph();
107117
let res = graph
108118
.iter()
@@ -128,7 +138,10 @@ impl<T: SourceDatabaseExt> FileLoader for FileLoaderDelegate<&'_ T> {
128138
source_root.resolve_path(path)
129139
}
130140

131-
fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>> {
141+
fn relevant_crates(
142+
&self,
143+
file_id: FileId,
144+
) -> Arc<IndexSet<CrateId, BuildHasherDefault<FxHasher>>> {
132145
let _p = profile::span("relevant_crates");
133146
let source_root = self.0.file_source_root(file_id);
134147
self.0.source_root_crates(source_root)

‎crates/hir-def/src/test_db.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
//! Database used for testing `hir_def`.
22
33
use std::{
4-
fmt, panic,
4+
fmt,
5+
hash::BuildHasherDefault,
6+
panic,
57
sync::{Arc, Mutex},
68
};
79

@@ -11,7 +13,8 @@ use base_db::{
1113
Upcast,
1214
};
1315
use hir_expand::{db::ExpandDatabase, InFile};
14-
use rustc_hash::FxHashSet;
16+
use indexmap::IndexSet;
17+
use rustc_hash::FxHasher;
1518
use syntax::{algo, ast, AstNode};
1619

1720
use crate::{
@@ -77,7 +80,10 @@ impl FileLoader for TestDB {
7780
fn resolve_path(&self, path: AnchoredPath<'_>) -> Option<FileId> {
7881
FileLoaderDelegate(self).resolve_path(path)
7982
}
80-
fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>> {
83+
fn relevant_crates(
84+
&self,
85+
file_id: FileId,
86+
) -> Arc<IndexSet<CrateId, BuildHasherDefault<FxHasher>>> {
8187
FileLoaderDelegate(self).relevant_crates(file_id)
8288
}
8389
}

‎crates/hir-ty/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ la-arena = { version = "0.3.0", path = "../../lib/la-arena" }
3030
once_cell = "1.17.0"
3131
typed-arena = "2.0.1"
3232
rustc_index = { version = "0.0.20221221", package = "hkalbasi-rustc-ap-rustc_index", default-features = false }
33+
indexmap = "1.6.0"
3334

3435
# local deps
3536
stdx.workspace = true

‎crates/hir-ty/src/test_db.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
//! Database used for testing `hir`.
22
33
use std::{
4-
fmt, panic,
4+
fmt,
5+
hash::BuildHasherDefault,
6+
panic,
57
sync::{Arc, Mutex},
68
};
79

@@ -11,7 +13,8 @@ use base_db::{
1113
};
1214
use hir_def::{db::DefDatabase, ModuleId};
1315
use hir_expand::db::ExpandDatabase;
14-
use rustc_hash::FxHashSet;
16+
use indexmap::IndexSet;
17+
use rustc_hash::FxHasher;
1518
use stdx::hash::NoHashHashMap;
1619
use syntax::TextRange;
1720
use test_utils::extract_annotations;
@@ -82,7 +85,10 @@ impl FileLoader for TestDB {
8285
fn resolve_path(&self, path: AnchoredPath<'_>) -> Option<FileId> {
8386
FileLoaderDelegate(self).resolve_path(path)
8487
}
85-
fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>> {
88+
fn relevant_crates(
89+
&self,
90+
file_id: FileId,
91+
) -> Arc<IndexSet<CrateId, BuildHasherDefault<FxHasher>>> {
8692
FileLoaderDelegate(self).relevant_crates(file_id)
8793
}
8894
}

‎crates/hir/src/semantics/source_to_def.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,9 @@ impl SourceToDefCtx<'_, '_> {
119119
pub(super) fn file_to_def(&self, file: FileId) -> SmallVec<[ModuleId; 1]> {
120120
let _p = profile::span("SourceBinder::to_module_def");
121121
let mut mods = SmallVec::new();
122-
for &crate_id in self.db.relevant_crates(file).iter() {
122+
// HACK: We iterate in reverse so that dev-dependency duplicated crates appear first in this
123+
// Most code only deals with one module and we want to prefer the test enabled code where possible
124+
for &crate_id in self.db.relevant_crates(file).iter().rev() {
123125
// FIXME: inner items
124126
let crate_def_map = self.db.crate_def_map(crate_id);
125127
mods.extend(

‎crates/ide-db/src/lib.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub mod syntax_helpers {
4343
pub use parser::LexedStr;
4444
}
4545

46-
use std::{fmt, mem::ManuallyDrop, sync::Arc};
46+
use std::{fmt, hash::BuildHasherDefault, mem::ManuallyDrop, sync::Arc};
4747

4848
use base_db::{
4949
salsa::{self, Durability},
@@ -53,6 +53,7 @@ use hir::{
5353
db::{DefDatabase, ExpandDatabase, HirDatabase},
5454
symbols::FileSymbolKind,
5555
};
56+
use indexmap::IndexSet;
5657

5758
use crate::{line_index::LineIndex, symbol_index::SymbolsDatabase};
5859
pub use rustc_hash::{FxHashMap, FxHashSet, FxHasher};
@@ -119,7 +120,10 @@ impl FileLoader for RootDatabase {
119120
fn resolve_path(&self, path: AnchoredPath<'_>) -> Option<FileId> {
120121
FileLoaderDelegate(self).resolve_path(path)
121122
}
122-
fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>> {
123+
fn relevant_crates(
124+
&self,
125+
file_id: FileId,
126+
) -> Arc<IndexSet<CrateId, BuildHasherDefault<FxHasher>>> {
123127
FileLoaderDelegate(self).relevant_crates(file_id)
124128
}
125129
}

‎crates/project-model/src/tests.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::{
55

66
use base_db::{CrateGraph, FileId, ProcMacroPaths};
77
use cfg::{CfgAtom, CfgDiff};
8-
use expect_test::{expect, Expect};
8+
use expect_test::{expect, expect_file, Expect, ExpectFile};
99
use paths::{AbsPath, AbsPathBuf};
1010
use serde::de::DeserializeOwned;
1111

@@ -114,6 +114,11 @@ fn check_crate_graph(crate_graph: CrateGraph, expect: Expect) {
114114
replace_root(&mut crate_graph, false);
115115
expect.assert_eq(&crate_graph);
116116
}
117+
fn check_crate_graph_f(crate_graph: CrateGraph, expect: ExpectFile) {
118+
let mut crate_graph = format!("{crate_graph:#?}");
119+
replace_root(&mut crate_graph, false);
120+
expect.assert_eq(&crate_graph);
121+
}
117122

118123
#[test]
119124
fn cargo_hello_world_project_model_with_wildcard_overrides() {
@@ -1666,3 +1671,12 @@ fn rust_project_is_proc_macro_has_proc_macro_dep() {
16661671
// on the proc_macro sysroot crate.
16671672
crate_data.dependencies.iter().find(|&dep| dep.name.deref() == "proc_macro").unwrap();
16681673
}
1674+
1675+
#[test]
1676+
fn cargo_dev_dependencies() {
1677+
let (crate_graph, _proc_macros) = load_cargo("complex-with-dev-deps.json");
1678+
check_crate_graph_f(
1679+
crate_graph,
1680+
expect_file!["../test_data/cargo_dev_dependencies-crate-graph.txt"],
1681+
)
1682+
}
There was a problem loading the remainder of the diff.

0 commit comments

Comments
 (0)
Failed to load comments.