Skip to content

Commit cbc0e62

Browse files
committed
port truncator to use visitor
1 parent 2c72044 commit cbc0e62

File tree

5 files changed

+92
-297
lines changed

5 files changed

+92
-297
lines changed

chalk-engine/src/context.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -369,8 +369,7 @@ pub trait UnificationOps<C: Context> {
369369
/// refers to the act of modifying a goal or answer that has become
370370
/// too large in order to guarantee termination.
371371
///
372-
/// The SLG solver doesn't care about the precise truncation function,
373-
/// so long as it's deterministic and so forth.
372+
/// Currently we don't perform truncation (but it might me readded later).
374373
///
375374
/// Citations:
376375
///
@@ -379,21 +378,15 @@ pub trait UnificationOps<C: Context> {
379378
/// - Radial Restraint
380379
/// - Grosof and Swift; 2013
381380
pub trait TruncateOps<C: Context> {
382-
/// If `subgoal` is too large, return a truncated variant (else
383-
/// return `None`).
384-
fn truncate_goal(
381+
/// Check if `subgoal` is too large
382+
fn goal_needs_truncation(
385383
&mut self,
386384
interner: &C::Interner,
387385
subgoal: &C::GoalInEnvironment,
388-
) -> Option<C::GoalInEnvironment>;
386+
) -> bool;
389387

390-
/// If `subst` is too large, return a truncated variant (else
391-
/// return `None`).
392-
fn truncate_answer(
393-
&mut self,
394-
interner: &C::Interner,
395-
subst: &C::Substitution,
396-
) -> Option<C::Substitution>;
388+
/// Check if `subst` is too large
389+
fn answer_needs_truncation(&mut self, interner: &C::Interner, subst: &C::Substitution) -> bool;
397390
}
398391

399392
pub trait ResolventOps<C: Context> {

chalk-engine/src/logic.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -328,9 +328,9 @@ impl<C: Context> Forest<C> {
328328
infer: &mut dyn InferenceTable<C>,
329329
subgoal: &C::GoalInEnvironment,
330330
) -> Option<(C::UCanonicalGoalInEnvironment, C::UniverseMap)> {
331-
match infer.truncate_goal(context.interner(), subgoal) {
332-
Some(_) => None,
333-
None => Some(infer.fully_canonicalize_goal(context.interner(), subgoal)),
331+
match infer.goal_needs_truncation(context.interner(), subgoal) {
332+
true => None,
333+
false => Some(infer.fully_canonicalize_goal(context.interner(), subgoal)),
334334
}
335335
}
336336

@@ -385,9 +385,9 @@ impl<C: Context> Forest<C> {
385385
// affect completeness when it comes to subgoal abstraction.
386386
let inverted_subgoal = infer.invert_goal(context.interner(), subgoal)?;
387387

388-
match infer.truncate_goal(context.interner(), &inverted_subgoal) {
389-
Some(_) => None,
390-
None => Some(infer.fully_canonicalize_goal(context.interner(), &inverted_subgoal)),
388+
match infer.goal_needs_truncation(context.interner(), &inverted_subgoal) {
389+
true => None,
390+
false => Some(infer.fully_canonicalize_goal(context.interner(), &inverted_subgoal)),
391391
}
392392
}
393393
}
@@ -1331,10 +1331,7 @@ impl<'forest, C: Context + 'forest, CO: ContextOps<C> + 'forest> SolveState<'for
13311331
// Ultimately, the current decision to flounder the entire table mostly boils
13321332
// down to "it works as we expect for the current tests". And, we likely don't
13331333
// even *need* the added complexity just for potentially more answers.
1334-
if infer
1335-
.truncate_answer(self.context.interner(), &subst)
1336-
.is_some()
1337-
{
1334+
if infer.answer_needs_truncation(self.context.interner(), &subst) {
13381335
self.forest.tables[table].mark_floundered();
13391336
return None;
13401337
}

chalk-solve/src/infer.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ impl<I: Interner> InferenceTable<I> {
8080
}
8181

8282
/// Current maximum universe -- one that can see all existing names.
83+
#[allow(dead_code)]
8384
pub(crate) fn max_universe(&self) -> UniverseIndex {
8485
self.max_universe
8586
}

chalk-solve/src/solve/slg.rs

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::coinductive_goal::IsCoinductive;
33
use crate::infer::ucanonicalize::{UCanonicalized, UniverseMap};
44
use crate::infer::unify::UnificationResult;
55
use crate::infer::InferenceTable;
6-
use crate::solve::truncate::{self, Truncated};
6+
use crate::solve::truncate;
77
use crate::solve::Solution;
88
use crate::RustIrDatabase;
99
use chalk_derive::HasInterner;
@@ -348,38 +348,14 @@ impl<I: Interner> TruncatingInferenceTable<I> {
348348
}
349349

350350
impl<I: Interner> context::TruncateOps<SlgContext<I>> for TruncatingInferenceTable<I> {
351-
fn truncate_goal(
352-
&mut self,
353-
interner: &I,
354-
subgoal: &InEnvironment<Goal<I>>,
355-
) -> Option<InEnvironment<Goal<I>>> {
356-
// We only want to truncate the goal itself. We keep the environment intact.
351+
fn goal_needs_truncation(&mut self, interner: &I, subgoal: &InEnvironment<Goal<I>>) -> bool {
352+
// We only want to check the goal itself. We keep the environment intact.
357353
// See rust-lang/chalk#280
358-
let InEnvironment { environment, goal } = subgoal;
359-
let Truncated { overflow, value } =
360-
truncate::truncate(interner, &mut self.infer, self.max_size, goal);
361-
if overflow {
362-
Some(InEnvironment {
363-
environment: environment.clone(),
364-
goal: value,
365-
})
366-
} else {
367-
None
368-
}
354+
truncate::needs_truncation(interner, &mut self.infer, self.max_size, &subgoal.goal)
369355
}
370356

371-
fn truncate_answer(
372-
&mut self,
373-
interner: &I,
374-
subst: &Substitution<I>,
375-
) -> Option<Substitution<I>> {
376-
let Truncated { overflow, value } =
377-
truncate::truncate(interner, &mut self.infer, self.max_size, subst);
378-
if overflow {
379-
Some(value)
380-
} else {
381-
None
382-
}
357+
fn answer_needs_truncation(&mut self, interner: &I, subst: &Substitution<I>) -> bool {
358+
truncate::needs_truncation(interner, &mut self.infer, self.max_size, subst)
383359
}
384360
}
385361

0 commit comments

Comments
 (0)