Skip to content

Commit 6a0f4e5

Browse files
Fix comparison of proc macros
Comparing the TypeId is not enough, they also contain data.
1 parent fa7a6c1 commit 6a0f4e5

File tree

4 files changed

+52
-6
lines changed

4 files changed

+52
-6
lines changed

src/tools/rust-analyzer/crates/hir-def/src/macro_expansion_tests/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ mod builtin_fn_macro;
1414
mod mbe;
1515
mod proc_macros;
1616

17-
use std::{iter, ops::Range, sync};
17+
use std::{any::TypeId, iter, ops::Range, sync};
1818

1919
use base_db::RootQueryDb;
2020
use expect_test::Expect;
@@ -380,4 +380,8 @@ impl ProcMacroExpander for IdentityWhenValidProcMacroExpander {
380380
panic!("got invalid macro input: {:?}", parse.errors());
381381
}
382382
}
383+
384+
fn eq_dyn(&self, other: &dyn ProcMacroExpander) -> bool {
385+
other.type_id() == TypeId::of::<Self>()
386+
}
383387
}

src/tools/rust-analyzer/crates/hir-expand/src/proc_macro.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@ pub trait ProcMacroExpander: fmt::Debug + Send + Sync + RefUnwindSafe + Any {
3434
current_dir: String,
3535
) -> Result<tt::TopSubtree, ProcMacroExpansionError>;
3636

37-
fn eq_dyn(&self, other: &dyn ProcMacroExpander) -> bool {
38-
other.type_id() == self.type_id()
39-
}
37+
fn eq_dyn(&self, other: &dyn ProcMacroExpander) -> bool;
4038
}
4139

4240
impl PartialEq for dyn ProcMacroExpander {

src/tools/rust-analyzer/crates/load-cargo/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//! for incorporating changes.
33
// Note, don't remove any public api from this. This API is consumed by external tools
44
// to run rust-analyzer as a library.
5-
use std::{collections::hash_map::Entry, mem, path::Path, sync};
5+
use std::{any::Any, collections::hash_map::Entry, mem, path::Path, sync};
66

77
use crossbeam_channel::{Receiver, unbounded};
88
use hir_expand::proc_macro::{
@@ -512,6 +512,10 @@ impl ProcMacroExpander for Expander {
512512
Err(err) => Err(ProcMacroExpansionError::System(err.to_string())),
513513
}
514514
}
515+
516+
fn eq_dyn(&self, other: &dyn ProcMacroExpander) -> bool {
517+
(other as &dyn Any).downcast_ref::<Self>() == Some(self)
518+
}
515519
}
516520

517521
#[cfg(test)]

src/tools/rust-analyzer/crates/test-fixture/src/lib.rs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//! A set of high-level utility fixture methods to use in tests.
2-
use std::{mem, str::FromStr, sync};
2+
use std::{any::TypeId, mem, str::FromStr, sync};
33

44
use base_db::{
55
Crate, CrateDisplayName, CrateGraphBuilder, CrateName, CrateOrigin, CrateWorkspaceData,
@@ -677,6 +677,10 @@ impl ProcMacroExpander for IdentityProcMacroExpander {
677677
) -> Result<TopSubtree, ProcMacroExpansionError> {
678678
Ok(subtree.clone())
679679
}
680+
681+
fn eq_dyn(&self, other: &dyn ProcMacroExpander) -> bool {
682+
other.type_id() == TypeId::of::<Self>()
683+
}
680684
}
681685

682686
// Expands to a macro_rules! macro, for issue #18089.
@@ -708,6 +712,10 @@ impl ProcMacroExpander for Issue18089ProcMacroExpander {
708712
#subtree
709713
})
710714
}
715+
716+
fn eq_dyn(&self, other: &dyn ProcMacroExpander) -> bool {
717+
other.type_id() == TypeId::of::<Self>()
718+
}
711719
}
712720

713721
// Pastes the attribute input as its output
@@ -728,6 +736,10 @@ impl ProcMacroExpander for AttributeInputReplaceProcMacroExpander {
728736
.cloned()
729737
.ok_or_else(|| ProcMacroExpansionError::Panic("Expected attribute input".into()))
730738
}
739+
740+
fn eq_dyn(&self, other: &dyn ProcMacroExpander) -> bool {
741+
other.type_id() == TypeId::of::<Self>()
742+
}
731743
}
732744

733745
#[derive(Debug)]
@@ -759,6 +771,10 @@ impl ProcMacroExpander for Issue18840ProcMacroExpander {
759771
top_subtree_delimiter_mut.close = def_site;
760772
Ok(result)
761773
}
774+
775+
fn eq_dyn(&self, other: &dyn ProcMacroExpander) -> bool {
776+
other.type_id() == TypeId::of::<Self>()
777+
}
762778
}
763779

764780
#[derive(Debug)]
@@ -790,6 +806,10 @@ impl ProcMacroExpander for MirrorProcMacroExpander {
790806
traverse(&mut builder, input.iter());
791807
Ok(builder.build())
792808
}
809+
810+
fn eq_dyn(&self, other: &dyn ProcMacroExpander) -> bool {
811+
other.type_id() == TypeId::of::<Self>()
812+
}
793813
}
794814

795815
// Replaces every literal with an empty string literal and every identifier with its first letter,
@@ -830,6 +850,10 @@ impl ProcMacroExpander for ShortenProcMacroExpander {
830850
}
831851
}
832852
}
853+
854+
fn eq_dyn(&self, other: &dyn ProcMacroExpander) -> bool {
855+
other.type_id() == TypeId::of::<Self>()
856+
}
833857
}
834858

835859
// Reads ident type within string quotes, for issue #17479.
@@ -855,6 +879,10 @@ impl ProcMacroExpander for Issue17479ProcMacroExpander {
855879
#symbol()
856880
})
857881
}
882+
883+
fn eq_dyn(&self, other: &dyn ProcMacroExpander) -> bool {
884+
other.type_id() == TypeId::of::<Self>()
885+
}
858886
}
859887

860888
// Reads ident type within string quotes, for issue #17479.
@@ -906,6 +934,10 @@ impl ProcMacroExpander for Issue18898ProcMacroExpander {
906934
}
907935
})
908936
}
937+
938+
fn eq_dyn(&self, other: &dyn ProcMacroExpander) -> bool {
939+
other.type_id() == TypeId::of::<Self>()
940+
}
909941
}
910942

911943
// Reads ident type within string quotes, for issue #17479.
@@ -933,6 +965,10 @@ impl ProcMacroExpander for DisallowCfgProcMacroExpander {
933965
}
934966
Ok(subtree.clone())
935967
}
968+
969+
fn eq_dyn(&self, other: &dyn ProcMacroExpander) -> bool {
970+
other.type_id() == TypeId::of::<Self>()
971+
}
936972
}
937973

938974
// Generates a new type by adding a suffix to the original name
@@ -987,4 +1023,8 @@ impl ProcMacroExpander for GenerateSuffixedTypeProcMacroExpander {
9871023

9881024
Ok(ret)
9891025
}
1026+
1027+
fn eq_dyn(&self, other: &dyn ProcMacroExpander) -> bool {
1028+
other.type_id() == TypeId::of::<Self>()
1029+
}
9901030
}

0 commit comments

Comments
 (0)