Skip to content

Commit b485887

Browse files
leoyvenspietroalbini
authored andcommittedMay 21, 2018
Fix fn main() -> impl Trait for non-Termination trait
Fixes rust-lang#50595. This bug currently affects stable. Why I think we can go for hard error: - It will in stable for at most one cycle and there is no legitimate reason to abuse it, nor any known uses in the wild. - It only affects `bin` crates (which have a `main`), so there is little practical difference between a hard error or a deny lint, both are a one line fix. The fix was to just unshadow a variable. Thanks @nikomatsakis for the mentoring! r? @nikomatsakis
1 parent 2d1afbd commit b485887

File tree

4 files changed

+41
-4
lines changed

4 files changed

+41
-4
lines changed
 

‎src/librustc_typeck/check/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1038,11 +1038,11 @@ fn check_fn<'a, 'gcx, 'tcx>(inherited: &'a Inherited<'a, 'gcx, 'tcx>,
10381038

10391039
let ret_ty = fn_sig.output();
10401040
fcx.require_type_is_sized(ret_ty, decl.output.span(), traits::SizedReturnType);
1041-
let ret_ty = fcx.instantiate_anon_types_from_return_value(fn_id, &ret_ty);
1042-
fcx.ret_coercion = Some(RefCell::new(CoerceMany::new(ret_ty)));
1041+
let revealed_ret_ty = fcx.instantiate_anon_types_from_return_value(fn_id, &ret_ty);
1042+
fcx.ret_coercion = Some(RefCell::new(CoerceMany::new(revealed_ret_ty)));
10431043
fn_sig = fcx.tcx.mk_fn_sig(
10441044
fn_sig.inputs().iter().cloned(),
1045-
ret_ty,
1045+
revealed_ret_ty,
10461046
fn_sig.variadic,
10471047
fn_sig.unsafety,
10481048
fn_sig.abi
@@ -1124,7 +1124,7 @@ fn check_fn<'a, 'gcx, 'tcx>(inherited: &'a Inherited<'a, 'gcx, 'tcx>,
11241124
actual_return_ty = fcx.next_diverging_ty_var(
11251125
TypeVariableOrigin::DivergingFn(span));
11261126
}
1127-
fcx.demand_suptype(span, ret_ty, actual_return_ty);
1127+
fcx.demand_suptype(span, revealed_ret_ty, actual_return_ty);
11281128

11291129
// Check that the main return type implements the termination trait.
11301130
if let Some(term_id) = fcx.tcx.lang_items().termination() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(termination_trait_lib)]
12+
13+
fn main() -> impl std::process::Termination { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Tests that an `impl Trait` that is not `impl Termination` will not work.
12+
fn main() -> impl Copy { }
13+
//~^ ERROR `main` has invalid return type `impl std::marker::Copy`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0277]: `main` has invalid return type `impl std::marker::Copy`
2+
--> $DIR/termination-trait-impl-trait.rs:12:14
3+
|
4+
LL | fn main() -> impl Copy { }
5+
| ^^^^^^^^^ `main` can only return types that implement `std::process::Termination`
6+
|
7+
= help: consider using `()`, or a `Result`
8+
9+
error: aborting due to previous error
10+
11+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)