Use CrateName for semantic names

This commit is contained in:
Aleksey Kladov 2020-07-01 09:53:53 +02:00
parent e336a96998
commit 307c6fec61
5 changed files with 30 additions and 14 deletions

View file

@ -94,6 +94,13 @@ impl fmt::Display for CrateName {
} }
} }
impl ops::Deref for CrateName {
type Target = str;
fn deref(&self) -> &Self::Target {
&*self.0
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct ProcMacroId(pub u32); pub struct ProcMacroId(pub u32);
@ -138,7 +145,7 @@ pub struct Env {
#[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: CrateName,
} }
impl CrateGraph { impl CrateGraph {
@ -178,7 +185,7 @@ impl CrateGraph {
if self.dfs_find(from, to, &mut FxHashSet::default()) { if self.dfs_find(from, to, &mut FxHashSet::default()) {
return Err(CyclicDependenciesError); return Err(CyclicDependenciesError);
} }
self.arena.get_mut(&from).unwrap().add_dep(name.0, to); self.arena.get_mut(&from).unwrap().add_dep(name, to);
Ok(()) Ok(())
} }
@ -247,7 +254,7 @@ impl CrateId {
} }
impl CrateData { impl CrateData {
fn add_dep(&mut self, name: SmolStr, crate_id: CrateId) { fn add_dep(&mut self, name: CrateName, crate_id: CrateId) {
self.dependencies.push(Dependency { name, crate_id }) self.dependencies.push(Dependency { name, crate_id })
} }
} }
@ -429,7 +436,10 @@ mod tests {
.is_ok()); .is_ok());
assert_eq!( assert_eq!(
graph[crate1].dependencies, graph[crate1].dependencies,
vec![Dependency { crate_id: crate2, name: "crate_name_with_dashes".into() }] vec![Dependency {
crate_id: crate2,
name: CrateName::new("crate_name_with_dashes").unwrap()
}]
); );
} }
} }

View file

@ -161,7 +161,7 @@ fn find_builtin_crate(db: &dyn AstDatabase, id: LazyMacroId) -> tt::TokenTree {
// XXX // XXX
// All crates except core itself should have a dependency on core, // All crates except core itself should have a dependency on core,
// We detect `core` by seeing whether it doesn't have such a dependency. // We detect `core` by seeing whether it doesn't have such a dependency.
let tt = if cg[krate].dependencies.iter().any(|dep| dep.name == "core") { let tt = if cg[krate].dependencies.iter().any(|dep| &*dep.name == "core") {
quote! { core } quote! { core }
} else { } else {
quote! { crate } quote! { crate }

View file

@ -117,7 +117,7 @@ impl AsName for ast::FieldKind {
impl AsName for ra_db::Dependency { impl AsName for ra_db::Dependency {
fn as_name(&self) -> Name { fn as_name(&self) -> Name {
Name::new_text(self.name.clone()) Name::new_text(SmolStr::new(&*self.name))
} }
} }

View file

@ -288,10 +288,7 @@ impl ProjectWorkspace {
if let (Some(&from), Some(&to)) = if let (Some(&from), Some(&to)) =
(crates.get(&from_crate_id), crates.get(&to_crate_id)) (crates.get(&from_crate_id), crates.get(&to_crate_id))
{ {
if crate_graph if crate_graph.add_dep(from, dep.name.clone(), to).is_err() {
.add_dep(from, CrateName::new(&dep.name).unwrap(), to)
.is_err()
{
log::error!( log::error!(
"cyclic dependency {:?} -> {:?}", "cyclic dependency {:?} -> {:?}",
from_crate_id, from_crate_id,

View file

@ -4,9 +4,9 @@ use std::path::PathBuf;
use paths::{AbsPath, AbsPathBuf}; use paths::{AbsPath, AbsPathBuf};
use ra_cfg::CfgOptions; use ra_cfg::CfgOptions;
use ra_db::{CrateId, Dependency, Edition}; use ra_db::{CrateId, CrateName, Dependency, Edition};
use rustc_hash::FxHashSet; use rustc_hash::FxHashSet;
use serde::Deserialize; use serde::{de, Deserialize};
use stdx::split_delim; use stdx::split_delim;
/// Roots and crates that compose this Rust project. /// Roots and crates that compose this Rust project.
@ -50,7 +50,7 @@ impl ProjectJson {
.into_iter() .into_iter()
.map(|dep_data| Dependency { .map(|dep_data| Dependency {
crate_id: CrateId(dep_data.krate as u32), crate_id: CrateId(dep_data.krate as u32),
name: dep_data.name.into(), name: dep_data.name,
}) })
.collect::<Vec<_>>(), .collect::<Vec<_>>(),
cfg: { cfg: {
@ -113,5 +113,14 @@ struct DepData {
/// Identifies a crate by position in the crates array. /// Identifies a crate by position in the crates array.
#[serde(rename = "crate")] #[serde(rename = "crate")]
krate: usize, krate: usize,
name: String, #[serde(deserialize_with = "deserialize_crate_name")]
name: CrateName,
}
fn deserialize_crate_name<'de, D>(de: D) -> Result<CrateName, D::Error>
where
D: de::Deserializer<'de>,
{
let name = String::deserialize(de)?;
CrateName::new(&name).map_err(|err| de::Error::custom(format!("invalid crate name: {:?}", err)))
} }