Skip to content

Commit 39d992e

Browse files
Intern Attr, MacroCall and Path components
1 parent b00266b commit 39d992e

File tree

7 files changed

+21
-17
lines changed

7 files changed

+21
-17
lines changed

crates/hir_def/src/attr.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use tt::Subtree;
1818

1919
use crate::{
2020
db::DefDatabase,
21+
intern::Interned,
2122
item_tree::{ItemTreeId, ItemTreeNode},
2223
nameres::ModuleSource,
2324
path::{ModPath, PathKind},
@@ -98,7 +99,7 @@ impl RawAttrs {
9899
Either::Right(comment) => comment.doc_comment().map(|doc| Attr {
99100
index: i as u32,
100101
input: Some(AttrInput::Literal(SmolStr::new(doc))),
101-
path: ModPath::from(hir_expand::name!(doc)),
102+
path: Interned::new(ModPath::from(hir_expand::name!(doc))),
102103
}),
103104
})
104105
.collect::<Arc<_>>();
@@ -510,7 +511,7 @@ impl AttrSourceMap {
510511
#[derive(Debug, Clone, PartialEq, Eq)]
511512
pub struct Attr {
512513
index: u32,
513-
pub(crate) path: ModPath,
514+
pub(crate) path: Interned<ModPath>,
514515
pub(crate) input: Option<AttrInput>,
515516
}
516517

@@ -524,7 +525,7 @@ pub enum AttrInput {
524525

525526
impl Attr {
526527
fn from_src(ast: ast::Attr, hygiene: &Hygiene, index: u32) -> Option<Attr> {
527-
let path = ModPath::from_src(ast.path()?, hygiene)?;
528+
let path = Interned::new(ModPath::from_src(ast.path()?, hygiene)?);
528529
let input = if let Some(ast::Expr::Literal(lit)) = ast.expr() {
529530
let value = match lit.kind() {
530531
ast::LiteralKind::String(string) => string.value()?.into(),

crates/hir_def/src/intern.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use rustc_hash::FxHasher;
1515

1616
type InternMap<T> = DashMap<Arc<T>, (), BuildHasherDefault<FxHasher>>;
1717

18+
#[derive(Hash)]
1819
pub struct Interned<T: Internable> {
1920
arc: Arc<T>,
2021
}
@@ -152,6 +153,6 @@ macro_rules! impl_internable {
152153
)+ };
153154
}
154155

155-
impl_internable!(crate::type_ref::TypeRef, crate::type_ref::TraitRef);
156+
impl_internable!(crate::type_ref::TypeRef, crate::type_ref::TraitRef, crate::path::ModPath);
156157

157158
// endregion

crates/hir_def/src/item_tree.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,7 @@ pub enum ModKind {
694694
#[derive(Debug, Clone, Eq, PartialEq)]
695695
pub struct MacroCall {
696696
/// Path to the called macro.
697-
pub path: ModPath,
697+
pub path: Interned<ModPath>,
698698
pub ast_id: FileAstId<ast::MacroCall>,
699699
}
700700

crates/hir_def/src/item_tree/lower.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,7 @@ impl Ctx {
606606
}
607607

608608
fn lower_macro_call(&mut self, m: &ast::MacroCall) -> Option<FileItemTreeId<MacroCall>> {
609-
let path = ModPath::from_src(m.path()?, &self.hygiene)?;
609+
let path = Interned::new(ModPath::from_src(m.path()?, &self.hygiene)?);
610610
let ast_id = self.source_ast_id_map.ast_id(m);
611611
let res = MacroCall { path, ast_id };
612612
Some(id(self.data().macro_calls.alloc(res)))

crates/hir_def/src/nameres/collector.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1464,7 +1464,7 @@ impl ModCollector<'_, '_> {
14641464
}
14651465

14661466
fn collect_macro_call(&mut self, mac: &MacroCall) {
1467-
let mut ast_id = AstIdWithPath::new(self.file_id, mac.ast_id, mac.path.clone());
1467+
let mut ast_id = AstIdWithPath::new(self.file_id, mac.ast_id, (*mac.path).clone());
14681468

14691469
// Case 1: try to resolve in legacy scope and expand macro_rules
14701470
let mut error = None;

crates/hir_def/src/path.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::{
77
sync::Arc,
88
};
99

10-
use crate::{body::LowerCtx, type_ref::LifetimeRef};
10+
use crate::{body::LowerCtx, intern::Interned, type_ref::LifetimeRef};
1111
use base_db::CrateId;
1212
use hir_expand::{
1313
hygiene::Hygiene,
@@ -48,7 +48,7 @@ pub enum ImportAlias {
4848

4949
impl ModPath {
5050
pub fn from_src(path: ast::Path, hygiene: &Hygiene) -> Option<ModPath> {
51-
lower::lower_path(path, hygiene).map(|it| it.mod_path)
51+
lower::lower_path(path, hygiene).map(|it| (*it.mod_path).clone())
5252
}
5353

5454
pub fn from_segments(kind: PathKind, segments: impl IntoIterator<Item = Name>) -> ModPath {
@@ -123,7 +123,7 @@ pub struct Path {
123123
/// Type based path like `<T>::foo`.
124124
/// Note that paths like `<Type as Trait>::foo` are desugard to `Trait::<Self=Type>::foo`.
125125
type_anchor: Option<Box<TypeRef>>,
126-
mod_path: ModPath,
126+
mod_path: Interned<ModPath>,
127127
/// Invariant: the same len as `self.mod_path.segments`
128128
generic_args: Vec<Option<Arc<GenericArgs>>>,
129129
}
@@ -176,7 +176,7 @@ impl Path {
176176
path: ModPath,
177177
generic_args: Vec<Option<Arc<GenericArgs>>>,
178178
) -> Path {
179-
Path { type_anchor: None, mod_path: path, generic_args }
179+
Path { type_anchor: None, mod_path: Interned::new(path), generic_args }
180180
}
181181

182182
pub fn kind(&self) -> &PathKind {
@@ -204,10 +204,10 @@ impl Path {
204204
}
205205
let res = Path {
206206
type_anchor: self.type_anchor.clone(),
207-
mod_path: ModPath::from_segments(
207+
mod_path: Interned::new(ModPath::from_segments(
208208
self.mod_path.kind.clone(),
209209
self.mod_path.segments[..self.mod_path.segments.len() - 1].iter().cloned(),
210-
),
210+
)),
211211
generic_args: self.generic_args[..self.generic_args.len() - 1].to_vec(),
212212
};
213213
Some(res)
@@ -283,7 +283,7 @@ impl From<Name> for Path {
283283
fn from(name: Name) -> Path {
284284
Path {
285285
type_anchor: None,
286-
mod_path: ModPath::from_segments(PathKind::Plain, iter::once(name)),
286+
mod_path: Interned::new(ModPath::from_segments(PathKind::Plain, iter::once(name))),
287287
generic_args: vec![None],
288288
}
289289
}

crates/hir_def/src/path/lower.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
mod lower_use;
44

5+
use crate::intern::Interned;
56
use std::sync::Arc;
67

78
use either::Either;
@@ -74,10 +75,11 @@ pub(super) fn lower_path(mut path: ast::Path, hygiene: &Hygiene) -> Option<Path>
7475
// <T as Trait<A>>::Foo desugars to Trait<Self=T, A>::Foo
7576
Some(trait_ref) => {
7677
let path = Path::from_src(trait_ref.path()?, hygiene)?;
78+
let mod_path = (*path.mod_path).clone();
7779
let num_segments = path.mod_path.segments.len();
78-
kind = path.mod_path.kind;
80+
kind = mod_path.kind;
7981

80-
let mut prefix_segments = path.mod_path.segments;
82+
let mut prefix_segments = mod_path.segments;
8183
prefix_segments.reverse();
8284
segments.extend(prefix_segments);
8385

@@ -140,7 +142,7 @@ pub(super) fn lower_path(mut path: ast::Path, hygiene: &Hygiene) -> Option<Path>
140142
}
141143
}
142144

143-
let mod_path = ModPath::from_segments(kind, segments);
145+
let mod_path = Interned::new(ModPath::from_segments(kind, segments));
144146
return Some(Path { type_anchor, mod_path, generic_args });
145147

146148
fn qualifier(path: &ast::Path) -> Option<ast::Path> {

0 commit comments

Comments
 (0)