Skip to content

Commit b2562fb

Browse files
committed
Tweak main type arguments and where clause spans
Tweak the spans for error when finding type arguments or where clauses in main and start functions.
1 parent 71e87be commit b2562fb

File tree

8 files changed

+55
-37
lines changed

8 files changed

+55
-37
lines changed

src/librustc/hir/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,18 @@ pub struct WhereClause {
598598
pub predicates: HirVec<WherePredicate>,
599599
}
600600

601+
impl WhereClause {
602+
pub fn span(&self) -> Option<Span> {
603+
self.predicates.iter().map(|predicate| predicate.span())
604+
.fold(None, |acc, i| match (acc, i) {
605+
(None, i) => Some(i),
606+
(Some(acc), i) => {
607+
Some(acc.to(i))
608+
}
609+
})
610+
}
611+
}
612+
601613
/// A single predicate in a `where` clause
602614
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
603615
pub enum WherePredicate {

src/librustc_typeck/lib.rs

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -186,18 +186,23 @@ fn check_main_fn_ty<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
186186
Some(hir_map::NodeItem(it)) => {
187187
match it.node {
188188
hir::ItemFn(.., ref generics, _) => {
189+
let mut error = false;
189190
if !generics.params.is_empty() {
190191
struct_span_err!(tcx.sess, generics.span, E0131,
191-
"main function is not allowed to have type parameters")
192+
"`main` function is not allowed to have type parameters")
192193
.span_label(generics.span,
193-
"main cannot have type parameters")
194+
"`main` cannot have type parameters")
194195
.emit();
195-
return;
196+
error = true;
197+
}
198+
if let Some(sp) = generics.where_clause.span() {
199+
struct_span_err!(tcx.sess, sp, E0646,
200+
"`main` function is not allowed to have a `where` clause")
201+
.span_label(sp, "`main` cannot have a `where` clause")
202+
.emit();
203+
error = true;
196204
}
197-
if !generics.where_clause.predicates.is_empty() {
198-
struct_span_err!(tcx.sess, main_span, E0646,
199-
"main function is not allowed to have a where clause")
200-
.emit();
205+
if error {
201206
return;
202207
}
203208
}
@@ -251,19 +256,24 @@ fn check_start_fn_ty<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
251256
match tcx.hir.find(start_id) {
252257
Some(hir_map::NodeItem(it)) => {
253258
match it.node {
254-
hir::ItemFn(..,ref ps,_) => {
255-
if !ps.params.is_empty() {
256-
struct_span_err!(tcx.sess, ps.span, E0132,
259+
hir::ItemFn(.., ref generics, _) => {
260+
let mut error = false;
261+
if !generics.params.is_empty() {
262+
struct_span_err!(tcx.sess, generics.span, E0132,
257263
"start function is not allowed to have type parameters")
258-
.span_label(ps.span,
264+
.span_label(generics.span,
259265
"start function cannot have type parameters")
260266
.emit();
261-
return;
267+
error = true;
268+
}
269+
if let Some(sp) = generics.where_clause.span() {
270+
struct_span_err!(tcx.sess, sp, E0647,
271+
"start function is not allowed to have a `where` clause")
272+
.span_label(sp, "start function cannot have a `where` clause")
273+
.emit();
274+
error = true;
262275
}
263-
if !ps.where_clause.predicates.is_empty() {
264-
struct_span_err!(tcx.sess, start_span, E0647,
265-
"start function is not allowed to have a where clause")
266-
.emit();
276+
if error {
267277
return;
268278
}
269279
}

src/test/compile-fail/issue-1900.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// error-pattern: main function is not allowed to have type parameters
11+
// error-pattern: `main` function is not allowed to have type parameters
1212
fn main<T>() { }

src/test/ui/error-codes/E0131.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0131]: main function is not allowed to have type parameters
1+
error[E0131]: `main` function is not allowed to have type parameters
22
--> $DIR/E0131.rs:11:8
33
|
44
LL | fn main<T>() {
5-
| ^^^ main cannot have type parameters
5+
| ^^^ `main` cannot have type parameters
66

77
error: aborting due to previous error
88

src/test/ui/error-codes/E0646.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0646]: main function is not allowed to have a where clause
2-
--> $DIR/E0646.rs:11:1
1+
error[E0646]: `main` function is not allowed to have a `where` clause
2+
--> $DIR/E0646.rs:11:17
33
|
44
LL | fn main() where (): Copy {} //~ ERROR [E0646]
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
5+
| ^^^^^^^^ `main` cannot have a `where` clause
66

77
error: aborting due to previous error
88

src/test/ui/error-codes/E0647.stderr

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
error[E0647]: start function is not allowed to have a where clause
2-
--> $DIR/E0647.rs:17:1
1+
error[E0647]: start function is not allowed to have a `where` clause
2+
--> $DIR/E0647.rs:17:56
33
|
4-
LL | / fn start(_: isize, _: *const *const u8) -> isize where (): Copy { //~ ERROR [E0647]
5-
LL | | 0
6-
LL | | }
7-
| |_^
4+
LL | fn start(_: isize, _: *const *const u8) -> isize where (): Copy { //~ ERROR [E0647]
5+
| ^^^^^^^^ start function cannot have a `where` clause
86

97
error: aborting due to previous error
108

src/test/ui/issue-50714-1.stderr

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
error[E0647]: start function is not allowed to have a where clause
2-
--> $DIR/issue-50714-1.rs:19:1
1+
error[E0647]: start function is not allowed to have a `where` clause
2+
--> $DIR/issue-50714-1.rs:19:56
33
|
4-
LL | / fn start(_: isize, _: *const *const u8) -> isize where fn(&()): Eq { //~ ERROR [E0647]
5-
LL | | 0
6-
LL | | }
7-
| |_^
4+
LL | fn start(_: isize, _: *const *const u8) -> isize where fn(&()): Eq { //~ ERROR [E0647]
5+
| ^^^^^^^^^^^ start function cannot have a `where` clause
86

97
error: aborting due to previous error
108

src/test/ui/issue-50714.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0646]: main function is not allowed to have a where clause
2-
--> $DIR/issue-50714.rs:13:1
1+
error[E0646]: `main` function is not allowed to have a `where` clause
2+
--> $DIR/issue-50714.rs:13:17
33
|
44
LL | fn main() where fn(&()): Eq {} //~ ERROR [E0646]
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5+
| ^^^^^^^^^^^ `main` cannot have a `where` clause
66

77
error: aborting due to previous error
88

0 commit comments

Comments
 (0)