diff options
author | Paul Daniel Faria <[email protected]> | 2020-06-08 15:23:20 +0100 |
---|---|---|
committer | Paul Daniel Faria <[email protected]> | 2020-06-08 15:23:29 +0100 |
commit | 9c35f135b9c872e904ee1e838cfa69fc5745c45f (patch) | |
tree | 6b170a9ccb8c8b4bfaf46e1554126ade289c6ef1 /crates | |
parent | 3937b225e7918ae6d75849a0959754af43fbf08c (diff) |
Remove default_cfg_options, pass target instead so it can be used for building cargo workspaces
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_project_model/src/lib.rs | 26 | ||||
-rw-r--r-- | crates/rust-analyzer/src/cli/load_cargo.rs | 20 | ||||
-rw-r--r-- | crates/rust-analyzer/src/global_state.rs | 11 |
3 files changed, 19 insertions, 38 deletions
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 { | |||
250 | 250 | ||
251 | pub fn to_crate_graph( | 251 | pub fn to_crate_graph( |
252 | &self, | 252 | &self, |
253 | default_cfg_options: &CfgOptions, | 253 | target: Option<&String>, |
254 | extern_source_roots: &FxHashMap<PathBuf, ExternSourceId>, | 254 | extern_source_roots: &FxHashMap<PathBuf, ExternSourceId>, |
255 | proc_macro_client: &ProcMacroClient, | 255 | proc_macro_client: &ProcMacroClient, |
256 | load: &mut dyn FnMut(&Path) -> Option<FileId>, | 256 | load: &mut dyn FnMut(&Path) -> Option<FileId>, |
@@ -269,7 +269,7 @@ impl ProjectWorkspace { | |||
269 | json_project::Edition::Edition2018 => Edition::Edition2018, | 269 | json_project::Edition::Edition2018 => Edition::Edition2018, |
270 | }; | 270 | }; |
271 | let cfg_options = { | 271 | let cfg_options = { |
272 | let mut opts = default_cfg_options.clone(); | 272 | let mut opts = CfgOptions::default(); |
273 | for cfg in &krate.cfg { | 273 | for cfg in &krate.cfg { |
274 | match cfg.find('=') { | 274 | match cfg.find('=') { |
275 | None => opts.insert_atom(cfg.into()), | 275 | None => opts.insert_atom(cfg.into()), |
@@ -343,14 +343,13 @@ impl ProjectWorkspace { | |||
343 | } | 343 | } |
344 | } | 344 | } |
345 | ProjectWorkspace::Cargo { cargo, sysroot } => { | 345 | ProjectWorkspace::Cargo { cargo, sysroot } => { |
346 | let mut cfg_options = get_rustc_cfg_options(target); | ||
347 | |||
346 | let sysroot_crates: FxHashMap<_, _> = sysroot | 348 | let sysroot_crates: FxHashMap<_, _> = sysroot |
347 | .crates() | 349 | .crates() |
348 | .filter_map(|krate| { | 350 | .filter_map(|krate| { |
349 | let file_id = load(&sysroot[krate].root)?; | 351 | let file_id = load(&sysroot[krate].root)?; |
350 | 352 | ||
351 | // Crates from sysroot have `cfg(test)` disabled | ||
352 | let cfg_options = default_cfg_options.clone(); | ||
353 | |||
354 | let env = Env::default(); | 353 | let env = Env::default(); |
355 | let extern_source = ExternSource::default(); | 354 | let extern_source = ExternSource::default(); |
356 | let proc_macro = vec![]; | 355 | let proc_macro = vec![]; |
@@ -361,7 +360,7 @@ impl ProjectWorkspace { | |||
361 | file_id, | 360 | file_id, |
362 | Edition::Edition2018, | 361 | Edition::Edition2018, |
363 | Some(crate_name), | 362 | Some(crate_name), |
364 | cfg_options, | 363 | cfg_options.clone(), |
365 | env, | 364 | env, |
366 | extern_source, | 365 | extern_source, |
367 | proc_macro, | 366 | proc_macro, |
@@ -392,6 +391,10 @@ impl ProjectWorkspace { | |||
392 | 391 | ||
393 | let mut pkg_to_lib_crate = FxHashMap::default(); | 392 | let mut pkg_to_lib_crate = FxHashMap::default(); |
394 | let mut pkg_crates = FxHashMap::default(); | 393 | let mut pkg_crates = FxHashMap::default(); |
394 | |||
395 | // Add test cfg for non-sysroot crates | ||
396 | cfg_options.insert_atom("test".into()); | ||
397 | |||
395 | // Next, create crates for each package, target pair | 398 | // Next, create crates for each package, target pair |
396 | for pkg in cargo.packages() { | 399 | for pkg in cargo.packages() { |
397 | let mut lib_tgt = None; | 400 | let mut lib_tgt = None; |
@@ -400,12 +403,7 @@ impl ProjectWorkspace { | |||
400 | if let Some(file_id) = load(root) { | 403 | if let Some(file_id) = load(root) { |
401 | let edition = cargo[pkg].edition; | 404 | let edition = cargo[pkg].edition; |
402 | let cfg_options = { | 405 | let cfg_options = { |
403 | let mut opts = { | 406 | let mut opts = cfg_options.clone(); |
404 | let mut opts = default_cfg_options.clone(); | ||
405 | opts.insert_atom("test".into()); | ||
406 | opts | ||
407 | }; | ||
408 | |||
409 | for feature in cargo[pkg].features.iter() { | 407 | for feature in cargo[pkg].features.iter() { |
410 | opts.insert_key_value("feature".into(), feature.into()); | 408 | opts.insert_key_value("feature".into(), feature.into()); |
411 | } | 409 | } |
@@ -562,7 +560,7 @@ impl ProjectWorkspace { | |||
562 | } | 560 | } |
563 | } | 561 | } |
564 | 562 | ||
565 | pub fn get_rustc_cfg_options(target: Option<&String>) -> CfgOptions { | 563 | fn get_rustc_cfg_options(target: Option<&String>) -> CfgOptions { |
566 | let mut cfg_options = CfgOptions::default(); | 564 | let mut cfg_options = CfgOptions::default(); |
567 | 565 | ||
568 | // Some nightly-only cfgs, which are required for stdlib | 566 | // Some nightly-only cfgs, which are required for stdlib |
@@ -602,6 +600,8 @@ pub fn get_rustc_cfg_options(target: Option<&String>) -> CfgOptions { | |||
602 | Err(e) => log::error!("failed to get rustc cfgs: {:#}", e), | 600 | Err(e) => log::error!("failed to get rustc cfgs: {:#}", e), |
603 | } | 601 | } |
604 | 602 | ||
603 | cfg_options.insert_atom("debug_assertion".into()); | ||
604 | |||
605 | cfg_options | 605 | cfg_options |
606 | } | 606 | } |
607 | 607 | ||
diff --git a/crates/rust-analyzer/src/cli/load_cargo.rs b/crates/rust-analyzer/src/cli/load_cargo.rs index 46181b677..8f2aeac77 100644 --- a/crates/rust-analyzer/src/cli/load_cargo.rs +++ b/crates/rust-analyzer/src/cli/load_cargo.rs | |||
@@ -8,8 +8,7 @@ use crossbeam_channel::{unbounded, Receiver}; | |||
8 | use ra_db::{ExternSourceId, FileId, SourceRootId}; | 8 | use ra_db::{ExternSourceId, FileId, SourceRootId}; |
9 | use ra_ide::{AnalysisChange, AnalysisHost}; | 9 | use ra_ide::{AnalysisChange, AnalysisHost}; |
10 | use ra_project_model::{ | 10 | use ra_project_model::{ |
11 | get_rustc_cfg_options, CargoConfig, PackageRoot, ProcMacroClient, ProjectManifest, | 11 | CargoConfig, PackageRoot, ProcMacroClient, ProjectManifest, ProjectWorkspace, |
12 | ProjectWorkspace, | ||
13 | }; | 12 | }; |
14 | use ra_vfs::{RootEntry, Vfs, VfsChange, VfsTask, Watch}; | 13 | use ra_vfs::{RootEntry, Vfs, VfsChange, VfsTask, Watch}; |
15 | use rustc_hash::{FxHashMap, FxHashSet}; | 14 | use rustc_hash::{FxHashMap, FxHashSet}; |
@@ -148,25 +147,14 @@ pub(crate) fn load( | |||
148 | } | 147 | } |
149 | } | 148 | } |
150 | 149 | ||
151 | // FIXME: cfg options? | 150 | let crate_graph = |
152 | let default_cfg_options = { | 151 | ws.to_crate_graph(None, &extern_source_roots, proc_macro_client, &mut |path: &Path| { |
153 | let mut opts = get_rustc_cfg_options(None); | ||
154 | opts.insert_atom("debug_assertion".into()); | ||
155 | opts | ||
156 | }; | ||
157 | |||
158 | let crate_graph = ws.to_crate_graph( | ||
159 | &default_cfg_options, | ||
160 | &extern_source_roots, | ||
161 | proc_macro_client, | ||
162 | &mut |path: &Path| { | ||
163 | // Some path from metadata will be non canonicalized, e.g. /foo/../bar/lib.rs | 152 | // Some path from metadata will be non canonicalized, e.g. /foo/../bar/lib.rs |
164 | let path = path.canonicalize().ok()?; | 153 | let path = path.canonicalize().ok()?; |
165 | let vfs_file = vfs.load(&path); | 154 | let vfs_file = vfs.load(&path); |
166 | log::debug!("vfs file {:?} -> {:?}", path, vfs_file); | 155 | log::debug!("vfs file {:?} -> {:?}", path, vfs_file); |
167 | vfs_file.map(vfs_file_to_id) | 156 | vfs_file.map(vfs_file_to_id) |
168 | }, | 157 | }); |
169 | ); | ||
170 | log::debug!("crate graph: {:?}", crate_graph); | 158 | log::debug!("crate graph: {:?}", crate_graph); |
171 | analysis_change.set_crate_graph(crate_graph); | 159 | analysis_change.set_crate_graph(crate_graph); |
172 | 160 | ||
diff --git a/crates/rust-analyzer/src/global_state.rs b/crates/rust-analyzer/src/global_state.rs index 4d871aa34..0b52030cf 100644 --- a/crates/rust-analyzer/src/global_state.rs +++ b/crates/rust-analyzer/src/global_state.rs | |||
@@ -15,7 +15,7 @@ use ra_flycheck::{Flycheck, FlycheckConfig}; | |||
15 | use ra_ide::{ | 15 | use ra_ide::{ |
16 | Analysis, AnalysisChange, AnalysisHost, CrateGraph, FileId, LibraryData, SourceRootId, | 16 | Analysis, AnalysisChange, AnalysisHost, CrateGraph, FileId, LibraryData, SourceRootId, |
17 | }; | 17 | }; |
18 | use ra_project_model::{get_rustc_cfg_options, ProcMacroClient, ProjectWorkspace}; | 18 | use ra_project_model::{ProcMacroClient, ProjectWorkspace}; |
19 | use ra_vfs::{LineEndings, RootEntry, Vfs, VfsChange, VfsFile, VfsRoot, VfsTask, Watch}; | 19 | use ra_vfs::{LineEndings, RootEntry, Vfs, VfsChange, VfsFile, VfsRoot, VfsTask, Watch}; |
20 | use relative_path::RelativePathBuf; | 20 | use relative_path::RelativePathBuf; |
21 | use stdx::format_to; | 21 | use stdx::format_to; |
@@ -135,13 +135,6 @@ impl GlobalState { | |||
135 | } | 135 | } |
136 | } | 136 | } |
137 | 137 | ||
138 | // FIXME: Read default cfgs from config | ||
139 | let default_cfg_options = { | ||
140 | let mut opts = get_rustc_cfg_options(config.cargo.target.as_ref()); | ||
141 | opts.insert_atom("debug_assertion".into()); | ||
142 | opts | ||
143 | }; | ||
144 | |||
145 | let proc_macro_client = match &config.proc_macro_srv { | 138 | let proc_macro_client = match &config.proc_macro_srv { |
146 | None => ProcMacroClient::dummy(), | 139 | None => ProcMacroClient::dummy(), |
147 | Some((path, args)) => match ProcMacroClient::extern_process(path.into(), args) { | 140 | Some((path, args)) => match ProcMacroClient::extern_process(path.into(), args) { |
@@ -167,7 +160,7 @@ impl GlobalState { | |||
167 | }; | 160 | }; |
168 | for ws in workspaces.iter() { | 161 | for ws in workspaces.iter() { |
169 | crate_graph.extend(ws.to_crate_graph( | 162 | crate_graph.extend(ws.to_crate_graph( |
170 | &default_cfg_options, | 163 | config.cargo.target.as_ref(), |
171 | &extern_source_roots, | 164 | &extern_source_roots, |
172 | &proc_macro_client, | 165 | &proc_macro_client, |
173 | &mut load, | 166 | &mut load, |