Skip to content

Commit b4345e9

Browse files
committed
Prefer test duped crates for ide features
1 parent a4c7a87 commit b4345e9

File tree

9 files changed

+61
-17
lines changed

9 files changed

+61
-17
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/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/workspace.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -938,7 +938,7 @@ fn cargo_to_crate_graph(
938938
// Get all dependencies of the workspace members that are used as dev-dependencies
939939
for pkg in cargo.packages() {
940940
for dep in &cargo[pkg].dependencies {
941-
if dep.kind == DepKind::Dev {
941+
if dep.kind == DepKind::Dev && cargo[dep.pkg].is_member {
942942
work.push(dep.pkg);
943943
}
944944
}
@@ -955,6 +955,10 @@ fn cargo_to_crate_graph(
955955
}
956956
v.insert({
957957
let duped = crate_graph.duplicate(to);
958+
tracing::info!(
959+
"duplicating workspace crate {:?} as it is being used as a dev-dependency: {to:?} -> {duped:?}",
960+
crate_graph[to].display_name
961+
);
958962
if let Some(proc_macro) = proc_macros.get(&to).cloned() {
959963
proc_macros.insert(duped, proc_macro);
960964
}
@@ -1008,7 +1012,12 @@ fn cargo_to_crate_graph(
10081012

10091013
for (&pkg, targets) in &pkg_crates {
10101014
for &(krate, _) in targets {
1011-
if test_dupes.get(&krate).is_some() {
1015+
if let Some(&dupe) = test_dupes.get(&krate) {
1016+
tracing::info!(
1017+
"{krate:?} {:?} {dupe:?} {:?}",
1018+
crate_graph[krate].cfg_options,
1019+
crate_graph[dupe].cfg_options
1020+
);
10121021
// if the crate got duped as a dev-dep the dupe already has test set
10131022
continue;
10141023
}

0 commit comments

Comments
 (0)