Use raw cfgs in json project and fix typo

This commit is contained in:
oxalica 2019-10-05 20:55:27 +08:00
parent b271cb18d5
commit c6303d9fee
No known key found for this signature in database
GPG key ID: CED392DE0C483D00
3 changed files with 18 additions and 9 deletions

View file

@ -286,7 +286,13 @@ fn test_missing_module_code_action_in_json_project() {
let project = json!({
"roots": [path],
"crates": [ { "root_module": path.join("src/lib.rs"), "deps": [], "edition": "2015" } ]
"crates": [ {
"root_module": path.join("src/lib.rs"),
"deps": [],
"edition": "2015",
"atom_cfgs": [],
"key_value_cfgs": {}
} ]
});
let code = format!(

View file

@ -2,6 +2,7 @@
use std::path::PathBuf;
use rustc_hash::{FxHashMap, FxHashSet};
use serde::Deserialize;
/// A root points to the directory which contains Rust crates. rust-analyzer watches all files in
@ -19,8 +20,8 @@ pub struct Crate {
pub(crate) root_module: PathBuf,
pub(crate) edition: Edition,
pub(crate) deps: Vec<Dep>,
#[serde(default)]
pub(crate) features: Vec<String>,
pub(crate) atom_cfgs: FxHashSet<String>,
pub(crate) key_value_cfgs: FxHashMap<String, String>,
}
#[derive(Clone, Copy, Debug, Deserialize)]

View file

@ -134,11 +134,13 @@ impl ProjectWorkspace {
json_project::Edition::Edition2015 => Edition::Edition2015,
json_project::Edition::Edition2018 => Edition::Edition2018,
};
// FIXME: cfg options
// Default to enable test for workspace crates.
let cfg_options = default_cfg_options
.clone()
.features(krate.features.iter().map(Into::into));
let mut cfg_options = default_cfg_options.clone();
for name in &krate.atom_cfgs {
cfg_options = cfg_options.atom(name.into());
}
for (key, value) in &krate.key_value_cfgs {
cfg_options = cfg_options.key_value(key.into(), value.into());
}
crates.insert(
crate_id,
crate_graph.add_crate_root(file_id, edition, cfg_options),
@ -309,7 +311,7 @@ pub fn get_rustc_cfg_options() -> CfgOptions {
let mut cfg_options = CfgOptions::default();
match (|| -> Result<_> {
// `cfg(test)` ans `cfg(debug_assertion)` is handled outside, so we suppress them here.
// `cfg(test)` and `cfg(debug_assertion)` are handled outside, so we suppress them here.
let output = Command::new("rustc").args(&["--print", "cfg", "-O"]).output()?;
if !output.status.success() {
Err("failed to get rustc cfgs")?;