Skip to content

Commit ab681b9

Browse files
committed
Extract struct DynSemantics<'db> from struct Semantics<'db, DB>
1 parent 76ee900 commit ab681b9

File tree

5 files changed

+69
-40
lines changed

5 files changed

+69
-40
lines changed

crates/hir/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ pub use crate::{
9797
diagnostics::*,
9898
has_source::HasSource,
9999
semantics::{
100-
PathResolution, PathResolutionPerNs, Semantics, SemanticsImpl, SemanticsScope, TypeInfo,
101-
VisibleTraits,
100+
DynSemantics, PathResolution, PathResolutionPerNs, Semantics, SemanticsImpl,
101+
SemanticsScope, TypeInfo, VisibleTraits,
102102
},
103103
};
104104

crates/hir/src/semantics.rs

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -146,17 +146,11 @@ impl TypeInfo {
146146
}
147147
}
148148

149-
/// Primary API to get semantic information, like types, from syntax trees.
149+
/// Primary API to get semantic information, like types, from syntax trees,
150+
/// with tight coupling to underlying database.
150151
pub struct Semantics<'db, DB> {
151152
pub db: &'db DB,
152-
imp: SemanticsImpl<'db>,
153-
}
154-
155-
pub struct SemanticsImpl<'db> {
156-
pub db: &'db dyn HirDatabase,
157-
s2d_cache: RefCell<SourceToDefCache>,
158-
/// MacroCall to its expansion's MacroCallId cache
159-
macro_call_cache: RefCell<FxHashMap<InFile<ast::MacroCall>, MacroCallId>>,
153+
sema: DynSemantics<'db>,
160154
}
161155

162156
impl<DB> fmt::Debug for Semantics<'_, DB> {
@@ -166,17 +160,46 @@ impl<DB> fmt::Debug for Semantics<'_, DB> {
166160
}
167161

168162
impl<'db, DB> ops::Deref for Semantics<'db, DB> {
163+
type Target = DynSemantics<'db>;
164+
165+
fn deref(&self) -> &Self::Target {
166+
&self.sema
167+
}
168+
}
169+
170+
impl<'db, DB: HirDatabase> Semantics<'db, DB> {
171+
pub fn new(db: &'db DB) -> Semantics<'db, DB> {
172+
let sema = DynSemantics::new(db);
173+
Self { db, sema }
174+
}
175+
176+
pub fn into_dyn(self) -> DynSemantics<'db> {
177+
self.sema
178+
}
179+
}
180+
181+
/// Primary API to get semantic information, like types, from syntax trees.
182+
pub struct DynSemantics<'db> {
183+
imp: SemanticsImpl<'db>,
184+
}
185+
186+
impl fmt::Debug for DynSemantics<'_> {
187+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
188+
write!(f, "Semantics {{ ... }}")
189+
}
190+
}
191+
192+
impl<'db> ops::Deref for DynSemantics<'db> {
169193
type Target = SemanticsImpl<'db>;
170194

171195
fn deref(&self) -> &Self::Target {
172196
&self.imp
173197
}
174198
}
175199

176-
impl<DB: HirDatabase> Semantics<'_, DB> {
177-
pub fn new(db: &DB) -> Semantics<'_, DB> {
178-
let impl_ = SemanticsImpl::new(db);
179-
Semantics { db, imp: impl_ }
200+
impl<'db> DynSemantics<'db> {
201+
pub fn new(db: &'db dyn HirDatabase) -> Self {
202+
Self { imp: SemanticsImpl::new(db) }
180203
}
181204

182205
pub fn hir_file_for(&self, syntax_node: &SyntaxNode) -> HirFileId {
@@ -319,6 +342,13 @@ impl<DB: HirDatabase> Semantics<'_, DB> {
319342
}
320343
}
321344

