Rust: error message when unit test type does not implement `Termination` is ungreat

Created on 27 Apr 2018  路  3Comments  路  Source: rust-lang/rust

https://github.com/rust-lang/rust/pull/50272 adds a test for the case where a unit test returns a value that does not implement Termination. The message currently talks about main and has an ugly multi-line span:

error[E0277]: `main` has invalid return type `std::result::Result<f32, std::num::ParseIntError>`
  --> $DIR/termination-trait-test-wrong-type.rs:18:1
   |
LL | / fn can_parse_zero_as_f32() -> Result<f32, ParseIntError> { //~ ERROR
LL | |     "0".parse()
LL | | }
   | |_^ `main` can only return types that implement `std::process::Termination`
   |
   = help: the trait `std::process::Termination` is not implemented for `std::result::Result<f32, std::num::ParseIntError>`
   = note: required by `__test::test::assert_test_result`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0277`.
A-attributes A-diagnostics C-enhancement T-compiler

Most helpful comment

All 3 comments

As long as we have the method signature around when we emit E0277, it has a field with the span for the return type, which should be used for this instead, but this will probably require some special casing this in that (already quite hairy) diagnostic code. Extending rustc_on_unimplemented could be possible, but I would advice against doing so to avoid turning that attribute into a bigger monster than it already is.

error[E0277]: `main` has invalid return type `std::result::Result<f32, std::num::ParseIntError>`
  --> $DIR/termination-trait-test-wrong-type.rs:18:1
   |
LL | fn can_parse_zero_as_f32() -> Result<f32, ParseIntError> { //~ ERROR
   |                               ^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't implement `std::process::Termination`
   |
   = help: the trait `std::process::Termination` is not implemented for `std::result::Result<f32, std::num::ParseIntError>`
   = note: required by `__test::test::assert_test_result`

Chiming in that this confused me as I was preparing for a Rust training session, where I expect it would confuse the students even more. Specifically, this should not talk about main at all as it's plausible to cause people to look in the wrong spot.

Was this page helpful?
0 / 5 - 0 ratings