From 3937b225e7918ae6d75849a0959754af43fbf08c Mon Sep 17 00:00:00 2001 From: Paul Daniel Faria Date: Sun, 7 Jun 2020 10:29:03 -0400 Subject: Change management of test cfg to better support json projects --- crates/ra_project_model/src/lib.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'crates/ra_project_model/src/lib.rs') diff --git a/crates/ra_project_model/src/lib.rs b/crates/ra_project_model/src/lib.rs index 7ad941279..ef443fc09 100644 --- a/crates/ra_project_model/src/lib.rs +++ b/crates/ra_project_model/src/lib.rs @@ -349,11 +349,7 @@ impl ProjectWorkspace { let file_id = load(&sysroot[krate].root)?; // Crates from sysroot have `cfg(test)` disabled - let cfg_options = { - let mut opts = default_cfg_options.clone(); - opts.remove_atom("test"); - opts - }; + let cfg_options = default_cfg_options.clone(); let env = Env::default(); let extern_source = ExternSource::default(); @@ -404,7 +400,12 @@ impl ProjectWorkspace { if let Some(file_id) = load(root) { let edition = cargo[pkg].edition; let cfg_options = { - let mut opts = default_cfg_options.clone(); + let mut opts = { + let mut opts = default_cfg_options.clone(); + opts.insert_atom("test".into()); + opts + }; + for feature in cargo[pkg].features.iter() { opts.insert_key_value("feature".into(), feature.into()); } -- cgit v1.2.3 From 9c35f135b9c872e904ee1e838cfa69fc5745c45f Mon Sep 17 00:00:00 2001 From: Paul Daniel Faria Date: Mon, 8 Jun 2020 10:23:20 -0400 Subject: Remove default_cfg_options, pass target instead so it can be used for building cargo workspaces --- crates/ra_project_model/src/lib.rs | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) (limited to 'crates/ra_project_model/src/lib.rs') diff --git a/crates/ra_project_model/src/lib.rs b/crates/ra_project_model/src/lib.rs index ef443fc09..4ef2e6f85 100644 --- a/crates/ra_project_model/src/lib.rs +++ b/crates/ra_project_model/src/lib.rs @@ -250,7 +250,7 @@ impl ProjectWorkspace { pub fn to_crate_graph( &self, - default_cfg_options: &CfgOptions, + target: Option<&String>, extern_source_roots: &FxHashMap, proc_macro_client: &ProcMacroClient, load: &mut dyn FnMut(&Path) -> Option, @@ -269,7 +269,7 @@ impl ProjectWorkspace { json_project::Edition::Edition2018 => Edition::Edition2018, }; let cfg_options = { - let mut opts = default_cfg_options.clone(); + let mut opts = CfgOptions::default(); for cfg in &krate.cfg { match cfg.find('=') { None => opts.insert_atom(cfg.into()), @@ -343,14 +343,13 @@ impl ProjectWorkspace { } } ProjectWorkspace::Cargo { cargo, sysroot } => { + let mut cfg_options = get_rustc_cfg_options(target); + let sysroot_crates: FxHashMap<_, _> = sysroot .crates() .filter_map(|krate| { let file_id = load(&sysroot[krate].root)?; - // Crates from sysroot have `cfg(test)` disabled - let cfg_options = default_cfg_options.clone(); - let env = Env::default(); let extern_source = ExternSource::default(); let proc_macro = vec![]; @@ -361,7 +360,7 @@ impl ProjectWorkspace { file_id, Edition::Edition2018, Some(crate_name), - cfg_options, + cfg_options.clone(), env, extern_source, proc_macro, @@ -392,6 +391,10 @@ impl ProjectWorkspace { let mut pkg_to_lib_crate = FxHashMap::default(); let mut pkg_crates = FxHashMap::default(); + + // Add test cfg for non-sysroot crates + cfg_options.insert_atom("test".into()); + // Next, create crates for each package, target pair for pkg in cargo.packages() { let mut lib_tgt = None; @@ -400,12 +403,7 @@ impl ProjectWorkspace { if let Some(file_id) = load(root) { let edition = cargo[pkg].edition; let cfg_options = { - let mut opts = { - let mut opts = default_cfg_options.clone(); - opts.insert_atom("test".into()); - opts - }; - + let mut opts = cfg_options.clone(); for feature in cargo[pkg].features.iter() { opts.insert_key_value("feature".into(), feature.into()); } @@ -562,7 +560,7 @@ impl ProjectWorkspace { } } -pub fn get_rustc_cfg_options(target: Option<&String>) -> CfgOptions { +fn get_rustc_cfg_options(target: Option<&String>) -> CfgOptions { let mut cfg_options = CfgOptions::default(); // Some nightly-only cfgs, which are required for stdlib @@ -602,6 +600,8 @@ pub fn get_rustc_cfg_options(target: Option<&String>) -> CfgOptions { Err(e) => log::error!("failed to get rustc cfgs: {:#}", e), } + cfg_options.insert_atom("debug_assertion".into()); + cfg_options } -- cgit v1.2.3 From dbceaf522b717bd0dda89f6af1684ebd8e033aee Mon Sep 17 00:00:00 2001 From: Paul Daniel Faria Date: Mon, 8 Jun 2020 12:10:23 -0400 Subject: Use Option<&str> for target instead of Option<&String> --- crates/ra_project_model/src/lib.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'crates/ra_project_model/src/lib.rs') diff --git a/crates/ra_project_model/src/lib.rs b/crates/ra_project_model/src/lib.rs index 4ef2e6f85..fe03b509e 100644 --- a/crates/ra_project_model/src/lib.rs +++ b/crates/ra_project_model/src/lib.rs @@ -250,7 +250,7 @@ impl ProjectWorkspace { pub fn to_crate_graph( &self, - target: Option<&String>, + target: Option<&str>, extern_source_roots: &FxHashMap, proc_macro_client: &ProcMacroClient, load: &mut dyn FnMut(&Path) -> Option, @@ -560,7 +560,7 @@ impl ProjectWorkspace { } } -fn get_rustc_cfg_options(target: Option<&String>) -> CfgOptions { +fn get_rustc_cfg_options(target: Option<&str>) -> CfgOptions { let mut cfg_options = CfgOptions::default(); // Some nightly-only cfgs, which are required for stdlib @@ -578,7 +578,7 @@ fn get_rustc_cfg_options(target: Option<&String>) -> CfgOptions { let mut cmd = Command::new(ra_toolchain::rustc()); cmd.args(&["--print", "cfg", "-O"]); if let Some(target) = target { - cmd.args(&["--target", target.as_str()]); + cmd.args(&["--target", target]); } let output = output(cmd)?; Ok(String::from_utf8(output.stdout)?) -- cgit v1.2.3