Skip to content

Commit 154020e

Browse files
authored
Merge pull request #389 from Areredify/truncator
port truncator to use visitor
2 parents 6222e41 + 6a6a4dd commit 154020e

File tree

5 files changed

+93
-302
lines changed

5 files changed

+93
-302
lines changed

chalk-engine/src/context.rs

+6-13
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

+9-10
Original file line numberDiff line numberDiff line change
@@ -328,9 +328,10 @@ 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+
if infer.goal_needs_truncation(context.interner(), subgoal) {
332+
None
333+
} else {
334+
Some(infer.fully_canonicalize_goal(context.interner(), subgoal))
334335
}
335336
}
336337

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

388-
match infer.truncate_goal(context.interner(), &inverted_subgoal) {
389-
Some(_) => None,
390-
None => Some(infer.fully_canonicalize_goal(context.interner(), &inverted_subgoal)),
389+
if infer.goal_needs_truncation(context.interner(), &inverted_subgoal) {
390+
None
391+
} else {
392+
Some(infer.fully_canonicalize_goal(context.interner(), &inverted_subgoal))
391393
}
392394
}
393395
}
@@ -1331,10 +1333,7 @@ impl<'forest, C: Context + 'forest, CO: ContextOps<C> + 'forest> SolveState<'for
13311333
// Ultimately, the current decision to flounder the entire table mostly boils
13321334
// down to "it works as we expect for the current tests". And, we likely don't
13331335
// even *need* the added complexity just for potentially more answers.
1334-
if infer
1335-
.truncate_answer(self.context.interner(), &subst)
1336-
.is_some()
1337-
{
1336+
if infer.answer_needs_truncation(self.context.interner(), &subst) {
13381337
self.forest.tables[table].mark_floundered();
13391338
return None;
13401339
}

chalk-solve/src/infer.rs

-5
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,6 @@ impl<I: Interner> InferenceTable<I> {
7979
u
8080
}
8181

82-
/// Current maximum universe -- one that can see all existing names.
83-
pub(crate) fn max_universe(&self) -> UniverseIndex {
84-
self.max_universe
85-
}
86-
8782
/// Creates a new inference variable and returns its index. The
8883
/// kind of the variable should be known by the caller, but is not
8984
/// tracked directly by the inference table.

chalk-solve/src/solve/slg.rs

+6-30
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;
@@ -347,38 +347,14 @@ impl<I: Interner> TruncatingInferenceTable<I> {
347347
}
348348

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

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

0 commit comments

Comments
 (0)