From 355419d4044704d13a902641d86ad5501af8714d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 8 Oct 2019 14:22:49 +0300 Subject: [PATCH] use slightly more idiomatic api for cfg --- crates/ra_batch/src/lib.rs | 8 +++++-- crates/ra_cfg/src/lib.rs | 24 ++++++++------------ crates/ra_hir/src/nameres/tests.rs | 14 +++++++----- crates/ra_hir/src/ty/tests.rs | 6 ++++- crates/ra_ide_api/src/lib.rs | 3 ++- crates/ra_lsp_server/src/world.rs | 8 +++++-- crates/ra_project_model/src/lib.rs | 36 +++++++++++++++++++----------- 7 files changed, 59 insertions(+), 40 deletions(-) diff --git a/crates/ra_batch/src/lib.rs b/crates/ra_batch/src/lib.rs index a5fc2a23e1..602beb4394 100644 --- a/crates/ra_batch/src/lib.rs +++ b/crates/ra_batch/src/lib.rs @@ -43,8 +43,12 @@ pub fn load_cargo(root: &Path) -> Result<(AnalysisHost, FxHashMap CfgOptions { - self.atoms.insert(name); - self + pub fn insert_atom(&mut self, key: SmolStr) { + self.atoms.insert(key); } - pub fn key_value(mut self, key: SmolStr, value: SmolStr) -> CfgOptions { + pub fn remove_atom(&mut self, name: &str) { + self.atoms.remove(name); + } + + pub fn insert_key_value(&mut self, key: SmolStr, value: SmolStr) { self.key_values.insert((key, value)); - self } /// Shortcut to set features - pub fn features(mut self, iter: impl IntoIterator) -> CfgOptions { - for feat in iter { - self = self.key_value("feature".into(), feat); - } - self - } - - pub fn remove_atom(mut self, name: &SmolStr) -> CfgOptions { - self.atoms.remove(name); - self + pub fn insert_features(&mut self, iter: impl IntoIterator) { + iter.into_iter().for_each(|feat| self.insert_key_value("feature".into(), feat)); } } diff --git a/crates/ra_hir/src/nameres/tests.rs b/crates/ra_hir/src/nameres/tests.rs index 34dd795743..208c2f16f0 100644 --- a/crates/ra_hir/src/nameres/tests.rs +++ b/crates/ra_hir/src/nameres/tests.rs @@ -561,12 +561,14 @@ fn cfg_test() { "#, crate_graph! { "main": ("/main.rs", ["std"]), - "std": ("/lib.rs", [], CfgOptions::default() - .atom("test".into()) - .key_value("feature".into(), "foo".into()) - .key_value("feature".into(), "bar".into()) - .key_value("opt".into(), "42".into()) - ), + "std": ("/lib.rs", [], { + let mut opts = CfgOptions::default(); + opts.insert_atom("test".into()); + opts.insert_key_value("feature".into(), "foo".into()); + opts.insert_key_value("feature".into(), "bar".into()); + opts.insert_key_value("opt".into(), "42".into()); + opts + }), }, ); diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs index 25dad81eb7..b16b482581 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs @@ -62,7 +62,11 @@ impl S { "#, ); db.set_crate_graph_from_fixture(crate_graph! { - "main": ("/main.rs", ["foo"], CfgOptions::default().atom("test".into())), + "main": ("/main.rs", ["foo"], { + let mut opts = CfgOptions::default(); + opts.insert_atom("test".into()); + opts + }), "foo": ("/foo.rs", []), }); assert_eq!("(i32, {unknown}, i32, {unknown})", type_at_pos(&db, pos)); diff --git a/crates/ra_ide_api/src/lib.rs b/crates/ra_ide_api/src/lib.rs index 24f1b91f68..2d92fe1c50 100644 --- a/crates/ra_ide_api/src/lib.rs +++ b/crates/ra_ide_api/src/lib.rs @@ -325,7 +325,8 @@ impl Analysis { let file_id = FileId(0); // FIXME: cfg options // Default to enable test for single file. - let cfg_options = CfgOptions::default().atom("test".into()); + let mut cfg_options = CfgOptions::default(); + cfg_options.insert_atom("test".into()); crate_graph.add_crate_root(file_id, Edition::Edition2018, cfg_options); change.add_file(source_root, file_id, "main.rs".into(), Arc::new(text)); change.set_crate_graph(crate_graph); diff --git a/crates/ra_lsp_server/src/world.rs b/crates/ra_lsp_server/src/world.rs index 27da751ab4..0eb684de5d 100644 --- a/crates/ra_lsp_server/src/world.rs +++ b/crates/ra_lsp_server/src/world.rs @@ -98,8 +98,12 @@ impl WorldState { } // FIXME: Read default cfgs from config - let default_cfg_options = - get_rustc_cfg_options().atom("test".into()).atom("debug_assertion".into()); + let default_cfg_options = { + let mut opts = get_rustc_cfg_options(); + opts.insert_atom("test".into()); + opts.insert_atom("debug_assertion".into()); + opts + }; // Create crate graph from all the workspaces let mut crate_graph = CrateGraph::default(); diff --git a/crates/ra_project_model/src/lib.rs b/crates/ra_project_model/src/lib.rs index 640a5ebd32..a4d117e34f 100644 --- a/crates/ra_project_model/src/lib.rs +++ b/crates/ra_project_model/src/lib.rs @@ -134,13 +134,16 @@ impl ProjectWorkspace { json_project::Edition::Edition2015 => Edition::Edition2015, json_project::Edition::Edition2018 => Edition::Edition2018, }; - 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()); - } + let cfg_options = { + let mut opts = default_cfg_options.clone(); + for name in &krate.atom_cfgs { + opts.insert_atom(name.into()); + } + for (key, value) in &krate.key_value_cfgs { + opts.insert_key_value(key.into(), value.into()); + } + opts + }; crates.insert( crate_id, crate_graph.add_crate_root(file_id, edition, cfg_options), @@ -171,7 +174,12 @@ impl ProjectWorkspace { for krate in sysroot.crates() { if let Some(file_id) = load(krate.root(&sysroot)) { // Crates from sysroot have `cfg(test)` disabled - let cfg_options = default_cfg_options.clone().remove_atom(&"test".into()); + let cfg_options = { + let mut opts = default_cfg_options.clone(); + opts.remove_atom("test"); + opts + }; + let crate_id = crate_graph.add_crate_root(file_id, Edition::Edition2018, cfg_options); sysroot_crates.insert(krate, crate_id); @@ -202,9 +210,11 @@ impl ProjectWorkspace { let root = tgt.root(&cargo); if let Some(file_id) = load(root) { let edition = pkg.edition(&cargo); - let cfg_options = default_cfg_options - .clone() - .features(pkg.features(&cargo).iter().map(Into::into)); + let cfg_options = { + let mut opts = default_cfg_options.clone(); + opts.insert_features(pkg.features(&cargo).iter().map(Into::into)); + opts + }; let crate_id = crate_graph.add_crate_root(file_id, edition, cfg_options); names.insert(crate_id, pkg.name(&cargo).to_string()); @@ -321,11 +331,11 @@ pub fn get_rustc_cfg_options() -> CfgOptions { Ok(rustc_cfgs) => { for line in rustc_cfgs.lines() { match line.find('=') { - None => cfg_options = cfg_options.atom(line.into()), + None => cfg_options.insert_atom(line.into()), Some(pos) => { let key = &line[..pos]; let value = line[pos + 1..].trim_matches('"'); - cfg_options = cfg_options.key_value(key.into(), value.into()); + cfg_options.insert_key_value(key.into(), value.into()); } } }