diff options
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_batch/src/lib.rs | 8 | ||||
-rw-r--r-- | crates/ra_cfg/src/lib.rs | 24 | ||||
-rw-r--r-- | crates/ra_hir/src/mock.rs | 19 | ||||
-rw-r--r-- | crates/ra_hir/src/nameres/tests.rs | 13 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/tests.rs | 3 | ||||
-rw-r--r-- | crates/ra_ide_api/src/lib.rs | 3 | ||||
-rw-r--r-- | crates/ra_ide_api/src/snapshots/highlighting.html | 2 | ||||
-rw-r--r-- | crates/ra_ide_api/src/syntax_highlighting.rs | 3 | ||||
-rw-r--r-- | crates/ra_lsp_server/src/world.rs | 8 | ||||
-rw-r--r-- | crates/ra_project_model/src/lib.rs | 44 |
10 files changed, 82 insertions, 45 deletions
diff --git a/crates/ra_batch/src/lib.rs b/crates/ra_batch/src/lib.rs index a5fc2a23e..602beb439 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<SourceRootId, | |||
43 | ); | 43 | ); |
44 | 44 | ||
45 | // FIXME: cfg options? | 45 | // FIXME: cfg options? |
46 | let default_cfg_options = | 46 | let default_cfg_options = { |
47 | get_rustc_cfg_options().atom("test".into()).atom("debug_assertion".into()); | 47 | let mut opts = get_rustc_cfg_options(); |
48 | opts.insert_atom("test".into()); | ||
49 | opts.insert_atom("debug_assertion".into()); | ||
50 | opts | ||
51 | }; | ||
48 | 52 | ||
49 | let (crate_graph, _crate_names) = | 53 | let (crate_graph, _crate_names) = |
50 | ws.to_crate_graph(&default_cfg_options, &mut |path: &Path| { | 54 | ws.to_crate_graph(&default_cfg_options, &mut |path: &Path| { |
diff --git a/crates/ra_cfg/src/lib.rs b/crates/ra_cfg/src/lib.rs index e1c92fbba..1bee3eb99 100644 --- a/crates/ra_cfg/src/lib.rs +++ b/crates/ra_cfg/src/lib.rs | |||
@@ -36,26 +36,20 @@ impl CfgOptions { | |||
36 | self.check(&parse_cfg(attr)) | 36 | self.check(&parse_cfg(attr)) |
37 | } | 37 | } |
38 | 38 | ||
39 | pub fn atom(mut self, name: SmolStr) -> CfgOptions { | 39 | pub fn insert_atom(&mut self, key: SmolStr) { |
40 | self.atoms.insert(name); | 40 | self.atoms.insert(key); |
41 | self | ||
42 | } | 41 | } |
43 | 42 | ||
44 | pub fn key_value(mut self, key: SmolStr, value: SmolStr) -> CfgOptions { | 43 | pub fn remove_atom(&mut self, name: &str) { |
45 | self.key_values.insert((key, value)); | 44 | self.atoms.remove(name); |
46 | self | ||
47 | } | 45 | } |
48 | 46 | ||
49 | /// Shortcut to set features | 47 | pub fn insert_key_value(&mut self, key: SmolStr, value: SmolStr) { |
50 | pub fn features(mut self, iter: impl IntoIterator<Item = SmolStr>) -> CfgOptions { | 48 | self.key_values.insert((key, value)); |
51 | for feat in iter { | ||
52 | self = self.key_value("feature".into(), feat); | ||
53 | } | ||
54 | self | ||
55 | } | 49 | } |
56 | 50 | ||
57 | pub fn remove_atom(mut self, name: &SmolStr) -> CfgOptions { | 51 | /// Shortcut to set features |
58 | self.atoms.remove(name); | 52 | pub fn insert_features(&mut self, iter: impl IntoIterator<Item = SmolStr>) { |
59 | self | 53 | iter.into_iter().for_each(|feat| self.insert_key_value("feature".into(), feat)); |
60 | } | 54 | } |
61 | } | 55 | } |
diff --git a/crates/ra_hir/src/mock.rs b/crates/ra_hir/src/mock.rs index f750986b8..827424983 100644 --- a/crates/ra_hir/src/mock.rs +++ b/crates/ra_hir/src/mock.rs | |||
@@ -278,7 +278,10 @@ macro_rules! crate_graph { | |||
278 | $crate_path:literal, | 278 | $crate_path:literal, |
279 | $($edition:literal,)? | 279 | $($edition:literal,)? |
280 | [$($dep:literal),*] | 280 | [$($dep:literal),*] |
281 | $(,$cfg:expr)? | 281 | $(, cfg = { |
282 | $($key:literal $(= $value:literal)?),* | ||
283 | $(,)? | ||
284 | })? | ||
282 | ), | 285 | ), |
283 | )*) => {{ | 286 | )*) => {{ |
284 | let mut res = $crate::mock::CrateGraphFixture::default(); | 287 | let mut res = $crate::mock::CrateGraphFixture::default(); |
@@ -286,7 +289,19 @@ macro_rules! crate_graph { | |||
286 | #[allow(unused_mut, unused_assignments)] | 289 | #[allow(unused_mut, unused_assignments)] |
287 | let mut edition = ra_db::Edition::Edition2018; | 290 | let mut edition = ra_db::Edition::Edition2018; |
288 | $(edition = ra_db::Edition::from_string($edition);)? | 291 | $(edition = ra_db::Edition::from_string($edition);)? |
289 | let cfg_options = { ::ra_cfg::CfgOptions::default() $(; $cfg)? }; | 292 | let cfg_options = { |
293 | #[allow(unused_mut)] | ||
294 | let mut cfg = ::ra_cfg::CfgOptions::default(); | ||
295 | $( | ||
296 | $( | ||
297 | if 0 == 0 $(+ { drop($value); 1})? { | ||
298 | cfg.insert_atom($key.into()); | ||
299 | } | ||
300 | $(cfg.insert_key_value($key.into(), $value.into());)? | ||
301 | )* | ||
302 | )? | ||
303 | cfg | ||
304 | }; | ||
290 | res.0.push(( | 305 | res.0.push(( |
291 | $crate_name.to_string(), | 306 | $crate_name.to_string(), |
292 | ($crate_path.to_string(), edition, cfg_options, vec![$($dep.to_string()),*]) | 307 | ($crate_path.to_string(), edition, cfg_options, vec![$($dep.to_string()),*]) |
diff --git a/crates/ra_hir/src/nameres/tests.rs b/crates/ra_hir/src/nameres/tests.rs index 34dd79574..8c6b40aaf 100644 --- a/crates/ra_hir/src/nameres/tests.rs +++ b/crates/ra_hir/src/nameres/tests.rs | |||
@@ -7,7 +7,6 @@ mod mod_resolution; | |||
7 | use std::sync::Arc; | 7 | use std::sync::Arc; |
8 | 8 | ||
9 | use insta::assert_snapshot; | 9 | use insta::assert_snapshot; |
10 | use ra_cfg::CfgOptions; | ||
11 | use ra_db::SourceDatabase; | 10 | use ra_db::SourceDatabase; |
12 | use test_utils::covers; | 11 | use test_utils::covers; |
13 | 12 | ||
@@ -561,12 +560,12 @@ fn cfg_test() { | |||
561 | "#, | 560 | "#, |
562 | crate_graph! { | 561 | crate_graph! { |
563 | "main": ("/main.rs", ["std"]), | 562 | "main": ("/main.rs", ["std"]), |
564 | "std": ("/lib.rs", [], CfgOptions::default() | 563 | "std": ("/lib.rs", [], cfg = { |
565 | .atom("test".into()) | 564 | "test", |
566 | .key_value("feature".into(), "foo".into()) | 565 | "feature" = "foo", |
567 | .key_value("feature".into(), "bar".into()) | 566 | "feature" = "bar", |
568 | .key_value("opt".into(), "42".into()) | 567 | "opt" = "42", |
569 | ), | 568 | }), |
570 | }, | 569 | }, |
571 | ); | 570 | ); |
572 | 571 | ||
diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs index 03b30adcd..c12326643 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs | |||
@@ -3,7 +3,6 @@ use std::sync::Arc; | |||
3 | 3 | ||
4 | use insta::assert_snapshot; | 4 | use insta::assert_snapshot; |
5 | 5 | ||
6 | use ra_cfg::CfgOptions; | ||
7 | use ra_db::{salsa::Database, FilePosition, SourceDatabase}; | 6 | use ra_db::{salsa::Database, FilePosition, SourceDatabase}; |
8 | use ra_syntax::{ | 7 | use ra_syntax::{ |
9 | algo, | 8 | algo, |
@@ -62,7 +61,7 @@ impl S { | |||
62 | "#, | 61 | "#, |
63 | ); | 62 | ); |
64 | db.set_crate_graph_from_fixture(crate_graph! { | 63 | db.set_crate_graph_from_fixture(crate_graph! { |
65 | "main": ("/main.rs", ["foo"], CfgOptions::default().atom("test".into())), | 64 | "main": ("/main.rs", ["foo"], cfg = { "test" }), |
66 | "foo": ("/foo.rs", []), | 65 | "foo": ("/foo.rs", []), |
67 | }); | 66 | }); |
68 | assert_eq!("(i32, {unknown}, i32, {unknown})", type_at_pos(&db, pos)); | 67 | 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 24f1b91f6..2d92fe1c5 100644 --- a/crates/ra_ide_api/src/lib.rs +++ b/crates/ra_ide_api/src/lib.rs | |||
@@ -325,7 +325,8 @@ impl Analysis { | |||
325 | let file_id = FileId(0); | 325 | let file_id = FileId(0); |
326 | // FIXME: cfg options | 326 | // FIXME: cfg options |
327 | // Default to enable test for single file. | 327 | // Default to enable test for single file. |
328 | let cfg_options = CfgOptions::default().atom("test".into()); | 328 | let mut cfg_options = CfgOptions::default(); |
329 | cfg_options.insert_atom("test".into()); | ||
329 | crate_graph.add_crate_root(file_id, Edition::Edition2018, cfg_options); | 330 | crate_graph.add_crate_root(file_id, Edition::Edition2018, cfg_options); |
330 | change.add_file(source_root, file_id, "main.rs".into(), Arc::new(text)); | 331 | change.add_file(source_root, file_id, "main.rs".into(), Arc::new(text)); |
331 | change.set_crate_graph(crate_graph); | 332 | change.set_crate_graph(crate_graph); |
diff --git a/crates/ra_ide_api/src/snapshots/highlighting.html b/crates/ra_ide_api/src/snapshots/highlighting.html index ae30ebba3..b39c4d371 100644 --- a/crates/ra_ide_api/src/snapshots/highlighting.html +++ b/crates/ra_ide_api/src/snapshots/highlighting.html | |||
@@ -19,7 +19,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd | |||
19 | .keyword\.unsafe { color: #DFAF8F; } | 19 | .keyword\.unsafe { color: #DFAF8F; } |
20 | .keyword\.control { color: #F0DFAF; font-weight: bold; } | 20 | .keyword\.control { color: #F0DFAF; font-weight: bold; } |
21 | </style> | 21 | </style> |
22 | <pre><code><span class="attribute">#</span><span class="attribute">[</span><span class="attribute text">derive</span><span class="attribute">(</span><span class="attribute">Clone</span><span class="attribute">,</span><span class="attribute"> </span><span class="attribute">Debug</span><span class="attribute">)</span><span class="attribute">]</span> | 22 | <pre><code><span class="attribute">#</span><span class="attribute">[</span><span class="attribute">derive</span><span class="attribute">(</span><span class="attribute">Clone</span><span class="attribute">,</span><span class="attribute"> </span><span class="attribute">Debug</span><span class="attribute">)</span><span class="attribute">]</span> |
23 | <span class="keyword">struct</span> <span class="type">Foo</span> { | 23 | <span class="keyword">struct</span> <span class="type">Foo</span> { |
24 | <span class="keyword">pub</span> <span class="field">x</span>: <span class="type">i32</span>, | 24 | <span class="keyword">pub</span> <span class="field">x</span>: <span class="type">i32</span>, |
25 | <span class="keyword">pub</span> <span class="field">y</span>: <span class="type">i32</span>, | 25 | <span class="keyword">pub</span> <span class="field">y</span>: <span class="type">i32</span>, |
diff --git a/crates/ra_ide_api/src/syntax_highlighting.rs b/crates/ra_ide_api/src/syntax_highlighting.rs index 9ae2dc061..1d290387c 100644 --- a/crates/ra_ide_api/src/syntax_highlighting.rs +++ b/crates/ra_ide_api/src/syntax_highlighting.rs | |||
@@ -97,6 +97,9 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRa | |||
97 | STRING | RAW_STRING | RAW_BYTE_STRING | BYTE_STRING => "string", | 97 | STRING | RAW_STRING | RAW_BYTE_STRING | BYTE_STRING => "string", |
98 | ATTR => "attribute", | 98 | ATTR => "attribute", |
99 | NAME_REF => { | 99 | NAME_REF => { |
100 | if node.ancestors().any(|it| it.kind() == ATTR) { | ||
101 | continue; | ||
102 | } | ||
100 | if let Some(name_ref) = node.as_node().cloned().and_then(ast::NameRef::cast) { | 103 | if let Some(name_ref) = node.as_node().cloned().and_then(ast::NameRef::cast) { |
101 | // FIXME: try to reuse the SourceAnalyzers | 104 | // FIXME: try to reuse the SourceAnalyzers |
102 | let analyzer = hir::SourceAnalyzer::new(db, file_id, name_ref.syntax(), None); | 105 | let analyzer = hir::SourceAnalyzer::new(db, file_id, name_ref.syntax(), None); |
diff --git a/crates/ra_lsp_server/src/world.rs b/crates/ra_lsp_server/src/world.rs index 27da751ab..0eb684de5 100644 --- a/crates/ra_lsp_server/src/world.rs +++ b/crates/ra_lsp_server/src/world.rs | |||
@@ -98,8 +98,12 @@ impl WorldState { | |||
98 | } | 98 | } |
99 | 99 | ||
100 | // FIXME: Read default cfgs from config | 100 | // FIXME: Read default cfgs from config |
101 | let default_cfg_options = | 101 | let default_cfg_options = { |
102 | get_rustc_cfg_options().atom("test".into()).atom("debug_assertion".into()); | 102 | let mut opts = get_rustc_cfg_options(); |
103 | opts.insert_atom("test".into()); | ||
104 | opts.insert_atom("debug_assertion".into()); | ||
105 | opts | ||
106 | }; | ||
103 | 107 | ||
104 | // Create crate graph from all the workspaces | 108 | // Create crate graph from all the workspaces |
105 | let mut crate_graph = CrateGraph::default(); | 109 | 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 640a5ebd3..8b8663a78 100644 --- a/crates/ra_project_model/src/lib.rs +++ b/crates/ra_project_model/src/lib.rs | |||
@@ -134,13 +134,16 @@ impl ProjectWorkspace { | |||
134 | json_project::Edition::Edition2015 => Edition::Edition2015, | 134 | json_project::Edition::Edition2015 => Edition::Edition2015, |
135 | json_project::Edition::Edition2018 => Edition::Edition2018, | 135 | json_project::Edition::Edition2018 => Edition::Edition2018, |
136 | }; | 136 | }; |
137 | let mut cfg_options = default_cfg_options.clone(); | 137 | let cfg_options = { |
138 | for name in &krate.atom_cfgs { | 138 | let mut opts = default_cfg_options.clone(); |
139 | cfg_options = cfg_options.atom(name.into()); | 139 | for name in &krate.atom_cfgs { |
140 | } | 140 | opts.insert_atom(name.into()); |
141 | for (key, value) in &krate.key_value_cfgs { | 141 | } |
142 | cfg_options = cfg_options.key_value(key.into(), value.into()); | 142 | for (key, value) in &krate.key_value_cfgs { |
143 | } | 143 | opts.insert_key_value(key.into(), value.into()); |
144 | } | ||
145 | opts | ||
146 | }; | ||
144 | crates.insert( | 147 | crates.insert( |
145 | crate_id, | 148 | crate_id, |
146 | crate_graph.add_crate_root(file_id, edition, cfg_options), | 149 | crate_graph.add_crate_root(file_id, edition, cfg_options), |
@@ -171,7 +174,12 @@ impl ProjectWorkspace { | |||
171 | for krate in sysroot.crates() { | 174 | for krate in sysroot.crates() { |
172 | if let Some(file_id) = load(krate.root(&sysroot)) { | 175 | if let Some(file_id) = load(krate.root(&sysroot)) { |
173 | // Crates from sysroot have `cfg(test)` disabled | 176 | // Crates from sysroot have `cfg(test)` disabled |
174 | let cfg_options = default_cfg_options.clone().remove_atom(&"test".into()); | 177 | let cfg_options = { |
178 | let mut opts = default_cfg_options.clone(); | ||
179 | opts.remove_atom("test"); | ||
180 | opts | ||
181 | }; | ||
182 | |||
175 | let crate_id = | 183 | let crate_id = |
176 | crate_graph.add_crate_root(file_id, Edition::Edition2018, cfg_options); | 184 | crate_graph.add_crate_root(file_id, Edition::Edition2018, cfg_options); |
177 | sysroot_crates.insert(krate, crate_id); | 185 | sysroot_crates.insert(krate, crate_id); |
@@ -202,9 +210,11 @@ impl ProjectWorkspace { | |||
202 | let root = tgt.root(&cargo); | 210 | let root = tgt.root(&cargo); |
203 | if let Some(file_id) = load(root) { | 211 | if let Some(file_id) = load(root) { |
204 | let edition = pkg.edition(&cargo); | 212 | let edition = pkg.edition(&cargo); |
205 | let cfg_options = default_cfg_options | 213 | let cfg_options = { |
206 | .clone() | 214 | let mut opts = default_cfg_options.clone(); |
207 | .features(pkg.features(&cargo).iter().map(Into::into)); | 215 | opts.insert_features(pkg.features(&cargo).iter().map(Into::into)); |
216 | opts | ||
217 | }; | ||
208 | let crate_id = | 218 | let crate_id = |
209 | crate_graph.add_crate_root(file_id, edition, cfg_options); | 219 | crate_graph.add_crate_root(file_id, edition, cfg_options); |
210 | names.insert(crate_id, pkg.name(&cargo).to_string()); | 220 | names.insert(crate_id, pkg.name(&cargo).to_string()); |
@@ -310,6 +320,14 @@ fn find_cargo_toml(path: &Path) -> Result<PathBuf> { | |||
310 | pub fn get_rustc_cfg_options() -> CfgOptions { | 320 | pub fn get_rustc_cfg_options() -> CfgOptions { |
311 | let mut cfg_options = CfgOptions::default(); | 321 | let mut cfg_options = CfgOptions::default(); |
312 | 322 | ||
323 | // Some nightly-only cfgs, which are required for stdlib | ||
324 | { | ||
325 | cfg_options.insert_atom("target_thread_local".into()); | ||
326 | for &target_has_atomic in ["16", "32", "64", "8", "cas", "ptr"].iter() { | ||
327 | cfg_options.insert_key_value("target_has_atomic".into(), target_has_atomic.into()) | ||
328 | } | ||
329 | } | ||
330 | |||
313 | match (|| -> Result<_> { | 331 | match (|| -> Result<_> { |
314 | // `cfg(test)` and `cfg(debug_assertion)` are handled outside, so we suppress them here. | 332 | // `cfg(test)` and `cfg(debug_assertion)` are handled outside, so we suppress them here. |
315 | let output = Command::new("rustc").args(&["--print", "cfg", "-O"]).output()?; | 333 | let output = Command::new("rustc").args(&["--print", "cfg", "-O"]).output()?; |
@@ -321,11 +339,11 @@ pub fn get_rustc_cfg_options() -> CfgOptions { | |||
321 | Ok(rustc_cfgs) => { | 339 | Ok(rustc_cfgs) => { |
322 | for line in rustc_cfgs.lines() { | 340 | for line in rustc_cfgs.lines() { |
323 | match line.find('=') { | 341 | match line.find('=') { |
324 | None => cfg_options = cfg_options.atom(line.into()), | 342 | None => cfg_options.insert_atom(line.into()), |
325 | Some(pos) => { | 343 | Some(pos) => { |
326 | let key = &line[..pos]; | 344 | let key = &line[..pos]; |
327 | let value = line[pos + 1..].trim_matches('"'); | 345 | let value = line[pos + 1..].trim_matches('"'); |
328 | cfg_options = cfg_options.key_value(key.into(), value.into()); | 346 | cfg_options.insert_key_value(key.into(), value.into()); |
329 | } | 347 | } |
330 | } | 348 | } |
331 | } | 349 | } |