Skip to content

Commit 9c63137

Browse files
Merge #8978
8978: internal: intern `AttrInput` r=jonas-schievink a=jonas-schievink saves ~10 MB on r-a bors r+ Co-authored-by: Jonas Schievink <[email protected]>
2 parents 86ca176 + 31175a7 commit 9c63137

File tree

3 files changed

+14
-13
lines changed

3 files changed

+14
-13
lines changed

crates/hir_def/src/attr.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ impl RawAttrs {
105105
Either::Left(attr) => Attr::from_src(db, attr, hygiene, id),
106106
Either::Right(comment) => comment.doc_comment().map(|doc| Attr {
107107
id,
108-
input: Some(AttrInput::Literal(SmolStr::new(doc))),
108+
input: Some(Interned::new(AttrInput::Literal(SmolStr::new(doc)))),
109109
path: Interned::new(ModPath::from(hir_expand::name!(doc))),
110110
}),
111111
})
@@ -151,7 +151,7 @@ impl RawAttrs {
151151
return smallvec![attr.clone()];
152152
}
153153

154-
let subtree = match &attr.input {
154+
let subtree = match attr.input.as_deref() {
155155
Some(AttrInput::TokenTree(it)) => it,
156156
_ => return smallvec![attr.clone()],
157157
};
@@ -251,7 +251,7 @@ impl Attrs {
251251
}
252252

253253
pub fn docs(&self) -> Option<Documentation> {
254-
let docs = self.by_key("doc").attrs().flat_map(|attr| match attr.input.as_ref()? {
254+
let docs = self.by_key("doc").attrs().flat_map(|attr| match attr.input.as_deref()? {
255255
AttrInput::Literal(s) => Some(s),
256256
AttrInput::TokenTree(_) => None,
257257
});
@@ -454,7 +454,7 @@ impl AttrsWithOwner {
454454
db: &dyn DefDatabase,
455455
) -> Option<(Documentation, DocsRangeMap)> {
456456
// FIXME: code duplication in `docs` above
457-
let docs = self.by_key("doc").attrs().flat_map(|attr| match attr.input.as_ref()? {
457+
let docs = self.by_key("doc").attrs().flat_map(|attr| match attr.input.as_deref()? {
458458
AttrInput::Literal(s) => Some((s, attr.id)),
459459
AttrInput::TokenTree(_) => None,
460460
});
@@ -637,10 +637,10 @@ pub(crate) struct AttrId {
637637
pub struct Attr {
638638
pub(crate) id: AttrId,
639639
pub(crate) path: Interned<ModPath>,
640-
pub(crate) input: Option<AttrInput>,
640+
pub(crate) input: Option<Interned<AttrInput>>,
641641
}
642642

643-
#[derive(Debug, Clone, PartialEq, Eq)]
643+
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
644644
pub enum AttrInput {
645645
/// `#[attr = "string"]`
646646
Literal(SmolStr),
@@ -670,9 +670,9 @@ impl Attr {
670670
ast::LiteralKind::String(string) => string.value()?.into(),
671671
_ => lit.syntax().first_token()?.text().trim_matches('"').into(),
672672
};
673-
Some(AttrInput::Literal(value))
673+
Some(Interned::new(AttrInput::Literal(value)))
674674
} else if let Some(tt) = ast.token_tree() {
675-
Some(AttrInput::TokenTree(ast_to_token_tree(&tt).0))
675+
Some(Interned::new(AttrInput::TokenTree(ast_to_token_tree(&tt).0)))
676676
} else {
677677
None
678678
};
@@ -688,7 +688,7 @@ impl Attr {
688688
return None;
689689
}
690690

691-
match &self.input {
691+
match self.input.as_deref() {
692692
Some(AttrInput::TokenTree(args)) => {
693693
let mut counter = 0;
694694
let paths = args
@@ -720,7 +720,7 @@ impl Attr {
720720
}
721721

722722
pub fn string_value(&self) -> Option<&SmolStr> {
723-
match self.input.as_ref()? {
723+
match self.input.as_deref()? {
724724
AttrInput::Literal(it) => Some(it),
725725
_ => None,
726726
}
@@ -735,14 +735,14 @@ pub struct AttrQuery<'a> {
735735

736736
impl<'a> AttrQuery<'a> {
737737
pub fn tt_values(self) -> impl Iterator<Item = &'a Subtree> {
738-
self.attrs().filter_map(|attr| match attr.input.as_ref()? {
738+
self.attrs().filter_map(|attr| match attr.input.as_deref()? {
739739
AttrInput::TokenTree(it) => Some(it),
740740
_ => None,
741741
})
742742
}
743743

744744
pub fn string_value(self) -> Option<&'a SmolStr> {
745-
self.attrs().find_map(|attr| match attr.input.as_ref()? {
745+
self.attrs().find_map(|attr| match attr.input.as_deref()? {
746746
AttrInput::Literal(it) => Some(it),
747747
_ => None,
748748
})

crates/hir_def/src/intern.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ impl_internable!(
219219
crate::type_ref::TypeBound,
220220
crate::path::ModPath,
221221
crate::path::GenericArgs,
222+
crate::attr::AttrInput,
222223
GenericParams,
223224
str,
224225
);

crates/hir_def/src/nameres/collector.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ impl DefCollector<'_> {
285285
let registered_name = if *attr_name == hir_expand::name![register_attr]
286286
|| *attr_name == hir_expand::name![register_tool]
287287
{
288-
match &attr.input {
288+
match attr.input.as_deref() {
289289
Some(AttrInput::TokenTree(subtree)) => match &*subtree.token_trees {
290290
[tt::TokenTree::Leaf(tt::Leaf::Ident(name))] => name.as_name(),
291291
_ => continue,

0 commit comments

Comments
 (0)