dhall-rust
dhall-rust copied to clipboard
Tracking issue for great error messages
I'd like to follow the style of Rust error messages, because they rock. Note to self: have a look at https://github.com/rust-lang/rust/blob/master/src/librustc_errors/lib.rs
First step done ! https://github.com/Nadrieril/dhall-rust/pull/114
Next step: https://github.com/Nadrieril/dhall-rust/issues/116
Most of the groundwork is done, now there's mostly a need of writing nice error messages.
The state of error messages can be seen in the .txt files in dhall/tests/.../failure/.... For example: https://github.com/Nadrieril/dhall-rust/blob/2ca97e97f1718141d826a78ab3da8197b2d55c69/dhall/tests/type-inference/failure/unit/AssertTriviallyFalse.txt#L1-L6
This is not amazing yet, but at least it points to the correct location in the source file. Some errors fail to do even that, either because of some complicated Span story or because we don't even try (see e.g. import errors).
The goal would be better error messages like this one: https://github.com/Nadrieril/dhall-rust/blob/2ca97e97f1718141d826a78ab3da8197b2d55c69/dhall/tests/type-inference/failure/unit/FunctionApplicationArgumentNotMatch.txt#L1-L9
Errors are not pretty printed in test outputs. It seems like it is attempting to use the Debug version instead. For example this is the output when I tried to create a test case for the issue in #173
---- test_rename_all_camel_case stdout ----
thread 'test_rename_all_camel_case' panicked at 'called `Result::unwrap()` on an `Err` value: Error(Dhall(Error { kind: Typecheck(TypeError { message: Custom("error: annot mismatch: { function : Text, mergeVariant : Optional Text, postVariant : Optional Text, preVariant : Optional Text } != { function : Text, `merge_variant` : Optional Text, `post_variant` : Optional Text, `pre_variant` : Optional Text }\n --> <current file>:2:1\n |\n... |\n6 | | }\n | |______^ annot mismatch: { function : Text, mergeVariant : Optional Text, postVariant : Optional Text, preVariant : Optional Text } != { function : Text, `merge_variant` : Optional Text, `post_variant` : Optional Text, `pre_variant` : Optional Text }\n |") }) }))', serde_dhall/tests/rename.rs:33:10
You get this output because you used .unwrap() in your test case. .unwrap() will indeed use the Debug impl to display the values, which is not nice here. The way I would do it here is just define a parse function that extracts the error and manually prints is with its Display impl. Something like
fn parse<T: FromDhall + StaticType>(s: &str) -> T {
match from_str(s).static_type_annotation().parse() {
Ok(x) => x,
Err(e) => panic!("{}", e),
}
}
and then use parse::<Transform>(test_str). It's a bit annoying but the Debug impl is often useful for tricky cases so I want to keep it around.
I created a relevant issue: #228