Skip to content

Commit

Permalink
termination_trait: Make error message more helpful
Browse files Browse the repository at this point in the history
  • Loading branch information
tmandry committed Mar 22, 2018
1 parent b6934c9 commit 2b13d95
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 16 deletions.
16 changes: 7 additions & 9 deletions src/librustc/traits/error_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]"
Expand Down
5 changes: 3 additions & 2 deletions src/libstd/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
}
Original file line number Diff line number Diff line change
@@ -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`

Expand Down

0 comments on commit 2b13d95

Please sign in to comment.