Skip to content

Commit

Permalink
Define a separate type for crate indexes in a rust-project.json
Browse files Browse the repository at this point in the history
  • Loading branch information
Wilfred committed Mar 27, 2024
1 parent 5e370b1 commit 7337b00
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 32 deletions.
41 changes: 18 additions & 23 deletions crates/project-model/src/project_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@
//! user explores them belongs to that extension (it's totally valid to change
//! rust-project.json over time via configuration request!)

use base_db::{CrateDisplayName, CrateId, CrateName, Dependency};
use la_arena::RawIdx;
use base_db::{CrateDisplayName, CrateName};
use paths::{AbsPath, AbsPathBuf, Utf8PathBuf};
use rustc_hash::FxHashMap;
use serde::{de, Deserialize};
Expand All @@ -77,7 +76,7 @@ pub struct Crate {
pub root_module: AbsPathBuf,
pub(crate) edition: Edition,
pub(crate) version: Option<String>,
pub(crate) deps: Vec<Dependency>,
pub(crate) deps: Vec<Dep>,
pub(crate) cfg: Vec<CfgFlag>,
pub(crate) target: Option<String>,
pub(crate) env: FxHashMap<String, String>,
Expand Down Expand Up @@ -128,16 +127,7 @@ impl ProjectJson {
root_module,
edition: crate_data.edition.into(),
version: crate_data.version.as_ref().map(ToString::to_string),
deps: crate_data
.deps
.into_iter()
.map(|dep_data| {
Dependency::new(
dep_data.name,
CrateId::from_raw(RawIdx::from(dep_data.krate as u32)),
)
})
.collect::<Vec<_>>(),
deps: crate_data.deps,
cfg: crate_data.cfg,
target: crate_data.target,
env: crate_data.env,
Expand All @@ -161,11 +151,8 @@ impl ProjectJson {
}

/// Returns an iterator over the crates in the project.
pub fn crates(&self) -> impl Iterator<Item = (CrateId, &Crate)> + '_ {
self.crates
.iter()
.enumerate()
.map(|(idx, krate)| (CrateId::from_raw(RawIdx::from(idx as u32)), krate))
pub fn crates(&self) -> impl Iterator<Item = (CrateArrayIdx, &Crate)> {
self.crates.iter().enumerate().map(|(idx, krate)| (CrateArrayIdx(idx), krate))
}

/// Returns the path to the project's root folder.
Expand All @@ -188,7 +175,7 @@ struct CrateData {
edition: EditionData,
#[serde(default)]
version: Option<semver::Version>,
deps: Vec<DepData>,
deps: Vec<Dep>,
#[serde(default)]
cfg: Vec<CfgFlag>,
target: Option<String>,
Expand Down Expand Up @@ -227,13 +214,21 @@ impl From<EditionData> for Edition {
}
}

#[derive(Deserialize, Debug, Clone)]
struct DepData {
/// Identifies a crate by position in the crates array.
///
/// This will differ from `CrateId` when multiple `ProjectJson`
/// workspaces are loaded.
#[derive(Deserialize, Debug, Clone, Copy, Eq, PartialEq, Hash)]
#[serde(transparent)]
pub struct CrateArrayIdx(pub usize);

#[derive(Deserialize, Debug, Clone, Eq, PartialEq)]
pub(crate) struct Dep {
/// Identifies a crate by position in the crates array.
#[serde(rename = "crate")]
krate: usize,
pub(crate) krate: CrateArrayIdx,
#[serde(deserialize_with = "deserialize_crate_name")]
name: CrateName,
pub(crate) name: CrateName,
}

#[derive(Deserialize, Debug, Clone)]
Expand Down
19 changes: 10 additions & 9 deletions crates/project-model/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::{
build_scripts::BuildScriptOutput,
cargo_workspace::{DepKind, PackageData, RustLibSource},
cfg_flag::CfgFlag,
project_json::Crate,
project_json::{Crate, CrateArrayIdx},
rustc_cfg::{self, RustcCfgConfig},
sysroot::{SysrootCrate, SysrootMode},
target_data_layout::{self, RustcDataLayoutConfig},
Expand Down Expand Up @@ -878,12 +878,13 @@ fn project_json_to_crate_graph(

let r_a_cfg_flag = CfgFlag::Atom("rust_analyzer".to_owned());
let mut cfg_cache: FxHashMap<&str, Vec<CfgFlag>> = FxHashMap::default();
let crates: FxHashMap<CrateId, CrateId> = project

let idx_to_crate_id: FxHashMap<CrateArrayIdx, CrateId> = project
.crates()
.filter_map(|(crate_id, krate)| Some((crate_id, krate, load(&krate.root_module)?)))
.filter_map(|(idx, krate)| Some((idx, krate, load(&krate.root_module)?)))
.map(
|(
crate_id,
idx,
Crate {
display_name,
edition,
Expand Down Expand Up @@ -939,13 +940,13 @@ fn project_json_to_crate_graph(
proc_macros.insert(crate_graph_crate_id, node);
}
}
(crate_id, crate_graph_crate_id)
(idx, crate_graph_crate_id)
},
)
.collect();

for (from, krate) in project.crates() {
if let Some(&from) = crates.get(&from) {
for (from_idx, krate) in project.crates() {
if let Some(&from) = idx_to_crate_id.get(&from_idx) {
if let Some((public_deps, libproc_macro)) = &sysroot_deps {
public_deps.add_to_crate_graph(crate_graph, from);
if let Some(proc_macro) = libproc_macro {
Expand All @@ -954,8 +955,8 @@ fn project_json_to_crate_graph(
}

Check warning on line 955 in crates/project-model/src/workspace.rs

View workflow job for this annotation

GitHub Actions / Rust (ubuntu-latest)

Diff in /home/runner/work/rust-analyzer/rust-analyzer/crates/project-model/src/workspace.rs

for dep in &krate.deps {
if let Some(&to) = crates.get(&dep.crate_id) {
add_dep(crate_graph, from, dep.name.clone(), to)
if let Some(&to) = idx_to_crate_id.get(&dep.krate) {
add_dep(crate_graph, from, dep.name.clone(), to);
}
}
}
Expand Down

0 comments on commit 7337b00

Please sign in to comment.