diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-10-05 15:25:59 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2019-10-05 15:25:59 +0100 |
commit | ae6305b90c80eb919cfde985cba66975b6222ed2 (patch) | |
tree | de601daf3714c4bda937e7cad05d048b69d16e71 /crates/ra_db/src | |
parent | dbf869b4d2dac09df17609edf6e67c1611b71dc5 (diff) | |
parent | c6303d9fee98232ac83a77f943c39d65c9c6b6db (diff) |
Merge #1928
1928: Support `#[cfg(..)]` r=matklad a=oxalica
This PR implement `#[cfg(..)]` conditional compilation. It read default cfg options from `rustc --print cfg` with also hard-coded `test` and `debug_assertion` enabled.
Front-end settings are **not** included in this PR.
There is also a known issue that inner control attributes are totally ignored. I think it is **not** a part of `cfg` and create a separated issue for it. #1949
Fixes #1920
Related: #1073
Co-authored-by: uHOOCCOOHu <[email protected]>
Co-authored-by: oxalica <[email protected]>
Diffstat (limited to 'crates/ra_db/src')
-rw-r--r-- | crates/ra_db/src/input.rs | 33 |
1 files changed, 22 insertions, 11 deletions
diff --git a/crates/ra_db/src/input.rs b/crates/ra_db/src/input.rs index 52f892891..23148096c 100644 --- a/crates/ra_db/src/input.rs +++ b/crates/ra_db/src/input.rs | |||
@@ -9,6 +9,7 @@ | |||
9 | use relative_path::{RelativePath, RelativePathBuf}; | 9 | use relative_path::{RelativePath, RelativePathBuf}; |
10 | use rustc_hash::FxHashMap; | 10 | use rustc_hash::FxHashMap; |
11 | 11 | ||
12 | use ra_cfg::CfgOptions; | ||
12 | use ra_syntax::SmolStr; | 13 | use ra_syntax::SmolStr; |
13 | use rustc_hash::FxHashSet; | 14 | use rustc_hash::FxHashSet; |
14 | 15 | ||
@@ -109,11 +110,12 @@ struct CrateData { | |||
109 | file_id: FileId, | 110 | file_id: FileId, |
110 | edition: Edition, | 111 | edition: Edition, |
111 | dependencies: Vec<Dependency>, | 112 | dependencies: Vec<Dependency>, |
113 | cfg_options: CfgOptions, | ||
112 | } | 114 | } |
113 | 115 | ||
114 | impl CrateData { | 116 | impl CrateData { |
115 | fn new(file_id: FileId, edition: Edition) -> CrateData { | 117 | fn new(file_id: FileId, edition: Edition, cfg_options: CfgOptions) -> CrateData { |
116 | CrateData { file_id, edition, dependencies: Vec::new() } | 118 | CrateData { file_id, edition, dependencies: Vec::new(), cfg_options } |
117 | } | 119 | } |
118 | 120 | ||
119 | fn add_dep(&mut self, name: SmolStr, crate_id: CrateId) { | 121 | fn add_dep(&mut self, name: SmolStr, crate_id: CrateId) { |
@@ -134,13 +136,22 @@ impl Dependency { | |||
134 | } | 136 | } |
135 | 137 | ||
136 | impl CrateGraph { | 138 | impl CrateGraph { |
137 | pub fn add_crate_root(&mut self, file_id: FileId, edition: Edition) -> CrateId { | 139 | pub fn add_crate_root( |
140 | &mut self, | ||
141 | file_id: FileId, | ||
142 | edition: Edition, | ||
143 | cfg_options: CfgOptions, | ||
144 | ) -> CrateId { | ||
138 | let crate_id = CrateId(self.arena.len() as u32); | 145 | let crate_id = CrateId(self.arena.len() as u32); |
139 | let prev = self.arena.insert(crate_id, CrateData::new(file_id, edition)); | 146 | let prev = self.arena.insert(crate_id, CrateData::new(file_id, edition, cfg_options)); |
140 | assert!(prev.is_none()); | 147 | assert!(prev.is_none()); |
141 | crate_id | 148 | crate_id |
142 | } | 149 | } |
143 | 150 | ||
151 | pub fn cfg_options(&self, crate_id: CrateId) -> &CfgOptions { | ||
152 | &self.arena[&crate_id].cfg_options | ||
153 | } | ||
154 | |||
144 | pub fn add_dep( | 155 | pub fn add_dep( |
145 | &mut self, | 156 | &mut self, |
146 | from: CrateId, | 157 | from: CrateId, |
@@ -221,14 +232,14 @@ impl CrateGraph { | |||
221 | 232 | ||
222 | #[cfg(test)] | 233 | #[cfg(test)] |
223 | mod tests { | 234 | mod tests { |
224 | use super::{CrateGraph, Edition::Edition2018, FileId, SmolStr}; | 235 | use super::{CfgOptions, CrateGraph, Edition::Edition2018, FileId, SmolStr}; |
225 | 236 | ||
226 | #[test] | 237 | #[test] |
227 | fn it_should_panic_because_of_cycle_dependencies() { | 238 | fn it_should_panic_because_of_cycle_dependencies() { |
228 | let mut graph = CrateGraph::default(); | 239 | let mut graph = CrateGraph::default(); |
229 | let crate1 = graph.add_crate_root(FileId(1u32), Edition2018); | 240 | let crate1 = graph.add_crate_root(FileId(1u32), Edition2018, CfgOptions::default()); |
230 | let crate2 = graph.add_crate_root(FileId(2u32), Edition2018); | 241 | let crate2 = graph.add_crate_root(FileId(2u32), Edition2018, CfgOptions::default()); |
231 | let crate3 = graph.add_crate_root(FileId(3u32), Edition2018); | 242 | let crate3 = graph.add_crate_root(FileId(3u32), Edition2018, CfgOptions::default()); |
232 | assert!(graph.add_dep(crate1, SmolStr::new("crate2"), crate2).is_ok()); | 243 | assert!(graph.add_dep(crate1, SmolStr::new("crate2"), crate2).is_ok()); |
233 | assert!(graph.add_dep(crate2, SmolStr::new("crate3"), crate3).is_ok()); | 244 | assert!(graph.add_dep(crate2, SmolStr::new("crate3"), crate3).is_ok()); |
234 | assert!(graph.add_dep(crate3, SmolStr::new("crate1"), crate1).is_err()); | 245 | assert!(graph.add_dep(crate3, SmolStr::new("crate1"), crate1).is_err()); |
@@ -237,9 +248,9 @@ mod tests { | |||
237 | #[test] | 248 | #[test] |
238 | fn it_works() { | 249 | fn it_works() { |
239 | let mut graph = CrateGraph::default(); | 250 | let mut graph = CrateGraph::default(); |
240 | let crate1 = graph.add_crate_root(FileId(1u32), Edition2018); | 251 | let crate1 = graph.add_crate_root(FileId(1u32), Edition2018, CfgOptions::default()); |
241 | let crate2 = graph.add_crate_root(FileId(2u32), Edition2018); | 252 | let crate2 = graph.add_crate_root(FileId(2u32), Edition2018, CfgOptions::default()); |
242 | let crate3 = graph.add_crate_root(FileId(3u32), Edition2018); | 253 | let crate3 = graph.add_crate_root(FileId(3u32), Edition2018, CfgOptions::default()); |
243 | assert!(graph.add_dep(crate1, SmolStr::new("crate2"), crate2).is_ok()); | 254 | assert!(graph.add_dep(crate1, SmolStr::new("crate2"), crate2).is_ok()); |
244 | assert!(graph.add_dep(crate2, SmolStr::new("crate3"), crate3).is_ok()); | 255 | assert!(graph.add_dep(crate2, SmolStr::new("crate3"), crate3).is_ok()); |
245 | } | 256 | } |