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

Commit 4de8c09

Browse files
committed
Auto merge of rust-lang#14857 - Veykril:perf, r=Veykril
internal: Shrink ProcMacroExpander from 8 to 4 bytes
2 parents bb78059 + 14dc1ac commit 4de8c09

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

crates/hir-expand/src/proc_macro.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,23 @@ use crate::{db::ExpandDatabase, tt, ExpandError, ExpandResult};
77

88
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
99
pub struct ProcMacroExpander {
10-
proc_macro_id: Option<ProcMacroId>,
10+
proc_macro_id: ProcMacroId,
1111
}
1212

13+
const DUMMY_ID: u32 = !0;
14+
1315
impl ProcMacroExpander {
1416
pub fn new(proc_macro_id: ProcMacroId) -> Self {
15-
Self { proc_macro_id: Some(proc_macro_id) }
17+
assert_ne!(proc_macro_id.0, DUMMY_ID);
18+
Self { proc_macro_id }
1619
}
1720

1821
pub fn dummy() -> Self {
19-
Self { proc_macro_id: None }
22+
Self { proc_macro_id: ProcMacroId(DUMMY_ID) }
2023
}
2124

2225
pub fn is_dummy(&self) -> bool {
23-
self.proc_macro_id.is_none()
26+
self.proc_macro_id.0 == DUMMY_ID
2427
}
2528

2629
pub fn expand(
@@ -32,7 +35,10 @@ impl ProcMacroExpander {
3235
attr_arg: Option<&tt::Subtree>,
3336
) -> ExpandResult<tt::Subtree> {
3437
match self.proc_macro_id {
35-
Some(id) => {
38+
ProcMacroId(DUMMY_ID) => {
39+
ExpandResult::new(tt::Subtree::empty(), ExpandError::UnresolvedProcMacro(def_crate))
40+
}
41+
ProcMacroId(id) => {
3642
let proc_macros = db.proc_macros();
3743
let proc_macros = match proc_macros.get(&def_crate) {
3844
Some(Ok(proc_macros)) => proc_macros,
@@ -44,13 +50,13 @@ impl ProcMacroExpander {
4450
);
4551
}
4652
};
47-
let proc_macro = match proc_macros.get(id.0 as usize) {
53+
let proc_macro = match proc_macros.get(id as usize) {
4854
Some(proc_macro) => proc_macro,
4955
None => {
5056
never!(
5157
"Proc macro index out of bounds: the length is {} but the index is {}",
5258
proc_macros.len(),
53-
id.0
59+
id
5460
);
5561
return ExpandResult::new(
5662
tt::Subtree::empty(),
@@ -81,9 +87,6 @@ impl ProcMacroExpander {
8187
},
8288
}
8389
}
84-
None => {
85-
ExpandResult::new(tt::Subtree::empty(), ExpandError::UnresolvedProcMacro(def_crate))
86-
}
8790
}
8891
}
8992
}

crates/hir/src/symbols.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl DeclarationLocation {
5050
pub fn original_name_range(&self, db: &dyn HirDatabase) -> Option<FileRange> {
5151
if let Some(file_id) = self.hir_file_id.file_id() {
5252
// fast path to prevent parsing
53-
return Some(FileRange { file_id, range: self.ptr.text_range() });
53+
return Some(FileRange { file_id, range: self.name_ptr.text_range() });
5454
}
5555
let node = resolve_node(db, self.hir_file_id, &self.name_ptr);
5656
node.as_ref().original_file_range_opt(db.upcast())

0 commit comments

Comments
 (0)