Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions crates/ra_db/src/fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,17 @@ fn with_files(
let crate_id = crate_graph.add_crate_root(
file_id,
meta.edition,
Some(CrateName::new(&krate).unwrap()),
Some(krate.clone()),
meta.cfg,
meta.env,
Default::default(),
);
let prev = crates.insert(krate.clone(), crate_id);
let crate_name = CrateName::new(&krate).unwrap();
let prev = crates.insert(crate_name.clone(), crate_id);
assert!(prev.is_none());
for dep in meta.deps {
crate_deps.push((krate.clone(), dep))
let dep = CrateName::new(&dep).unwrap();
crate_deps.push((crate_name.clone(), dep))
}
} else if meta.path == "/main.rs" || meta.path == "/lib.rs" {
assert!(default_crate_root.is_none());
Expand Down
24 changes: 17 additions & 7 deletions crates/ra_db/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub struct CrateGraph {
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct CrateId(pub u32);

#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct CrateName(SmolStr);

impl CrateName {
Expand All @@ -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)]
pub struct ProcMacroId(pub u32);

Expand All @@ -117,7 +124,7 @@ pub struct CrateData {
/// The name to display to the end user.
/// This actual crate name can be different in a particular dependent crate
/// or may even be missing for some cases, such as a dummy crate for the code snippet.
pub display_name: Option<CrateName>,
pub display_name: Option<String>,
pub cfg_options: CfgOptions,
pub env: Env,
pub dependencies: Vec<Dependency>,
Expand All @@ -138,15 +145,15 @@ pub struct Env {
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Dependency {
pub crate_id: CrateId,
pub name: SmolStr,
pub name: CrateName,
}

impl CrateGraph {
pub fn add_crate_root(
&mut self,
file_id: FileId,
edition: Edition,
display_name: Option<CrateName>,
display_name: Option<String>,
cfg_options: CfgOptions,
env: Env,
proc_macro: Vec<(SmolStr, Arc<dyn ra_tt::TokenExpander>)>,
Expand Down Expand Up @@ -178,7 +185,7 @@ impl CrateGraph {
if self.dfs_find(from, to, &mut FxHashSet::default()) {
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(())
}

Expand Down Expand Up @@ -247,7 +254,7 @@ impl CrateId {
}

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 })
}
}
Expand Down Expand Up @@ -429,7 +436,10 @@ mod tests {
.is_ok());
assert_eq!(
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()
}]
);
}
}
6 changes: 3 additions & 3 deletions crates/ra_hir/src/code_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use hir_ty::{
ApplicationTy, Canonical, GenericPredicate, InEnvironment, Substs, TraitEnvironment, Ty,
TyDefId, TypeCtor,
};
use ra_db::{CrateId, CrateName, Edition, FileId};
use ra_db::{CrateId, Edition, FileId};
use ra_prof::profile;
use ra_syntax::ast::{self, AttrsOwner, NameOwner};
use rustc_hash::FxHashSet;
Expand Down Expand Up @@ -94,8 +94,8 @@ impl Crate {
db.crate_graph()[self.id].edition
}

pub fn display_name(self, db: &dyn HirDatabase) -> Option<CrateName> {
db.crate_graph()[self.id].display_name.as_ref().cloned()
pub fn display_name(self, db: &dyn HirDatabase) -> Option<String> {
db.crate_graph()[self.id].display_name.clone()
}

pub fn query_external_importables(
Expand Down
2 changes: 1 addition & 1 deletion crates/ra_hir_expand/src/builtin_derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ fn find_builtin_crate(db: &dyn AstDatabase, id: LazyMacroId) -> tt::TokenTree {
// XXX
// All crates except core itself should have a dependency on core,
// 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 }
} else {
quote! { crate }
Expand Down
2 changes: 1 addition & 1 deletion crates/ra_hir_expand/src/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ impl AsName for ast::FieldKind {

impl AsName for ra_db::Dependency {
fn as_name(&self) -> Name {
Name::new_text(self.name.clone())
Name::new_text(SmolStr::new(&*self.name))
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/ra_ide/src/mock_analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ impl MockAnalysis {
let other_crate = crate_graph.add_crate_root(
file_id,
edition,
Some(CrateName::new(crate_name).unwrap()),
Some(crate_name.to_string()),
cfg,
env,
Default::default(),
Expand Down
13 changes: 4 additions & 9 deletions crates/ra_project_model/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,10 +288,7 @@ impl ProjectWorkspace {
if let (Some(&from), Some(&to)) =
(crates.get(&from_crate_id), crates.get(&to_crate_id))
{
if crate_graph
.add_dep(from, CrateName::new(&dep.name).unwrap(), to)
.is_err()
{
if crate_graph.add_dep(from, dep.name.clone(), to).is_err() {
log::error!(
"cyclic dependency {:?} -> {:?}",
from_crate_id,
Expand All @@ -312,13 +309,11 @@ impl ProjectWorkspace {

let env = Env::default();
let proc_macro = vec![];
let crate_name = CrateName::new(&sysroot[krate].name)
.expect("Sysroot crate names should not contain dashes");

let name = sysroot[krate].name.clone();
let crate_id = crate_graph.add_crate_root(
file_id,
Edition::Edition2018,
Some(crate_name),
Some(name),
cfg_options.clone(),
env,
proc_macro,
Expand Down Expand Up @@ -392,7 +387,7 @@ impl ProjectWorkspace {
let crate_id = crate_graph.add_crate_root(
file_id,
edition,
Some(CrateName::normalize_dashes(&cargo[pkg].name)),
Some(cargo[pkg].name.clone()),
cfg_options,
env,
proc_macro.clone(),
Expand Down
17 changes: 13 additions & 4 deletions crates/ra_project_model/src/project_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use std::path::PathBuf;

use paths::{AbsPath, AbsPathBuf};
use ra_cfg::CfgOptions;
use ra_db::{CrateId, Dependency, Edition};
use ra_db::{CrateId, CrateName, Dependency, Edition};
use rustc_hash::FxHashSet;
use serde::Deserialize;
use serde::{de, Deserialize};
use stdx::split_delim;

/// Roots and crates that compose this Rust project.
Expand Down Expand Up @@ -50,7 +50,7 @@ impl ProjectJson {
.into_iter()
.map(|dep_data| Dependency {
crate_id: CrateId(dep_data.krate as u32),
name: dep_data.name.into(),
name: dep_data.name,
})
.collect::<Vec<_>>(),
cfg: {
Expand Down Expand Up @@ -113,5 +113,14 @@ struct DepData {
/// Identifies a crate by position in the crates array.
#[serde(rename = "crate")]
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)))
}