Skip to content

Commit 2b13d95

Browse files
committed
termination_trait: Make error message more helpful
1 parent b6934c9 commit 2b13d95

File tree

5 files changed

+16
-16
lines changed

5 files changed

+16
-16
lines changed

src/librustc/traits/error_reporting.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -585,17 +585,15 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
585585
trait_ref.to_predicate(), post_message)
586586
}));
587587

588-
let explanation = match obligation.cause.code {
589-
ObligationCauseCode::MainFunctionType => {
588+
let explanation =
589+
if obligation.cause.code == ObligationCauseCode::MainFunctionType {
590590
"consider using `()`, or a `Result`".to_owned()
591-
}
592-
_ => {
591+
} else {
593592
format!("{}the trait `{}` is not implemented for `{}`",
594-
pre_message,
595-
trait_ref,
596-
trait_ref.self_ty())
597-
}
598-
};
593+
pre_message,
594+
trait_ref,
595+
trait_ref.self_ty())
596+
};
599597

600598
if let Some(ref s) = label {
601599
// If it has a custom "#[rustc_on_unimplemented]"

src/libstd/process.rs

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

src/test/compile-fail/rfc-1937-termination-trait/termination-trait-main-i32.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
// except according to those terms.
1010

1111
fn main() -> i32 {
12-
//~^ ERROR `i32: std::process::Termination` is not satisfied
13-
//~| NOTE `main` can only return types that implement std::process::Termination, not `i32`
12+
//~^ ERROR `main` has invalid return type `i32`
13+
//~| NOTE `main` can only return types that implement std::process::Termination
14+
//~| HELP consider using `()`, or a `Result`
1415
0
1516
}

src/test/compile-fail/rfc-1937-termination-trait/termination-trait-not-satisfied.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010

1111
struct ReturnType {}
1212

13-
fn main() -> ReturnType { //~ ERROR `ReturnType: std::process::Termination` is not satisfied
13+
fn main() -> ReturnType { //~ ERROR `main` has invalid return type `ReturnType`
1414
ReturnType {}
1515
}

src/test/ui/rfc-1937-termination-trait/termination-trait-main-wrong-type.stderr

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

0 commit comments

Comments
 (0)