Enable rust_analyzer for cfgs when code is being analyzed by rust-analyzer

This commit is contained in:
Lukas Wirth 2023-08-29 10:08:34 +02:00
parent 3325622230
commit ca6ddd8ea3
2 changed files with 24 additions and 15 deletions

View File

@ -221,7 +221,6 @@ impl Definition {
} }
// def is crate root // def is crate root
// FIXME: We don't do searches for crates currently, as a crate does not actually have a single name
if let &Definition::Module(module) = self { if let &Definition::Module(module) = self {
if module.is_crate_root() { if module.is_crate_root() {
return SearchScope::reverse_dependencies(db, module.krate()); return SearchScope::reverse_dependencies(db, module.krate());
@ -393,7 +392,10 @@ impl<'a> FindUsages<'a> {
let name = match self.def { let name = match self.def {
// special case crate modules as these do not have a proper name // special case crate modules as these do not have a proper name
Definition::Module(module) if module.is_crate_root() => { Definition::Module(module) if module.is_crate_root() => {
// FIXME: This assumes the crate name is always equal to its display name when it really isn't // FIXME: This assumes the crate name is always equal to its display name when it
// really isn't
// we should instead look at the dependency edge name and recursively search our way
// up the ancestors
module module
.krate() .krate()
.display_name(self.sema.db) .display_name(self.sema.db)

View File

@ -2,7 +2,7 @@
//! metadata` or `rust-project.json`) into representation stored in the salsa //! metadata` or `rust-project.json`) into representation stored in the salsa
//! database -- `CrateGraph`. //! database -- `CrateGraph`.
use std::{collections::VecDeque, fmt, fs, process::Command, str::FromStr, sync}; use std::{collections::VecDeque, fmt, fs, iter, process::Command, str::FromStr, sync};
use anyhow::{format_err, Context}; use anyhow::{format_err, Context};
use base_db::{ use base_db::{
@ -730,6 +730,7 @@ 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 mut cfg_cache: FxHashMap<&str, Vec<CfgFlag>> = FxHashMap::default();
let crates: FxHashMap<CrateId, CrateId> = project let crates: FxHashMap<CrateId, CrateId> = project
.crates() .crates()
@ -765,7 +766,12 @@ fn project_json_to_crate_graph(
*edition, *edition,
display_name.clone(), display_name.clone(),
version.clone(), version.clone(),
target_cfgs.iter().chain(cfg.iter()).cloned().collect(), target_cfgs
.iter()
.chain(cfg.iter())
.chain(iter::once(&r_a_cfg_flag))
.cloned()
.collect(),
None, None,
env, env,
*is_proc_macro, *is_proc_macro,
@ -820,7 +826,7 @@ fn cargo_to_crate_graph(
sysroot: Option<&Sysroot>, sysroot: Option<&Sysroot>,
rustc_cfg: Vec<CfgFlag>, rustc_cfg: Vec<CfgFlag>,
override_cfg: &CfgOverrides, override_cfg: &CfgOverrides,
// Don't compute cfg and use this if present // Don't compute cfg and use this if present, only used for the sysroot experiment hack
forced_cfg: Option<CfgOptions>, forced_cfg: Option<CfgOptions>,
build_scripts: &WorkspaceBuildScripts, build_scripts: &WorkspaceBuildScripts,
target_layout: TargetLayoutLoadResult, target_layout: TargetLayoutLoadResult,
@ -842,12 +848,7 @@ fn cargo_to_crate_graph(
None => (SysrootPublicDeps::default(), None), None => (SysrootPublicDeps::default(), None),
}; };
let cfg_options = { let cfg_options = create_cfg_options(rustc_cfg);
let mut cfg_options = CfgOptions::default();
cfg_options.extend(rustc_cfg);
cfg_options.insert_atom("debug_assertions".into());
cfg_options
};
// Mapping of a package to its library target // Mapping of a package to its library target
let mut pkg_to_lib_crate = FxHashMap::default(); let mut pkg_to_lib_crate = FxHashMap::default();
@ -1029,8 +1030,7 @@ fn detached_files_to_crate_graph(
None => (SysrootPublicDeps::default(), None), None => (SysrootPublicDeps::default(), None),
}; };
let mut cfg_options = CfgOptions::default(); let cfg_options = create_cfg_options(rustc_cfg);
cfg_options.extend(rustc_cfg);
for detached_file in detached_files { for detached_file in detached_files {
let file_id = match load(detached_file) { let file_id = match load(detached_file) {
@ -1295,8 +1295,7 @@ fn sysroot_to_crate_graph(
channel: Option<ReleaseChannel>, channel: Option<ReleaseChannel>,
) -> (SysrootPublicDeps, Option<CrateId>) { ) -> (SysrootPublicDeps, Option<CrateId>) {
let _p = profile::span("sysroot_to_crate_graph"); let _p = profile::span("sysroot_to_crate_graph");
let mut cfg_options = CfgOptions::default(); let cfg_options = create_cfg_options(rustc_cfg.clone());
cfg_options.extend(rustc_cfg.clone());
let sysroot_crates: FxHashMap<SysrootCrate, CrateId> = match &sysroot.hack_cargo_workspace { let sysroot_crates: FxHashMap<SysrootCrate, CrateId> = match &sysroot.hack_cargo_workspace {
Some(cargo) => handle_hack_cargo_workspace( Some(cargo) => handle_hack_cargo_workspace(
load, load,
@ -1475,3 +1474,11 @@ fn inject_cargo_env(package: &PackageData, env: &mut Env) {
env.set("CARGO_PKG_LICENSE_FILE", String::new()); env.set("CARGO_PKG_LICENSE_FILE", String::new());
} }
fn create_cfg_options(rustc_cfg: Vec<CfgFlag>) -> CfgOptions {
let mut cfg_options = CfgOptions::default();
cfg_options.extend(rustc_cfg);
cfg_options.insert_atom("debug_assertions".into());
cfg_options.insert_atom("rust_analyzer".into());
cfg_options
}