From 2c48fa087b6cc79ebfd81af9daf4a07d8019fd67 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 22 Nov 2019 13:55:03 +0300 Subject: Add support for environment to CrateGraph --- crates/ra_db/src/fixture.rs | 19 +++++++++++++++---- crates/ra_db/src/input.rs | 34 ++++++++++++++++++++++++---------- crates/ra_db/src/lib.rs | 2 +- 3 files changed, 40 insertions(+), 15 deletions(-) (limited to 'crates/ra_db/src') diff --git a/crates/ra_db/src/fixture.rs b/crates/ra_db/src/fixture.rs index ade187629..e8f335e33 100644 --- a/crates/ra_db/src/fixture.rs +++ b/crates/ra_db/src/fixture.rs @@ -8,7 +8,7 @@ use rustc_hash::FxHashMap; use test_utils::{extract_offset, parse_fixture, CURSOR_MARKER}; use crate::{ - CrateGraph, CrateId, Edition, FileId, FilePosition, RelativePathBuf, SourceDatabaseExt, + CrateGraph, CrateId, Edition, Env, FileId, FilePosition, RelativePathBuf, SourceDatabaseExt, SourceRoot, SourceRootId, }; @@ -53,7 +53,12 @@ fn with_single_file(db: &mut dyn SourceDatabaseExt, text: &str) -> FileId { source_root.insert_file(rel_path.clone(), file_id); let mut crate_graph = CrateGraph::default(); - crate_graph.add_crate_root(file_id, Edition::Edition2018, CfgOptions::default()); + crate_graph.add_crate_root( + file_id, + Edition::Edition2018, + CfgOptions::default(), + Env::default(), + ); db.set_file_text(file_id, Arc::new(text.to_string())); db.set_file_relative_path(file_id, rel_path); @@ -93,7 +98,8 @@ fn with_files(db: &mut dyn SourceDatabaseExt, fixture: &str) -> Option Option, +} + #[derive(Debug, Clone, PartialEq, Eq)] struct CrateData { file_id: FileId, edition: Edition, dependencies: Vec, cfg_options: CfgOptions, + env: Env, } impl CrateData { - fn new(file_id: FileId, edition: Edition, cfg_options: CfgOptions) -> CrateData { - CrateData { file_id, edition, dependencies: Vec::new(), cfg_options } + fn new(file_id: FileId, edition: Edition, cfg_options: CfgOptions, env: Env) -> CrateData { + CrateData { file_id, edition, dependencies: Vec::new(), cfg_options, env } } fn add_dep(&mut self, name: SmolStr, crate_id: CrateId) { @@ -150,9 +156,11 @@ impl CrateGraph { file_id: FileId, edition: Edition, cfg_options: CfgOptions, + env: Env, ) -> CrateId { + let data = CrateData::new(file_id, edition, cfg_options, env); let crate_id = CrateId(self.arena.len() as u32); - let prev = self.arena.insert(crate_id, CrateData::new(file_id, edition, cfg_options)); + let prev = self.arena.insert(crate_id, data); assert!(prev.is_none()); crate_id } @@ -241,14 +249,17 @@ impl CrateGraph { #[cfg(test)] mod tests { - use super::{CfgOptions, CrateGraph, Edition::Edition2018, FileId, SmolStr}; + use super::{CfgOptions, CrateGraph, Edition::Edition2018, Env, FileId, SmolStr}; #[test] fn it_should_panic_because_of_cycle_dependencies() { let mut graph = CrateGraph::default(); - let crate1 = graph.add_crate_root(FileId(1u32), Edition2018, CfgOptions::default()); - let crate2 = graph.add_crate_root(FileId(2u32), Edition2018, CfgOptions::default()); - let crate3 = graph.add_crate_root(FileId(3u32), Edition2018, CfgOptions::default()); + let crate1 = + graph.add_crate_root(FileId(1u32), Edition2018, CfgOptions::default(), Env::default()); + let crate2 = + graph.add_crate_root(FileId(2u32), Edition2018, CfgOptions::default(), Env::default()); + let crate3 = + graph.add_crate_root(FileId(3u32), Edition2018, CfgOptions::default(), Env::default()); assert!(graph.add_dep(crate1, SmolStr::new("crate2"), crate2).is_ok()); assert!(graph.add_dep(crate2, SmolStr::new("crate3"), crate3).is_ok()); assert!(graph.add_dep(crate3, SmolStr::new("crate1"), crate1).is_err()); @@ -257,9 +268,12 @@ mod tests { #[test] fn it_works() { let mut graph = CrateGraph::default(); - let crate1 = graph.add_crate_root(FileId(1u32), Edition2018, CfgOptions::default()); - let crate2 = graph.add_crate_root(FileId(2u32), Edition2018, CfgOptions::default()); - let crate3 = graph.add_crate_root(FileId(3u32), Edition2018, CfgOptions::default()); + let crate1 = + graph.add_crate_root(FileId(1u32), Edition2018, CfgOptions::default(), Env::default()); + let crate2 = + graph.add_crate_root(FileId(2u32), Edition2018, CfgOptions::default(), Env::default()); + let crate3 = + graph.add_crate_root(FileId(3u32), Edition2018, CfgOptions::default(), Env::default()); assert!(graph.add_dep(crate1, SmolStr::new("crate2"), crate2).is_ok()); assert!(graph.add_dep(crate2, SmolStr::new("crate3"), crate3).is_ok()); } diff --git a/crates/ra_db/src/lib.rs b/crates/ra_db/src/lib.rs index b6bfd531d..f9d012cb0 100644 --- a/crates/ra_db/src/lib.rs +++ b/crates/ra_db/src/lib.rs @@ -10,7 +10,7 @@ use ra_syntax::{ast, Parse, SourceFile, TextRange, TextUnit}; pub use crate::{ cancellation::Canceled, - input::{CrateGraph, CrateId, Dependency, Edition, FileId, SourceRoot, SourceRootId}, + input::{CrateGraph, CrateId, Dependency, Edition, Env, FileId, SourceRoot, SourceRootId}, }; pub use relative_path::{RelativePath, RelativePathBuf}; pub use salsa; -- cgit v1.2.3 From 0861ac061ef5c8b5d7ed2a95ef30f4a17710229d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 22 Nov 2019 14:08:18 +0300 Subject: Cleanup errors --- crates/ra_db/src/input.rs | 46 ++++++++++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 18 deletions(-) (limited to 'crates/ra_db/src') diff --git a/crates/ra_db/src/input.rs b/crates/ra_db/src/input.rs index 0015d6b5e..777e63442 100644 --- a/crates/ra_db/src/input.rs +++ b/crates/ra_db/src/input.rs @@ -6,14 +6,14 @@ //! actual IO. See `vfs` and `project_model` in the `ra_lsp_server` crate for how //! actual IO is done and lowered to input. -use rustc_hash::FxHashMap; +use std::{fmt, str::FromStr}; use ra_cfg::CfgOptions; use ra_syntax::SmolStr; +use rustc_hash::FxHashMap; use rustc_hash::FxHashSet; use crate::{RelativePath, RelativePathBuf}; -use std::str::FromStr; /// `FileId` is an integer which uniquely identifies a file. File paths are /// messy and system-dependent, so most of the code should work directly with @@ -98,22 +98,6 @@ pub enum Edition { Edition2015, } -#[derive(Debug)] -pub struct ParseEditionError { - pub msg: String, -} - -impl FromStr for Edition { - type Err = ParseEditionError; - fn from_str(s: &str) -> Result { - match s { - "2015" => Ok(Edition::Edition2015), - "2018" => Ok(Edition::Edition2018), - _ => Err(ParseEditionError { msg: format!("unknown edition: {}", s) }), - } - } -} - #[derive(Default, Debug, Clone, PartialEq, Eq)] pub struct Env { entries: FxHashMap, @@ -247,6 +231,32 @@ impl CrateGraph { } } +#[derive(Debug)] +pub struct ParseEditionError { + invalid_input: String, +} + +impl FromStr for Edition { + type Err = ParseEditionError; + + fn from_str(s: &str) -> Result { + let res = match s { + "2015" => Edition::Edition2015, + "2018" => Edition::Edition2018, + _ => Err(ParseEditionError { invalid_input: s.to_string() })?, + }; + Ok(res) + } +} + +impl fmt::Display for ParseEditionError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "invalid edition: {:?}", self.invalid_input) + } +} + +impl std::error::Error for ParseEditionError {} + #[cfg(test)] mod tests { use super::{CfgOptions, CrateGraph, Edition::Edition2018, Env, FileId, SmolStr}; -- cgit v1.2.3 From f4b1fb1554b639374adeffa50d4719f834a0d475 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 22 Nov 2019 14:12:45 +0300 Subject: Reorder --- crates/ra_db/src/input.rs | 74 +++++++++++++++++++++++------------------------ 1 file changed, 37 insertions(+), 37 deletions(-) (limited to 'crates/ra_db/src') diff --git a/crates/ra_db/src/input.rs b/crates/ra_db/src/input.rs index 777e63442..b6d851776 100644 --- a/crates/ra_db/src/input.rs +++ b/crates/ra_db/src/input.rs @@ -80,16 +80,16 @@ pub struct CrateGraph { arena: FxHashMap, } -#[derive(Debug)] -pub struct CyclicDependencies; - #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct CrateId(pub u32); -impl CrateId { - pub fn shift(self, amount: u32) -> CrateId { - CrateId(self.0 + amount) - } +#[derive(Debug, Clone, PartialEq, Eq)] +struct CrateData { + file_id: FileId, + edition: Edition, + cfg_options: CfgOptions, + env: Env, + dependencies: Vec, } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] @@ -103,37 +103,12 @@ pub struct Env { entries: FxHashMap, } -#[derive(Debug, Clone, PartialEq, Eq)] -struct CrateData { - file_id: FileId, - edition: Edition, - dependencies: Vec, - cfg_options: CfgOptions, - env: Env, -} - -impl CrateData { - fn new(file_id: FileId, edition: Edition, cfg_options: CfgOptions, env: Env) -> CrateData { - CrateData { file_id, edition, dependencies: Vec::new(), cfg_options, env } - } - - fn add_dep(&mut self, name: SmolStr, crate_id: CrateId) { - self.dependencies.push(Dependency { name, crate_id }) - } -} - #[derive(Debug, Clone, PartialEq, Eq)] pub struct Dependency { pub crate_id: CrateId, pub name: SmolStr, } -impl Dependency { - pub fn crate_id(&self) -> CrateId { - self.crate_id - } -} - impl CrateGraph { pub fn add_crate_root( &mut self, @@ -158,9 +133,9 @@ impl CrateGraph { from: CrateId, name: SmolStr, to: CrateId, - ) -> Result<(), CyclicDependencies> { + ) -> Result<(), CyclicDependenciesError> { if self.dfs_find(from, to, &mut FxHashSet::default()) { - return Err(CyclicDependencies); + return Err(CyclicDependenciesError); } self.arena.get_mut(&from).unwrap().add_dep(name, to); Ok(()) @@ -231,9 +206,20 @@ impl CrateGraph { } } -#[derive(Debug)] -pub struct ParseEditionError { - invalid_input: String, +impl CrateId { + pub fn shift(self, amount: u32) -> CrateId { + CrateId(self.0 + amount) + } +} + +impl CrateData { + fn new(file_id: FileId, edition: Edition, cfg_options: CfgOptions, env: Env) -> CrateData { + CrateData { file_id, edition, dependencies: Vec::new(), cfg_options, env } + } + + fn add_dep(&mut self, name: SmolStr, crate_id: CrateId) { + self.dependencies.push(Dependency { name, crate_id }) + } } impl FromStr for Edition { @@ -249,6 +235,17 @@ impl FromStr for Edition { } } +impl Dependency { + pub fn crate_id(&self) -> CrateId { + self.crate_id + } +} + +#[derive(Debug)] +pub struct ParseEditionError { + invalid_input: String, +} + impl fmt::Display for ParseEditionError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "invalid edition: {:?}", self.invalid_input) @@ -257,6 +254,9 @@ impl fmt::Display for ParseEditionError { impl std::error::Error for ParseEditionError {} +#[derive(Debug)] +pub struct CyclicDependenciesError; + #[cfg(test)] mod tests { use super::{CfgOptions, CrateGraph, Edition::Edition2018, Env, FileId, SmolStr}; -- cgit v1.2.3 From 53506a755220557a558be4da244b70d79f5b0fa9 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 24 Nov 2019 14:13:51 +0300 Subject: Pull macro up --- crates/ra_db/src/lib.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'crates/ra_db/src') diff --git a/crates/ra_db/src/lib.rs b/crates/ra_db/src/lib.rs index f9d012cb0..e8852531b 100644 --- a/crates/ra_db/src/lib.rs +++ b/crates/ra_db/src/lib.rs @@ -15,6 +15,20 @@ pub use crate::{ pub use relative_path::{RelativePath, RelativePathBuf}; pub use salsa; +#[macro_export] +macro_rules! impl_intern_key { + ($name:ident) => { + impl $crate::salsa::InternKey for $name { + fn from_intern_id(v: $crate::salsa::InternId) -> Self { + $name(v) + } + fn as_intern_id(&self) -> $crate::salsa::InternId { + self.0 + } + } + }; +} + pub trait CheckCanceled { /// Aborts current query if there are pending changes. /// -- cgit v1.2.3