Skip to content

Commit

Permalink
Auto merge of rust-lang#14857 - Veykril:perf, r=Veykril
Browse files Browse the repository at this point in the history
internal: Shrink ProcMacroExpander from 8 to 4 bytes
  • Loading branch information
bors committed May 20, 2023
2 parents bb78059 + 14dc1ac commit 4de8c09
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
23 changes: 13 additions & 10 deletions crates/hir-expand/src/proc_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,23 @@ use crate::{db::ExpandDatabase, tt, ExpandError, ExpandResult};

#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
pub struct ProcMacroExpander {
proc_macro_id: Option<ProcMacroId>,
proc_macro_id: ProcMacroId,
}

const DUMMY_ID: u32 = !0;

impl ProcMacroExpander {
pub fn new(proc_macro_id: ProcMacroId) -> Self {
Self { proc_macro_id: Some(proc_macro_id) }
assert_ne!(proc_macro_id.0, DUMMY_ID);
Self { proc_macro_id }
}

pub fn dummy() -> Self {
Self { proc_macro_id: None }
Self { proc_macro_id: ProcMacroId(DUMMY_ID) }
}

pub fn is_dummy(&self) -> bool {
self.proc_macro_id.is_none()
self.proc_macro_id.0 == DUMMY_ID
}

pub fn expand(
Expand All @@ -32,7 +35,10 @@ impl ProcMacroExpander {
attr_arg: Option<&tt::Subtree>,
) -> ExpandResult<tt::Subtree> {
match self.proc_macro_id {
Some(id) => {
ProcMacroId(DUMMY_ID) => {
ExpandResult::new(tt::Subtree::empty(), ExpandError::UnresolvedProcMacro(def_crate))
}
ProcMacroId(id) => {
let proc_macros = db.proc_macros();
let proc_macros = match proc_macros.get(&def_crate) {
Some(Ok(proc_macros)) => proc_macros,
Expand All @@ -44,13 +50,13 @@ impl ProcMacroExpander {
);
}
};
let proc_macro = match proc_macros.get(id.0 as usize) {
let proc_macro = match proc_macros.get(id as usize) {
Some(proc_macro) => proc_macro,
None => {
never!(
"Proc macro index out of bounds: the length is {} but the index is {}",
proc_macros.len(),
id.0
id
);
return ExpandResult::new(
tt::Subtree::empty(),
Expand Down Expand Up @@ -81,9 +87,6 @@ impl ProcMacroExpander {
},
}
}
None => {
ExpandResult::new(tt::Subtree::empty(), ExpandError::UnresolvedProcMacro(def_crate))
}
}
}
}
2 changes: 1 addition & 1 deletion crates/hir/src/symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl DeclarationLocation {
pub fn original_name_range(&self, db: &dyn HirDatabase) -> Option<FileRange> {
if let Some(file_id) = self.hir_file_id.file_id() {
// fast path to prevent parsing
return Some(FileRange { file_id, range: self.ptr.text_range() });
return Some(FileRange { file_id, range: self.name_ptr.text_range() });
}
let node = resolve_node(db, self.hir_file_id, &self.name_ptr);
node.as_ref().original_file_range_opt(db.upcast())
Expand Down

0 comments on commit 4de8c09

Please sign in to comment.