about summary refs log tree commit diff
diff options
context:
space:
mode:
authorLukas Wirth <lukastw97@gmail.com>2024-04-19 11:06:55 +0200
committerLukas Wirth <lukastw97@gmail.com>2024-04-19 11:06:55 +0200
commit0485a85ee2af65b3c73eda0df80fbacf838b862e (patch)
tree24587d816792823f110a2653c52d3d0afaf2fe5c
parent8989dcffd6e94d402cf1cbce20c91eb78230118a (diff)
downloadrust-0485a85ee2af65b3c73eda0df80fbacf838b862e.tar.gz
rust-0485a85ee2af65b3c73eda0df80fbacf838b862e.zip
Set debug_assertions and miri cfgs as config defaults, allowing them to be overwritten
-rw-r--r--crates/project-model/src/env.rs1
-rw-r--r--crates/project-model/src/rustc_cfg.rs3
-rw-r--r--crates/project-model/src/workspace.rs11
-rw-r--r--crates/rust-analyzer/src/config.rs19
-rw-r--r--docs/user/generated_config.adoc10
-rw-r--r--editors/code/package.json5
6 files changed, 35 insertions, 14 deletions
diff --git a/crates/project-model/src/env.rs b/crates/project-model/src/env.rs
index 541298585ad..762e01c9177 100644
--- a/crates/project-model/src/env.rs
+++ b/crates/project-model/src/env.rs
@@ -1,3 +1,4 @@
+//! Cargo-like environment variables injection.
 use base_db::Env;
 use rustc_hash::FxHashMap;
 use toolchain::Tool;
diff --git a/crates/project-model/src/rustc_cfg.rs b/crates/project-model/src/rustc_cfg.rs
index 194bae55701..4f69b2b96f0 100644
--- a/crates/project-model/src/rustc_cfg.rs
+++ b/crates/project-model/src/rustc_cfg.rs
@@ -32,9 +32,6 @@ pub(crate) fn get(
         }
     }
 
-    // Add miri cfg, which is useful for mir eval in stdlib
-    res.push(CfgFlag::Atom("miri".into()));
-
     let rustc_cfgs = get_rust_cfgs(target, extra_env, config);
 
     let rustc_cfgs = match rustc_cfgs {
diff --git a/crates/project-model/src/workspace.rs b/crates/project-model/src/workspace.rs
index 85e000bc0ba..a5e74763d70 100644
--- a/crates/project-model/src/workspace.rs
+++ b/crates/project-model/src/workspace.rs
@@ -1454,8 +1454,14 @@ fn sysroot_to_crate_graph(
                 None,
                 rustc_cfg,
                 &CfgOverrides {
-                    global: CfgDiff::new(vec![CfgAtom::Flag("debug_assertions".into())], vec![])
-                        .unwrap(),
+                    global: CfgDiff::new(
+                        vec![
+                            CfgAtom::Flag("debug_assertions".into()),
+                            CfgAtom::Flag("miri".into()),
+                        ],
+                        vec![],
+                    )
+                    .unwrap(),
                     ..Default::default()
                 },
                 &WorkspaceBuildScripts::default(),
@@ -1519,6 +1525,7 @@ fn sysroot_to_crate_graph(
                 let mut cfg_options = CfgOptions::default();
                 cfg_options.extend(rustc_cfg);
                 cfg_options.insert_atom("debug_assertions".into());
+                cfg_options.insert_atom("miri".into());
                 cfg_options
             });
             let sysroot_crates: FxHashMap<SysrootCrate, CrateId> = stitched
diff --git a/crates/rust-analyzer/src/config.rs b/crates/rust-analyzer/src/config.rs
index 00782036217..0d99a8f113b 100644
--- a/crates/rust-analyzer/src/config.rs
+++ b/crates/rust-analyzer/src/config.rs
@@ -124,7 +124,12 @@ config_data! {
         /// avoid checking unnecessary things.
         cargo_buildScripts_useRustcWrapper: bool = true,
         /// List of cfg options to enable with the given values.
-        cargo_cfgs: FxHashMap<String, String> = FxHashMap::default(),
+        cargo_cfgs: FxHashMap<String, Option<String>> = {
+            let mut m = FxHashMap::default();
+            m.insert("debug_assertions".to_owned(), None);
+            m.insert("miri".to_owned(), None);
+            m
+        },
         /// Extra arguments that are passed to every cargo invocation.
         cargo_extraArgs: Vec<String> = vec![],
         /// Extra environment variables that will be set when running cargo, rustc
@@ -1591,12 +1596,9 @@ impl Config {
                 global: CfgDiff::new(
                     self.cargo_cfgs()
                         .iter()
-                        .map(|(key, val)| {
-                            if val.is_empty() {
-                                CfgAtom::Flag(key.into())
-                            } else {
-                                CfgAtom::KeyValue { key: key.into(), value: val.into() }
-                            }
+                        .map(|(key, val)| match val {
+                            Some(val) => CfgAtom::KeyValue { key: key.into(), value: val.into() },
+                            None => CfgAtom::Flag(key.into()),
                         })
                         .collect(),
                     vec![],
@@ -2667,6 +2669,9 @@ fn field_props(field: &str, ty: &str, doc: &[&str], default: &str) -> serde_json
         "FxHashMap<Box<str>, usize>" => set! {
             "type": "object",
         },
+        "FxHashMap<String, Option<String>>" => set! {
+            "type": "object",
+        },
         "Option<usize>" => set! {
             "type": ["null", "integer"],
             "minimum": 0,
diff --git a/docs/user/generated_config.adoc b/docs/user/generated_config.adoc
index 14bce2083e2..7bd3012056b 100644
--- a/docs/user/generated_config.adoc
+++ b/docs/user/generated_config.adoc
@@ -88,10 +88,18 @@ or build-script sources change and are saved.
 Use `RUSTC_WRAPPER=rust-analyzer` when running build scripts to
 avoid checking unnecessary things.
 --
-[[rust-analyzer.cargo.cfgs]]rust-analyzer.cargo.cfgs (default: `{}`)::
+[[rust-analyzer.cargo.cfgs]]rust-analyzer.cargo.cfgs::
 +
 --
+Default:
+----
+{
+  "debug_assertions": null,
+  "miri": null
+}
+----
 List of cfg options to enable with the given values.
+
 --
 [[rust-analyzer.cargo.extraArgs]]rust-analyzer.cargo.extraArgs (default: `[]`)::
 +
diff --git a/editors/code/package.json b/editors/code/package.json
index 9b4875dabe0..0fd17f385e7 100644
--- a/editors/code/package.json
+++ b/editors/code/package.json
@@ -610,7 +610,10 @@
                 },
                 "rust-analyzer.cargo.cfgs": {
                     "markdownDescription": "List of cfg options to enable with the given values.",
-                    "default": {},
+                    "default": {
+                        "debug_assertions": null,
+                        "miri": null
+                    },
                     "type": "object"
                 },
                 "rust-analyzer.cargo.extraArgs": {