Skip to content

Commit

Permalink
Expose symbol of CrateName
Browse files Browse the repository at this point in the history
  • Loading branch information
Veykril committed Feb 4, 2025
1 parent 4d9be70 commit a373fb2
Show file tree
Hide file tree
Showing 14 changed files with 33 additions and 34 deletions.
10 changes: 5 additions & 5 deletions src/tools/rust-analyzer/crates/base-db/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,9 @@ impl fmt::Display for CrateName {
}

impl ops::Deref for CrateName {
type Target = str;
fn deref(&self) -> &str {
self.0.as_str()
type Target = Symbol;
fn deref(&self) -> &Symbol {
&self.0
}
}

Expand Down Expand Up @@ -230,8 +230,8 @@ impl fmt::Display for CrateDisplayName {
}

impl ops::Deref for CrateDisplayName {
type Target = str;
fn deref(&self) -> &str {
type Target = Symbol;
fn deref(&self) -> &Symbol {
&self.crate_name
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/tools/rust-analyzer/crates/hir-def/src/import_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ mod tests {
crate_graph[krate]
.display_name
.as_ref()
.is_some_and(|it| &**it.crate_name() == crate_name)
.is_some_and(|it| it.crate_name().as_str() == crate_name)
})
.expect("could not find crate");

Expand Down
2 changes: 1 addition & 1 deletion src/tools/rust-analyzer/crates/hir-def/src/nameres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ impl DefMap {
pub(crate) fn crate_def_map_query(db: &dyn DefDatabase, crate_id: CrateId) -> Arc<DefMap> {
let crate_graph = db.crate_graph();
let krate = &crate_graph[crate_id];
let name = krate.display_name.as_deref().unwrap_or_default();
let name = krate.display_name.as_deref().map(Symbol::as_str).unwrap_or_default();
let _p = tracing::info_span!("crate_def_map_query", ?name).entered();

let module_data = ModuleData::new(
Expand Down
2 changes: 1 addition & 1 deletion src/tools/rust-analyzer/crates/hir-expand/src/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,6 @@ impl AsName for ast::FieldKind {

impl AsName for base_db::Dependency {
fn as_name(&self) -> Name {
Name::new_root(&self.name)
Name::new_symbol_root((*self.name).clone())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ pub fn prettify_macro_expansion(
} else if let Some(dep) =
target_crate.dependencies.iter().find(|dep| dep.crate_id == macro_def_crate)
{
make::tokens::ident(&dep.name)
make::tokens::ident(dep.name.as_str())
} else if let Some(crate_name) = &crate_graph[macro_def_crate].display_name {
make::tokens::ident(crate_name.crate_name())
make::tokens::ident(crate_name.crate_name().as_str())
} else {
return dollar_crate.clone();
}
Expand Down
13 changes: 8 additions & 5 deletions src/tools/rust-analyzer/crates/ide-db/src/prime_caches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod topologic_sort;

use std::time::Duration;

use hir::db::DefDatabase;
use hir::{db::DefDatabase, Symbol};
use itertools::Itertools;

use crate::{
Expand All @@ -22,7 +22,7 @@ use crate::{
#[derive(Debug)]
pub struct ParallelPrimeCachesProgress {
/// the crates that we are currently priming.
pub crates_currently_indexing: Vec<String>,
pub crates_currently_indexing: Vec<Symbol>,
/// the total number of crates we want to prime.
pub crates_total: usize,
/// the total number of crates that have finished priming
Expand All @@ -49,7 +49,7 @@ pub fn parallel_prime_caches(
};

enum ParallelPrimeCacheWorkerProgress {
BeginCrate { crate_id: CrateId, crate_name: String },
BeginCrate { crate_id: CrateId, crate_name: Symbol },
EndCrate { crate_id: CrateId },
}

Expand Down Expand Up @@ -112,7 +112,11 @@ pub fn parallel_prime_caches(

for crate_id in &mut crates_to_prime {
let krate = &graph[crate_id];
let name = krate.display_name.as_deref().unwrap_or_default().to_owned();
let name = krate
.display_name
.as_deref()
.cloned()
.unwrap_or_else(|| Symbol::integer(crate_id.into_raw().into_u32() as usize));
if krate.origin.is_lang() {
additional_phases.push((crate_id, name.clone(), PrimingPhase::ImportMap));
} else if krate.origin.is_local() {
Expand Down Expand Up @@ -195,7 +199,6 @@ pub fn parallel_prime_caches(
}
ParallelPrimeCacheWorkerProgress::EndCrate { crate_id } => {
crates_currently_indexing.swap_remove(&crate_id);
crates_to_prime.mark_done(crate_id);
crates_done += 1;
}
};
Expand Down
3 changes: 2 additions & 1 deletion src/tools/rust-analyzer/crates/ide/src/view_crate_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ impl<'a> dot::Labeller<'a, CrateId, Edge<'a>> for DotCrateGraph {
}

fn node_label(&'a self, n: &CrateId) -> LabelText<'a> {
let name = self.graph[*n].display_name.as_ref().map_or("(unnamed crate)", |name| name);
let name =
self.graph[*n].display_name.as_ref().map_or("(unnamed crate)", |name| name.as_str());
LabelText::LabelStr(name.into())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,7 @@ define_symbols! {
u64,
u8,
unadjusted,
unknown,
Unknown,
unpin,
unreachable_2015,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -508,5 +508,5 @@ fn serialize_crate_name<S>(name: &CrateName, se: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
se.serialize_str(name)
se.serialize_str(name.as_str())
}
2 changes: 1 addition & 1 deletion src/tools/rust-analyzer/crates/project-model/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ fn rust_project_is_proc_macro_has_proc_macro_dep() {
let crate_data = &crate_graph[crate_id];
// Assert that the project crate with `is_proc_macro` has a dependency
// on the proc_macro sysroot crate.
crate_data.dependencies.iter().find(|&dep| dep.name.deref() == "proc_macro").unwrap();
crate_data.dependencies.iter().find(|&dep| *dep.name.deref() == sym::proc_macro).unwrap();
}

#[test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use project_model::{CargoConfig, RustLibSource};
use rustc_hash::FxHashSet;

use hir::{db::HirDatabase, Crate, HirFileIdExt, Module};
use hir::{db::HirDatabase, sym, Crate, HirFileIdExt, Module};
use ide::{AnalysisHost, AssistResolveStrategy, Diagnostic, DiagnosticsConfig, Severity};
use ide_db::{base_db::SourceRootDatabase, LineIndexDatabase};
use load_cargo::{load_workspace_at, LoadCargoConfig, ProcMacroServerChoice};
Expand Down Expand Up @@ -60,7 +60,7 @@ impl flags::Diagnostics {
let file_id = module.definition_source_file_id(db).original_file(db);
if !visited_files.contains(&file_id) {
let crate_name =
module.krate().display_name(db).as_deref().unwrap_or("unknown").to_owned();
module.krate().display_name(db).as_deref().unwrap_or(&sym::unknown).to_owned();
println!(
"processing crate: {crate_name}, module: {}",
_vfs.file_path(file_id.into())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Reports references in code that the IDE layer cannot resolve.
use hir::{db::HirDatabase, AnyDiagnostic, Crate, HirFileIdExt as _, Module, Semantics};
use hir::{db::HirDatabase, sym, AnyDiagnostic, Crate, HirFileIdExt as _, Module, Semantics};
use ide::{AnalysisHost, RootDatabase, TextRange};
use ide_db::{
base_db::{SourceDatabase, SourceRootDatabase},
Expand Down Expand Up @@ -66,7 +66,7 @@ impl flags::UnresolvedReferences {
let file_id = module.definition_source_file_id(db).original_file(db);
if !visited_files.contains(&file_id) {
let crate_name =
module.krate().display_name(db).as_deref().unwrap_or("unknown").to_owned();
module.krate().display_name(db).as_deref().unwrap_or(&sym::unknown).to_owned();
let file_path = vfs.file_path(file_id.into());
eprintln!("processing crate: {crate_name}, module: {file_path}",);

Expand Down
8 changes: 5 additions & 3 deletions src/tools/rust-analyzer/crates/rust-analyzer/src/main_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,14 +339,16 @@ impl GlobalState {

message = match &*report.crates_currently_indexing {
[crate_name] => Some(format!(
"{}/{} ({crate_name})",
report.crates_done, report.crates_total
"{}/{} ({})",
report.crates_done,
report.crates_total,
crate_name.as_str(),
)),
[crate_name, rest @ ..] => Some(format!(
"{}/{} ({} + {} more)",
report.crates_done,
report.crates_total,
crate_name,
crate_name.as_str(),
rest.len()
)),
_ => None,
Expand Down
10 changes: 1 addition & 9 deletions src/tools/rust-analyzer/crates/test-fixture/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,15 +258,7 @@ impl ChangeFixture {
let to_id = crates[&to];
let sysroot = crate_graph[to_id].origin.is_lang();
crate_graph
.add_dep(
from_id,
Dependency::with_prelude(
CrateName::new(&to).unwrap(),
to_id,
prelude,
sysroot,
),
)
.add_dep(from_id, Dependency::with_prelude(to.clone(), to_id, prelude, sysroot))
.unwrap();
}
}
Expand Down

0 comments on commit a373fb2

Please sign in to comment.