diff --git a/src/librustc/ty/sty.rs b/src/librustc/ty/sty.rs index 382db571b524e..9dc58574acc03 100644 --- a/src/librustc/ty/sty.rs +++ b/src/librustc/ty/sty.rs @@ -1299,6 +1299,13 @@ impl<'a, 'gcx, 'tcx> TyS<'tcx> { } } + pub fn is_ty_anon(&self) -> bool { + match self.sty { + TyAnon(..) => true, + _ => false, + } + } + pub fn is_ty_infer(&self) -> bool { match self.sty { TyInfer(_) => true, diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index db859e42057e9..a681934d7e072 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -1132,9 +1132,17 @@ fn check_fn<'a, 'gcx, 'tcx>(inherited: &'a Inherited<'a, 'gcx, 'tcx>, if id == fn_id { match entry_type { config::EntryMain => { + let return_ty_span = decl.output.span(); + if ret_ty.is_ty_anon() { + struct_span_err!(fcx.sess(), + return_ty_span, + E0912, + "impl Trait is not allowed in the return type of `main`") + .help("consider using `()`, or a `Result`") + .emit(); + } let substs = fcx.tcx.mk_substs(iter::once(Kind::from(ret_ty))); let trait_ref = ty::TraitRef::new(term_id, substs); - let return_ty_span = decl.output.span(); let cause = traits::ObligationCause::new( return_ty_span, fn_id, ObligationCauseCode::MainFunctionType); diff --git a/src/librustc_typeck/diagnostics.rs b/src/librustc_typeck/diagnostics.rs index e3f7ff5cb3f74..49b79b4990e5d 100644 --- a/src/librustc_typeck/diagnostics.rs +++ b/src/librustc_typeck/diagnostics.rs @@ -4770,4 +4770,5 @@ register_diagnostics! { E0641, // cannot cast to/from a pointer with an unknown kind E0645, // trait aliases not finished E0907, // type inside generator must be known in this context + E0912, // impl Trait cannot be returned from main }