Skip to content

Commit 40b25d5

Browse files
authored
Merge pull request #133 from oli-obk/cleanups
Make parser errors readable in ui tests
2 parents f4fc5ba + 78b7469 commit 40b25d5

File tree

6 files changed

+42
-10
lines changed

6 files changed

+42
-10
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ formality-rust = { version = "0.1.0", path = "crates/formality-rust" }
2222
formality-types = { version = "0.1.0", path = "crates/formality-types" }
2323
formality-check = { version = "0.1.0", path = "crates/formality-check" }
2424
formality-prove = { version = "0.1.0", path = "crates/formality-prove" }
25+
formality-core = { version = "0.1.0", path = "crates/formality-core" }
2526
ui_test = "0.12"
2627

2728
[workspace]

crates/formality-types/src/parse.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ mod test;
1414
/// Parses `text` as a term with no bindings in scope.
1515
#[track_caller]
1616
pub fn term<T>(text: &str) -> T
17+
where
18+
T: Parse,
19+
{
20+
try_term(text).unwrap()
21+
}
22+
23+
/// Parses `text` as a term with no bindings in scope.
24+
#[track_caller]
25+
pub fn try_term<T>(text: &str) -> anyhow::Result<T>
1726
where
1827
T: Parse,
1928
{
@@ -25,7 +34,7 @@ where
2534
/// References to the given string will be replaced with the given parameter
2635
/// when parsing types, lifetimes, etc.
2736
#[track_caller]
28-
pub fn term_with<T, B>(bindings: impl IntoIterator<Item = B>, text: &str) -> T
37+
pub fn term_with<T, B>(bindings: impl IntoIterator<Item = B>, text: &str) -> anyhow::Result<T>
2938
where
3039
T: Parse,
3140
B: Upcast<(String, Parameter)>,
@@ -34,13 +43,17 @@ where
3443
let (t, remainder) = match T::parse(&scope, text) {
3544
Ok(v) => v,
3645
Err(errors) => {
37-
panic!("failed to parse {text:?}: {errors:#?}");
46+
let mut err = anyhow::anyhow!("failed to parse {text}");
47+
for error in errors {
48+
err = err.context(error.text.to_owned()).context(error.message);
49+
}
50+
return Err(err);
3851
}
3952
};
4053
if !skip_whitespace(remainder).is_empty() {
41-
panic!("extra tokens after parsing {text:?} to {t:?}: {remainder:?}");
54+
anyhow::bail!("extra tokens after parsing {text:?} to {t:?}: {remainder:?}");
4255
}
43-
t
56+
Ok(t)
4457
}
4558

4659
/// Trait for parsing a [`Term`](`crate::term::Term`) as input.

src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use clap::Parser;
44
use formality_check::check_all_crates;
55
use formality_prove::{test_util::TestAssertion, Constraints};
66
use formality_rust::grammar::Program;
7-
use formality_types::{collections::Set, parse::term};
7+
use formality_types::{collections::Set, parse::try_term};
88

99
#[derive(Parser, Debug)]
1010
#[command(author, version, about, long_about = None)]
@@ -30,7 +30,7 @@ struct Args {
3030
pub fn main() -> anyhow::Result<()> {
3131
let args = Args::parse();
3232
let input: String = std::fs::read_to_string(&args.input_path)?;
33-
let program: Program = term(&input);
33+
let program: Program = try_term(&input)?;
3434

3535
if args.print_rust {
3636
eprintln!("{:#?}", program);
@@ -40,14 +40,14 @@ pub fn main() -> anyhow::Result<()> {
4040
}
4141

4242
pub fn test_program_ok(input: &str) -> anyhow::Result<()> {
43-
let program: Program = term(&input);
43+
let program: Program = try_term(&input)?;
4444
check_all_crates(&program)
4545
}
4646

4747
pub fn test_where_clause(program: &str, assertion: &str) -> anyhow::Result<Set<Constraints>> {
48-
let program: Program = term(&program);
48+
let program: Program = try_term(&program)?;
4949
check_all_crates(&program)?;
50-
let assertion: Arc<TestAssertion> = term(assertion);
50+
let assertion: Arc<TestAssertion> = try_term(assertion)?;
5151
let decls = program.to_prove_decls();
5252
Ok(formality_prove::test_util::test_prove(decls, assertion))
5353
}

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
fn main() -> anyhow::Result<()> {
2-
formality::main()
2+
formality_core::with_tracing_logs(|| formality::main())
33
}

tests/ui/parser.stderr

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Error: expected `:`
2+
3+
Caused by:
4+
0: ] {}
5+
}
6+
]
7+
8+
1: failed to parse [
9+
crate Foo {
10+
trait Baz<> where [ cake ] {}
11+
}
12+
]
13+

tests/ui/parser.🔬

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[
2+
crate Foo {
3+
trait Baz<> where [ cake ] {}
4+
}
5+
]

0 commit comments

Comments
 (0)