Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Guard against infinite loop in recursive solver #569

Merged
merged 2 commits into from
Aug 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions chalk-recursive/src/fulfill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,9 +447,17 @@ impl<'s, I: Interner, Solver: SolveDatabase<I>, Infer: RecursiveInferenceTable<I
solution,
} = self.prove(wc, minimums)?;

if solution.has_definite() {
if let Some(constrained_subst) =
solution.constrained_subst(self.interner())
if let Some(constrained_subst) = solution.definite_subst(self.interner()) {
// If the substitution is empty, we won't actually make any progress by applying it!
// So we need to check this to prevent endless loops.
// (An ambiguous solution with empty substitution
// can probably not happen in valid code, but it can
// happen e.g. when there are overlapping impls.)
if !constrained_subst.value.subst.is_empty(self.interner())
|| !constrained_subst
.value
.constraints
.is_empty(self.interner())
{
self.apply_solution(free_vars, universes, constrained_subst);
progress = true;
Expand Down
21 changes: 15 additions & 6 deletions chalk-recursive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,21 @@ impl<I: Interner> Solution<I> {
}

/// Determine whether this solution contains type information that *must*
/// hold.
pub(crate) fn has_definite(&self) -> bool {
match *self {
Solution::Unique(_) => true,
Solution::Ambig(Guidance::Definite(_)) => true,
_ => false,
/// hold, and returns the subst in that case.
pub(crate) fn definite_subst(&self, interner: &I) -> Option<Canonical<ConstrainedSubst<I>>> {
match self {
Solution::Unique(constrained) => Some(constrained.clone()),
Solution::Ambig(Guidance::Definite(canonical)) => {
let value = ConstrainedSubst {
subst: canonical.value.clone(),
constraints: Constraints::empty(interner),
};
Some(Canonical {
value,
binders: canonical.binders.clone(),
})
}
_ => None,
}
}

Expand Down
32 changes: 32 additions & 0 deletions tests/test/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -727,3 +727,35 @@ fn canonicalization_regression() {
}
}
}

#[test]
fn empty_definite_guidance() {
test! {
disable_coherence;
program {
trait Trait<T> {}

struct S<'a> {}
struct A {}

impl<'a> Trait<S<'a>> for A {}
impl<'a> Trait<S<'a>> for A where A: 'a {}

trait OtherTrait<'a> {}
impl<'a> OtherTrait<'a> for A where A: Trait<S<'a>> {}
}

goal {
forall<'static> {
A: OtherTrait<'static>
}
// the program fails coherence, so which answer we get here exactly
// isn't that important -- this is mainly a regression test for a
// recursive solver infinite loop.
} yields[SolverChoice::slg_default()] {
"Unique"
} yields[SolverChoice::recursive()] {
"Ambiguous"
}
}
}
14 changes: 11 additions & 3 deletions tests/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ pub enum TestGoal {
macro_rules! test {
(program $program:tt $($goals:tt)*) => {{
let (program, goals) = parse_test_data!(program $program $($goals)*);
solve_goal(program, goals)
solve_goal(program, goals, true)
}};
(disable_coherence; program $program:tt $($goals:tt)*) => {{
let (program, goals) = parse_test_data!(program $program $($goals)*);
solve_goal(program, goals, false)
}};
}

Expand Down Expand Up @@ -215,7 +219,7 @@ macro_rules! parse_test_data {
};
}

fn solve_goal(program_text: &str, goals: Vec<(&str, SolverChoice, TestGoal)>) {
fn solve_goal(program_text: &str, goals: Vec<(&str, SolverChoice, TestGoal)>, coherence: bool) {
with_tracing_logs(|| {
println!("program {}", program_text);
assert!(program_text.starts_with("{"));
Expand All @@ -226,7 +230,11 @@ fn solve_goal(program_text: &str, goals: Vec<(&str, SolverChoice, TestGoal)>) {
SolverChoice::default(),
);

let program = db.checked_program().unwrap();
let program = if coherence {
db.checked_program().unwrap()
} else {
db.program_ir().unwrap()
};

for (goal_text, solver_choice, expected) in goals {
match (&solver_choice, &expected) {
Expand Down