Skip to content

Commit e205af2

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

File tree

10 files changed

+65
-119
lines changed

10 files changed

+65
-119
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
}

crates/project-model/test_data/cargo_dev_dependencies-crate-graph.txt

+4-102
Original file line numberDiff line numberDiff line change
@@ -244,14 +244,14 @@
244244
prelude: true,
245245
},
246246
Dependency {
247-
crate_id: Idx::<CrateData>(8),
247+
crate_id: Idx::<CrateData>(7),
248248
name: CrateName(
249249
"ra_playground",
250250
),
251251
prelude: true,
252252
},
253253
Dependency {
254-
crate_id: Idx::<CrateData>(7),
254+
crate_id: Idx::<CrateData>(5),
255255
name: CrateName(
256256
"regex",
257257
),
@@ -328,14 +328,14 @@
328328
prelude: true,
329329
},
330330
Dependency {
331-
crate_id: Idx::<CrateData>(8),
331+
crate_id: Idx::<CrateData>(7),
332332
name: CrateName(
333333
"ra_playground",
334334
),
335335
prelude: true,
336336
},
337337
Dependency {
338-
crate_id: Idx::<CrateData>(7),
338+
crate_id: Idx::<CrateData>(5),
339339
name: CrateName(
340340
"regex",
341341
),
@@ -552,104 +552,6 @@
552552
channel: None,
553553
},
554554
7: CrateData {
555-
root_file_id: FileId(
556-
6,
557-
),
558-
edition: Edition2018,
559-
version: Some(
560-
"1.7.3",
561-
),
562-
display_name: Some(
563-
CrateDisplayName {
564-
crate_name: CrateName(
565-
"regex",
566-
),
567-
canonical_name: "regex",
568-
},
569-
),
570-
cfg_options: CfgOptions(
571-
[
572-
"debug_assertions",
573-
"feature=aho-corasick",
574-
"feature=default",
575-
"feature=memchr",
576-
"feature=perf",
577-
"feature=perf-cache",
578-
"feature=perf-dfa",
579-
"feature=perf-inline",
580-
"feature=perf-literal",
581-
"feature=std",
582-
"feature=unicode",
583-
"feature=unicode-age",
584-
"feature=unicode-bool",
585-
"feature=unicode-case",
586-
"feature=unicode-gencat",
587-
"feature=unicode-perl",
588-
"feature=unicode-script",
589-
"feature=unicode-segment",
590-
"test",
591-
],
592-
),
593-
potential_cfg_options: Some(
594-
CfgOptions(
595-
[
596-
"debug_assertions",
597-
"feature=aho-corasick",
598-
"feature=default",
599-
"feature=memchr",
600-
"feature=pattern",
601-
"feature=perf",
602-
"feature=perf-cache",
603-
"feature=perf-dfa",
604-
"feature=perf-inline",
605-
"feature=perf-literal",
606-
"feature=std",
607-
"feature=unicode",
608-
"feature=unicode-age",
609-
"feature=unicode-bool",
610-
"feature=unicode-case",
611-
"feature=unicode-gencat",
612-
"feature=unicode-perl",
613-
"feature=unicode-script",
614-
"feature=unicode-segment",
615-
"feature=unstable",
616-
"feature=use_std",
617-
],
618-
),
619-
),
620-
env: Env {
621-
entries: {
622-
"CARGO_PKG_LICENSE": "",
623-
"CARGO_PKG_VERSION_MAJOR": "1",
624-
"CARGO_MANIFEST_DIR": "$ROOT$.cargo/registry/src/github.com-1ecc6299db9ec823/regex-1.7.3",
625-
"CARGO_PKG_VERSION": "1.7.3",
626-
"CARGO_PKG_AUTHORS": "",
627-
"CARGO_CRATE_NAME": "regex",
628-
"CARGO_PKG_LICENSE_FILE": "",
629-
"CARGO_PKG_HOMEPAGE": "",
630-
"CARGO_PKG_DESCRIPTION": "",
631-
"CARGO_PKG_NAME": "regex",
632-
"CARGO_PKG_VERSION_PATCH": "3",
633-
"CARGO": "cargo",
634-
"CARGO_PKG_REPOSITORY": "",
635-
"CARGO_PKG_VERSION_MINOR": "7",
636-
"CARGO_PKG_VERSION_PRE": "",
637-
},
638-
},
639-
dependencies: [],
640-
origin: Library {
641-
repo: Some(
642-
"https://github.com/rust-lang/regex",
643-
),
644-
name: "regex",
645-
},
646-
is_proc_macro: false,
647-
target_layout: Err(
648-
"target_data_layout not loaded",
649-
),
650-
channel: None,
651-
},
652-
8: CrateData {
653555
root_file_id: FileId(
654556
4,
655557
),

0 commit comments

Comments
 (0)