Skip to content

Commit e8cf5f3

Browse files
committed
Clean up closure type mismatch errors
1 parent 2bd4b5c commit e8cf5f3

File tree

7 files changed

+173
-8
lines changed

7 files changed

+173
-8
lines changed

src/librustc/diagnostics.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1850,4 +1850,6 @@ register_diagnostics! {
18501850
E0495, // cannot infer an appropriate lifetime due to conflicting requirements
18511851
E0566, // conflicting representation hints
18521852
E0587, // conflicting packed and align representation hints
1853+
E0593, // closure argument count mismatch
1854+
E0594 // closure mismatch
18531855
}

src/librustc/traits/error_reporting.rs

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use rustc::lint::builtin::EXTRA_REQUIREMENT_IN_IMPL;
3535
use std::fmt;
3636
use syntax::ast::{self, NodeId};
3737
use ty::{self, AdtKind, ToPredicate, ToPolyTraitRef, Ty, TyCtxt, TypeFoldable, TyInfer, TyVar};
38-
use ty::error::ExpectedFound;
38+
use ty::error::{ExpectedFound, TypeError};
3939
use ty::fast_reject;
4040
use ty::fold::TypeFolder;
4141
use ty::subst::Subst;
@@ -663,13 +663,63 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
663663
if actual_trait_ref.self_ty().references_error() {
664664
return;
665665
}
666-
struct_span_err!(self.tcx.sess, span, E0281,
667-
"type mismatch: the type `{}` implements the trait `{}`, \
668-
but the trait `{}` is required ({})",
669-
expected_trait_ref.self_ty(),
670-
expected_trait_ref,
671-
actual_trait_ref,
672-
e)
666+
let expected_trait_ty = expected_trait_ref.self_ty();
667+
if expected_trait_ty.is_closure() {
668+
if let &TypeError::TupleSize(ref expected_found) = e {
669+
let mut err = struct_span_err!(self.tcx.sess, span, E0593,
670+
"closure takes {} parameter{} but {} parameter{} are required here",
671+
expected_found.found,
672+
if expected_found.found == 1 { "" } else { "s" },
673+
expected_found.expected,
674+
if expected_found.expected == 1 { "" } else { "s" });
675+
676+
err.span_label(span, &format!("expected closure that takes {} parameter{}",
677+
expected_found.expected,
678+
if expected_found.expected == 1 {
679+
""
680+
} else {
681+
"s"
682+
}));
683+
let closure_span = expected_trait_ty.ty_to_def_id().and_then(|did| {
684+
self.tcx.hir.span_if_local(did)
685+
});
686+
if let Some(span) = closure_span {
687+
err.span_label(span, &format!("takes {} parameter{}",
688+
expected_found.found,
689+
if expected_found.found == 1 {
690+
""
691+
} else {
692+
"s"
693+
}));
694+
}
695+
err
696+
} else {
697+
let mut err = struct_span_err!(self.tcx.sess, span, E0594,
698+
"closure mismatch: `{}` implements the trait `{}`, \
699+
but the trait `{}` is required",
700+
expected_trait_ty,
701+
expected_trait_ref,
702+
actual_trait_ref);
703+
704+
let closure_span = expected_trait_ty.ty_to_def_id().and_then(|did| {
705+
self.tcx.hir.span_if_local(did)
706+
});
707+
if let Some(span) = closure_span {
708+
err.span_label(span, &format!("{}", e));
709+
} else {
710+
err.note(&format!("{}", e));
711+
}
712+
err
713+
}
714+
} else {
715+
struct_span_err!(self.tcx.sess, span, E0281,
716+
"type mismatch: the type `{}` implements the trait `{}`, \
717+
but the trait `{}` is required ({})",
718+
expected_trait_ty,
719+
expected_trait_ref,
720+
actual_trait_ref,
721+
e)
722+
}
673723
}
674724

675725
TraitNotObjectSafe(did) => {

src/librustc/ty/sty.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,15 @@ pub enum BoundRegion {
6868
BrEnv,
6969
}
7070

71+
impl BoundRegion {
72+
pub fn is_named(&self) -> bool {
73+
match *self {
74+
BoundRegion::BrNamed(..) => true,
75+
_ => false,
76+
}
77+
}
78+
}
79+
7180
/// When a region changed from late-bound to early-bound when #32330
7281
/// was fixed, its `RegionParameterDef` will have one of these
7382
/// structures that we can use to give nicer errors.
@@ -1193,6 +1202,13 @@ impl<'a, 'gcx, 'tcx> TyS<'tcx> {
11931202
}
11941203
}
11951204

