cairo_lang_semantic/
ids.rs

1use std::hash::{Hash, Hasher};
2use std::sync::Arc;
3
4use cairo_lang_utils::define_short_id;
5
6use crate::db::SemanticGroup;
7use crate::plugin::AnalyzerPlugin;
8
9/// An Id allowing interning [`AnalyzerPlugin`] into Salsa database.
10#[derive(Clone, Debug)]
11pub struct AnalyzerPluginLongId(pub Arc<dyn AnalyzerPlugin>);
12
13impl AnalyzerPlugin for AnalyzerPluginLongId {
14    fn diagnostics(
15        &self,
16        db: &dyn crate::db::SemanticGroup,
17        module_id: cairo_lang_defs::ids::ModuleId,
18    ) -> Vec<cairo_lang_defs::plugin::PluginDiagnostic> {
19        self.0.diagnostics(db, module_id)
20    }
21
22    fn declared_allows(&self) -> Vec<String> {
23        self.0.declared_allows()
24    }
25
26    fn plugin_type_id(&self) -> std::any::TypeId {
27        // Ensure the implementation for `AnalyzerPluginLongId` returns the same value
28        // as the underlying plugin object.
29        self.0.plugin_type_id()
30    }
31}
32
33// `PartialEq` and `Hash` cannot be derived on `Arc<dyn ...>`,
34// but pointer-based equality and hash semantics are enough in this case.
35impl PartialEq for AnalyzerPluginLongId {
36    fn eq(&self, other: &Self) -> bool {
37        Arc::ptr_eq(&self.0, &other.0)
38    }
39}
40
41impl Eq for AnalyzerPluginLongId {}
42
43impl Hash for AnalyzerPluginLongId {
44    fn hash<H: Hasher>(&self, state: &mut H) {
45        Arc::as_ptr(&self.0).hash(state)
46    }
47}
48
49define_short_id!(
50    AnalyzerPluginId,
51    AnalyzerPluginLongId,
52    SemanticGroup,
53    lookup_intern_analyzer_plugin,
54    intern_analyzer_plugin
55);