From 218a81b06c2669f9d4a579db7c8ebab57d91c1e4 Mon Sep 17 00:00:00 2001 From: Matthew Jasper Date: Fri, 18 May 2018 22:03:24 +0100 Subject: [PATCH] Prevent main and start from having a where clause. --- src/librustc_typeck/diagnostics.rs | 26 +++++++++++++++++++++++++ src/librustc_typeck/lib.rs | 29 ++++++++++++++++++++-------- src/test/ui/error-codes/E0646.rs | 11 +++++++++++ src/test/ui/error-codes/E0646.stderr | 9 +++++++++ src/test/ui/error-codes/E0647.rs | 19 ++++++++++++++++++ src/test/ui/error-codes/E0647.stderr | 11 +++++++++++ src/test/ui/issue-50714-1.rs | 22 +++++++++++++++++++++ src/test/ui/issue-50714-1.stderr | 11 +++++++++++ src/test/ui/issue-50714.rs | 14 ++++++++++++++ src/test/ui/issue-50714.stderr | 9 +++++++++ 10 files changed, 153 insertions(+), 8 deletions(-) create mode 100644 src/test/ui/error-codes/E0646.rs create mode 100644 src/test/ui/error-codes/E0646.stderr create mode 100644 src/test/ui/error-codes/E0647.rs create mode 100644 src/test/ui/error-codes/E0647.stderr create mode 100644 src/test/ui/issue-50714-1.rs create mode 100644 src/test/ui/issue-50714-1.stderr create mode 100644 src/test/ui/issue-50714.rs create mode 100644 src/test/ui/issue-50714.stderr diff --git a/src/librustc_typeck/diagnostics.rs b/src/librustc_typeck/diagnostics.rs index e3f7ff5cb3f..96c7cdf8761 100644 --- a/src/librustc_typeck/diagnostics.rs +++ b/src/librustc_typeck/diagnostics.rs @@ -4519,6 +4519,32 @@ fn foo(&self, _: &U) { } // error method `foo` has incompatible ``` "##, +E0646: r##" +It is not possible to define `main` with a where clause. +Erroneous code example: + +```compile_fail,E0646 +fn main() where i32: Copy { // error: main function is not allowed to have + // a where clause +} +``` +"##, + +E0647: r##" +It is not possible to define `start` with a where clause. +Erroneous code example: + +```compile_fail,E0647 +#![feature(start)] + +#[start] +fn start(_: isize, _: *const *const u8) -> isize where (): Copy { + //^ error: start function is not allowed to have a where clause + 0 +} +``` +"##, + E0689: r##" This error indicates that the numeric value for the method being passed exists but the type of the numeric value or binding could not be identified. diff --git a/src/librustc_typeck/lib.rs b/src/librustc_typeck/lib.rs index 350b53a406b..cfde6c21c7f 100644 --- a/src/librustc_typeck/lib.rs +++ b/src/librustc_typeck/lib.rs @@ -196,6 +196,12 @@ fn check_main_fn_ty<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, .emit(); return; } + if !generics.where_clause.predicates.is_empty() { + struct_span_err!(tcx.sess, main_span, E0646, + "main function is not allowed to have a where clause") + .emit(); + return; + } } _ => () } @@ -247,14 +253,21 @@ fn check_start_fn_ty<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, match tcx.hir.find(start_id) { Some(hir_map::NodeItem(it)) => { match it.node { - hir::ItemFn(..,ref ps,_) - if !ps.params.is_empty() => { - struct_span_err!(tcx.sess, ps.span, E0132, - "start function is not allowed to have type parameters") - .span_label(ps.span, - "start function cannot have type parameters") - .emit(); - return; + hir::ItemFn(..,ref ps,_) => { + if !ps.params.is_empty() { + struct_span_err!(tcx.sess, ps.span, E0132, + "start function is not allowed to have type parameters") + .span_label(ps.span, + "start function cannot have type parameters") + .emit(); + return; + } + if !ps.where_clause.predicates.is_empty() { + struct_span_err!(tcx.sess, start_span, E0647, + "start function is not allowed to have a where clause") + .emit(); + return; + } } _ => () } diff --git a/src/test/ui/error-codes/E0646.rs b/src/test/ui/error-codes/E0646.rs new file mode 100644 index 00000000000..5fc711d9408 --- /dev/null +++ b/src/test/ui/error-codes/E0646.rs @@ -0,0 +1,11 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +fn main() where (): Copy {} //~ ERROR [E0646] diff --git a/src/test/ui/error-codes/E0646.stderr b/src/test/ui/error-codes/E0646.stderr new file mode 100644 index 00000000000..96da22a643c --- /dev/null +++ b/src/test/ui/error-codes/E0646.stderr @@ -0,0 +1,9 @@ +error[E0646]: main function is not allowed to have a where clause + --> $DIR/E0646.rs:11:1 + | +LL | fn main() where (): Copy {} //~ ERROR [E0646] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0646`. diff --git a/src/test/ui/error-codes/E0647.rs b/src/test/ui/error-codes/E0647.rs new file mode 100644 index 00000000000..0a0ffefdf95 --- /dev/null +++ b/src/test/ui/error-codes/E0647.rs @@ -0,0 +1,19 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![no_std] +#![feature(start)] + +extern crate std; + +#[start] +fn start(_: isize, _: *const *const u8) -> isize where (): Copy { //~ ERROR [E0647] + 0 +} diff --git a/src/test/ui/error-codes/E0647.stderr b/src/test/ui/error-codes/E0647.stderr new file mode 100644 index 00000000000..f02407dcea6 --- /dev/null +++ b/src/test/ui/error-codes/E0647.stderr @@ -0,0 +1,11 @@ +error[E0647]: start function is not allowed to have a where clause + --> $DIR/E0647.rs:17:1 + | +LL | / fn start(_: isize, _: *const *const u8) -> isize where (): Copy { //~ ERROR [E0647] +LL | | 0 +LL | | } + | |_^ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0647`. diff --git a/src/test/ui/issue-50714-1.rs b/src/test/ui/issue-50714-1.rs new file mode 100644 index 00000000000..f0e496a88fb --- /dev/null +++ b/src/test/ui/issue-50714-1.rs @@ -0,0 +1,22 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// Regression test for issue 50714, make sure that this isn't a linker error. + +#![no_std] +#![feature(start)] + +extern crate std; + +#[start] +fn start(_: isize, _: *const *const u8) -> isize where fn(&()): Eq { //~ ERROR [E0647] + 0 +} + diff --git a/src/test/ui/issue-50714-1.stderr b/src/test/ui/issue-50714-1.stderr new file mode 100644 index 00000000000..b93183d2f24 --- /dev/null +++ b/src/test/ui/issue-50714-1.stderr @@ -0,0 +1,11 @@ +error[E0647]: start function is not allowed to have a where clause + --> $DIR/issue-50714-1.rs:19:1 + | +LL | / fn start(_: isize, _: *const *const u8) -> isize where fn(&()): Eq { //~ ERROR [E0647] +LL | | 0 +LL | | } + | |_^ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0647`. diff --git a/src/test/ui/issue-50714.rs b/src/test/ui/issue-50714.rs new file mode 100644 index 00000000000..08d975326df --- /dev/null +++ b/src/test/ui/issue-50714.rs @@ -0,0 +1,14 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// Regression test for issue 50714, make sure that this isn't a linker error. + +fn main() where fn(&()): Eq {} //~ ERROR [E0646] + diff --git a/src/test/ui/issue-50714.stderr b/src/test/ui/issue-50714.stderr new file mode 100644 index 00000000000..8cdcfe4abe2 --- /dev/null +++ b/src/test/ui/issue-50714.stderr @@ -0,0 +1,9 @@ +error[E0646]: main function is not allowed to have a where clause + --> $DIR/issue-50714.rs:13:1 + | +LL | fn main() where fn(&()): Eq {} //~ ERROR [E0646] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0646`.