mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 04:44:57 +00:00
Split out proc-macros from the CrateGraph
This commit is contained in:
parent
71b23360e7
commit
d154ea88f9
12 changed files with 141 additions and 176 deletions
|
@ -7,7 +7,7 @@ use std::{collections::VecDeque, fmt, fs, process::Command, sync::Arc};
|
|||
use anyhow::{bail, format_err, Context, Result};
|
||||
use base_db::{
|
||||
CrateDisplayName, CrateGraph, CrateId, CrateName, CrateOrigin, Dependency, Edition, Env,
|
||||
FileId, LangCrateOrigin, ProcMacroLoadResult, TargetLayoutLoadResult,
|
||||
FileId, LangCrateOrigin, ProcMacroLoadResult, ProcMacros, TargetLayoutLoadResult,
|
||||
};
|
||||
use cfg::{CfgDiff, CfgOptions};
|
||||
use paths::{AbsPath, AbsPathBuf};
|
||||
|
@ -579,10 +579,10 @@ impl ProjectWorkspace {
|
|||
load_proc_macro: &mut dyn FnMut(&str, &AbsPath) -> ProcMacroLoadResult,
|
||||
load: &mut dyn FnMut(&AbsPath) -> Option<FileId>,
|
||||
extra_env: &FxHashMap<String, String>,
|
||||
) -> CrateGraph {
|
||||
) -> (CrateGraph, ProcMacros) {
|
||||
let _p = profile::span("ProjectWorkspace::to_crate_graph");
|
||||
|
||||
let mut crate_graph = match self {
|
||||
let (mut crate_graph, proc_macros) = match self {
|
||||
ProjectWorkspace::Json { project, sysroot, rustc_cfg } => project_json_to_crate_graph(
|
||||
rustc_cfg.clone(),
|
||||
load_proc_macro,
|
||||
|
@ -630,7 +630,7 @@ impl ProjectWorkspace {
|
|||
} else {
|
||||
tracing::debug!("Did not patch std to depend on cfg-if")
|
||||
}
|
||||
crate_graph
|
||||
(crate_graph, proc_macros)
|
||||
}
|
||||
|
||||
pub fn eq_ignore_build_data(&self, other: &Self) -> bool {
|
||||
|
@ -685,8 +685,9 @@ fn project_json_to_crate_graph(
|
|||
sysroot: Option<&Sysroot>,
|
||||
extra_env: &FxHashMap<String, String>,
|
||||
target_layout: TargetLayoutLoadResult,
|
||||
) -> CrateGraph {
|
||||
) -> (CrateGraph, ProcMacros) {
|
||||
let mut crate_graph = CrateGraph::default();
|
||||
let mut proc_macros = FxHashMap::<_, _>::default();
|
||||
let sysroot_deps = sysroot.as_ref().map(|sysroot| {
|
||||
sysroot_to_crate_graph(
|
||||
&mut crate_graph,
|
||||
|
@ -707,13 +708,15 @@ fn project_json_to_crate_graph(
|
|||
})
|
||||
.map(|(crate_id, krate, file_id)| {
|
||||
let env = krate.env.clone().into_iter().collect();
|
||||
let proc_macro = match krate.proc_macro_dylib_path.clone() {
|
||||
Some(it) => load_proc_macro(
|
||||
krate.display_name.as_ref().map(|it| it.canonical_name()).unwrap_or(""),
|
||||
&it,
|
||||
),
|
||||
None => Err("no proc macro dylib present".into()),
|
||||
};
|
||||
if let Some(it) = krate.proc_macro_dylib_path.clone() {
|
||||
proc_macros.insert(
|
||||
crate_id,
|
||||
load_proc_macro(
|
||||
krate.display_name.as_ref().map(|it| it.canonical_name()).unwrap_or(""),
|
||||
&it,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
let target_cfgs = match krate.target.as_deref() {
|
||||
Some(target) => cfg_cache
|
||||
|
@ -734,7 +737,6 @@ fn project_json_to_crate_graph(
|
|||
cfg_options.clone(),
|
||||
cfg_options,
|
||||
env,
|
||||
proc_macro,
|
||||
krate.is_proc_macro,
|
||||
if krate.display_name.is_some() {
|
||||
CrateOrigin::CratesIo {
|
||||
|
@ -776,7 +778,7 @@ fn project_json_to_crate_graph(
|
|||
}
|
||||
}
|
||||
}
|
||||
crate_graph
|
||||
(crate_graph, proc_macros)
|
||||
}
|
||||
|
||||
fn cargo_to_crate_graph(
|
||||
|
@ -789,9 +791,10 @@ fn cargo_to_crate_graph(
|
|||
override_cfg: &CfgOverrides,
|
||||
build_scripts: &WorkspaceBuildScripts,
|
||||
target_layout: TargetLayoutLoadResult,
|
||||
) -> CrateGraph {
|
||||
) -> (CrateGraph, ProcMacros) {
|
||||
let _p = profile::span("cargo_to_crate_graph");
|
||||
let mut crate_graph = CrateGraph::default();
|
||||
let mut proc_macros = FxHashMap::default();
|
||||
let (public_deps, libproc_macro) = match sysroot {
|
||||
Some(sysroot) => sysroot_to_crate_graph(
|
||||
&mut crate_graph,
|
||||
|
@ -855,6 +858,7 @@ fn cargo_to_crate_graph(
|
|||
if let Some(file_id) = load(&cargo[tgt].root) {
|
||||
let crate_id = add_target_crate_root(
|
||||
&mut crate_graph,
|
||||
&mut proc_macros,
|
||||
&cargo[pkg],
|
||||
build_scripts.get_output(pkg),
|
||||
cfg_options.clone(),
|
||||
|
@ -931,6 +935,7 @@ fn cargo_to_crate_graph(
|
|||
if let Some((rustc_workspace, rustc_build_scripts)) = rustc {
|
||||
handle_rustc_crates(
|
||||
&mut crate_graph,
|
||||
&mut proc_macros,
|
||||
&mut pkg_to_lib_crate,
|
||||
load,
|
||||
load_proc_macro,
|
||||
|
@ -952,7 +957,7 @@ fn cargo_to_crate_graph(
|
|||
);
|
||||
}
|
||||
}
|
||||
crate_graph
|
||||
(crate_graph, proc_macros)
|
||||
}
|
||||
|
||||
fn detached_files_to_crate_graph(
|
||||
|
@ -961,7 +966,7 @@ fn detached_files_to_crate_graph(
|
|||
detached_files: &[AbsPathBuf],
|
||||
sysroot: Option<&Sysroot>,
|
||||
target_layout: TargetLayoutLoadResult,
|
||||
) -> CrateGraph {
|
||||
) -> (CrateGraph, ProcMacros) {
|
||||
let _p = profile::span("detached_files_to_crate_graph");
|
||||
let mut crate_graph = CrateGraph::default();
|
||||
let (public_deps, _libproc_macro) = match sysroot {
|
||||
|
@ -998,7 +1003,6 @@ fn detached_files_to_crate_graph(
|
|||
cfg_options.clone(),
|
||||
cfg_options.clone(),
|
||||
Env::default(),
|
||||
Ok(Vec::new()),
|
||||
false,
|
||||
CrateOrigin::CratesIo {
|
||||
repo: None,
|
||||
|
@ -1009,11 +1013,12 @@ fn detached_files_to_crate_graph(
|
|||
|
||||
public_deps.add_to_crate_graph(&mut crate_graph, detached_file_crate);
|
||||
}
|
||||
crate_graph
|
||||
(crate_graph, FxHashMap::default())
|
||||
}
|
||||
|
||||
fn handle_rustc_crates(
|
||||
crate_graph: &mut CrateGraph,
|
||||
proc_macros: &mut ProcMacros,
|
||||
pkg_to_lib_crate: &mut FxHashMap<Package, CrateId>,
|
||||
load: &mut dyn FnMut(&AbsPath) -> Option<FileId>,
|
||||
load_proc_macro: &mut dyn FnMut(&str, &AbsPath) -> ProcMacroLoadResult,
|
||||
|
@ -1075,6 +1080,7 @@ fn handle_rustc_crates(
|
|||
if let Some(file_id) = load(&rustc_workspace[tgt].root) {
|
||||
let crate_id = add_target_crate_root(
|
||||
crate_graph,
|
||||
proc_macros,
|
||||
&rustc_workspace[pkg],
|
||||
build_scripts.get_output(pkg),
|
||||
cfg_options.clone(),
|
||||
|
@ -1140,6 +1146,7 @@ fn handle_rustc_crates(
|
|||
|
||||
fn add_target_crate_root(
|
||||
crate_graph: &mut CrateGraph,
|
||||
proc_macros: &mut ProcMacros,
|
||||
pkg: &PackageData,
|
||||
build_data: Option<&BuildScriptOutput>,
|
||||
cfg_options: CfgOptions,
|
||||
|
@ -1176,14 +1183,8 @@ fn add_target_crate_root(
|
|||
}
|
||||
}
|
||||
|
||||
let proc_macro = match build_data.as_ref().map(|it| it.proc_macro_dylib_path.as_ref()) {
|
||||
Some(Some(it)) => load_proc_macro(it),
|
||||
Some(None) => Err("no proc macro dylib present".into()),
|
||||
None => Err("crate has not (yet) been built".into()),
|
||||
};
|
||||
|
||||
let display_name = CrateDisplayName::from_canonical_name(cargo_name.to_string());
|
||||
crate_graph.add_crate_root(
|
||||
let crate_id = crate_graph.add_crate_root(
|
||||
file_id,
|
||||
edition,
|
||||
Some(display_name),
|
||||
|
@ -1191,11 +1192,19 @@ fn add_target_crate_root(
|
|||
cfg_options,
|
||||
potential_cfg_options,
|
||||
env,
|
||||
proc_macro,
|
||||
is_proc_macro,
|
||||
CrateOrigin::CratesIo { repo: pkg.repository.clone(), name: Some(pkg.name.clone()) },
|
||||
target_layout,
|
||||
)
|
||||
);
|
||||
let proc_macro = match build_data.as_ref().map(|it| &it.proc_macro_dylib_path) {
|
||||
Some(it) => it.as_deref().map(load_proc_macro),
|
||||
None => Some(Err("crate has not (yet) been built".into())),
|
||||
};
|
||||
if let Some(proc_macro) = proc_macro {
|
||||
proc_macros.insert(crate_id, proc_macro);
|
||||
}
|
||||
|
||||
crate_id
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
|
@ -1237,7 +1246,6 @@ fn sysroot_to_crate_graph(
|
|||
cfg_options.clone(),
|
||||
cfg_options.clone(),
|
||||
env,
|
||||
Err("no proc macro loaded for sysroot crate".into()),
|
||||
false,
|
||||
CrateOrigin::Lang(LangCrateOrigin::from(&*sysroot[krate].name)),
|
||||
target_layout.clone(),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue