Skip to content

Commit

Permalink
Prefer test duped crates for ide features
Browse files Browse the repository at this point in the history
  • Loading branch information
Veykril committed Apr 13, 2023
1 parent 56de543 commit 4d5b7b1
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 17 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/base-db/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ doctest = false
[dependencies]
salsa = "0.17.0-pre.2"
rustc-hash = "1.1.0"
indexmap = "1.6.0"

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

Expand Down
25 changes: 19 additions & 6 deletions crates/base-db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ mod input;
mod change;
pub mod fixture;

use std::{panic, sync::Arc};
use std::{hash::BuildHasherDefault, panic, sync::Arc};

use rustc_hash::FxHashSet;
use indexmap::IndexSet;
use rustc_hash::FxHasher;
use syntax::{ast, Parse, SourceFile, TextRange, TextSize};

pub use crate::{
Expand Down Expand Up @@ -59,7 +60,10 @@ pub trait FileLoader {
/// Text of the file.
fn file_text(&self, file_id: FileId) -> Arc<String>;
fn resolve_path(&self, path: AnchoredPath<'_>) -> Option<FileId>;
fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>>;
fn relevant_crates(
&self,
file_id: FileId,
) -> Arc<IndexSet<CrateId, BuildHasherDefault<FxHasher>>>;
}

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

fn source_root_crates(&self, id: SourceRootId) -> Arc<FxHashSet<CrateId>>;
fn source_root_crates(
&self,
id: SourceRootId,
) -> Arc<IndexSet<CrateId, BuildHasherDefault<FxHasher>>>;
}

fn source_root_crates(db: &dyn SourceDatabaseExt, id: SourceRootId) -> Arc<FxHashSet<CrateId>> {
fn source_root_crates(
db: &dyn SourceDatabaseExt,
id: SourceRootId,
) -> Arc<IndexSet<CrateId, BuildHasherDefault<FxHasher>>> {
let graph = db.crate_graph();
let res = graph
.iter()
Expand All @@ -128,7 +138,10 @@ impl<T: SourceDatabaseExt> FileLoader for FileLoaderDelegate<&'_ T> {
source_root.resolve_path(path)
}

fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>> {
fn relevant_crates(
&self,
file_id: FileId,
) -> Arc<IndexSet<CrateId, BuildHasherDefault<FxHasher>>> {
let _p = profile::span("relevant_crates");
let source_root = self.0.file_source_root(file_id);
self.0.source_root_crates(source_root)
Expand Down
12 changes: 9 additions & 3 deletions crates/hir-def/src/test_db.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
//! Database used for testing `hir_def`.

use std::{
fmt, panic,
fmt,
hash::BuildHasherDefault,
panic,
sync::{Arc, Mutex},
};

Expand All @@ -11,7 +13,8 @@ use base_db::{
Upcast,
};
use hir_expand::{db::ExpandDatabase, InFile};
use rustc_hash::FxHashSet;
use indexmap::IndexSet;
use rustc_hash::FxHasher;
use syntax::{algo, ast, AstNode};

use crate::{
Expand Down Expand Up @@ -77,7 +80,10 @@ impl FileLoader for TestDB {
fn resolve_path(&self, path: AnchoredPath<'_>) -> Option<FileId> {
FileLoaderDelegate(self).resolve_path(path)
}
fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>> {
fn relevant_crates(
&self,
file_id: FileId,
) -> Arc<IndexSet<CrateId, BuildHasherDefault<FxHasher>>> {
FileLoaderDelegate(self).relevant_crates(file_id)
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/hir-ty/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ la-arena = { version = "0.3.0", path = "../../lib/la-arena" }
once_cell = "1.17.0"
typed-arena = "2.0.1"
rustc_index = { version = "0.0.20221221", package = "hkalbasi-rustc-ap-rustc_index", default-features = false }
indexmap = "1.6.0"

# local deps
stdx.workspace = true
Expand Down
12 changes: 9 additions & 3 deletions crates/hir-ty/src/test_db.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
//! Database used for testing `hir`.

use std::{
fmt, panic,
fmt,
hash::BuildHasherDefault,
panic,
sync::{Arc, Mutex},
};

Expand All @@ -11,7 +13,8 @@ use base_db::{
};
use hir_def::{db::DefDatabase, ModuleId};
use hir_expand::db::ExpandDatabase;
use rustc_hash::FxHashSet;
use indexmap::IndexSet;
use rustc_hash::FxHasher;
use stdx::hash::NoHashHashMap;
use syntax::TextRange;
use test_utils::extract_annotations;
Expand Down Expand Up @@ -82,7 +85,10 @@ impl FileLoader for TestDB {
fn resolve_path(&self, path: AnchoredPath<'_>) -> Option<FileId> {
FileLoaderDelegate(self).resolve_path(path)
}
fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>> {
fn relevant_crates(
&self,
file_id: FileId,
) -> Arc<IndexSet<CrateId, BuildHasherDefault<FxHasher>>> {
FileLoaderDelegate(self).relevant_crates(file_id)
}
}
Expand Down
4 changes: 3 additions & 1 deletion crates/hir/src/semantics/source_to_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ impl SourceToDefCtx<'_, '_> {
pub(super) fn file_to_def(&self, file: FileId) -> SmallVec<[ModuleId; 1]> {
let _p = profile::span("SourceBinder::to_module_def");
let mut mods = SmallVec::new();
for &crate_id in self.db.relevant_crates(file).iter() {
// HACK: We iterate in reverse so that dev-dependency duplicated crates appear first in this
// Most code only deals with one module and we want to prefer the test enabled code where possible
for &crate_id in self.db.relevant_crates(file).iter().rev() {
// FIXME: inner items
let crate_def_map = self.db.crate_def_map(crate_id);
mods.extend(
Expand Down
8 changes: 6 additions & 2 deletions crates/ide-db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub mod syntax_helpers {
pub use parser::LexedStr;
}

use std::{fmt, mem::ManuallyDrop, sync::Arc};
use std::{fmt, hash::BuildHasherDefault, mem::ManuallyDrop, sync::Arc};

use base_db::{
salsa::{self, Durability},
Expand All @@ -53,6 +53,7 @@ use hir::{
db::{DefDatabase, ExpandDatabase, HirDatabase},
symbols::FileSymbolKind,
};
use indexmap::IndexSet;

use crate::{line_index::LineIndex, symbol_index::SymbolsDatabase};
pub use rustc_hash::{FxHashMap, FxHashSet, FxHasher};
Expand Down Expand Up @@ -119,7 +120,10 @@ impl FileLoader for RootDatabase {
fn resolve_path(&self, path: AnchoredPath<'_>) -> Option<FileId> {
FileLoaderDelegate(self).resolve_path(path)
}
fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>> {
fn relevant_crates(
&self,
file_id: FileId,
) -> Arc<IndexSet<CrateId, BuildHasherDefault<FxHasher>>> {
FileLoaderDelegate(self).relevant_crates(file_id)
}
}
Expand Down
13 changes: 11 additions & 2 deletions crates/project-model/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -935,7 +935,7 @@ fn cargo_to_crate_graph(
// Get all dependencies of the workspace members that are used as dev-dependencies
for pkg in cargo.packages() {
for dep in &cargo[pkg].dependencies {
if dep.kind == DepKind::Dev {
if dep.kind == DepKind::Dev && cargo[dep.pkg].is_member {
work.push(dep.pkg);
}
}
Expand All @@ -952,6 +952,10 @@ fn cargo_to_crate_graph(
}
v.insert({
let duped = crate_graph.duplicate(to);
tracing::info!(
"duplicating workspace crate {:?} as it is being used as a dev-dependency: {to:?} -> {duped:?}",
crate_graph[to].display_name
);
if let Some(proc_macro) = proc_macros.get(&to).cloned() {
proc_macros.insert(duped, proc_macro);
}
Expand Down Expand Up @@ -1005,7 +1009,12 @@ fn cargo_to_crate_graph(

for (&pkg, targets) in &pkg_crates {
for &(krate, _) in targets {
if test_dupes.get(&krate).is_some() {
if let Some(&dupe) = test_dupes.get(&krate) {
tracing::info!(
"{krate:?} {:?} {dupe:?} {:?}",
crate_graph[krate].cfg_options,
crate_graph[dupe].cfg_options
);
// if the crate got duped as a dev-dep the dupe already has test set
continue;
}
Expand Down

0 comments on commit 4d5b7b1

Please sign in to comment.