Skip to content

Commit 2dfa9b8

Browse files
committed
Auto merge of #16384 - Veykril:smolstr, r=Veykril
minor: Make use of some new `SmolStr` improvements
2 parents 8bb500a + 8359126 commit 2dfa9b8

File tree

18 files changed

+80
-61
lines changed

18 files changed

+80
-61
lines changed

crates/hir-expand/src/name.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use std::fmt;
44

5-
use syntax::{ast, utils::is_raw_identifier, SmolStr};
5+
use syntax::{ast, format_smolstr, utils::is_raw_identifier, SmolStr};
66

77
/// `Name` is a wrapper around string, which is used in hir for both references
88
/// and declarations. In theory, names should also carry hygiene info, but we are
@@ -69,8 +69,8 @@ impl Name {
6969
}
7070

7171
/// Shortcut to create inline plain text name. Panics if `text.len() > 22`
72-
const fn new_inline(text: &str) -> Name {
73-
Name::new_text(SmolStr::new_inline(text))
72+
const fn new_static(text: &'static str) -> Name {
73+
Name::new_text(SmolStr::new_static(text))
7474
}
7575

7676
/// Resolve a name from the text of token.
@@ -83,7 +83,7 @@ impl Name {
8383
// Rust, e.g. "try" in Rust 2015. Even in such cases, we keep track of them in their
8484
// escaped form.
8585
None if is_raw_identifier(raw_text) => {
86-
Name::new_text(SmolStr::from_iter(["r#", raw_text]))
86+
Name::new_text(format_smolstr!("r#{}", raw_text))
8787
}
8888
_ => Name::new_text(raw_text.into()),
8989
}
@@ -99,7 +99,7 @@ impl Name {
9999
/// name is equal only to itself. It's not clear how to implement this in
100100
/// salsa though, so we punt on that bit for a moment.
101101
pub const fn missing() -> Name {
102-
Name::new_inline("[missing name]")
102+
Name::new_static("[missing name]")
103103
}
104104

105105
/// Returns true if this is a fake name for things missing in the source code. See
@@ -119,7 +119,7 @@ impl Name {
119119
use std::sync::atomic::{AtomicUsize, Ordering};
120120
static CNT: AtomicUsize = AtomicUsize::new(0);
121121
let c = CNT.fetch_add(1, Ordering::Relaxed);
122-
Name::new_text(format!("<ra@gennew>{c}").into())
122+
Name::new_text(format_smolstr!("<ra@gennew>{c}"))
123123
}
124124

125125
/// Returns the tuple index this name represents if it is a tuple field.
@@ -260,7 +260,7 @@ pub mod known {
260260
$(
261261
#[allow(bad_style)]
262262
pub const $ident: super::Name =
263-
super::Name::new_inline(stringify!($ident));
263+
super::Name::new_static(stringify!($ident));
264264
)*
265265
};
266266
}
@@ -471,11 +471,11 @@ pub mod known {
471471
);
472472

473473
// self/Self cannot be used as an identifier
474-
pub const SELF_PARAM: super::Name = super::Name::new_inline("self");
475-
pub const SELF_TYPE: super::Name = super::Name::new_inline("Self");
474+
pub const SELF_PARAM: super::Name = super::Name::new_static("self");
475+
pub const SELF_TYPE: super::Name = super::Name::new_static("Self");
476476

477-
pub const STATIC_LIFETIME: super::Name = super::Name::new_inline("'static");
478-
pub const DOLLAR_CRATE: super::Name = super::Name::new_inline("$crate");
477+
pub const STATIC_LIFETIME: super::Name = super::Name::new_static("'static");
478+
pub const DOLLAR_CRATE: super::Name = super::Name::new_static("$crate");
479479

480480
#[macro_export]
481481
macro_rules! name {

crates/hir-expand/src/quote.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
//! A simplified version of quote-crate like quasi quote macro
22
33
use span::Span;
4+
use syntax::format_smolstr;
45

56
use crate::name::Name;
67

7-
pub(crate) fn dollar_crate(span: Span) -> tt::Ident<Span> {
8-
tt::Ident { text: syntax::SmolStr::new_inline("$crate"), span }
8+
pub(crate) const fn dollar_crate(span: Span) -> tt::Ident<Span> {
9+
tt::Ident { text: syntax::SmolStr::new_static("$crate"), span }
910
}
1011

1112
// A helper macro quote macro
@@ -214,8 +215,8 @@ impl_to_to_tokentrees! {
214215
_span: crate::tt::Literal => self { self };
215216
_span: crate::tt::Ident => self { self };
216217
_span: crate::tt::Punct => self { self };
217-
span: &str => self { crate::tt::Literal{text: format!("\"{}\"", self.escape_default()).into(), span}};
218-
span: String => self { crate::tt::Literal{text: format!("\"{}\"", self.escape_default()).into(), span}};
218+
span: &str => self { crate::tt::Literal{text: format_smolstr!("\"{}\"", self.escape_default()), span}};
219+
span: String => self { crate::tt::Literal{text: format_smolstr!("\"{}\"", self.escape_default()), span}};
219220
span: Name => self { crate::tt::Ident{text: self.to_smol_str(), span}};
220221
}
221222

crates/hir/src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ use rustc_hash::FxHashSet;
8181
use stdx::{impl_from, never};
8282
use syntax::{
8383
ast::{self, HasAttrs as _, HasName},
84-
AstNode, AstPtr, SmolStr, SyntaxNode, SyntaxNodePtr, TextRange, T,
84+
format_smolstr, AstNode, AstPtr, SmolStr, SyntaxNode, SyntaxNodePtr, TextRange, T,
8585
};
8686
use triomphe::Arc;
8787

@@ -4284,9 +4284,9 @@ impl Type {
42844284
.filter_map(|arg| {
42854285
// arg can be either a `Ty` or `constant`
42864286
if let Some(ty) = arg.ty(Interner) {
4287-
Some(SmolStr::new(ty.display(db).to_string()))
4287+
Some(format_smolstr!("{}", ty.display(db)))
42884288
} else if let Some(const_) = arg.constant(Interner) {
4289-
Some(SmolStr::new_inline(&const_.display(db).to_string()))
4289+
Some(format_smolstr!("{}", const_.display(db)))
42904290
} else {
42914291
None
42924292
}

crates/ide-completion/src/completions.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use std::iter;
2626

2727
use hir::{known, HasAttrs, ScopeDef, Variant};
2828
use ide_db::{imports::import_assets::LocatedImport, RootDatabase, SymbolKind};
29-
use syntax::ast;
29+
use syntax::{ast, SmolStr};
3030

3131
use crate::{
3232
context::{
@@ -80,7 +80,11 @@ impl Completions {
8080
}
8181

8282
pub(crate) fn add_keyword(&mut self, ctx: &CompletionContext<'_>, keyword: &'static str) {
83-
let item = CompletionItem::new(CompletionItemKind::Keyword, ctx.source_range(), keyword);
83+
let item = CompletionItem::new(
84+
CompletionItemKind::Keyword,
85+
ctx.source_range(),
86+
SmolStr::new_static(keyword),
87+
);
8488
item.add_to(self, ctx.db);
8589
}
8690

crates/ide-completion/src/completions/dot.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Completes references after dot (fields and method calls).
22
33
use ide_db::FxHashSet;
4+
use syntax::SmolStr;
45

56
use crate::{
67
context::{CompletionContext, DotAccess, DotAccessKind, ExprCtx, PathCompletionCtx, Qualified},
@@ -20,8 +21,11 @@ pub(crate) fn complete_dot(
2021

2122
// Suggest .await syntax for types that implement Future trait
2223
if receiver_ty.impls_into_future(ctx.db) {
23-
let mut item =
24-
CompletionItem::new(CompletionItemKind::Keyword, ctx.source_range(), "await");
24+
let mut item = CompletionItem::new(
25+
CompletionItemKind::Keyword,
26+
ctx.source_range(),
27+
SmolStr::new_static("await"),
28+
);
2529
item.detail("expr.await");
2630
item.add_to(acc, ctx.db);
2731
}

crates/ide-completion/src/completions/extern_abi.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Completes function abi strings.
22
use syntax::{
33
ast::{self, IsString},
4-
AstNode, AstToken,
4+
AstNode, AstToken, SmolStr,
55
};
66

77
use crate::{
@@ -53,7 +53,8 @@ pub(crate) fn complete_extern_abi(
5353
let abi_str = expanded;
5454
let source_range = abi_str.text_range_between_quotes()?;
5555
for &abi in SUPPORTED_CALLING_CONVENTIONS {
56-
CompletionItem::new(CompletionItemKind::Keyword, source_range, abi).add_to(acc, ctx.db);
56+
CompletionItem::new(CompletionItemKind::Keyword, source_range, SmolStr::new_static(abi))
57+
.add_to(acc, ctx.db);
5758
}
5859
Some(())
5960
}

crates/ide-completion/src/completions/item_list/trait_impl.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use ide_db::{
3838
};
3939
use syntax::{
4040
ast::{self, edit_in_place::AttrsOwnerEdit, HasTypeBounds},
41-
AstNode, SyntaxElement, SyntaxKind, TextRange, T,
41+
format_smolstr, AstNode, SmolStr, SyntaxElement, SyntaxKind, TextRange, T,
4242
};
4343
use text_edit::TextEdit;
4444

@@ -180,7 +180,7 @@ fn add_function_impl(
180180
) {
181181
let fn_name = func.name(ctx.db);
182182

183-
let label = format!(
183+
let label = format_smolstr!(
184184
"fn {}({})",
185185
fn_name.display(ctx.db),
186186
if func.assoc_fn_params(ctx.db).is_empty() { "" } else { ".." }
@@ -254,7 +254,7 @@ fn add_type_alias_impl(
254254
) {
255255
let alias_name = type_alias.name(ctx.db).unescaped().to_smol_str();
256256

257-
let label = format!("type {alias_name} =");
257+
let label = format_smolstr!("type {alias_name} =");
258258

259259
let mut item = CompletionItem::new(SymbolKind::TypeAlias, replacement_range, label);
260260
item.lookup_by(format!("type {alias_name}"))
@@ -329,7 +329,7 @@ fn add_const_impl(
329329
let replacement = format!("{label} ");
330330

331331
let mut item = CompletionItem::new(SymbolKind::Const, replacement_range, label);
332-
item.lookup_by(format!("const {const_name}"))
332+
item.lookup_by(format_smolstr!("const {const_name}"))
333333
.set_documentation(const_.docs(ctx.db))
334334
.set_relevance(CompletionRelevance {
335335
is_item_from_trait: true,
@@ -348,7 +348,7 @@ fn add_const_impl(
348348
}
349349
}
350350

351-
fn make_const_compl_syntax(const_: &ast::Const, needs_whitespace: bool) -> String {
351+
fn make_const_compl_syntax(const_: &ast::Const, needs_whitespace: bool) -> SmolStr {
352352
let const_ = if needs_whitespace {
353353
insert_whitespace_into_node::insert_ws_into(const_.syntax().clone())
354354
} else {
@@ -368,7 +368,7 @@ fn make_const_compl_syntax(const_: &ast::Const, needs_whitespace: bool) -> Strin
368368

369369
let syntax = const_.text().slice(range).to_string();
370370

371-
format!("{} =", syntax.trim_end())
371+
format_smolstr!("{} =", syntax.trim_end())
372372
}
373373

374374
fn function_declaration(node: &ast::Fn, needs_whitespace: bool) -> String {

crates/ide-completion/src/completions/record.rs

+14-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
//! Complete fields in record literals and patterns.
22
use ide_db::SymbolKind;
3-
use syntax::ast::{self, Expr};
3+
use syntax::{
4+
ast::{self, Expr},
5+
SmolStr,
6+
};
47

58
use crate::{
69
context::{DotAccess, DotAccessKind, PatternContext},
@@ -66,8 +69,11 @@ pub(crate) fn complete_record_expr_fields(
6669
}
6770
if dot_prefix {
6871
cov_mark::hit!(functional_update_one_dot);
69-
let mut item =
70-
CompletionItem::new(CompletionItemKind::Snippet, ctx.source_range(), "..");
72+
let mut item = CompletionItem::new(
73+
CompletionItemKind::Snippet,
74+
ctx.source_range(),
75+
SmolStr::new_static(".."),
76+
);
7177
item.insert_text(".");
7278
item.add_to(acc, ctx.db);
7379
return;
@@ -91,7 +97,11 @@ pub(crate) fn add_default_update(
9197
// FIXME: This should make use of scope_def like completions so we get all the other goodies
9298
// that is we should handle this like actually completing the default function
9399
let completion_text = "..Default::default()";
94-
let mut item = CompletionItem::new(SymbolKind::Field, ctx.source_range(), completion_text);
100+
let mut item = CompletionItem::new(
101+
SymbolKind::Field,
102+
ctx.source_range(),
103+
SmolStr::new_static(completion_text),
104+
);
95105
let completion_text =
96106
completion_text.strip_prefix(ctx.token.text()).unwrap_or(completion_text);
97107
item.insert_text(completion_text).set_relevance(CompletionRelevance {

crates/ide-completion/src/completions/use_.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use hir::ScopeDef;
44
use ide_db::{FxHashSet, SymbolKind};
5-
use syntax::{ast, AstNode};
5+
use syntax::{ast, format_smolstr, AstNode};
66

77
use crate::{
88
context::{CompletionContext, PathCompletionCtx, Qualified},
@@ -108,7 +108,7 @@ pub(crate) fn complete_use_path(
108108
let item = CompletionItem::new(
109109
CompletionItemKind::SymbolKind(SymbolKind::Enum),
110110
ctx.source_range(),
111-
format!("{}::", e.name(ctx.db).display(ctx.db)),
111+
format_smolstr!("{}::", e.name(ctx.db).display(ctx.db)),
112112
);
113113
acc.add(item.build(ctx.db));
114114
}

crates/ide-completion/src/item.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use ide_db::{
1010
use itertools::Itertools;
1111
use smallvec::SmallVec;
1212
use stdx::{impl_from, never};
13-
use syntax::{SmolStr, TextRange, TextSize};
13+
use syntax::{format_smolstr, SmolStr, TextRange, TextSize};
1414
use text_edit::TextEdit;
1515

1616
use crate::{
@@ -442,7 +442,7 @@ impl Builder {
442442

443443
if !self.doc_aliases.is_empty() {
444444
let doc_aliases = self.doc_aliases.iter().join(", ");
445-
label_detail.replace(SmolStr::from(format!(" (alias {doc_aliases})")));
445+
label_detail.replace(format_smolstr!(" (alias {doc_aliases})"));
446446
let lookup_doc_aliases = self
447447
.doc_aliases
448448
.iter()
@@ -459,21 +459,21 @@ impl Builder {
459459
// after typing a comma or space.
460460
.join("");
461461
if !lookup_doc_aliases.is_empty() {
462-
lookup = SmolStr::from(format!("{lookup}{lookup_doc_aliases}"));
462+
lookup = format_smolstr!("{lookup}{lookup_doc_aliases}");
463463
}
464464
}
465465
if let [import_edit] = &*self.imports_to_add {
466466
// snippets can have multiple imports, but normal completions only have up to one
467-
label_detail.replace(SmolStr::from(format!(
467+
label_detail.replace(format_smolstr!(
468468
"{} (use {})",
469469
label_detail.as_deref().unwrap_or_default(),
470470
import_edit.import_path.display(db)
471-
)));
471+
));
472472
} else if let Some(trait_name) = self.trait_name {
473-
label_detail.replace(SmolStr::from(format!(
473+
label_detail.replace(format_smolstr!(
474474
"{} (as {trait_name})",
475475
label_detail.as_deref().unwrap_or_default(),
476-
)));
476+
));
477477
}
478478

479479
let text_edit = match self.text_edit {

crates/ide-completion/src/render.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use ide_db::{
1717
imports::import_assets::LocatedImport,
1818
RootDatabase, SnippetCap, SymbolKind,
1919
};
20-
use syntax::{AstNode, SmolStr, SyntaxKind, TextRange};
20+
use syntax::{format_smolstr, AstNode, SmolStr, SyntaxKind, TextRange};
2121
use text_edit::TextEdit;
2222

2323
use crate::{
@@ -202,7 +202,7 @@ fn field_with_receiver(
202202
) -> SmolStr {
203203
receiver.map_or_else(
204204
|| field_name.into(),
205-
|receiver| format!("{}.{field_name}", receiver.display(db)).into(),
205+
|receiver| format_smolstr!("{}.{field_name}", receiver.display(db)),
206206
)
207207
}
208208

crates/ide-completion/src/render/function.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use hir::{db::HirDatabase, AsAssocItem, HirDisplay};
44
use ide_db::{SnippetCap, SymbolKind};
55
use itertools::Itertools;
66
use stdx::{format_to, to_lower_snake_case};
7-
use syntax::{AstNode, SmolStr};
7+
use syntax::{format_smolstr, AstNode, SmolStr};
88

99
use crate::{
1010
context::{CompletionContext, DotAccess, DotAccessKind, PathCompletionCtx, PathKind},
@@ -52,13 +52,12 @@ fn render(
5252

5353
let (call, escaped_call) = match &func_kind {
5454
FuncKind::Method(_, Some(receiver)) => (
55-
format!(
55+
format_smolstr!(
5656
"{}.{}",
5757
receiver.unescaped().display(ctx.db()),
5858
name.unescaped().display(ctx.db())
59-
)
60-
.into(),
61-
format!("{}.{}", receiver.display(ctx.db()), name.display(ctx.db())).into(),
59+
),
60+
format_smolstr!("{}.{}", receiver.display(ctx.db()), name.display(ctx.db())),
6261
),
6362
_ => (name.unescaped().to_smol_str(), name.to_smol_str()),
6463
};

crates/ide-completion/src/render/macro_.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use hir::HirDisplay;
44
use ide_db::{documentation::Documentation, SymbolKind};
5-
use syntax::SmolStr;
5+
use syntax::{format_smolstr, SmolStr};
66

77
use crate::{
88
context::{PathCompletionCtx, PathKind, PatternContext},
@@ -94,7 +94,7 @@ fn label(
9494
) -> SmolStr {
9595
if needs_bang {
9696
if ctx.snippet_cap().is_some() {
97-
SmolStr::from_iter([&*name, "!", bra, "…", ket])
97+
format_smolstr!("{name}!{bra}…{ket}",)
9898
} else {
9999
banged_name(name)
100100
}

0 commit comments

Comments
 (0)