From 31175a7f88bd593bc4ca5a37b24183c62a438366 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Tue, 25 May 2021 00:50:19 +0200 Subject: internal: intern `AttrInput` --- crates/hir_def/src/attr.rs | 24 ++++++++++++------------ crates/hir_def/src/intern.rs | 1 + crates/hir_def/src/nameres/collector.rs | 2 +- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/crates/hir_def/src/attr.rs b/crates/hir_def/src/attr.rs index 89a1ea770..385ba8c80 100644 --- a/crates/hir_def/src/attr.rs +++ b/crates/hir_def/src/attr.rs @@ -105,7 +105,7 @@ impl RawAttrs { Either::Left(attr) => Attr::from_src(db, attr, hygiene, id), Either::Right(comment) => comment.doc_comment().map(|doc| Attr { id, - input: Some(AttrInput::Literal(SmolStr::new(doc))), + input: Some(Interned::new(AttrInput::Literal(SmolStr::new(doc)))), path: Interned::new(ModPath::from(hir_expand::name!(doc))), }), }) @@ -151,7 +151,7 @@ impl RawAttrs { return smallvec![attr.clone()]; } - let subtree = match &attr.input { + let subtree = match attr.input.as_deref() { Some(AttrInput::TokenTree(it)) => it, _ => return smallvec![attr.clone()], }; @@ -251,7 +251,7 @@ impl Attrs { } pub fn docs(&self) -> Option { - let docs = self.by_key("doc").attrs().flat_map(|attr| match attr.input.as_ref()? { + let docs = self.by_key("doc").attrs().flat_map(|attr| match attr.input.as_deref()? { AttrInput::Literal(s) => Some(s), AttrInput::TokenTree(_) => None, }); @@ -454,7 +454,7 @@ impl AttrsWithOwner { db: &dyn DefDatabase, ) -> Option<(Documentation, DocsRangeMap)> { // FIXME: code duplication in `docs` above - let docs = self.by_key("doc").attrs().flat_map(|attr| match attr.input.as_ref()? { + let docs = self.by_key("doc").attrs().flat_map(|attr| match attr.input.as_deref()? { AttrInput::Literal(s) => Some((s, attr.id)), AttrInput::TokenTree(_) => None, }); @@ -637,10 +637,10 @@ pub(crate) struct AttrId { pub struct Attr { pub(crate) id: AttrId, pub(crate) path: Interned, - pub(crate) input: Option, + pub(crate) input: Option>, } -#[derive(Debug, Clone, PartialEq, Eq)] +#[derive(Debug, Clone, PartialEq, Eq, Hash)] pub enum AttrInput { /// `#[attr = "string"]` Literal(SmolStr), @@ -670,9 +670,9 @@ impl Attr { ast::LiteralKind::String(string) => string.value()?.into(), _ => lit.syntax().first_token()?.text().trim_matches('"').into(), }; - Some(AttrInput::Literal(value)) + Some(Interned::new(AttrInput::Literal(value))) } else if let Some(tt) = ast.token_tree() { - Some(AttrInput::TokenTree(ast_to_token_tree(&tt).0)) + Some(Interned::new(AttrInput::TokenTree(ast_to_token_tree(&tt).0))) } else { None }; @@ -688,7 +688,7 @@ impl Attr { return None; } - match &self.input { + match self.input.as_deref() { Some(AttrInput::TokenTree(args)) => { let mut counter = 0; let paths = args @@ -720,7 +720,7 @@ impl Attr { } pub fn string_value(&self) -> Option<&SmolStr> { - match self.input.as_ref()? { + match self.input.as_deref()? { AttrInput::Literal(it) => Some(it), _ => None, } @@ -735,14 +735,14 @@ pub struct AttrQuery<'a> { impl<'a> AttrQuery<'a> { pub fn tt_values(self) -> impl Iterator { - self.attrs().filter_map(|attr| match attr.input.as_ref()? { + self.attrs().filter_map(|attr| match attr.input.as_deref()? { AttrInput::TokenTree(it) => Some(it), _ => None, }) } pub fn string_value(self) -> Option<&'a SmolStr> { - self.attrs().find_map(|attr| match attr.input.as_ref()? { + self.attrs().find_map(|attr| match attr.input.as_deref()? { AttrInput::Literal(it) => Some(it), _ => None, }) diff --git a/crates/hir_def/src/intern.rs b/crates/hir_def/src/intern.rs index 1189c9327..79ba970e7 100644 --- a/crates/hir_def/src/intern.rs +++ b/crates/hir_def/src/intern.rs @@ -219,6 +219,7 @@ impl_internable!( crate::type_ref::TypeBound, crate::path::ModPath, crate::path::GenericArgs, + crate::attr::AttrInput, GenericParams, str, ); diff --git a/crates/hir_def/src/nameres/collector.rs b/crates/hir_def/src/nameres/collector.rs index 014ea4de4..2ae740d0e 100644 --- a/crates/hir_def/src/nameres/collector.rs +++ b/crates/hir_def/src/nameres/collector.rs @@ -285,7 +285,7 @@ impl DefCollector<'_> { let registered_name = if *attr_name == hir_expand::name![register_attr] || *attr_name == hir_expand::name![register_tool] { - match &attr.input { + match attr.input.as_deref() { Some(AttrInput::TokenTree(subtree)) => match &*subtree.token_trees { [tt::TokenTree::Leaf(tt::Leaf::Ident(name))] => name.as_name(), _ => continue, -- cgit v1.2.3