Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle dev-dependency cycles #14475

Merged
merged 3 commits into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
11 changes: 11 additions & 0 deletions crates/base-db/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,11 @@ impl CrateGraph {
Ok(())
}

pub fn duplicate(&mut self, id: CrateId) -> CrateId {
let data = self[id].clone();
self.arena.alloc(data)
}

pub fn add_dep(
&mut self,
from: CrateId,
Expand Down Expand Up @@ -612,6 +617,12 @@ impl ops::Index<CrateId> for CrateGraph {
}
}

impl ops::IndexMut<CrateId> for CrateGraph {
fn index_mut(&mut self, crate_id: CrateId) -> &mut CrateData {
&mut self.arena[crate_id]
}
}

impl CrateData {
fn add_dep(&mut self, dep: Dependency) {
self.dependencies.push(dep)
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<str>;
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
16 changes: 15 additions & 1 deletion crates/project-model/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{

use base_db::{CrateGraph, FileId, ProcMacroPaths};
use cfg::{CfgAtom, CfgDiff};
use expect_test::{expect, Expect};
use expect_test::{expect, expect_file, Expect, ExpectFile};
use paths::{AbsPath, AbsPathBuf};
use serde::de::DeserializeOwned;

Expand Down Expand Up @@ -114,6 +114,11 @@ fn check_crate_graph(crate_graph: CrateGraph, expect: Expect) {
replace_root(&mut crate_graph, false);
expect.assert_eq(&crate_graph);
}
fn check_crate_graph_f(crate_graph: CrateGraph, expect: ExpectFile) {
let mut crate_graph = format!("{crate_graph:#?}");
replace_root(&mut crate_graph, false);
expect.assert_eq(&crate_graph);
}

#[test]
fn cargo_hello_world_project_model_with_wildcard_overrides() {
Expand Down Expand Up @@ -1666,3 +1671,12 @@ fn rust_project_is_proc_macro_has_proc_macro_dep() {
// on the proc_macro sysroot crate.
crate_data.dependencies.iter().find(|&dep| dep.name.deref() == "proc_macro").unwrap();
}

#[test]
fn cargo_dev_dependencies() {
let (crate_graph, _proc_macros) = load_cargo("complex-with-dev-deps.json");
check_crate_graph_f(
crate_graph,
expect_file!["../test_data/cargo_dev_dependencies-crate-graph.txt"],
)
}
Loading