Skip to content

Tweak main type arguments and where clause spans #50986

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/librustc/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,18 @@ pub struct WhereClause {
pub predicates: HirVec<WherePredicate>,
}

impl WhereClause {
pub fn span(&self) -> Option<Span> {
self.predicates.iter().map(|predicate| predicate.span())
.fold(None, |acc, i| match (acc, i) {
(None, i) => Some(i),
(Some(acc), i) => {
Some(acc.to(i))
}
})
}
}

/// A single predicate in a `where` clause
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
pub enum WherePredicate {
Expand Down
42 changes: 26 additions & 16 deletions src/librustc_typeck/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,18 +186,23 @@ fn check_main_fn_ty<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
Some(hir_map::NodeItem(it)) => {
match it.node {
hir::ItemFn(.., ref generics, _) => {
let mut error = false;
if !generics.params.is_empty() {
struct_span_err!(tcx.sess, generics.span, E0131,
"main function is not allowed to have type parameters")
"`main` function is not allowed to have type parameters")
.span_label(generics.span,
"main cannot have type parameters")
"`main` cannot have type parameters")
.emit();
return;
error = true;
}
if let Some(sp) = generics.where_clause.span() {
struct_span_err!(tcx.sess, sp, E0646,
"`main` function is not allowed to have a `where` clause")
.span_label(sp, "`main` cannot have a `where` clause")
.emit();
error = true;
}
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();
if error {
return;
}
}
Expand Down Expand Up @@ -251,19 +256,24 @@ 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,
hir::ItemFn(.., ref generics, _) => {
let mut error = false;
if !generics.params.is_empty() {
struct_span_err!(tcx.sess, generics.span, E0132,
"start function is not allowed to have type parameters")
.span_label(ps.span,
.span_label(generics.span,
"start function cannot have type parameters")
.emit();
return;
error = true;
}
if let Some(sp) = generics.where_clause.span() {
struct_span_err!(tcx.sess, sp, E0647,
"start function is not allowed to have a `where` clause")
.span_label(sp, "start function cannot have a `where` clause")
.emit();
error = true;
}
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();
if error {
return;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/compile-fail/issue-1900.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// error-pattern: main function is not allowed to have type parameters
// error-pattern: `main` function is not allowed to have type parameters
fn main<T>() { }
4 changes: 2 additions & 2 deletions src/test/ui/error-codes/E0131.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0131]: main function is not allowed to have type parameters
error[E0131]: `main` function is not allowed to have type parameters
--> $DIR/E0131.rs:11:8
|
LL | fn main<T>() {
| ^^^ main cannot have type parameters
| ^^^ `main` cannot have type parameters

error: aborting due to previous error

Expand Down
6 changes: 3 additions & 3 deletions src/test/ui/error-codes/E0646.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0646]: main function is not allowed to have a where clause
--> $DIR/E0646.rs:11:1
error[E0646]: `main` function is not allowed to have a `where` clause
--> $DIR/E0646.rs:11:17
|
LL | fn main() where (): Copy {} //~ ERROR [E0646]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^ `main` cannot have a `where` clause

error: aborting due to previous error

Expand Down
10 changes: 4 additions & 6 deletions src/test/ui/error-codes/E0647.stderr
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
error[E0647]: start function is not allowed to have a where clause
--> $DIR/E0647.rs:17:1
error[E0647]: start function is not allowed to have a `where` clause
--> $DIR/E0647.rs:17:56
|
LL | / fn start(_: isize, _: *const *const u8) -> isize where (): Copy { //~ ERROR [E0647]
LL | | 0
LL | | }
| |_^
LL | fn start(_: isize, _: *const *const u8) -> isize where (): Copy { //~ ERROR [E0647]
| ^^^^^^^^ start function cannot have a `where` clause

error: aborting due to previous error

Expand Down
10 changes: 4 additions & 6 deletions src/test/ui/issue-50714-1.stderr
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
error[E0647]: start function is not allowed to have a where clause
--> $DIR/issue-50714-1.rs:19:1
error[E0647]: start function is not allowed to have a `where` clause
--> $DIR/issue-50714-1.rs:19:56
|
LL | / fn start(_: isize, _: *const *const u8) -> isize where fn(&()): Eq { //~ ERROR [E0647]
LL | | 0
LL | | }
| |_^
LL | fn start(_: isize, _: *const *const u8) -> isize where fn(&()): Eq { //~ ERROR [E0647]
| ^^^^^^^^^^^ start function cannot have a `where` clause

error: aborting due to previous error

Expand Down
6 changes: 3 additions & 3 deletions src/test/ui/issue-50714.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0646]: main function is not allowed to have a where clause
--> $DIR/issue-50714.rs:13:1
error[E0646]: `main` function is not allowed to have a `where` clause
--> $DIR/issue-50714.rs:13:17
|
LL | fn main() where fn(&()): Eq {} //~ ERROR [E0646]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^ `main` cannot have a `where` clause

error: aborting due to previous error

Expand Down