Skip to content

Commit 599142c

Browse files
committed
Auto merge of #13590 - Veykril:proc-macro-rustc-src, r=Veykril
internal: Add proc-macro dependency to rustc_private crates
2 parents 392784a + 3c35d44 commit 599142c

File tree

1 file changed

+41
-28
lines changed

1 file changed

+41
-28
lines changed

crates/project-model/src/workspace.rs

+41-28
Original file line numberDiff line numberDiff line change
@@ -509,14 +509,14 @@ impl ProjectWorkspace {
509509
build_scripts,
510510
toolchain: _,
511511
} => cargo_to_crate_graph(
512-
rustc_cfg.clone(),
513-
cfg_overrides,
514512
load_proc_macro,
515513
load,
514+
rustc,
516515
cargo,
517-
build_scripts,
518516
sysroot.as_ref(),
519-
rustc,
517+
rustc_cfg.clone(),
518+
cfg_overrides,
519+
build_scripts,
520520
),
521521
ProjectWorkspace::DetachedFiles { files, sysroot, rustc_cfg } => {
522522
detached_files_to_crate_graph(rustc_cfg.clone(), load, files, sysroot)
@@ -602,7 +602,7 @@ fn project_json_to_crate_graph(
602602
for (from, krate) in project.crates() {
603603
if let Some(&from) = crates.get(&from) {
604604
if let Some((public_deps, libproc_macro)) = &sysroot_deps {
605-
public_deps.add(from, &mut crate_graph);
605+
public_deps.add_to_crate_graph(&mut crate_graph, from);
606606
if krate.is_proc_macro {
607607
if let Some(proc_macro) = libproc_macro {
608608
add_dep(
@@ -626,14 +626,14 @@ fn project_json_to_crate_graph(
626626
}
627627

628628
fn cargo_to_crate_graph(
629-
rustc_cfg: Vec<CfgFlag>,
630-
override_cfg: &CfgOverrides,
631629
load_proc_macro: &mut dyn FnMut(&str, &AbsPath) -> ProcMacroLoadResult,
632630
load: &mut dyn FnMut(&AbsPath) -> Option<FileId>,
631+
rustc: &Option<CargoWorkspace>,
633632
cargo: &CargoWorkspace,
634-
build_scripts: &WorkspaceBuildScripts,
635633
sysroot: Option<&Sysroot>,
636-
rustc: &Option<CargoWorkspace>,
634+
rustc_cfg: Vec<CfgFlag>,
635+
override_cfg: &CfgOverrides,
636+
build_scripts: &WorkspaceBuildScripts,
637637
) -> CrateGraph {
638638
let _p = profile::span("cargo_to_crate_graph");
639639
let mut crate_graph = CrateGraph::default();
@@ -642,13 +642,15 @@ fn cargo_to_crate_graph(
642642
None => (SysrootPublicDeps::default(), None),
643643
};
644644

645-
let mut cfg_options = CfgOptions::default();
646-
cfg_options.extend(rustc_cfg);
645+
let cfg_options = {
646+
let mut cfg_options = CfgOptions::default();
647+
cfg_options.extend(rustc_cfg);
648+
cfg_options.insert_atom("debug_assertions".into());
649+
cfg_options
650+
};
647651

648652
let mut pkg_to_lib_crate = FxHashMap::default();
649653

650-
cfg_options.insert_atom("debug_assertions".into());
651-
652654
let mut pkg_crates = FxHashMap::default();
653655
// Does any crate signal to rust-analyzer that they need the rustc_private crates?
654656
let mut has_private = false;
@@ -723,7 +725,7 @@ fn cargo_to_crate_graph(
723725
// Set deps to the core, std and to the lib target of the current package
724726
for &(from, kind) in pkg_crates.get(&pkg).into_iter().flatten() {
725727
// Add sysroot deps first so that a lib target named `core` etc. can overwrite them.
726-
public_deps.add(from, &mut crate_graph);
728+
public_deps.add_to_crate_graph(&mut crate_graph, from);
727729

728730
if let Some((to, name)) = lib_tgt.clone() {
729731
if to != from && kind != TargetKind::BuildScript {
@@ -767,15 +769,16 @@ fn cargo_to_crate_graph(
767769
if let Some(rustc_workspace) = rustc {
768770
handle_rustc_crates(
769771
&mut crate_graph,
770-
rustc_workspace,
772+
&mut pkg_to_lib_crate,
771773
load,
772-
&cfg_options,
773-
override_cfg,
774774
load_proc_macro,
775-
&mut pkg_to_lib_crate,
776-
&public_deps,
775+
rustc_workspace,
777776
cargo,
777+
&public_deps,
778+
libproc_macro,
778779
&pkg_crates,
780+
&cfg_options,
781+
override_cfg,
779782
build_scripts,
780783
);
781784
}
@@ -825,28 +828,29 @@ fn detached_files_to_crate_graph(
825828
},
826829
);
827830

828-
public_deps.add(detached_file_crate, &mut crate_graph);
831+
public_deps.add_to_crate_graph(&mut crate_graph, detached_file_crate);
829832
}
830833
crate_graph
831834
}
832835

833836
fn handle_rustc_crates(
834837
crate_graph: &mut CrateGraph,
835-
rustc_workspace: &CargoWorkspace,
838+
pkg_to_lib_crate: &mut FxHashMap<Package, CrateId>,
836839
load: &mut dyn FnMut(&AbsPath) -> Option<FileId>,
837-
cfg_options: &CfgOptions,
838-
override_cfg: &CfgOverrides,
839840
load_proc_macro: &mut dyn FnMut(&str, &AbsPath) -> ProcMacroLoadResult,
840-
pkg_to_lib_crate: &mut FxHashMap<Package, CrateId>,
841-
public_deps: &SysrootPublicDeps,
841+
rustc_workspace: &CargoWorkspace,
842842
cargo: &CargoWorkspace,
843+
public_deps: &SysrootPublicDeps,
844+
libproc_macro: Option<CrateId>,
843845
pkg_crates: &FxHashMap<Package, Vec<(CrateId, TargetKind)>>,
846+
cfg_options: &CfgOptions,
847+
override_cfg: &CfgOverrides,
844848
build_scripts: &WorkspaceBuildScripts,
845849
) {
846850
let mut rustc_pkg_crates = FxHashMap::default();
847851
// The root package of the rustc-dev component is rustc_driver, so we match that
848852
let root_pkg =
849-
rustc_workspace.packages().find(|package| rustc_workspace[*package].name == "rustc_driver");
853+
rustc_workspace.packages().find(|&package| rustc_workspace[package].name == "rustc_driver");
850854
// The rustc workspace might be incomplete (such as if rustc-dev is not
851855
// installed for the current toolchain) and `rustc_source` is set to discover.
852856
if let Some(root_pkg) = root_pkg {
@@ -901,7 +905,16 @@ fn handle_rustc_crates(
901905
);
902906
pkg_to_lib_crate.insert(pkg, crate_id);
903907
// Add dependencies on core / std / alloc for this crate
904-
public_deps.add(crate_id, crate_graph);
908+
public_deps.add_to_crate_graph(crate_graph, crate_id);
909+
if let Some(proc_macro) = libproc_macro {
910+
add_dep_with_prelude(
911+
crate_graph,
912+
crate_id,
913+
CrateName::new("proc_macro").unwrap(),
914+
proc_macro,
915+
rustc_workspace[tgt].is_proc_macro,
916+
);
917+
}
905918
rustc_pkg_crates.entry(pkg).or_insert_with(Vec::new).push(crate_id);
906919
}
907920
}
@@ -1009,7 +1022,7 @@ struct SysrootPublicDeps {
10091022

10101023
impl SysrootPublicDeps {
10111024
/// Makes `from` depend on the public sysroot crates.
1012-
fn add(&self, from: CrateId, crate_graph: &mut CrateGraph) {
1025+
fn add_to_crate_graph(&self, crate_graph: &mut CrateGraph, from: CrateId) {
10131026
for (name, krate, prelude) in &self.deps {
10141027
add_dep_with_prelude(crate_graph, from, name.clone(), *krate, *prelude);
10151028
}

0 commit comments

Comments
 (0)