termination_trait: Make error message more helpful

This commit is contained in:
Tyler Mandry 2018-03-21 23:28:48 -05:00
parent b6934c91b2
commit 2b13d95da0
5 changed files with 16 additions and 16 deletions

View File

@ -585,17 +585,15 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
trait_ref.to_predicate(), post_message)
}));
let explanation = match obligation.cause.code {
ObligationCauseCode::MainFunctionType => {
let explanation =
if obligation.cause.code == ObligationCauseCode::MainFunctionType {
"consider using `()`, or a `Result`".to_owned()
}
_ => {
} else {
format!("{}the trait `{}` is not implemented for `{}`",
pre_message,
trait_ref,
trait_ref.self_ty())
}
};
pre_message,
trait_ref,
trait_ref.self_ty())
};
if let Some(ref s) = label {
// If it has a custom "#[rustc_on_unimplemented]"

View File

@ -1442,8 +1442,9 @@ pub fn id() -> u32 {
/// a successful execution. In case of a failure, `libc::EXIT_FAILURE` is returned.
#[cfg_attr(not(test), lang = "termination")]
#[unstable(feature = "termination_trait_lib", issue = "43301")]
#[rustc_on_unimplemented =
"`main` can only return types that implement {Termination}, not `{Self}`"]
#[rustc_on_unimplemented(
message="`main` has invalid return type `{Self}`",
label="`main` can only return types that implement {Termination}")]
pub trait Termination {
/// Is called to get the representation of the value as status code.
/// This status code is returned to the operating system.

View File

@ -9,7 +9,8 @@
// except according to those terms.
fn main() -> i32 {
//~^ ERROR `i32: std::process::Termination` is not satisfied
//~| NOTE `main` can only return types that implement std::process::Termination, not `i32`
//~^ ERROR `main` has invalid return type `i32`
//~| NOTE `main` can only return types that implement std::process::Termination
//~| HELP consider using `()`, or a `Result`
0
}

View File

@ -10,6 +10,6 @@
struct ReturnType {}
fn main() -> ReturnType { //~ ERROR `ReturnType: std::process::Termination` is not satisfied
fn main() -> ReturnType { //~ ERROR `main` has invalid return type `ReturnType`
ReturnType {}
}

View File

@ -1,8 +1,8 @@
error[E0277]: the trait bound `char: std::process::Termination` is not satisfied
error[E0277]: `main` has invalid return type `char`
--> $DIR/termination-trait-main-wrong-type.rs:11:14
|
LL | fn main() -> char { //~ ERROR
| ^^^^ `main` can only return types that implement std::process::Termination, not `char`
| ^^^^ `main` can only return types that implement std::process::Termination
|
= help: consider using `()`, or a `Result`