Skip to content

Commit 4227d18

Browse files
committed
Allow setting cfgs
1 parent ee8c18c commit 4227d18

File tree

4 files changed

+42
-46
lines changed

4 files changed

+42
-46
lines changed

crates/cfg/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ impl CfgOptions {
8686
}
8787
}
8888

89-
#[derive(Clone, Debug, PartialEq, Eq)]
89+
#[derive(Default, Clone, Debug, PartialEq, Eq)]
9090
pub struct CfgDiff {
9191
// Invariants: No duplicates, no atom that's both in `enable` and `disable`.
9292
enable: Vec<CfgAtom>,

crates/project-model/src/cargo_workspace.rs

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,7 @@ pub struct CargoConfig {
100100
pub sysroot_src: Option<AbsPathBuf>,
101101
/// rustc private crate source
102102
pub rustc_source: Option<RustLibSource>,
103-
/// crates to disable `#[cfg(test)]` on
104-
pub unset_test_crates: UnsetTestCrates,
103+
pub cfg_overrides: CfgOverrides,
105104
/// Invoke `cargo check` through the RUSTC_WRAPPER.
106105
pub wrap_rustc_in_build_scripts: bool,
107106
/// The command to run instead of `cargo check` for building build scripts.
@@ -114,27 +113,6 @@ pub struct CargoConfig {
114113
pub invocation_location: InvocationLocation,
115114
}
116115

117-
impl CargoConfig {
118-
pub fn cfg_overrides(&self) -> CfgOverrides {
119-
match &self.unset_test_crates {
120-
UnsetTestCrates::None => CfgOverrides::Selective(iter::empty().collect()),
121-
UnsetTestCrates::Only(unset_test_crates) => CfgOverrides::Selective(
122-
unset_test_crates
123-
.iter()
124-
.cloned()
125-
.zip(iter::repeat_with(|| {
126-
cfg::CfgDiff::new(Vec::new(), vec![cfg::CfgAtom::Flag("test".into())])
127-
.unwrap()
128-
}))
129-
.collect(),
130-
),
131-
UnsetTestCrates::All => CfgOverrides::Wildcard(
132-
cfg::CfgDiff::new(Vec::new(), vec![cfg::CfgAtom::Flag("test".into())]).unwrap(),
133-
),
134-
}
135-
}
136-
}
137-
138116
pub type Package = Idx<PackageData>;
139117

140118
pub type Target = Idx<TargetData>;

crates/project-model/src/workspace.rs

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,29 +28,17 @@ use crate::{
2828
};
2929

3030
/// A set of cfg-overrides per crate.
31-
///
32-
/// `Wildcard(..)` is useful e.g. disabling `#[cfg(test)]` on all crates,
33-
/// without having to first obtain a list of all crates.
3431
#[derive(Debug, Clone, Eq, PartialEq)]
35-
pub enum CfgOverrides {
36-
/// A single global set of overrides matching all crates.
37-
Wildcard(CfgDiff),
32+
pub struct CfgOverrides {
33+
/// A global set of overrides matching all crates.
34+
global: CfgDiff,
3835
/// A set of overrides matching specific crates.
39-
Selective(FxHashMap<String, CfgDiff>),
40-
}
41-
42-
impl Default for CfgOverrides {
43-
fn default() -> Self {
44-
Self::Selective(FxHashMap::default())
45-
}
36+
selective: FxHashMap<String, CfgDiff>,
4637
}
4738

4839
impl CfgOverrides {
4940
pub fn len(&self) -> usize {
50-
match self {
51-
CfgOverrides::Wildcard(_) => 1,
52-
CfgOverrides::Selective(hash_map) => hash_map.len(),
53-
}
41+
self.global.len() + self.selective.iter().map(|(_, it)| it.len()).sum()
5442
}
5543
}
5644

@@ -292,7 +280,7 @@ impl ProjectWorkspace {
292280
let rustc_cfg =
293281
rustc_cfg::get(Some(&cargo_toml), config.target.as_deref(), &config.extra_env);
294282

295-
let cfg_overrides = config.cfg_overrides();
283+
let cfg_overrides = config.cfg_overrides.clone();
296284
let data_layout = target_data_layout::get(
297285
Some(&cargo_toml),
298286
config.target.as_deref(),

crates/rust-analyzer/src/config.rs

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
1010
use std::{fmt, iter, ops::Not, path::PathBuf};
1111

12+
use cfg::{CfgAtom, CfgDiff};
1213
use flycheck::FlycheckConfig;
1314
use ide::{
1415
AssistConfig, CallableSnippets, CompletionConfig, DiagnosticsConfig, ExprFillDefaultMode,
@@ -22,8 +23,8 @@ use ide_db::{
2223
use itertools::Itertools;
2324
use lsp_types::{ClientCapabilities, MarkupKind};
2425
use project_model::{
25-
CargoConfig, CargoFeatures, ProjectJson, ProjectJsonData, ProjectManifest, RustLibSource,
26-
UnsetTestCrates,
26+
CargoConfig, CargoFeatures, CfgOverrides, ProjectJson, ProjectJsonData, ProjectManifest,
27+
RustLibSource, UnsetTestCrates,
2728
};
2829
use rustc_hash::{FxHashMap, FxHashSet};
2930
use serde::{de::DeserializeOwned, Deserialize};
@@ -101,6 +102,8 @@ config_data! {
101102
/// Use `RUSTC_WRAPPER=rust-analyzer` when running build scripts to
102103
/// avoid checking unnecessary things.
103104
cargo_buildScripts_useRustcWrapper: bool = "true",
105+
/// List of cfg options to enable with the given values.
106+
cargo_cfgs: FxHashMap<String, String> = "{}",
104107
/// Extra arguments that are passed to every cargo invocation.
105108
cargo_extraArgs: Vec<String> = "[]",
106109
/// Extra environment variables that will be set when running cargo, rustc
@@ -128,7 +131,7 @@ config_data! {
128131
// FIXME(@poliorcetics): move to multiple targets here too, but this will need more work
129132
// than `checkOnSave_target`
130133
cargo_target: Option<String> = "null",
131-
/// Unsets `#[cfg(test)]` for the specified crates.
134+
/// Unsets the implicit `#[cfg(test)]` for the specified crates.
132135
cargo_unsetTest: Vec<String> = "[\"core\"]",
133136

134137
/// Run the check command for diagnostics on save.
@@ -1189,7 +1192,34 @@ impl Config {
11891192
sysroot,
11901193
sysroot_src,
11911194
rustc_source,
1192-
unset_test_crates: UnsetTestCrates::Only(self.data.cargo_unsetTest.clone()),
1195+
cfg_overrides: project_model::CfgOverrides {
1196+
global: CfgDiff::new(
1197+
self.data
1198+
.cargo_cfgs
1199+
.iter()
1200+
.map(|(key, val)| {
1201+
if val.is_empty() {
1202+
CfgAtom::Flag(key.into())
1203+
} else {
1204+
CfgAtom::KeyValue { key: key.into(), value: val.into() }
1205+
}
1206+
})
1207+
.collect(),
1208+
vec![],
1209+
)
1210+
.unwrap(),
1211+
selective: self
1212+
.data
1213+
.cargo_unsetTest
1214+
.iter()
1215+
.map(|it| {
1216+
(
1217+
it.clone(),
1218+
CfgDiff::new(vec![], vec![CfgAtom::Flag("test".into())]).unwrap(),
1219+
)
1220+
})
1221+
.collect(),
1222+
},
11931223
wrap_rustc_in_build_scripts: self.data.cargo_buildScripts_useRustcWrapper,
11941224
invocation_strategy: match self.data.cargo_buildScripts_invocationStrategy {
11951225
InvocationStrategy::Once => project_model::InvocationStrategy::Once,

0 commit comments

Comments
 (0)