Skip to content

Commit 2eb1677

Browse files
Merge #3603
3603: Fix crate display name dashes r=matklad a=SomeoneToIgnore A follow-up of #3602 (comment) Co-authored-by: Kirill Bulatov <mail4score@gmail.com>
2 parents 200c275 + 92fd430 commit 2eb1677

File tree

6 files changed

+28
-10
lines changed

6 files changed

+28
-10
lines changed

crates/ra_db/src/fixture.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ fn with_single_file(db: &mut dyn SourceDatabaseExt, ra_fixture: &str) -> FileId
6464
crate_graph.add_crate_root(
6565
file_id,
6666
meta.edition,
67-
meta.krate,
67+
meta.krate.map(|name| {
68+
CrateName::new(&name).expect("Fixture crate name should not contain dashes")
69+
}),
6870
meta.cfg,
6971
meta.env,
7072
Default::default(),
@@ -124,7 +126,7 @@ fn with_files(db: &mut dyn SourceDatabaseExt, fixture: &str) -> Option<FilePosit
124126
let crate_id = crate_graph.add_crate_root(
125127
file_id,
126128
meta.edition,
127-
Some(krate.clone()),
129+
Some(CrateName::new(&krate).unwrap()),
128130
meta.cfg,
129131
meta.env,
130132
Default::default(),

crates/ra_db/src/input.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use rustc_hash::FxHashMap;
1414
use rustc_hash::FxHashSet;
1515

1616
use crate::{RelativePath, RelativePathBuf};
17+
use fmt::Display;
1718

1819
/// `FileId` is an integer which uniquely identifies a file. File paths are
1920
/// messy and system-dependent, so most of the code should work directly with
@@ -83,6 +84,7 @@ pub struct CrateGraph {
8384
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
8485
pub struct CrateId(pub u32);
8586

87+
#[derive(Debug, Clone, PartialEq, Eq)]
8688
pub struct CrateName(SmolStr);
8789

8890
impl CrateName {
@@ -103,14 +105,20 @@ impl CrateName {
103105
}
104106
}
105107

108+
impl Display for CrateName {
109+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
110+
write!(f, "{}", self.0)
111+
}
112+
}
113+
106114
#[derive(Debug, Clone, PartialEq, Eq)]
107115
pub struct CrateData {
108116
pub root_file_id: FileId,
109117
pub edition: Edition,
110118
/// The name to display to the end user.
111119
/// This actual crate name can be different in a particular dependent crate
112120
/// or may even be missing for some cases, such as a dummy crate for the code snippet.
113-
pub display_name: Option<String>,
121+
pub display_name: Option<CrateName>,
114122
pub cfg_options: CfgOptions,
115123
pub env: Env,
116124
pub extern_source: ExternSource,
@@ -150,7 +158,7 @@ impl CrateGraph {
150158
&mut self,
151159
file_id: FileId,
152160
edition: Edition,
153-
display_name: Option<String>,
161+
display_name: Option<CrateName>,
154162
cfg_options: CfgOptions,
155163
env: Env,
156164
extern_source: ExternSource,

crates/ra_hir_def/src/nameres.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,13 @@ pub struct ModuleData {
177177

178178
impl CrateDefMap {
179179
pub(crate) fn crate_def_map_query(db: &impl DefDatabase, krate: CrateId) -> Arc<CrateDefMap> {
180-
let _p = profile("crate_def_map_query")
181-
.detail(|| db.crate_graph()[krate].display_name.clone().unwrap_or_default());
180+
let _p = profile("crate_def_map_query").detail(|| {
181+
db.crate_graph()[krate]
182+
.display_name
183+
.as_ref()
184+
.map(ToString::to_string)
185+
.unwrap_or_default()
186+
});
182187
let def_map = {
183188
let edition = db.crate_graph()[krate].edition;
184189
let mut modules: Arena<LocalModuleId, ModuleData> = Arena::default();

crates/ra_ide/src/hover.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ fn definition_owner_name(db: &RootDatabase, def: &Definition) -> Option<String>
9494

9595
fn determine_mod_path(db: &RootDatabase, def: &Definition) -> Option<String> {
9696
let mod_path = def.module(db).map(|module| {
97-
once(db.crate_graph()[module.krate().into()].display_name.clone())
97+
once(db.crate_graph()[module.krate().into()].display_name.as_ref().map(ToString::to_string))
9898
.chain(
9999
module
100100
.path_to_root(db)

crates/ra_ide/src/mock_analysis.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ impl MockAnalysis {
109109
let other_crate = crate_graph.add_crate_root(
110110
file_id,
111111
Edition2018,
112-
Some(crate_name.to_owned()),
112+
Some(CrateName::new(crate_name).unwrap()),
113113
cfg_options,
114114
Env::default(),
115115
Default::default(),

crates/ra_project_model/src/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,10 @@ impl ProjectWorkspace {
245245
let crate_id = crate_graph.add_crate_root(
246246
file_id,
247247
Edition::Edition2018,
248-
Some(krate.name(&sysroot).to_string()),
248+
Some(
249+
CrateName::new(krate.name(&sysroot))
250+
.expect("Sysroot crate names should not contain dashes"),
251+
),
249252
cfg_options,
250253
env,
251254
extern_source,
@@ -296,7 +299,7 @@ impl ProjectWorkspace {
296299
let crate_id = crate_graph.add_crate_root(
297300
file_id,
298301
edition,
299-
Some(pkg.name(&cargo).to_string()),
302+
Some(CrateName::normalize_dashes(pkg.name(&cargo))),
300303
cfg_options,
301304
env,
302305
extern_source,

0 commit comments

Comments
 (0)