Fix comparison of proc macros

Comparing the TypeId is not enough, they also contain data.
This commit is contained in:
Chayim Refael Friedman 2025-06-12 13:22:44 +03:00
parent c15fc9a344
commit 4f54885901
4 changed files with 52 additions and 6 deletions

View file

@ -14,7 +14,7 @@ mod builtin_fn_macro;
mod mbe;
mod proc_macros;
use std::{iter, ops::Range, sync};
use std::{any::TypeId, iter, ops::Range, sync};
use base_db::RootQueryDb;
use expect_test::Expect;
@ -380,4 +380,8 @@ impl ProcMacroExpander for IdentityWhenValidProcMacroExpander {
panic!("got invalid macro input: {:?}", parse.errors());
}
}
fn eq_dyn(&self, other: &dyn ProcMacroExpander) -> bool {
other.type_id() == TypeId::of::<Self>()
}
}

View file

@ -34,9 +34,7 @@ pub trait ProcMacroExpander: fmt::Debug + Send + Sync + RefUnwindSafe + Any {
current_dir: String,
) -> Result<tt::TopSubtree, ProcMacroExpansionError>;
fn eq_dyn(&self, other: &dyn ProcMacroExpander) -> bool {
other.type_id() == self.type_id()
}
fn eq_dyn(&self, other: &dyn ProcMacroExpander) -> bool;
}
impl PartialEq for dyn ProcMacroExpander {

View file

@ -2,7 +2,7 @@
//! for incorporating changes.
// Note, don't remove any public api from this. This API is consumed by external tools
// to run rust-analyzer as a library.
use std::{collections::hash_map::Entry, mem, path::Path, sync};
use std::{any::Any, collections::hash_map::Entry, mem, path::Path, sync};
use crossbeam_channel::{Receiver, unbounded};
use hir_expand::proc_macro::{
@ -512,6 +512,10 @@ impl ProcMacroExpander for Expander {
Err(err) => Err(ProcMacroExpansionError::System(err.to_string())),
}
}
fn eq_dyn(&self, other: &dyn ProcMacroExpander) -> bool {
(other as &dyn Any).downcast_ref::<Self>() == Some(self)
}
}
#[cfg(test)]

View file

@ -1,5 +1,5 @@
//! A set of high-level utility fixture methods to use in tests.
use std::{mem, str::FromStr, sync};
use std::{any::TypeId, mem, str::FromStr, sync};
use base_db::{
Crate, CrateDisplayName, CrateGraphBuilder, CrateName, CrateOrigin, CrateWorkspaceData,
@ -677,6 +677,10 @@ impl ProcMacroExpander for IdentityProcMacroExpander {
) -> Result<TopSubtree, ProcMacroExpansionError> {
Ok(subtree.clone())
}
fn eq_dyn(&self, other: &dyn ProcMacroExpander) -> bool {
other.type_id() == TypeId::of::<Self>()
}
}
// Expands to a macro_rules! macro, for issue #18089.
@ -708,6 +712,10 @@ impl ProcMacroExpander for Issue18089ProcMacroExpander {
#subtree
})
}
fn eq_dyn(&self, other: &dyn ProcMacroExpander) -> bool {
other.type_id() == TypeId::of::<Self>()
}
}
// Pastes the attribute input as its output
@ -728,6 +736,10 @@ impl ProcMacroExpander for AttributeInputReplaceProcMacroExpander {
.cloned()
.ok_or_else(|| ProcMacroExpansionError::Panic("Expected attribute input".into()))
}
fn eq_dyn(&self, other: &dyn ProcMacroExpander) -> bool {
other.type_id() == TypeId::of::<Self>()
}
}
#[derive(Debug)]
@ -759,6 +771,10 @@ impl ProcMacroExpander for Issue18840ProcMacroExpander {
top_subtree_delimiter_mut.close = def_site;
Ok(result)
}
fn eq_dyn(&self, other: &dyn ProcMacroExpander) -> bool {
other.type_id() == TypeId::of::<Self>()
}
}
#[derive(Debug)]
@ -790,6 +806,10 @@ impl ProcMacroExpander for MirrorProcMacroExpander {
traverse(&mut builder, input.iter());
Ok(builder.build())
}
fn eq_dyn(&self, other: &dyn ProcMacroExpander) -> bool {
other.type_id() == TypeId::of::<Self>()
}
}
// Replaces every literal with an empty string literal and every identifier with its first letter,
@ -830,6 +850,10 @@ impl ProcMacroExpander for ShortenProcMacroExpander {
}
}
}
fn eq_dyn(&self, other: &dyn ProcMacroExpander) -> bool {
other.type_id() == TypeId::of::<Self>()
}
}
// Reads ident type within string quotes, for issue #17479.
@ -855,6 +879,10 @@ impl ProcMacroExpander for Issue17479ProcMacroExpander {
#symbol()
})
}
fn eq_dyn(&self, other: &dyn ProcMacroExpander) -> bool {
other.type_id() == TypeId::of::<Self>()
}
}
// Reads ident type within string quotes, for issue #17479.
@ -906,6 +934,10 @@ impl ProcMacroExpander for Issue18898ProcMacroExpander {
}
})
}
fn eq_dyn(&self, other: &dyn ProcMacroExpander) -> bool {
other.type_id() == TypeId::of::<Self>()
}
}
// Reads ident type within string quotes, for issue #17479.
@ -933,6 +965,10 @@ impl ProcMacroExpander for DisallowCfgProcMacroExpander {
}
Ok(subtree.clone())
}
fn eq_dyn(&self, other: &dyn ProcMacroExpander) -> bool {
other.type_id() == TypeId::of::<Self>()
}
}
// Generates a new type by adding a suffix to the original name
@ -987,4 +1023,8 @@ impl ProcMacroExpander for GenerateSuffixedTypeProcMacroExpander {
Ok(ret)
}
fn eq_dyn(&self, other: &dyn ProcMacroExpander) -> bool {
other.type_id() == TypeId::of::<Self>()
}
}