Skip to content

Commit 536faf5

Browse files
committed
Illustrate how Semantics<'db, DB: ?Sized> can't actually work
1 parent 634596e commit 536faf5

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

crates/hir/src/semantics.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,23 +159,27 @@ 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-
impl<DB: HirDatabase> Semantics<'_, DB> {
176+
impl<DB: HirDatabase + ?Sized> Semantics<'_, DB> {
177177
pub fn new(db: &DB) -> Semantics<'_, DB> {
178+
// This doesn't work:
178179
let impl_ = SemanticsImpl::new(db);
180+
// Neither does this:
181+
let impl_ = SemanticsImpl::new(db as &(dyn HirDatabase));
182+
179183
Semantics { db, imp: impl_ }
180184
}
181185

@@ -2387,3 +2391,16 @@ impl RenameConflictsVisitor<'_> {
23872391
self.body.walk_child_exprs(expr, |expr| self.rename_conflicts(expr));
23882392
}
23892393
}
2394+
2395+
#[cfg(test)]
2396+
mod tests {
2397+
use hir_ty::db::HirDatabase;
2398+
2399+
use super::*;
2400+
2401+
#[test]
2402+
fn semantics_with_dyn_db() {
2403+
let maybe_db: Option<&dyn HirDatabase> = None;
2404+
let maybe_sema = maybe_db.map(|db| Semantics::new(db));
2405+
}
2406+
}

0 commit comments

Comments
 (0)