detect wrong number of args when type-checking a closure

Instead of creating inference variables for those argument types, use
the trait error-reporting code to give a nicer error.
This commit is contained in:
Niko Matsakis 2018-02-12 16:00:15 -05:00
parent bd10ef7b27
commit cc05561048
5 changed files with 191 additions and 27 deletions

View file

@ -0,0 +1,29 @@
// Copyright 2016 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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// Regression test for #47244: in this specific scenario, when the
// expected type indicated 1 argument but the closure takes two, we
// would (early on) create type variables for the type of `b`. If the
// user then attempts to invoke a method on `b`, we would get an error
// saying that the type of `b` must be known, which was not very
// helpful.
use std::collections::HashMap;
fn main() {
let m = HashMap::new();
m.insert( "foo", "bar" );
m.iter().map( |_, b| {
//~^ ERROR closure is expected to take a single 2-tuple
b.to_string()
});
}

View file

@ -0,0 +1,14 @@
error[E0593]: closure is expected to take a single 2-tuple as argument, but it takes 2 distinct arguments
--> $DIR/closure-arg-count-expected-type-issue-47244.rs:24:14
|
24 | m.iter().map( |_, b| {
| ^^^ ------ takes 2 distinct arguments
| |
| expected closure that takes a single 2-tuple as argument
help: change the closure to accept a tuple instead of individual arguments
|
24 | m.iter().map( |(_, b)| {
| ^^^^^^^^
error: aborting due to previous error