1205+
pub fn is_closure(&self) -> bool {
1206+
match self.sty {
1207+
TyClosure(..) => true,
1208+
_ => false,
1209+
}
1210+
}
1211+
11961212
pub fn is_integral(&self) -> bool {
11971213
match self.sty {
11981214
TyInfer(IntVar(_)) | TyInt(_) | TyUint(_) => true,
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2017 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+
fn main() {
12+
[1, 2, 3].sort_by(|tuple| panic!());
13+
[1, 2, 3].sort_by(|(tuple, tuple2)| panic!());
14+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
error[E0593]: closure takes 1 parameter but 2 parameters are required here
2+
--> $DIR/closure-arg-count.rs:12:15
3+
|
4+
12 | [1, 2, 3].sort_by(|tuple| panic!());
5+
| ^^^^^^^ ---------------- takes 1 parameter
6+
| |
7+
| expected closure that takes 2 parameters
8+
9+
error[E0593]: closure takes 1 parameter but 2 parameters are required here
10+
--> $DIR/closure-arg-count.rs:12:15
11+
|
12+
12 | [1, 2, 3].sort_by(|tuple| panic!());
13+
| ^^^^^^^ ---------------- takes 1 parameter
14+
| |
15+
| expected closure that takes 2 parameters
16+
17+
error[E0308]: mismatched types
18+
--> $DIR/closure-arg-count.rs:13:24
19+
|
20+
13 | [1, 2, 3].sort_by(|(tuple, tuple2)| panic!());
21+
| ^^^^^^^^^^^^^^^ expected &{integer}, found tuple
22+
|
23+
= note: expected type `&{integer}`
24+
found type `(_, _)`
25+
26+
error[E0593]: closure takes 1 parameter but 2 parameters are required here
27+
--> $DIR/closure-arg-count.rs:13:15
28+
|
29+
13 | [1, 2, 3].sort_by(|(tuple, tuple2)| panic!());
30+
| ^^^^^^^ -------------------------- takes 1 parameter
31+
| |
32+
| expected closure that takes 2 parameters
33+
34+
error[E0593]: closure takes 1 parameter but 2 parameters are required here
35+
--> $DIR/closure-arg-count.rs:13:15
36+
|
37+
13 | [1, 2, 3].sort_by(|(tuple, tuple2)| panic!());
38+
| ^^^^^^^ -------------------------- takes 1 parameter
39+
| |
40+
| expected closure that takes 2 parameters
41+
42+
error: aborting due to 5 previous errors
43+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2017 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+
trait Foo {}
12+
13+
impl<T: Fn(&())> Foo for T {}
14+
15+
fn baz<T: Foo>(_: T) {}
16+
17+
fn main() {
18+
baz(|_| ());
19+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error[E0271]: type mismatch resolving `for<'r> <[closure@$DIR/closure-mismatch.rs:18:9: 18:15] as std::ops::FnOnce<(&'r (),)>>::Output == ()`
2+
--> $DIR/closure-mismatch.rs:18:5
3+
|
4+
18 | baz(|_| ());
5+
| ^^^ expected bound lifetime parameter, found concrete lifetime
6+
|
7+
= note: concrete lifetime that was found is lifetime '_#0r
8+
= note: required because of the requirements on the impl of `Foo` for `[closure@$DIR/closure-mismatch.rs:18:9: 18:15]`
9+
= note: required by `baz`
10+
11+
error[E0594]: closure mismatch: `[closure@$DIR/closure-mismatch.rs:18:9: 18:15]` implements the trait `std::ops::Fn<(_,)>`, but the trait `for<'r> std::ops::Fn<(&'r (),)>` is required
12+
--> $DIR/closure-mismatch.rs:18:5
13+
|
14+
18 | baz(|_| ());
15+
| ^^^ ------ expected concrete lifetime, found bound lifetime parameter
16+
|
17+
= note: required because of the requirements on the impl of `Foo` for `[closure@$DIR/closure-mismatch.rs:18:9: 18:15]`
18+
= note: required by `baz`
19+
20+
error: aborting due to 2 previous errors
21+

0 commit comments

Comments
 (0)