8000 fix: Fix comparison of proc macros by ChayimFriedman2 · Pull Request #19983 · rust-lang/rust-analyzer · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fix: Fix comparison of proc macros #19983

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion crates/hir-def/src/macro_expansion_tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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>()
}
}
4 changes: 1 addition & 3 deletions crates/hir-expand/src/proc_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 5 additions & 1 deletion crates/load-cargo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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)]
Expand Down
42 changes: 41 additions & 1 deletion crates/test-fixture/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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)]
Expand Down Expand Up @@ -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)]
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -987,4 +1023,8 @@ impl ProcMacroExpander for GenerateSuffixedTypeProcMacroExpander {

Ok(ret)
}

fn eq_dyn(&self, other: &dyn ProcMacroExpander) -> bool {
other.type_id() == TypeId::of::<Self>()
}
}
0