aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-10-20 16:04:38 +0100
committerAleksey Kladov <[email protected]>2020-10-20 16:09:03 +0100
commit3b1a648539487c08bc613b6fd6e573b0e0e38948 (patch)
treec7f1ebcc5979838d1747cf19dd562004916a7274
parenta85c4280bf7af3ea25c34c0cd72d05c8de17454d (diff)
More type safety around names
-rw-r--r--crates/base_db/src/fixture.rs4
-rw-r--r--crates/base_db/src/input.rs27
-rw-r--r--crates/base_db/src/lib.rs4
-rw-r--r--crates/hir/src/code_model.rs4
-rw-r--r--crates/ide/src/inlay_hints.rs5
-rw-r--r--crates/project_model/src/lib.rs8
6 files changed, 33 insertions, 19 deletions
diff --git a/crates/base_db/src/fixture.rs b/crates/base_db/src/fixture.rs
index 72f1fd667..66e6443cb 100644
--- a/crates/base_db/src/fixture.rs
+++ b/crates/base_db/src/fixture.rs
@@ -158,7 +158,7 @@ impl ChangeFixture {
158 let crate_id = crate_graph.add_crate_root( 158 let crate_id = crate_graph.add_crate_root(
159 file_id, 159 file_id,
160 meta.edition, 160 meta.edition,
161 Some(crate_name.clone()), 161 Some(crate_name.clone().into()),
162 meta.cfg, 162 meta.cfg,
163 meta.env, 163 meta.env,
164 Default::default(), 164 Default::default(),
@@ -187,7 +187,7 @@ impl ChangeFixture {
187 crate_graph.add_crate_root( 187 crate_graph.add_crate_root(
188 crate_root, 188 crate_root,
189 Edition::Edition2018, 189 Edition::Edition2018,
190 Some(CrateName::new("test").unwrap()), 190 Some(CrateName::new("test").unwrap().into()),
191 default_cfg, 191 default_cfg,
192 Env::default(), 192 Env::default(),
193 Default::default(), 193 Default::default(),
diff --git a/crates/base_db/src/input.rs b/crates/base_db/src/input.rs
index 02c7348ff..87f0a0ce5 100644
--- a/crates/base_db/src/input.rs
+++ b/crates/base_db/src/input.rs
@@ -108,24 +108,37 @@ impl ops::Deref for CrateName {
108} 108}
109 109
110#[derive(Debug, Clone, PartialEq, Eq, Hash)] 110#[derive(Debug, Clone, PartialEq, Eq, Hash)]
111pub struct CrateDisplayName(CrateName); 111pub struct CrateDisplayName {
112 // The name we use to display various paths (with `_`).
113 crate_name: CrateName,
114 // The name as specified in Cargo.toml (with `-`).
115 canonical_name: String,
116}
112 117
113impl From<CrateName> for CrateDisplayName { 118impl From<CrateName> for CrateDisplayName {
114 fn from(inner: CrateName) -> CrateDisplayName { 119 fn from(crate_name: CrateName) -> CrateDisplayName {
115 CrateDisplayName(inner) 120 let canonical_name = crate_name.to_string();
121 CrateDisplayName { crate_name, canonical_name }
116 } 122 }
117} 123}
118 124
119impl fmt::Display for CrateDisplayName { 125impl fmt::Display for CrateDisplayName {
120 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 126 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
121 write!(f, "{}", self.0) 127 write!(f, "{}", self.crate_name)
122 } 128 }
123} 129}
124 130
125impl ops::Deref for CrateDisplayName { 131impl ops::Deref for CrateDisplayName {
126 type Target = str; 132 type Target = str;
127 fn deref(&self) -> &str { 133 fn deref(&self) -> &str {
128 &*self.0 134 &*self.crate_name
135 }
136}
137
138impl CrateDisplayName {
139 pub fn from_canonical_name(canonical_name: String) -> CrateDisplayName {
140 let crate_name = CrateName::normalize_dashes(&canonical_name);
141 CrateDisplayName { crate_name, canonical_name }
129 } 142 }
130} 143}
131 144
@@ -155,7 +168,7 @@ pub struct CrateData {
155 /// 168 ///
156 /// For purposes of analysis, crates are anonymous (only names in 169 /// For purposes of analysis, crates are anonymous (only names in
157 /// `Dependency` matters), this name should only be used for UI. 170 /// `Dependency` matters), this name should only be used for UI.
158 pub display_name: Option<CrateName>, 171 pub display_name: Option<CrateDisplayName>,
159 pub cfg_options: CfgOptions, 172 pub cfg_options: CfgOptions,
160 pub env: Env, 173 pub env: Env,
161 pub dependencies: Vec<Dependency>, 174 pub dependencies: Vec<Dependency>,
@@ -184,7 +197,7 @@ impl CrateGraph {
184 &mut self, 197 &mut self,
185 file_id: FileId, 198 file_id: FileId,
186 edition: Edition, 199 edition: Edition,
187 display_name: Option<CrateName>, 200 display_name: Option<CrateDisplayName>,
188 cfg_options: CfgOptions, 201 cfg_options: CfgOptions,
189 env: Env, 202 env: Env,
190 proc_macro: Vec<(SmolStr, Arc<dyn tt::TokenExpander>)>, 203 proc_macro: Vec<(SmolStr, Arc<dyn tt::TokenExpander>)>,
diff --git a/crates/base_db/src/lib.rs b/crates/base_db/src/lib.rs
index e38aa7257..0804202d6 100644
--- a/crates/base_db/src/lib.rs
+++ b/crates/base_db/src/lib.rs
@@ -13,8 +13,8 @@ pub use crate::{
13 cancellation::Canceled, 13 cancellation::Canceled,
14 change::Change, 14 change::Change,
15 input::{ 15 input::{
16 CrateData, CrateGraph, CrateId, CrateName, Dependency, Edition, Env, FileId, ProcMacroId, 16 CrateData, CrateDisplayName, CrateGraph, CrateId, CrateName, Dependency, Edition, Env,
17 SourceRoot, SourceRootId, 17 FileId, ProcMacroId, SourceRoot, SourceRootId,
18 }, 18 },
19}; 19};
20pub use salsa; 20pub use salsa;
diff --git a/crates/hir/src/code_model.rs b/crates/hir/src/code_model.rs
index 64f3fbe31..7f169ccd2 100644
--- a/crates/hir/src/code_model.rs
+++ b/crates/hir/src/code_model.rs
@@ -2,7 +2,7 @@
2use std::{iter, sync::Arc}; 2use std::{iter, sync::Arc};
3 3
4use arrayvec::ArrayVec; 4use arrayvec::ArrayVec;
5use base_db::{CrateId, CrateName, Edition, FileId}; 5use base_db::{CrateDisplayName, CrateId, Edition, FileId};
6use either::Either; 6use either::Either;
7use hir_def::find_path::PrefixKind; 7use hir_def::find_path::PrefixKind;
8use hir_def::{ 8use hir_def::{
@@ -103,7 +103,7 @@ impl Crate {
103 db.crate_graph()[self.id].edition 103 db.crate_graph()[self.id].edition
104 } 104 }
105 105
106 pub fn display_name(self, db: &dyn HirDatabase) -> Option<CrateName> { 106 pub fn display_name(self, db: &dyn HirDatabase) -> Option<CrateDisplayName> {
107 db.crate_graph()[self.id].display_name.clone() 107 db.crate_graph()[self.id].display_name.clone()
108 } 108 }
109 109
diff --git a/crates/ide/src/inlay_hints.rs b/crates/ide/src/inlay_hints.rs
index f5f366354..56b985e80 100644
--- a/crates/ide/src/inlay_hints.rs
+++ b/crates/ide/src/inlay_hints.rs
@@ -1,15 +1,14 @@
1use assists::utils::FamousDefs; 1use assists::utils::FamousDefs;
2use either::Either;
2use hir::{known, HirDisplay, Semantics}; 3use hir::{known, HirDisplay, Semantics};
3use ide_db::RootDatabase; 4use ide_db::RootDatabase;
4use stdx::to_lower_snake_case; 5use stdx::to_lower_snake_case;
5use syntax::{ 6use syntax::{
6 ast::{self, ArgListOwner, AstNode}, 7 ast::{self, ArgListOwner, AstNode, NameOwner},
7 match_ast, Direction, NodeOrToken, SmolStr, SyntaxKind, TextRange, T, 8 match_ast, Direction, NodeOrToken, SmolStr, SyntaxKind, TextRange, T,
8}; 9};
9 10
10use crate::FileId; 11use crate::FileId;
11use ast::NameOwner;
12use either::Either;
13 12
14#[derive(Clone, Debug, PartialEq, Eq)] 13#[derive(Clone, Debug, PartialEq, Eq)]
15pub struct InlayHintsConfig { 14pub struct InlayHintsConfig {
diff --git a/crates/project_model/src/lib.rs b/crates/project_model/src/lib.rs
index ea95d1c77..5db41bc16 100644
--- a/crates/project_model/src/lib.rs
+++ b/crates/project_model/src/lib.rs
@@ -13,7 +13,7 @@ use std::{
13}; 13};
14 14
15use anyhow::{bail, Context, Result}; 15use anyhow::{bail, Context, Result};
16use base_db::{CrateGraph, CrateId, CrateName, Edition, Env, FileId}; 16use base_db::{CrateDisplayName, CrateGraph, CrateId, CrateName, Edition, Env, FileId};
17use cfg::CfgOptions; 17use cfg::CfgOptions;
18use paths::{AbsPath, AbsPathBuf}; 18use paths::{AbsPath, AbsPathBuf};
19use rustc_hash::{FxHashMap, FxHashSet}; 19use rustc_hash::{FxHashMap, FxHashSet};
@@ -408,10 +408,12 @@ impl ProjectWorkspace {
408 .map(|it| proc_macro_client.by_dylib_path(&it)) 408 .map(|it| proc_macro_client.by_dylib_path(&it))
409 .unwrap_or_default(); 409 .unwrap_or_default();
410 410
411 let display_name =
412 CrateDisplayName::from_canonical_name(cargo[pkg].name.clone());
411 let crate_id = crate_graph.add_crate_root( 413 let crate_id = crate_graph.add_crate_root(
412 file_id, 414 file_id,
413 edition, 415 edition,
414 Some(CrateName::normalize_dashes(&cargo[pkg].name)), 416 Some(display_name),
415 cfg_options, 417 cfg_options,
416 env, 418 env,
417 proc_macro.clone(), 419 proc_macro.clone(),
@@ -556,7 +558,7 @@ fn sysroot_to_crate_graph(
556 let crate_id = crate_graph.add_crate_root( 558 let crate_id = crate_graph.add_crate_root(
557 file_id, 559 file_id,
558 Edition::Edition2018, 560 Edition::Edition2018,
559 Some(name), 561 Some(name.into()),
560 cfg_options.clone(), 562 cfg_options.clone(),
561 env, 563 env,
562 proc_macro, 564 proc_macro,