Skip to main content

hir_ide/
impl_db_macro.rs

1//! A macro to implement `HirDatabase` for any (`Sized`) type implementing `SourceDatabase`.
2
3#[macro_export]
4macro_rules! impl_hir_database {
5    ($ty:ty) => {
6        const _: () = {
7            use $crate::{
8                __private::{hir_def::ModuleId, salsa},
9                db::HirDatabase,
10                display::{DisplayTarget, HirDisplay},
11                next_solver::Ty,
12            };
13
14            #[salsa::db]
15            impl $crate::db::HirDatabase for $ty {
16                fn as_dyn(&self) -> &dyn HirDatabase {
17                    self
18                }
19
20                fn type_name<'db>(&'db self, ty: Ty<'db>, module: ModuleId) -> String {
21                    match ty.display_source_code(self, module, true) {
22                        Ok(ty_name) => ty_name,
23                        // Fallback to human readable display in case of `Err`. Ideally we want to use `display_source_code` to
24                        // render full paths.
25                        Err(_) => {
26                            let krate = module.krate(self);
27                            ty.display(self, DisplayTarget::from_crate(self, krate)).to_string()
28                        }
29                    }
30                }
31            }
32        };
33    };
34}