Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 928d847

Browse files
committed
Keep the span for Attr::Literal
1 parent 6d10719 commit 928d847

File tree

7 files changed

+44
-36
lines changed

7 files changed

+44
-36
lines changed

crates/hir-def/src/attr.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,12 @@ impl Attrs {
148148
}
149149
}
150150

151-
pub fn lang(&self) -> Option<&SmolStr> {
151+
pub fn lang(&self) -> Option<&str> {
152152
self.by_key("lang").string_value()
153153
}
154154

155155
pub fn lang_item(&self) -> Option<LangItem> {
156-
self.by_key("lang").string_value().and_then(|it| LangItem::from_str(it))
156+
self.by_key("lang").string_value().and_then(LangItem::from_str)
157157
}
158158

159159
pub fn has_doc_hidden(&self) -> bool {
@@ -178,7 +178,7 @@ impl Attrs {
178178
self.doc_exprs().flat_map(|doc_expr| doc_expr.aliases().to_vec())
179179
}
180180

181-
pub fn export_name(&self) -> Option<&SmolStr> {
181+
pub fn export_name(&self) -> Option<&str> {
182182
self.by_key("export_name").string_value()
183183
}
184184

@@ -565,7 +565,7 @@ impl<'attr> AttrQuery<'attr> {
565565
self.attrs().filter_map(|attr| attr.token_tree_value())
566566
}
567567

568-
pub fn string_value(self) -> Option<&'attr SmolStr> {
568+
pub fn string_value(self) -> Option<&'attr str> {
569569
self.attrs().find_map(|attr| attr.string_value())
570570
}
571571

crates/hir-def/src/lang_item.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ impl LangItems {
192192

193193
pub(crate) fn lang_attr(db: &dyn DefDatabase, item: AttrDefId) -> Option<LangItem> {
194194
let attrs = db.attrs(item);
195-
attrs.by_key("lang").string_value().and_then(|it| LangItem::from_str(it))
195+
attrs.by_key("lang").string_value().and_then(LangItem::from_str)
196196
}
197197

198198
pub(crate) fn notable_traits_in_deps(

crates/hir-def/src/nameres/collector.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use limit::Limit;
2424
use rustc_hash::{FxHashMap, FxHashSet};
2525
use span::{Edition, ErasedFileAstId, FileAstId, Span, SyntaxContextId};
2626
use stdx::always;
27-
use syntax::{ast, SmolStr};
27+
use syntax::ast;
2828
use triomphe::Arc;
2929

3030
use crate::{
@@ -312,7 +312,7 @@ impl DefCollector<'_> {
312312
}
313313
}
314314
() if *attr_name == hir_expand::name![crate_type] => {
315-
if let Some("proc-macro") = attr.string_value().map(SmolStr::as_str) {
315+
if let Some("proc-macro") = attr.string_value() {
316316
self.is_proc_macro = true;
317317
}
318318
}
@@ -1902,7 +1902,7 @@ impl ModCollector<'_, '_> {
19021902
}
19031903

19041904
fn collect_module(&mut self, module_id: FileItemTreeId<Mod>, attrs: &Attrs) {
1905-
let path_attr = attrs.by_key("path").string_value().map(SmolStr::as_str);
1905+
let path_attr = attrs.by_key("path").string_value();
19061906
let is_macro_use = attrs.by_key("macro_use").exists();
19071907
let module = &self.item_tree[module_id];
19081908
match &module.kind {
@@ -2146,7 +2146,7 @@ impl ModCollector<'_, '_> {
21462146
Some(it) => {
21472147
// FIXME: a hacky way to create a Name from string.
21482148
name = tt::Ident {
2149-
text: it.clone(),
2149+
text: it.into(),
21502150
span: Span {
21512151
range: syntax::TextRange::empty(syntax::TextSize::new(0)),
21522152
anchor: span::SpanAnchor {

crates/hir-expand/src/attrs.rs

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use intern::Interned;
88
use mbe::{syntax_node_to_token_tree, DelimiterKind, Punct};
99
use smallvec::{smallvec, SmallVec};
1010
use span::{Span, SyntaxContextId};
11-
use syntax::{ast, match_ast, AstNode, AstToken, SmolStr, SyntaxNode};
11+
use syntax::{ast, format_smolstr, match_ast, AstNode, AstToken, SmolStr, SyntaxNode};
1212
use triomphe::Arc;
1313

1414
use crate::{
@@ -49,11 +49,18 @@ impl RawAttrs {
4949
Either::Left(attr) => {
5050
attr.meta().and_then(|meta| Attr::from_src(db, meta, span_map, id))
5151
}
52-
Either::Right(comment) => comment.doc_comment().map(|doc| Attr {
53-
id,
54-
input: Some(Interned::new(AttrInput::Literal(SmolStr::new(doc)))),
55-
path: Interned::new(ModPath::from(crate::name!(doc))),
56-
ctxt: span_map.span_for_range(comment.syntax().text_range()).ctx,
52+
Either::Right(comment) => comment.doc_comment().map(|doc| {
53+
let span = span_map.span_for_range(comment.syntax().text_range());
54+
Attr {
55+
id,
56+
input: Some(Interned::new(AttrInput::Literal(tt::Literal {
57+
// FIXME: Escape quotes from comment content
58+
text: SmolStr::new(format_smolstr!("\"{doc}\"",)),
59+
span,
60+
}))),
61+
path: Interned::new(ModPath::from(crate::name!(doc))),
62+
ctxt: span.ctx,
63+
}
5764
}),
5865
});
5966
let entries: Arc<[Attr]> = Arc::from_iter(entries);
@@ -179,16 +186,15 @@ pub struct Attr {
179186
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
180187
pub enum AttrInput {
181188
/// `#[attr = "string"]`
182-
// FIXME: This is losing span
183-
Literal(SmolStr),
189+
Literal(tt::Literal),
184190
/// `#[attr(subtree)]`
185191
TokenTree(Box<tt::Subtree>),
186192
}
187193

188194
impl fmt::Display for AttrInput {
189195
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
190196
match self {
191-
AttrInput::Literal(lit) => write!(f, " = \"{}\"", lit.escape_debug()),
197+
AttrInput::Literal(lit) => write!(f, " = {lit}"),
192198
AttrInput::TokenTree(tt) => tt.fmt(f),
193199
}
194200
}
@@ -208,11 +214,10 @@ impl Attr {
208214
})?);
209215
let span = span_map.span_for_range(range);
210216
let input = if let Some(ast::Expr::Literal(lit)) = ast.expr() {
211-
let value = match lit.kind() {
212-
ast::LiteralKind::String(string) => string.value()?.into(),
213-
_ => lit.syntax().first_token()?.text().trim_matches('"').into(),
214-
};
215-
Some(Interned::new(AttrInput::Literal(value)))
217+
Some(Interned::new(AttrInput::Literal(tt::Literal {
218+
text: lit.token().text().into(),
219+
span,
220+
})))
216221
} else if let Some(tt) = ast.token_tree() {
217222
let tree = syntax_node_to_token_tree(tt.syntax(), span_map, span);
218223
Some(Interned::new(AttrInput::TokenTree(Box::new(tree))))
@@ -245,9 +250,8 @@ impl Attr {
245250
}
246251
Some(tt::TokenTree::Leaf(tt::Leaf::Punct(tt::Punct { char: '=', .. }))) => {
247252
let input = match input.get(1) {
248-
Some(tt::TokenTree::Leaf(tt::Leaf::Literal(tt::Literal { text, .. }))) => {
249-
//FIXME the trimming here isn't quite right, raw strings are not handled
250-
Some(Interned::new(AttrInput::Literal(text.trim_matches('"').into())))
253+
Some(tt::TokenTree::Leaf(tt::Leaf::Literal(lit))) => {
254+
Some(Interned::new(AttrInput::Literal(lit.clone())))
251255
}
252256
_ => None,
253257
};
@@ -265,9 +269,14 @@ impl Attr {
265269

266270
impl Attr {
267271
/// #[path = "string"]
268-
pub fn string_value(&self) -> Option<&SmolStr> {
272+
pub fn string_value(&self) -> Option<&str> {
269273
match self.input.as_deref()? {
270-
AttrInput::Literal(it) => Some(it),
274+
AttrInput::Literal(it) => match it.text.strip_prefix('r') {
275+
Some(it) => it.trim_matches('#'),
276+
None => it.text.as_str(),
277+
}
278+
.strip_prefix('"')?
279+
.strip_suffix('"'),
271280
_ => None,
272281
}
273282
}

crates/hir/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2008,8 +2008,7 @@ impl Function {
20082008
}
20092009
let data = db.function_data(self.id);
20102010

2011-
data.name.to_smol_str() == "main"
2012-
|| data.attrs.export_name().map(core::ops::Deref::deref) == Some("main")
2011+
data.name.to_smol_str() == "main" || data.attrs.export_name() == Some("main")
20132012
}
20142013

20152014
/// Does this function have the ignore attribute?

crates/ide-completion/src/context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ impl CompletionContext<'_> {
540540
/// Whether the given trait is an operator trait or not.
541541
pub(crate) fn is_ops_trait(&self, trait_: hir::Trait) -> bool {
542542
match trait_.attrs(self.db).lang() {
543-
Some(lang) => OP_TRAIT_LANG_NAMES.contains(&lang.as_str()),
543+
Some(lang) => OP_TRAIT_LANG_NAMES.contains(&lang),
544544
None => false,
545545
}
546546
}

crates/ide/src/status.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use ide_db::{
1010
debug::{DebugQueryTable, TableEntry},
1111
Query, QueryTable,
1212
},
13-
CrateData, FileId, FileTextQuery, ParseQuery, SourceDatabase, SourceRootId,
13+
CompressedFileTextQuery, CrateData, FileId, ParseQuery, SourceDatabase, SourceRootId,
1414
},
1515
symbol_index::ModuleSymbolsQuery,
1616
};
@@ -38,7 +38,7 @@ use triomphe::Arc;
3838
pub(crate) fn status(db: &RootDatabase, file_id: Option<FileId>) -> String {
3939
let mut buf = String::new();
4040

41-
format_to!(buf, "{}\n", collect_query(FileTextQuery.in_db(db)));
41+
format_to!(buf, "{}\n", collect_query(CompressedFileTextQuery.in_db(db)));
4242
format_to!(buf, "{}\n", collect_query(ParseQuery.in_db(db)));
4343
format_to!(buf, "{}\n", collect_query(ParseMacroExpansionQuery.in_db(db)));
4444
format_to!(buf, "{}\n", collect_query(LibrarySymbolsQuery.in_db(db)));
@@ -160,7 +160,7 @@ impl QueryCollect for ParseMacroExpansionQuery {
160160
type Collector = SyntaxTreeStats<true>;
161161
}
162162

163-
impl QueryCollect for FileTextQuery {
163+
impl QueryCollect for CompressedFileTextQuery {
164164
type Collector = FilesStats;
165165
}
166166

@@ -188,8 +188,8 @@ impl fmt::Display for FilesStats {
188188
}
189189
}
190190

191-
impl StatCollect<FileId, Arc<str>> for FilesStats {
192-
fn collect_entry(&mut self, _: FileId, value: Option<Arc<str>>) {
191+
impl StatCollect<FileId, Arc<[u8]>> for FilesStats {
192+
fn collect_entry(&mut self, _: FileId, value: Option<Arc<[u8]>>) {
193193
self.total += 1;
194194
self.size += value.unwrap().len();
195195
}

0 commit comments

Comments
 (0)