345+
pub struct SemanticsImpl<'db> {
346+
pub db: &'db dyn HirDatabase,
347+
s2d_cache: RefCell<SourceToDefCache>,
348+
/// MacroCall to its expansion's MacroCallId cache
349+
macro_call_cache: RefCell<FxHashMap<InFile<ast::MacroCall>, MacroCallId>>,
350+
}
351+
322352
impl<'db> SemanticsImpl<'db> {
323353
fn new(db: &'db dyn HirDatabase) -> Self {
324354
SemanticsImpl { db, s2d_cache: Default::default(), macro_call_cache: Default::default() }

crates/hir/src/symbols.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use intern::Symbol;
1919
use rustc_hash::FxHashMap;
2020
use syntax::{AstNode, AstPtr, SmolStr, SyntaxNode, SyntaxNodePtr, ToSmolStr, ast::HasName};
2121

22-
use crate::{HasCrate, Module, ModuleDef, Semantics};
22+
use crate::{HasCrate, Module, ModuleDef, semantics::DynSemantics};
2323

2424
pub type FxIndexSet<T> = indexmap::IndexSet<T, std::hash::BuildHasherDefault<rustc_hash::FxHasher>>;
2525

@@ -48,7 +48,7 @@ pub struct DeclarationLocation {
4848
}
4949

5050
impl DeclarationLocation {
51-
pub fn syntax<DB: HirDatabase>(&self, sema: &Semantics<'_, DB>) -> SyntaxNode {
51+
pub fn syntax(&self, sema: &DynSemantics<'_>) -> SyntaxNode {
5252
let root = sema.parse_or_expand(self.hir_file_id);
5353
self.ptr.to_node(&root)
5454
}

crates/hir/src/term_search.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use hir_ty::db::HirDatabase;
55
use itertools::Itertools;
66
use rustc_hash::{FxHashMap, FxHashSet};
77

8-
use crate::{ModuleDef, ScopeDef, Semantics, SemanticsScope, Type};
8+
use crate::{DynSemantics, ModuleDef, ScopeDef, SemanticsScope, Type};
99

1010
mod expr;
1111
pub use expr::Expr;
@@ -214,9 +214,9 @@ impl LookupTable {
214214

215215
/// Context for the `term_search` function
216216
#[derive(Debug)]
217-
pub struct TermSearchCtx<'a, DB: HirDatabase> {
217+
pub struct TermSearchCtx<'a> {
218218
/// Semantics for the program
219-
pub sema: &'a Semantics<'a, DB>,
219+
pub sema: &'a DynSemantics<'a>,
220220
/// Semantic scope, captures context for the term search
221221
pub scope: &'a SemanticsScope<'a>,
222222
/// Target / expected output type
@@ -263,7 +263,7 @@ impl Default for TermSearchConfig {
263263
/// Note that there are usually more ways we can get to the `goal` type but some are discarded to
264264
/// reduce the memory consumption. It is also unlikely anyone is willing ti browse through
265265
/// thousands of possible responses so we currently take first 10 from every tactic.
266-
pub fn term_search<DB: HirDatabase>(ctx: &TermSearchCtx<'_, DB>) -> Vec<Expr> {
266+
pub fn term_search(ctx: &TermSearchCtx<'_>) -> Vec<Expr> {
267267
let module = ctx.scope.module();
268268
let mut defs = FxHashSet::default();
269269
defs.insert(ScopeDef::ModuleDef(ModuleDef::Module(module)));

crates/hir/src/term_search/tactics.rs

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use std::iter;
1212

1313
use hir_ty::TyBuilder;
14-
use hir_ty::db::HirDatabase;
1514
use hir_ty::mir::BorrowKind;
1615
use itertools::Itertools;
1716
use rustc_hash::FxHashSet;
@@ -40,8 +39,8 @@ use super::{LookupTable, NewTypesKey, TermSearchCtx};
4039
///
4140
/// _Note that there is no use of calling this tactic in every iteration as the output does not
4241
/// depend on the current state of `lookup`_
43-
pub(super) fn trivial<'a, DB: HirDatabase>(
44-
ctx: &'a TermSearchCtx<'a, DB>,
42+
pub(super) fn trivial<'a>(
43+
ctx: &'a TermSearchCtx<'a>,
4544
defs: &'a FxHashSet<ScopeDef>,
4645
lookup: &'a mut LookupTable,
4746
) -> impl Iterator<Item = Expr> + 'a {
@@ -104,8 +103,8 @@ pub(super) fn trivial<'a, DB: HirDatabase>(
104103
///
105104
/// _Note that there is no use of calling this tactic in every iteration as the output does not
106105
/// depend on the current state of `lookup`_
107-
pub(super) fn assoc_const<'a, DB: HirDatabase>(
108-
ctx: &'a TermSearchCtx<'a, DB>,
106+
pub(super) fn assoc_const<'a>(
107+
ctx: &'a TermSearchCtx<'a>,
109108
defs: &'a FxHashSet<ScopeDef>,
110109
lookup: &'a mut LookupTable,
111110
) -> impl Iterator<Item = Expr> + 'a {
@@ -152,8 +151,8 @@ pub(super) fn assoc_const<'a, DB: HirDatabase>(
152151
/// * `defs` - Set of items in scope at term search target location
153152
/// * `lookup` - Lookup table for types
154153
/// * `should_continue` - Function that indicates when to stop iterating
155-
pub(super) fn data_constructor<'a, DB: HirDatabase>(
156-
ctx: &'a TermSearchCtx<'a, DB>,
154+
pub(super) fn data_constructor<'a>(
155+
ctx: &'a TermSearchCtx<'a>,
157156
_defs: &'a FxHashSet<ScopeDef>,
158157
lookup: &'a mut LookupTable,
159158
should_continue: &'a dyn std::ops::Fn() -> bool,
@@ -301,8 +300,8 @@ pub(super) fn data_constructor<'a, DB: HirDatabase>(
301300
/// * `defs` - Set of items in scope at term search target location
302301
/// * `lookup` - Lookup table for types
303302
/// * `should_continue` - Function that indicates when to stop iterating
304-
pub(super) fn free_function<'a, DB: HirDatabase>(
305-
ctx: &'a TermSearchCtx<'a, DB>,
303+
pub(super) fn free_function<'a>(
304+
ctx: &'a TermSearchCtx<'a>,
306305
defs: &'a FxHashSet<ScopeDef>,
307306
lookup: &'a mut LookupTable,
308307
should_continue: &'a dyn std::ops::Fn() -> bool,
@@ -432,8 +431,8 @@ pub(super) fn free_function<'a, DB: HirDatabase>(
432431
/// * `defs` - Set of items in scope at term search target location
433432
/// * `lookup` - Lookup table for types
434433
/// * `should_continue` - Function that indicates when to stop iterating
435-
pub(super) fn impl_method<'a, DB: HirDatabase>(
436-
ctx: &'a TermSearchCtx<'a, DB>,
434+
pub(super) fn impl_method<'a>(
435+
ctx: &'a TermSearchCtx<'a>,
437436
_defs: &'a FxHashSet<ScopeDef>,
438437
lookup: &'a mut LookupTable,
439438
should_continue: &'a dyn std::ops::Fn() -> bool,
@@ -547,8 +546,8 @@ pub(super) fn impl_method<'a, DB: HirDatabase>(
547546
/// * `defs` - Set of items in scope at term search target location
548547
/// * `lookup` - Lookup table for types
549548
/// * `should_continue` - Function that indicates when to stop iterating
550-
pub(super) fn struct_projection<'a, DB: HirDatabase>(
551-
ctx: &'a TermSearchCtx<'a, DB>,
549+
pub(super) fn struct_projection<'a>(
550+
ctx: &'a TermSearchCtx<'a>,
552551
_defs: &'a FxHashSet<ScopeDef>,
553552
lookup: &'a mut LookupTable,
554553
should_continue: &'a dyn std::ops::Fn() -> bool,
@@ -589,8 +588,8 @@ pub(super) fn struct_projection<'a, DB: HirDatabase>(
589588
/// * `ctx` - Context for the term search
590589
/// * `defs` - Set of items in scope at term search target location
591590
/// * `lookup` - Lookup table for types
592-
pub(super) fn famous_types<'a, DB: HirDatabase>(
593-
ctx: &'a TermSearchCtx<'a, DB>,
591+
pub(super) fn famous_types<'a>(
592+
ctx: &'a TermSearchCtx<'a>,
594593
_defs: &'a FxHashSet<ScopeDef>,
595594
lookup: &'a mut LookupTable,
596595
) -> impl Iterator<Item = Expr> + 'a {
@@ -620,8 +619,8 @@ pub(super) fn famous_types<'a, DB: HirDatabase>(
620619
/// * `defs` - Set of items in scope at term search target location
621620
/// * `lookup` - Lookup table for types
622621
/// * `should_continue` - Function that indicates when to stop iterating
623-
pub(super) fn impl_static_method<'a, DB: HirDatabase>(
624-
ctx: &'a TermSearchCtx<'a, DB>,
622+
pub(super) fn impl_static_method<'a>(
623+
ctx: &'a TermSearchCtx<'a>,
625624
_defs: &'a FxHashSet<ScopeDef>,
626625
lookup: &'a mut LookupTable,
627626
should_continue: &'a dyn std::ops::Fn() -> bool,
@@ -722,8 +721,8 @@ pub(super) fn impl_static_method<'a, DB: HirDatabase>(
722721
/// * `defs` - Set of items in scope at term search target location
723722
/// * `lookup` - Lookup table for types
724723
/// * `should_continue` - Function that indicates when to stop iterating
725-
pub(super) fn make_tuple<'a, DB: HirDatabase>(
726-
ctx: &'a TermSearchCtx<'a, DB>,
724+
pub(super) fn make_tuple<'a>(
725+
ctx: &'a TermSearchCtx<'a>,
727726
_defs: &'a FxHashSet<ScopeDef>,
728727
lookup: &'a mut LookupTable,
729728
should_continue: &'a dyn std::ops::Fn() -> bool,

0 commit comments

Comments
 (0)