aboutsummaryrefslogtreecommitdiff
path: root/crates/syntax
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2021-03-26 17:59:06 +0000
committerGitHub <[email protected]>2021-03-26 17:59:06 +0000
commit77a447dcda82a9f0eb6e1f04bd4b1dd53a226c65 (patch)
treee691982730fe01802f874066927b2ebbe8badc75 /crates/syntax
parent4ecaad98e074c42dbf637a11afcb630aafffd7b3 (diff)
parent5ff3299dd61ea5c5790c01819994c9d8fa6afc09 (diff)
Merge #8191
8191: syntax: return owned string instead of leaking string r=cynecx a=cynecx Quick hack? to alleviate https://github.com/rust-analyzer/rust-analyzer/issues/8181#issuecomment-806019126. I haven't run any tests but this should affect performance since we are deallocating more. r? @matklad Co-authored-by: cynecx <[email protected]>
Diffstat (limited to 'crates/syntax')
-rw-r--r--crates/syntax/src/ast/make.rs4
-rw-r--r--crates/syntax/src/ast/node_ext.rs14
2 files changed, 8 insertions, 10 deletions
diff --git a/crates/syntax/src/ast/make.rs b/crates/syntax/src/ast/make.rs
index c08f2c14f..c6a7b99b7 100644
--- a/crates/syntax/src/ast/make.rs
+++ b/crates/syntax/src/ast/make.rs
@@ -268,14 +268,14 @@ pub fn arg_list(args: impl IntoIterator<Item = ast::Expr>) -> ast::ArgList {
268} 268}
269 269
270pub fn ident_pat(name: ast::Name) -> ast::IdentPat { 270pub fn ident_pat(name: ast::Name) -> ast::IdentPat {
271 return from_text(name.text()); 271 return from_text(&name.text());
272 272
273 fn from_text(text: &str) -> ast::IdentPat { 273 fn from_text(text: &str) -> ast::IdentPat {
274 ast_from_text(&format!("fn f({}: ())", text)) 274 ast_from_text(&format!("fn f({}: ())", text))
275 } 275 }
276} 276}
277pub fn ident_mut_pat(name: ast::Name) -> ast::IdentPat { 277pub fn ident_mut_pat(name: ast::Name) -> ast::IdentPat {
278 return from_text(name.text()); 278 return from_text(&name.text());
279 279
280 fn from_text(text: &str) -> ast::IdentPat { 280 fn from_text(text: &str) -> ast::IdentPat {
281 ast_from_text(&format!("fn f(mut {}: ())", text)) 281 ast_from_text(&format!("fn f(mut {}: ())", text))
diff --git a/crates/syntax/src/ast/node_ext.rs b/crates/syntax/src/ast/node_ext.rs
index bdf907a21..6d7db5fb2 100644
--- a/crates/syntax/src/ast/node_ext.rs
+++ b/crates/syntax/src/ast/node_ext.rs
@@ -12,19 +12,19 @@ use crate::{
12}; 12};
13 13
14impl ast::Lifetime { 14impl ast::Lifetime {
15 pub fn text(&self) -> &str { 15 pub fn text(&self) -> SmolStr {
16 text_of_first_token(self.syntax()) 16 text_of_first_token(self.syntax())
17 } 17 }
18} 18}
19 19
20impl ast::Name { 20impl ast::Name {
21 pub fn text(&self) -> &str { 21 pub fn text(&self) -> SmolStr {
22 text_of_first_token(self.syntax()) 22 text_of_first_token(self.syntax())
23 } 23 }
24} 24}
25 25
26impl ast::NameRef { 26impl ast::NameRef {
27 pub fn text(&self) -> &str { 27 pub fn text(&self) -> SmolStr {
28 text_of_first_token(self.syntax()) 28 text_of_first_token(self.syntax())
29 } 29 }
30 30
@@ -33,10 +33,8 @@ impl ast::NameRef {
33 } 33 }
34} 34}
35 35
36fn text_of_first_token(node: &SyntaxNode) -> &str { 36fn text_of_first_token(node: &SyntaxNode) -> SmolStr {
37 let t = 37 node.green().children().next().and_then(|it| it.into_token()).unwrap().text().into()
38 node.green().children().next().and_then(|it| it.into_token()).unwrap().text().to_string();
39 Box::leak(Box::new(t))
40} 38}
41 39
42pub enum Macro { 40pub enum Macro {
@@ -378,7 +376,7 @@ impl fmt::Display for NameOrNameRef {
378} 376}
379 377
380impl NameOrNameRef { 378impl NameOrNameRef {
381 pub fn text(&self) -> &str { 379 pub fn text(&self) -> SmolStr {
382 match self { 380 match self {
383 NameOrNameRef::Name(name) => name.text(), 381 NameOrNameRef::Name(name) => name.text(),
384 NameOrNameRef::NameRef(name_ref) => name_ref.text(), 382 NameOrNameRef::NameRef(name_ref) => name_ref.text(),