2354: Cleanup errors r=matklad a=matklad



Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2019-11-22 11:13:24 +00:00 committed by GitHub
commit a132cc715e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 63 additions and 55 deletions

View File

@ -6,14 +6,14 @@
//! actual IO. See `vfs` and `project_model` in the `ra_lsp_server` crate for how //! actual IO. See `vfs` and `project_model` in the `ra_lsp_server` crate for how
//! actual IO is done and lowered to input. //! actual IO is done and lowered to input.
use rustc_hash::FxHashMap; use std::{fmt, str::FromStr};
use ra_cfg::CfgOptions; use ra_cfg::CfgOptions;
use ra_syntax::SmolStr; use ra_syntax::SmolStr;
use rustc_hash::FxHashMap;
use rustc_hash::FxHashSet; use rustc_hash::FxHashSet;
use crate::{RelativePath, RelativePathBuf}; use crate::{RelativePath, RelativePathBuf};
use std::str::FromStr;
/// `FileId` is an integer which uniquely identifies a file. File paths are /// `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 /// messy and system-dependent, so most of the code should work directly with
@ -80,16 +80,16 @@ pub struct CrateGraph {
arena: FxHashMap<CrateId, CrateData>, arena: FxHashMap<CrateId, CrateData>,
} }
#[derive(Debug)]
pub struct CyclicDependencies;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct CrateId(pub u32); pub struct CrateId(pub u32);
impl CrateId { #[derive(Debug, Clone, PartialEq, Eq)]
pub fn shift(self, amount: u32) -> CrateId { struct CrateData {
CrateId(self.0 + amount) file_id: FileId,
} edition: Edition,
cfg_options: CfgOptions,
env: Env,
dependencies: Vec<Dependency>,
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@ -98,58 +98,17 @@ pub enum Edition {
Edition2015, Edition2015,
} }
#[derive(Debug)]
pub struct ParseEditionError {
pub msg: String,
}
impl FromStr for Edition {
type Err = ParseEditionError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"2015" => Ok(Edition::Edition2015),
"2018" => Ok(Edition::Edition2018),
_ => Err(ParseEditionError { msg: format!("unknown edition: {}", s) }),
}
}
}
#[derive(Default, Debug, Clone, PartialEq, Eq)] #[derive(Default, Debug, Clone, PartialEq, Eq)]
pub struct Env { pub struct Env {
entries: FxHashMap<String, String>, entries: FxHashMap<String, String>,
} }
#[derive(Debug, Clone, PartialEq, Eq)]
struct CrateData {
file_id: FileId,
edition: Edition,
dependencies: Vec<Dependency>,
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)] #[derive(Debug, Clone, PartialEq, Eq)]
pub struct Dependency { pub struct Dependency {
pub crate_id: CrateId, pub crate_id: CrateId,
pub name: SmolStr, pub name: SmolStr,
} }
impl Dependency {
pub fn crate_id(&self) -> CrateId {
self.crate_id
}
}
impl CrateGraph { impl CrateGraph {
pub fn add_crate_root( pub fn add_crate_root(
&mut self, &mut self,
@ -174,9 +133,9 @@ pub fn add_dep(
from: CrateId, from: CrateId,
name: SmolStr, name: SmolStr,
to: CrateId, to: CrateId,
) -> Result<(), CyclicDependencies> { ) -> Result<(), CyclicDependenciesError> {
if self.dfs_find(from, to, &mut FxHashSet::default()) { 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); self.arena.get_mut(&from).unwrap().add_dep(name, to);
Ok(()) Ok(())
@ -247,6 +206,57 @@ fn dfs_find(&self, target: CrateId, from: CrateId, visited: &mut FxHashSet<Crate
} }
} }
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 {
type Err = ParseEditionError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let res = match s {
"2015" => Edition::Edition2015,
"2018" => Edition::Edition2018,
_ => Err(ParseEditionError { invalid_input: s.to_string() })?,
};
Ok(res)
}
}
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)
}
}
impl std::error::Error for ParseEditionError {}
#[derive(Debug)]
pub struct CyclicDependenciesError;
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::{CfgOptions, CrateGraph, Edition::Edition2018, Env, FileId, SmolStr}; use super::{CfgOptions, CrateGraph, Edition::Edition2018, Env, FileId, SmolStr};

View File

@ -1,7 +1,6 @@
//! FIXME: write short doc here //! FIXME: write short doc here
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::str::FromStr;
use cargo_metadata::{CargoOpt, MetadataCommand}; use cargo_metadata::{CargoOpt, MetadataCommand};
use ra_arena::{impl_arena_id, Arena, RawId}; use ra_arena::{impl_arena_id, Arena, RawId};
@ -143,8 +142,7 @@ pub fn from_cargo_metadata(cargo_toml: &Path) -> Result<CargoWorkspace> {
for meta_pkg in meta.packages { for meta_pkg in meta.packages {
let cargo_metadata::Package { id, edition, name, manifest_path, .. } = meta_pkg; let cargo_metadata::Package { id, edition, name, manifest_path, .. } = meta_pkg;
let is_member = ws_members.contains(&id); let is_member = ws_members.contains(&id);
let edition = Edition::from_str(&edition) let edition = edition.parse::<Edition>()?;
.map_err(|e| (format!("metadata for package {} failed: {}", &name, e.msg)))?;
let pkg = packages.alloc(PackageData { let pkg = packages.alloc(PackageData {
name, name,
manifest: manifest_path, manifest: manifest_path,