Skip to content

Commit 41451a2

Browse files
committed
Remove Name::to_smol_str
1 parent 1a20a08 commit 41451a2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+288
-190
lines changed

src/tools/rust-analyzer/Cargo.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -971,6 +971,7 @@ dependencies = [
971971
"crossbeam-channel",
972972
"hir-expand",
973973
"ide-db",
974+
"intern",
974975
"itertools",
975976
"paths",
976977
"proc-macro-api",

src/tools/rust-analyzer/crates/hir-def/src/import_map.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use itertools::Itertools;
99
use rustc_hash::FxHashSet;
1010
use smallvec::SmallVec;
1111
use stdx::{format_to, TupleExt};
12+
use syntax::ToSmolStr;
1213
use triomphe::Arc;
1314

1415
use crate::{
@@ -81,9 +82,9 @@ impl ImportMap {
8182
.iter()
8283
// We've only collected items, whose name cannot be tuple field so unwrapping is fine.
8384
.flat_map(|(&item, (info, _))| {
84-
info.iter()
85-
.enumerate()
86-
.map(move |(idx, info)| (item, info.name.to_smol_str(), idx as u32))
85+
info.iter().enumerate().map(move |(idx, info)| {
86+
(item, info.name.display(db.upcast()).to_smolstr(), idx as u32)
87+
})
8788
})
8889
.collect();
8990
importables.sort_by(|(_, l_info, _), (_, r_info, _)| {
@@ -412,28 +413,29 @@ pub fn search_dependencies(
412413
for map in &import_maps {
413414
op = op.add(map.fst.search(&automaton));
414415
}
415-
search_maps(&import_maps, op.union(), query)
416+
search_maps(db, &import_maps, op.union(), query)
416417
}
417418
SearchMode::Fuzzy => {
418419
let automaton = fst::automaton::Subsequence::new(&query.lowercased);
419420

420421
for map in &import_maps {
421422
op = op.add(map.fst.search(&automaton));
422423
}
423-
search_maps(&import_maps, op.union(), query)
424+
search_maps(db, &import_maps, op.union(), query)
424425
}
425426
SearchMode::Prefix => {
426427
let automaton = fst::automaton::Str::new(&query.lowercased).starts_with();
427428

428429
for map in &import_maps {
429430
op = op.add(map.fst.search(&automaton));
430431
}
431-
search_maps(&import_maps, op.union(), query)
432+
search_maps(db, &import_maps, op.union(), query)
432433
}
433434
}
434435
}
435436

436437
fn search_maps(
438+
db: &dyn DefDatabase,
437439
import_maps: &[Arc<ImportMap>],
438440
mut stream: fst::map::Union<'_>,
439441
query: &Query,
@@ -459,7 +461,7 @@ fn search_maps(
459461
query.search_mode.check(
460462
&query.query,
461463
query.case_sensitive,
462-
&info.name.to_smol_str(),
464+
&info.name.display(db.upcast()).to_smolstr(),
463465
)
464466
});
465467
res.extend(iter.map(TupleExt::head));

src/tools/rust-analyzer/crates/hir-def/src/macro_expansion_tests/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use hir_expand::{
2424
span_map::SpanMapRef,
2525
InFile, MacroFileId, MacroFileIdExt,
2626
};
27+
use intern::Symbol;
2728
use span::Span;
2829
use stdx::{format_to, format_to_acc};
2930
use syntax::{
@@ -55,7 +56,7 @@ pub fn identity_when_valid(_attr: TokenStream, item: TokenStream) -> TokenStream
5556
"#
5657
.into(),
5758
ProcMacro {
58-
name: "identity_when_valid".into(),
59+
name: Symbol::intern("identity_when_valid"),
5960
kind: ProcMacroKind::Attr,
6061
expander: sync::Arc::new(IdentityWhenValidProcMacroExpander),
6162
disabled: false,

src/tools/rust-analyzer/crates/hir-def/src/nameres/collector.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ pub(super) fn collect_defs(db: &dyn DefDatabase, def_map: DefMap, tree_id: TreeI
8282
.iter()
8383
.enumerate()
8484
.map(|(idx, it)| {
85-
let name = Name::new(&it.name, tt::IdentIsRaw::No, ctx);
85+
let name = Name::new_symbol(it.name.clone(), ctx);
8686
(
8787
name,
8888
if !db.expand_proc_attr_macros() {

src/tools/rust-analyzer/crates/hir-def/src/nameres/mod_resolution.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use arrayvec::ArrayVec;
33
use base_db::{AnchoredPath, FileId};
44
use hir_expand::{name::Name, HirFileIdExt, MacroFileIdExt};
55
use limit::Limit;
6+
use syntax::ToSmolStr as _;
67

78
use crate::{db::DefDatabase, HirFileId};
89

@@ -33,7 +34,7 @@ impl ModDir {
3334
let path = match attr_path {
3435
None => {
3536
let mut path = self.dir_path.clone();
36-
path.push(&name.unescaped().to_smol_str());
37+
path.push(&name.unescaped().display_no_db().to_smolstr());
3738
path
3839
}
3940
Some(attr_path) => {

src/tools/rust-analyzer/crates/hir-def/src/path.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::{
1313
};
1414
use hir_expand::name::Name;
1515
use intern::Interned;
16-
use syntax::ast;
16+
use syntax::{ast, ToSmolStr};
1717

1818
pub use hir_expand::mod_path::{path, ModPath, PathKind};
1919

@@ -29,7 +29,7 @@ impl Display for ImportAlias {
2929
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3030
match self {
3131
ImportAlias::Underscore => f.write_str("_"),
32-
ImportAlias::Alias(name) => f.write_str(&name.to_smol_str()),
32+
ImportAlias::Alias(name) => f.write_str(&name.display_no_db().to_smolstr()),
3333
}
3434
}
3535
}

src/tools/rust-analyzer/crates/hir-expand/src/name.rs

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::fmt;
44

55
use intern::{sym, Symbol};
66
use span::SyntaxContextId;
7-
use syntax::{ast, format_smolstr, utils::is_raw_identifier, SmolStr};
7+
use syntax::{ast, utils::is_raw_identifier};
88

99
/// `Name` is a wrapper around string, which is used in hir for both references
1010
/// and declarations. In theory, names should also carry hygiene info, but we are
@@ -59,21 +59,14 @@ impl PartialEq<Name> for Symbol {
5959
pub struct UnescapedName<'a>(&'a Name);
6060

6161
impl UnescapedName<'_> {
62-
/// Returns the textual representation of this name as a [`SmolStr`]. Prefer using this over
63-
/// [`ToString::to_string`] if possible as this conversion is cheaper in the general case.
64-
pub fn to_smol_str(&self) -> SmolStr {
65-
let it = self.0.symbol.as_str();
66-
if let Some(stripped) = it.strip_prefix("r#") {
67-
SmolStr::new(stripped)
68-
} else {
69-
it.into()
70-
}
71-
}
72-
7362
pub fn display(&self, db: &dyn crate::db::ExpandDatabase) -> impl fmt::Display + '_ {
7463
_ = db;
7564
UnescapedDisplay { name: self }
7665
}
66+
#[doc(hidden)]
67+
pub fn display_no_db(&self) -> impl fmt::Display + '_ {
68+
UnescapedDisplay { name: self }
69+
}
7770
}
7871

7972
impl Name {
@@ -88,7 +81,7 @@ impl Name {
8881
_ = ctx;
8982
Name {
9083
symbol: if raw.yes() {
91-
Symbol::intern(&format_smolstr!("{}{text}", raw.as_str()))
84+
Symbol::intern(&format!("{}{text}", raw.as_str()))
9285
} else {
9386
Symbol::intern(text)
9487
},
@@ -118,9 +111,7 @@ impl Name {
118111
// Keywords (in the current edition) *can* be used as a name in earlier editions of
119112
// Rust, e.g. "try" in Rust 2015. Even in such cases, we keep track of them in their
120113
// escaped form.
121-
None if is_raw_identifier(raw_text) => {
122-
Name::new_text(&format_smolstr!("r#{}", raw_text))
123-
}
114+
None if is_raw_identifier(raw_text) => Name::new_text(&format!("r#{}", raw_text)),
124115
_ => Name::new_text(raw_text),
125116
}
126117
}
@@ -151,7 +142,7 @@ impl Name {
151142
/// creating desugared locals and labels. The caller is responsible for picking an index
152143
/// that is stable across re-executions
153144
pub fn generate_new_name(idx: usize) -> Name {
154-
Name::new_text(&format_smolstr!("<ra@gennew>{idx}"))
145+
Name::new_text(&format!("<ra@gennew>{idx}"))
155146
}
156147

157148
/// Returns the tuple index this name represents if it is a tuple field.
@@ -164,14 +155,6 @@ impl Name {
164155
self.symbol.as_str()
165156
}
166157

167-
// FIXME: Remove this
168-
/// Returns the textual representation of this name as a [`SmolStr`].
169-
/// Prefer using this over [`ToString::to_string`] if possible as this conversion is cheaper in
170-
/// the general case.
171-
pub fn to_smol_str(&self) -> SmolStr {
172-
self.symbol.as_str().into()
173-
}
174-
175158
pub fn unescaped(&self) -> UnescapedName<'_> {
176159
UnescapedName(self)
177160
}
@@ -185,6 +168,12 @@ impl Name {
185168
Display { name: self }
186169
}
187170

171+
// FIXME: Remove this
172+
#[doc(hidden)]
173+
pub fn display_no_db(&self) -> impl fmt::Display + '_ {
174+
Display { name: self }
175+
}
176+
188177
pub fn symbol(&self) -> &Symbol {
189178
&self.symbol
190179
}

src/tools/rust-analyzer/crates/hir-expand/src/proc_macro.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ use core::fmt;
44
use std::{panic::RefUnwindSafe, sync};
55

66
use base_db::{CrateId, Env};
7+
use intern::Symbol;
78
use rustc_hash::FxHashMap;
89
use span::Span;
910
use stdx::never;
10-
use syntax::SmolStr;
1111
use triomphe::Arc;
1212

1313
use crate::{db::ExpandDatabase, tt, ExpandError, ExpandResult};
@@ -53,7 +53,7 @@ pub type ProcMacros = FxHashMap<CrateId, ProcMacroLoadResult>;
5353

5454
#[derive(Debug, Clone)]
5555
pub struct ProcMacro {
56-
pub name: SmolStr,
56+
pub name: Symbol,
5757
pub kind: ProcMacroKind,
5858
pub expander: sync::Arc<dyn ProcMacroExpander>,
5959
pub disabled: bool,

src/tools/rust-analyzer/crates/hir-ty/src/diagnostics/decl_check.rs

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use intern::sym;
2828
use stdx::{always, never};
2929
use syntax::{
3030
ast::{self, HasName},
31-
AstNode, AstPtr,
31+
AstNode, AstPtr, ToSmolStr,
3232
};
3333

3434
use crate::db::HirDatabase;
@@ -326,7 +326,9 @@ impl<'a> DeclValidator<'a> {
326326
let bind_name = &body.bindings[*id].name;
327327
let replacement = Replacement {
328328
current_name: bind_name.clone(),
329-
suggested_text: to_lower_snake_case(&bind_name.to_smol_str())?,
329+
suggested_text: to_lower_snake_case(
330+
&bind_name.display_no_db().to_smolstr(),
331+
)?,
330332
expected_case: CaseType::LowerSnakeCase,
331333
};
332334
Some((pat_id, replacement))
@@ -406,10 +408,12 @@ impl<'a> DeclValidator<'a> {
406408
let mut struct_fields_replacements = fields
407409
.iter()
408410
.filter_map(|(_, field)| {
409-
to_lower_snake_case(&field.name.to_smol_str()).map(|new_name| Replacement {
410-
current_name: field.name.clone(),
411-
suggested_text: new_name,
412-
expected_case: CaseType::LowerSnakeCase,
411+
to_lower_snake_case(&field.name.display_no_db().to_smolstr()).map(|new_name| {
412+
Replacement {
413+
current_name: field.name.clone(),
414+
suggested_text: new_name,
415+
expected_case: CaseType::LowerSnakeCase,
416+
}
413417
})
414418
})
415419
.peekable();
@@ -498,7 +502,7 @@ impl<'a> DeclValidator<'a> {
498502
.variants
499503
.iter()
500504
.filter_map(|(_, name)| {
501-
to_camel_case(&name.to_smol_str()).map(|new_name| Replacement {
505+
to_camel_case(&name.display_no_db().to_smolstr()).map(|new_name| Replacement {
502506
current_name: name.clone(),
503507
suggested_text: new_name,
504508
expected_case: CaseType::UpperCamelCase,
@@ -565,10 +569,12 @@ impl<'a> DeclValidator<'a> {
565569
let mut variant_field_replacements = fields
566570
.iter()
567571
.filter_map(|(_, field)| {
568-
to_lower_snake_case(&field.name.to_smol_str()).map(|new_name| Replacement {
569-
current_name: field.name.clone(),
570-
suggested_text: new_name,
571-
expected_case: CaseType::LowerSnakeCase,
572+
to_lower_snake_case(&field.name.display_no_db().to_smolstr()).map(|new_name| {
573+
Replacement {
574+
current_name: field.name.clone(),
575+
suggested_text: new_name,
576+
expected_case: CaseType::LowerSnakeCase,
577+
}
572578
})
573579
})
574580
.peekable();
@@ -705,9 +711,11 @@ impl<'a> DeclValidator<'a> {
705711
CaseType::UpperSnakeCase => to_upper_snake_case,
706712
CaseType::UpperCamelCase => to_camel_case,
707713
};
708-
let Some(replacement) = to_expected_case_type(&name.to_smol_str()).map(|new_name| {
709-
Replacement { current_name: name.clone(), suggested_text: new_name, expected_case }
710-
}) else {
714+
let Some(replacement) =
715+
to_expected_case_type(&name.display(self.db.upcast()).to_smolstr()).map(|new_name| {
716+
Replacement { current_name: name.clone(), suggested_text: new_name, expected_case }
717+
})
718+
else {
711719
return;
712720
};
713721

src/tools/rust-analyzer/crates/hir-ty/src/layout/tests.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use either::Either;
33
use hir_def::db::DefDatabase;
44
use project_model::{target_data_layout::RustcDataLayoutConfig, Sysroot};
55
use rustc_hash::FxHashMap;
6+
use syntax::ToSmolStr;
67
use test_fixture::WithFixture;
78
use triomphe::Arc;
89

@@ -40,14 +41,20 @@ fn eval_goal(ra_fixture: &str, minicore: &str) -> Result<Arc<Layout>, LayoutErro
4041
let adt_or_type_alias_id = scope.declarations().find_map(|x| match x {
4142
hir_def::ModuleDefId::AdtId(x) => {
4243
let name = match x {
43-
hir_def::AdtId::StructId(x) => db.struct_data(x).name.to_smol_str(),
44-
hir_def::AdtId::UnionId(x) => db.union_data(x).name.to_smol_str(),
45-
hir_def::AdtId::EnumId(x) => db.enum_data(x).name.to_smol_str(),
44+
hir_def::AdtId::StructId(x) => {
45+
db.struct_data(x).name.display_no_db().to_smolstr()
46+
}
47+
hir_def::AdtId::UnionId(x) => {
48+
db.union_data(x).name.display_no_db().to_smolstr()
49+
}
50+
hir_def::AdtId::EnumId(x) => {
51+
db.enum_data(x).name.display_no_db().to_smolstr()
52+
}
4653
};
4754
(name == "Goal").then_some(Either::Left(x))
4855
}
4956
hir_def::ModuleDefId::TypeAliasId(x) => {
50-
let name = db.type_alias_data(x).name.to_smol_str();
57+
let name = db.type_alias_data(x).name.display_no_db().to_smolstr();
5158
(name == "Goal").then_some(Either::Right(x))
5259
}
5360
_ => None,
@@ -87,14 +94,19 @@ fn eval_expr(ra_fixture: &str, minicore: &str) -> Result<Arc<Layout>, LayoutErro
8794
.declarations()
8895
.find_map(|x| match x {
8996
hir_def::ModuleDefId::FunctionId(x) => {
90-
let name = db.function_data(x).name.to_smol_str();
97+
let name = db.function_data(x).name.display_no_db().to_smolstr();
9198
(name == "main").then_some(x)
9299
}
93100
_ => None,
94101
})
95102
.unwrap();
96103
let hir_body = db.body(function_id.into());
97-
let b = hir_body.bindings.iter().find(|x| x.1.name.to_smol_str() == "goal").unwrap().0;
104+
let b = hir_body
105+
.bindings
106+
.iter()
107+
.find(|x| x.1.name.display_no_db().to_smolstr() == "goal")
108+
.unwrap()
109+
.0;
98110
let infer = db.infer(function_id.into());
99111
let goal_ty = infer.type_of_binding[b].clone();
100112
db.layout_of_ty(goal_ty, db.trait_environment(function_id.into()))

src/tools/rust-analyzer/crates/hir/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ use span::{Edition, MacroCallId};
8282
use stdx::{impl_from, never};
8383
use syntax::{
8484
ast::{self, HasAttrs as _, HasName},
85-
format_smolstr, AstNode, AstPtr, SmolStr, SyntaxNode, SyntaxNodePtr, TextRange, T,
85+
format_smolstr, AstNode, AstPtr, SmolStr, SyntaxNode, SyntaxNodePtr, TextRange, ToSmolStr, T,
8686
};
8787
use triomphe::Arc;
8888

@@ -4607,7 +4607,7 @@ impl Type {
46074607
) -> impl Iterator<Item = SmolStr> + 'a {
46084608
// iterate the lifetime
46094609
self.as_adt()
4610-
.and_then(|a| a.lifetime(db).map(|lt| lt.name.to_smol_str()))
4610+
.and_then(|a| a.lifetime(db).map(|lt| lt.name.display_no_db().to_smolstr()))
46114611
.into_iter()
46124612
// add the type and const parameters
46134613
.chain(self.type_and_const_arguments(db))

0 commit comments

Comments
 (0)