Skip to content

Commit 1a3feee

Browse files
committed
Make Semantics<'db, dyn HirDatabase> actually usable
1 parent 6acff6c commit 1a3feee

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

crates/hir/src/semantics.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,26 +159,42 @@ pub struct SemanticsImpl<'db> {
159159
macro_call_cache: RefCell<FxHashMap<InFile<ast::MacroCall>, MacroCallId>>,
160160
}
161161

162-
impl<DB> fmt::Debug for Semantics<'_, DB> {
162+
impl<DB: ?Sized> fmt::Debug for Semantics<'_, DB> {
163163
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
164164
write!(f, "Semantics {{ ... }}")
165165
}
166166
}
167167

168-
impl<'db, DB> ops::Deref for Semantics<'db, DB> {
168+
impl<'db, DB: ?Sized> ops::Deref for Semantics<'db, DB> {
169169
type Target = SemanticsImpl<'db>;
170170

171171
fn deref(&self) -> &Self::Target {
172172
&self.imp
173173
}
174174
}
175175

176+
// Note: while this variant of `Semantics<'_, _>` might seem unused, as it does not
177+
// find actual use within the rust-analyzer project itself, it exists to enable the use
178+
// within e.g. tracked salsa functions in third-party crates that build upon `ra_ap_hir`.
179+
impl Semantics<'_, dyn HirDatabase> {
180+
/// Creates an instance that's weakly coupled to its underlying database type.
181+
pub fn new_dyn(db: &'_ dyn HirDatabase) -> Semantics<'_, dyn HirDatabase> {
182+
let impl_ = SemanticsImpl::new(db);
183+
Semantics { db, imp: impl_ }
184+
}
185+
}
186+
176187
impl<DB: HirDatabase> Semantics<'_, DB> {
188+
/// Creates an instance that's strongly coupled to its underlying database type.
177189
pub fn new(db: &DB) -> Semantics<'_, DB> {
178190
let impl_ = SemanticsImpl::new(db);
179191
Semantics { db, imp: impl_ }
180192
}
193+
}
181194

195+
// Note: We take `DB` as `?Sized` here in order to support type-erased
196+
// use of `Semantics` via `Semantics<'_, dyn HirDatabase>`:
197+
impl<DB: HirDatabase + ?Sized> Semantics<'_, DB> {
182198
pub fn hir_file_for(&self, syntax_node: &SyntaxNode) -> HirFileId {
183199
self.imp.find_file(syntax_node).file_id
184200
}

0 commit comments

Comments
 (0)