aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_db/src/input.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_db/src/input.rs')
-rw-r--r--crates/ra_db/src/input.rs48
1 files changed, 30 insertions, 18 deletions
diff --git a/crates/ra_db/src/input.rs b/crates/ra_db/src/input.rs
index a1ace61b6..23148096c 100644
--- a/crates/ra_db/src/input.rs
+++ b/crates/ra_db/src/input.rs
@@ -1,13 +1,15 @@
1/// This module specifies the input to rust-analyzer. In some sense, this is 1//! This module specifies the input to rust-analyzer. In some sense, this is
2/// **the** most important module, because all other fancy stuff is strictly 2//! **the** most important module, because all other fancy stuff is strictly
3/// derived from this input. 3//! derived from this input.
4/// 4//!
5/// Note that neither this module, nor any other part of the analyzer's core do 5//! Note that neither this module, nor any other part of the analyzer's core do
6/// actual IO. See `vfs` and `project_model` in the `ra_lsp_server` crate for how 6//! actual IO. See `vfs` and `project_model` in the `ra_lsp_server` crate for how
7/// actual IO is done and lowered to input. 7//! actual IO is done and lowered to input.
8
8use relative_path::{RelativePath, RelativePathBuf}; 9use relative_path::{RelativePath, RelativePathBuf};
9use rustc_hash::FxHashMap; 10use rustc_hash::FxHashMap;
10 11
12use ra_cfg::CfgOptions;
11use ra_syntax::SmolStr; 13use ra_syntax::SmolStr;
12use rustc_hash::FxHashSet; 14use rustc_hash::FxHashSet;
13 15
@@ -108,11 +110,12 @@ struct CrateData {
108 file_id: FileId, 110 file_id: FileId,
109 edition: Edition, 111 edition: Edition,
110 dependencies: Vec<Dependency>, 112 dependencies: Vec<Dependency>,
113 cfg_options: CfgOptions,
111} 114}
112 115
113impl CrateData { 116impl CrateData {
114 fn new(file_id: FileId, edition: Edition) -> CrateData { 117 fn new(file_id: FileId, edition: Edition, cfg_options: CfgOptions) -> CrateData {
115 CrateData { file_id, edition, dependencies: Vec::new() } 118 CrateData { file_id, edition, dependencies: Vec::new(), cfg_options }
116 } 119 }
117 120
118 fn add_dep(&mut self, name: SmolStr, crate_id: CrateId) { 121 fn add_dep(&mut self, name: SmolStr, crate_id: CrateId) {
@@ -133,13 +136,22 @@ impl Dependency {
133} 136}
134 137
135impl CrateGraph { 138impl CrateGraph {
136 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 {
137 let crate_id = CrateId(self.arena.len() as u32); 145 let crate_id = CrateId(self.arena.len() as u32);
138 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));
139 assert!(prev.is_none()); 147 assert!(prev.is_none());
140 crate_id 148 crate_id
141 } 149 }
142 150
151 pub fn cfg_options(&self, crate_id: CrateId) -> &CfgOptions {
152 &self.arena[&crate_id].cfg_options
153 }
154
143 pub fn add_dep( 155 pub fn add_dep(
144 &mut self, 156 &mut self,
145 from: CrateId, 157 from: CrateId,
@@ -220,14 +232,14 @@ impl CrateGraph {
220 232
221#[cfg(test)] 233#[cfg(test)]
222mod tests { 234mod tests {
223 use super::{CrateGraph, Edition::Edition2018, FileId, SmolStr}; 235 use super::{CfgOptions, CrateGraph, Edition::Edition2018, FileId, SmolStr};
224 236
225 #[test] 237 #[test]
226 fn it_should_panic_because_of_cycle_dependencies() { 238 fn it_should_panic_because_of_cycle_dependencies() {
227 let mut graph = CrateGraph::default(); 239 let mut graph = CrateGraph::default();
228 let crate1 = graph.add_crate_root(FileId(1u32), Edition2018); 240 let crate1 = graph.add_crate_root(FileId(1u32), Edition2018, CfgOptions::default());
229 let crate2 = graph.add_crate_root(FileId(2u32), Edition2018); 241 let crate2 = graph.add_crate_root(FileId(2u32), Edition2018, CfgOptions::default());
230 let crate3 = graph.add_crate_root(FileId(3u32), Edition2018); 242 let crate3 = graph.add_crate_root(FileId(3u32), Edition2018, CfgOptions::default());
231 assert!(graph.add_dep(crate1, SmolStr::new("crate2"), crate2).is_ok()); 243 assert!(graph.add_dep(crate1, SmolStr::new("crate2"), crate2).is_ok());
232 assert!(graph.add_dep(crate2, SmolStr::new("crate3"), crate3).is_ok()); 244 assert!(graph.add_dep(crate2, SmolStr::new("crate3"), crate3).is_ok());
233 assert!(graph.add_dep(crate3, SmolStr::new("crate1"), crate1).is_err()); 245 assert!(graph.add_dep(crate3, SmolStr::new("crate1"), crate1).is_err());
@@ -236,9 +248,9 @@ mod tests {
236 #[test] 248 #[test]
237 fn it_works() { 249 fn it_works() {
238 let mut graph = CrateGraph::default(); 250 let mut graph = CrateGraph::default();
239 let crate1 = graph.add_crate_root(FileId(1u32), Edition2018); 251 let crate1 = graph.add_crate_root(FileId(1u32), Edition2018, CfgOptions::default());
240 let crate2 = graph.add_crate_root(FileId(2u32), Edition2018); 252 let crate2 = graph.add_crate_root(FileId(2u32), Edition2018, CfgOptions::default());
241 let crate3 = graph.add_crate_root(FileId(3u32), Edition2018); 253 let crate3 = graph.add_crate_root(FileId(3u32), Edition2018, CfgOptions::default());
242 assert!(graph.add_dep(crate1, SmolStr::new("crate2"), crate2).is_ok()); 254 assert!(graph.add_dep(crate1, SmolStr::new("crate2"), crate2).is_ok());
243 assert!(graph.add_dep(crate2, SmolStr::new("crate3"), crate3).is_ok()); 255 assert!(graph.add_dep(crate2, SmolStr::new("crate3"), crate3).is_ok());
244 } 256 }