Skip to content

Commit 5a591ee

Browse files
committed
Remove actual SlgContext creation
1 parent 514aacc commit 5a591ee

File tree

5 files changed

+52
-50
lines changed

5 files changed

+52
-50
lines changed

chalk-engine/src/context.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ pub trait AggregateOps<I: Interner, C: Context<I>> {
148148
fn make_solution(
149149
&self,
150150
root_goal: &UCanonical<InEnvironment<Goal<I>>>,
151-
answers: impl AnswerStream<I, C>,
151+
answers: impl AnswerStream<I>,
152152
should_continue: impl Fn() -> bool,
153153
) -> Option<C::Solution>;
154154
}
@@ -330,7 +330,7 @@ impl<I: Interner> Debug for AnswerResult<I> {
330330
}
331331
}
332332

333-
pub trait AnswerStream<I: Interner, C: Context<I>> {
333+
pub trait AnswerStream<I: Interner> {
334334
/// Gets the next answer for a given goal, but doesn't increment the answer index.
335335
/// Calling this or `next_answer` again will give the same answer.
336336
fn peek_answer(&mut self, should_continue: impl Fn() -> bool) -> AnswerResult<I>;

chalk-engine/src/forest.rs

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,25 @@ use chalk_ir::interner::Interner;
99
use chalk_ir::{Canonical, ConstrainedSubst, Goal, InEnvironment, Substitution, UCanonical};
1010

1111
pub struct Forest<I: Interner, C: Context<I>> {
12-
context: C,
1312
pub(crate) tables: Tables<I>,
1413

1514
/// This is a clock which always increases. It is
1615
/// incremented every time a new subgoal is followed.
1716
/// This effectively gives us way to track what depth
1817
/// and loop a table or strand was last followed.
1918
pub(crate) clock: TimeStamp,
19+
_context: std::marker::PhantomData<C>,
2020
}
2121

2222
impl<I: Interner, C: Context<I>> Forest<I, C> {
23-
pub fn new(context: C) -> Self {
23+
pub fn new() -> Self {
2424
Forest {
25-
context,
2625
tables: Tables::new(),
2726
clock: TimeStamp::default(),
27+
_context: std::marker::PhantomData,
2828
}
2929
}
3030

31-
/// Gives access to `self.context`. In fact, the SLG solver
32-
/// doesn't ever use `self.context` for anything, and only cares
33-
/// about the associated types and methods defined on it. But the
34-
/// creator of the forest can use the context field to store
35-
/// configuration info (e.g., in chalk, we store the max size of a
36-
/// term in here).
37-
pub fn context(&self) -> &C {
38-
&self.context
39-
}
40-
4131
// Gets the next clock TimeStamp. This will never decrease.
4232
pub(crate) fn increment_clock(&mut self) -> TimeStamp {
4333
self.clock.increment();
@@ -52,14 +42,15 @@ impl<I: Interner, C: Context<I>> Forest<I, C> {
5242
&'f mut self,
5343
context: &'f impl ContextOps<I, C>,
5444
goal: &UCanonical<InEnvironment<Goal<I>>>,
55-
) -> impl AnswerStream<I, C> + 'f {
45+
) -> impl AnswerStream<I> + 'f {
5646
let table = self.get_or_create_table_for_ucanonical_goal(context, goal.clone());
5747
let answer = AnswerIndex::ZERO;
5848
ForestSolver {
5949
forest: self,
6050
context,
6151
table,
6252
answer,
53+
_context: std::marker::PhantomData::<C>,
6354
}
6455
}
6556

@@ -148,9 +139,10 @@ struct ForestSolver<'me, I: Interner, C: Context<I>, CO: ContextOps<I, C>> {
148139
context: &'me CO,
149140
table: TableIndex,
150141
answer: AnswerIndex,
142+
_context: std::marker::PhantomData<C>,
151143
}
152144

153-
impl<'me, I: Interner, C: Context<I>, CO: ContextOps<I, C>> AnswerStream<I, C>
145+
impl<'me, I: Interner, C: Context<I>, CO: ContextOps<I, C>> AnswerStream<I>
154146
for ForestSolver<'me, I, C, CO>
155147
{
156148
/// # Panics

chalk-solve/src/solve.rs

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::fmt;
55

66
#[cfg(feature = "slg-solver")]
77
use {
8-
crate::solve::slg::SlgContext,
8+
crate::solve::slg::{SlgContext, SlgContextOps},
99
chalk_engine::forest::{Forest, SubstitutionResult},
1010
};
1111

@@ -233,7 +233,9 @@ impl SolverChoice {
233233
max_size,
234234
expected_answers,
235235
} => Solver(SolverImpl::Slg {
236-
forest: Box::new(Forest::new(SlgContext::new(max_size, expected_answers))),
236+
forest: Box::new(Forest::new()),
237+
max_size,
238+
expected_answers,
237239
}),
238240
#[cfg(feature = "recursive-solver")]
239241
SolverChoice::Recursive {
@@ -271,6 +273,12 @@ enum SolverImpl<I: Interner> {
271273
#[cfg(feature = "slg-solver")]
272274
Slg {
273275
forest: Box<Forest<I, SlgContext<I>>>,
276+
max_size: usize,
277+
/// The expected number of answers for a solution.
278+
/// Only really sseful for tests, since `make_solution`
279+
/// will panic if the number of cached answers does not
280+
/// equal this when a solution is made.
281+
expected_answers: Option<usize>,
274282
},
275283
#[cfg(feature = "recursive-solver")]
276284
Recursive(Box<RecursiveContext<I>>),
@@ -302,8 +310,12 @@ impl<I: Interner> Solver<I> {
302310
) -> Option<Solution<I>> {
303311
match &mut self.0 {
304312
#[cfg(feature = "slg-solver")]
305-
SolverImpl::Slg { forest } => {
306-
let ops = forest.context().ops(program);
313+
SolverImpl::Slg {
314+
forest,
315+
max_size,
316+
expected_answers,
317+
} => {
318+
let ops = SlgContextOps::new(program, *max_size, *expected_answers);
307319
forest.solve(&ops, goal, || true)
308320
}
309321
#[cfg(feature = "recursive-solver")]
@@ -341,8 +353,12 @@ impl<I: Interner> Solver<I> {
341353
) -> Option<Solution<I>> {
342354
match &mut self.0 {
343355
#[cfg(feature = "slg-solver")]
344-
SolverImpl::Slg { forest } => {
345-
let ops = forest.context().ops(program);
356+
SolverImpl::Slg {
357+
forest,
358+
max_size,
359+
expected_answers,
360+
} => {
361+
let ops = SlgContextOps::new(program, *max_size, *expected_answers);
346362
forest.solve(&ops, goal, should_continue)
347363
}
348364
#[cfg(feature = "recursive-solver")]
@@ -383,8 +399,12 @@ impl<I: Interner> Solver<I> {
383399
f: impl FnMut(SubstitutionResult<Canonical<ConstrainedSubst<I>>>, bool) -> bool,
384400
) -> bool {
385401
match &mut self.0 {
386-
SolverImpl::Slg { forest } => {
387-
let ops = forest.context().ops(program);
402+
SolverImpl::Slg {
403+
forest,
404+
max_size,
405+
expected_answers,
406+
} => {
407+
let ops = SlgContextOps::new(program, *max_size, *expected_answers);
388408
forest.solve_multiple(&ops, goal, f)
389409
}
390410
#[cfg(feature = "recursive-solver")]

chalk-solve/src/solve/slg.rs

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,40 +23,30 @@ mod resolvent;
2323

2424
#[derive(Clone, Debug, HasInterner)]
2525
pub(crate) struct SlgContext<I: Interner> {
26-
max_size: usize,
27-
/// The expected number of answers for a solution.
28-
/// Only really sseful for tests, since `make_solution`
29-
/// will panic if the number of cached answers does not
30-
/// equal this when a solution is made.
31-
expected_answers: Option<usize>,
3226
phantom: PhantomData<I>,
3327
}
3428

35-
impl<I: Interner> SlgContext<I> {
36-
pub(crate) fn new(max_size: usize, expected_answers: Option<usize>) -> SlgContext<I> {
37-
SlgContext {
38-
max_size,
39-
expected_answers,
40-
phantom: PhantomData,
41-
}
42-
}
43-
44-
pub(crate) fn ops<'p>(&self, program: &'p dyn RustIrDatabase<I>) -> SlgContextOps<'p, I> {
45-
SlgContextOps {
46-
program,
47-
max_size: self.max_size,
48-
expected_answers: self.expected_answers,
49-
}
50-
}
51-
}
52-
5329
#[derive(Clone, Debug)]
5430
pub(crate) struct SlgContextOps<'me, I: Interner> {
5531
program: &'me dyn RustIrDatabase<I>,
5632
max_size: usize,
5733
expected_answers: Option<usize>,
5834
}
5935

36+
impl<I: Interner> SlgContextOps<'_, I> {
37+
pub(crate) fn new<'p>(
38+
program: &'p dyn RustIrDatabase<I>,
39+
max_size: usize,
40+
expected_answers: Option<usize>,
41+
) -> SlgContextOps<'p, I> {
42+
SlgContextOps {
43+
program,
44+
max_size,
45+
expected_answers,
46+
}
47+
}
48+
}
49+
6050
#[derive(Clone)]
6151
pub struct TruncatingInferenceTable<I: Interner> {
6252
max_size: usize,

chalk-solve/src/solve/slg/aggregate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ impl<I: Interner> context::AggregateOps<I, SlgContext<I>> for SlgContextOps<'_,
1818
fn make_solution(
1919
&self,
2020
root_goal: &UCanonical<InEnvironment<Goal<I>>>,
21-
mut answers: impl context::AnswerStream<I, SlgContext<I>>,
21+
mut answers: impl context::AnswerStream<I>,
2222
should_continue: impl std::ops::Fn() -> bool,
2323
) -> Option<Solution<I>> {
2424
let interner = self.program.interner();

0 commit comments

Comments
 (0)