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

Allow tests to be updated automatically #744

Merged
merged 8 commits into from
Feb 13, 2022
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
17 changes: 17 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,6 @@ chalk-integration = { version = "0.76.0-dev.0", path = "chalk-integration" }
[dev-dependencies]
# used for program_writer test errors
diff = "0.1"
expect-test = "1.2.1"
pretty_assertions = "0.6.1"
regex = "1"
39 changes: 33 additions & 6 deletions chalk-ir/src/debug.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
//! Debug impls for types.

use std::fmt::{Debug, Display, Error, Formatter};
use std::fmt::{self, Debug, Display, Error, Formatter};

use super::*;

/// Wrapper to allow forwarding to `Display::fmt`, `Debug::fmt`, etc.
pub struct Fmt<F>(pub F)
where
F: Fn(&mut fmt::Formatter<'_>) -> fmt::Result;

impl<F> fmt::Display for Fmt<F>
where
F: Fn(&mut fmt::Formatter<'_>) -> fmt::Result,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
(self.0)(f)
}
}

impl<I: Interner> Debug for TraitId<I> {
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
I::debug_trait_id(*self, fmt).unwrap_or_else(|| write!(fmt, "TraitId({:?})", self.0))
Expand Down Expand Up @@ -959,14 +973,27 @@ impl<I: Interner> Debug for Constraint<I> {
}

impl<I: Interner> Display for ConstrainedSubst<I> {
#[rustfmt::skip]
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
let ConstrainedSubst { subst, constraints } = self;

write!(
f,
"substitution {}, lifetime constraints {:?}",
subst, constraints,
)
let mut first = true;

let subst = format!("{}", Fmt(|f| Display::fmt(subst, f)));
if subst != "[]" {
write!(f, "substitution {}", subst)?;
first = false;
}

let constraints = format!("{}", Fmt(|f| Debug::fmt(constraints, f)));
if constraints != "[]" {
if !first { write!(f, ", ")?; }
write!(f, "lifetime constraints {}", constraints)?;
first = false;
}

let _ = first;
Ok(())
}
}

Expand Down
14 changes: 11 additions & 3 deletions chalk-solve/src/solve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,20 @@ pub struct SolutionDisplay<'a, I: Interner> {
}

impl<'a, I: Interner> fmt::Display for SolutionDisplay<'a, I> {
#[rustfmt::skip]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
let SolutionDisplay { solution, interner } = self;
match solution {
Solution::Unique(constrained) => {
write!(f, "Unique; {}", constrained.display(*interner))
}
// If a `Unique` solution has no associated data, omit the trailing semicolon.
// This makes blessed test output nicer to read.
Solution::Unique(Canonical { binders, value: ConstrainedSubst { subst, constraints } } )
if interner.constraints_data(constraints.interned()).is_empty()
&& interner.substitution_data(subst.interned()).is_empty()
&& interner.canonical_var_kinds_data(binders.interned()).is_empty()
=> write!(f, "Unique"),

Solution::Unique(constrained) => write!(f, "Unique; {}", constrained.display(*interner)),

Solution::Ambig(Guidance::Definite(subst)) => write!(
f,
"Ambiguous; definite substitution {}",
Expand Down
15 changes: 11 additions & 4 deletions tests/logging_db/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ use chalk_solve::ext::*;
use chalk_solve::logging_db::LoggingRustIrDatabase;
use chalk_solve::RustIrDatabase;

use crate::test::{assert_result, TestGoal};
use crate::test::assert_result_str;

type TestGoal = crate::test::TestGoal<&'static str>;

macro_rules! logging_db_output_sufficient {
($($arg:tt)*) => {{
Expand All @@ -25,12 +27,16 @@ macro_rules! logging_db_output_sufficient {

pub fn logging_db_output_sufficient(
program_text: &str,
goals: Vec<(&str, SolverChoice, TestGoal)>,
goals: Vec<(&str, Vec<SolverChoice>, TestGoal)>,
) {
println!("program {}", program_text);
assert!(program_text.starts_with("{"));
assert!(program_text.ends_with("}"));

let goals = goals
.iter()
.flat_map(|(a, bs, c)| bs.into_iter().map(move |b| (a, b, c)));

let output_text = {
let db = ChalkDatabase::with(
&program_text[1..program_text.len() - 1],
Expand All @@ -39,6 +45,7 @@ pub fn logging_db_output_sufficient(

let program = db.program_ir().unwrap();
let wrapped = LoggingRustIrDatabase::<_, Program, _>::new(program.clone());

chalk_integration::tls::set_current_program(&program, || {
for (goal_text, solver_choice, expected) in goals.clone() {
let mut solver = solver_choice.into_solver();
Expand All @@ -59,7 +66,7 @@ pub fn logging_db_output_sufficient(
match expected {
TestGoal::Aggregated(expected) => {
let result = solver.solve(&wrapped, &peeled_goal);
assert_result(result, expected, db.interner());
assert_result_str(result, expected, db.interner());
}
_ => panic!("only aggregated test goals supported for logger goals"),
}
Expand Down Expand Up @@ -101,7 +108,7 @@ pub fn logging_db_output_sufficient(
match expected {
TestGoal::Aggregated(expected) => {
let result = solver.solve(&db, &peeled_goal);
assert_result(result, expected, db.interner());
assert_result_str(result, expected, db.interner());
}
_ => panic!("only aggregated test goals supported for logger goals"),
}
Expand Down
16 changes: 8 additions & 8 deletions tests/test/arrays.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn arrays_are_sized() {
[u32; N]: Sized
}
} yields {
"Unique"
expect![["Unique"]]
}

}
Expand All @@ -35,7 +35,7 @@ fn arrays_are_copy_if_element_copy() {
[Foo; N]: Copy
}
} yields {
"Unique"
expect![["Unique"]]
}
}
}
Expand All @@ -55,7 +55,7 @@ fn arrays_are_not_copy_if_element_not_copy() {
[Foo; N]: Copy
}
} yields {
"No possible solution"
expect![["No possible solution"]]
}
}
}
Expand All @@ -76,7 +76,7 @@ fn arrays_are_clone_if_element_clone() {
[Foo; N]: Clone
}
} yields {
"Unique"
expect![["Unique"]]
}
}
}
Expand All @@ -96,7 +96,7 @@ fn arrays_are_not_clone_if_element_not_clone() {
[Foo; N]: Clone
}
} yields {
"No possible solution"
expect![["No possible solution"]]
}
}
}
Expand All @@ -116,23 +116,23 @@ fn arrays_are_well_formed_if_elem_sized() {
}
}
} yields {
"Unique"
expect![["Unique"]]
}

goal {
forall<const N, T> {
WellFormed([T; N])
}
} yields {
"No possible solution"
expect![["No possible solution"]]
}

goal {
exists<const N, T> {
WellFormed([T; N])
}
} yields {
"Ambiguous; no inference guidance"
expect![["Ambiguous; no inference guidance"]]
}
}
}
Loading