Skip to content

Commit 6006b67

Browse files
committed
Fix regression: account for impl methods in arg count mismatch error
1 parent 4cf26f8 commit 6006b67

File tree

3 files changed

+86
-41
lines changed

3 files changed

+86
-41
lines changed

src/librustc/traits/error_reporting.rs

Lines changed: 49 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -794,48 +794,56 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
794794
}
795795

796796
fn get_fn_like_arguments(&self, node: hir::map::Node) -> (Span, Vec<ArgKind>) {
797-
if let hir::map::NodeExpr(&hir::Expr {
798-
node: hir::ExprClosure(_, ref _decl, id, span, _),
799-
..
800-
}) = node {
801-
(self.tcx.sess.codemap().def_span(span), self.tcx.hir.body(id).arguments.iter()
802-
.map(|arg| {
803-
if let hir::Pat {
804-
node: hir::PatKind::Tuple(args, _),
805-
span,
806-
..
807-
} = arg.pat.clone().into_inner() {
808-
ArgKind::Tuple(
797+
match node {
798+
hir::map::NodeExpr(&hir::Expr {
799+
node: hir::ExprClosure(_, ref _decl, id, span, _),
800+
..
801+
}) => {
802+
(self.tcx.sess.codemap().def_span(span), self.tcx.hir.body(id).arguments.iter()
803+
.map(|arg| {
804+
if let hir::Pat {
805+
node: hir::PatKind::Tuple(args, _),
809806
span,
810-
args.iter().map(|pat| {
811-
let snippet = self.tcx.sess.codemap()
812-
.span_to_snippet(pat.span).unwrap();
813-
(snippet, "_".to_owned())
814-
}).collect::<Vec<_>>(),
815-
)
816-
} else {
817-
let name = self.tcx.sess.codemap().span_to_snippet(arg.pat.span).unwrap();
818-
ArgKind::Arg(name, "_".to_owned())
819-
}
820-
})
821-
.collect::<Vec<ArgKind>>())
822-
} else if let hir::map::NodeItem(&hir::Item {
823-
span,
824-
node: hir::ItemFn(ref decl, ..),
825-
..
826-
}) = node {
827-
(self.tcx.sess.codemap().def_span(span), decl.inputs.iter()
828-
.map(|arg| match arg.clone().into_inner().node {
829-
hir::TyTup(ref tys) => ArgKind::Tuple(
830-
arg.span,
831-
tys.iter()
832-
.map(|_| ("_".to_owned(), "_".to_owned()))
833-
.collect::<Vec<_>>(),
834-
),
835-
_ => ArgKind::Arg("_".to_owned(), "_".to_owned())
836-
}).collect::<Vec<ArgKind>>())
837-
} else {
838-
panic!("non-FnLike node found: {:?}", node);
807+
..
808+
} = arg.pat.clone().into_inner() {
809+
ArgKind::Tuple(
810+
span,
811+
args.iter().map(|pat| {
812+
let snippet = self.tcx.sess.codemap()
813+
.span_to_snippet(pat.span).unwrap();
814+
(snippet, "_".to_owned())
815+
}).collect::<Vec<_>>(),
816+
)
817+
} else {
818+
let name = self.tcx.sess.codemap()
819+
.span_to_snippet(arg.pat.span).unwrap();
820+
ArgKind::Arg(name, "_".to_owned())
821+
}
822+
})
823+
.collect::<Vec<ArgKind>>())
824+
}
825+
hir::map::NodeItem(&hir::Item {
826+
span,
827+
node: hir::ItemFn(ref decl, ..),
828+
..
829+
}) |
830+
hir::map::NodeImplItem(&hir::ImplItem {
831+
span,
832+
node: hir::ImplItemKind::Method(hir::MethodSig { ref decl, .. }, _),
833+
..
834+
}) => {
835+
(self.tcx.sess.codemap().def_span(span), decl.inputs.iter()
836+
.map(|arg| match arg.clone().into_inner().node {
837+
hir::TyTup(ref tys) => ArgKind::Tuple(
838+
arg.span,
839+
tys.iter()
840+
.map(|_| ("_".to_owned(), "_".to_owned()))
841+
.collect::<Vec<_>>(),
842+
),
843+
_ => ArgKind::Arg("_".to_owned(), "_".to_owned())
844+
}).collect::<Vec<ArgKind>>())
845+
}
846+
_ => panic!("non-FnLike node found: {:?}", node),
839847
}
840848
}
841849

src/test/ui/issue-47706.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2018 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+
pub struct Foo {
12+
foo: Option<i32>,
13+
}
14+
15+
impl Foo {
16+
pub fn new(foo: Option<i32>, _: ()) -> Foo {
17+
Foo { foo }
18+
}
19+
20+
pub fn map(self) -> Option<Foo> {
21+
self.foo.map(Foo::new)
22+
}
23+
//~^^ ERROR function is expected to take 1 argument, but it takes 2 arguments [E0593]
24+
}

src/test/ui/issue-47706.stderr

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error[E0601]: main function not found
2+
3+
error[E0593]: function is expected to take 1 argument, but it takes 2 arguments
4+
--> $DIR/issue-47706.rs:21:18
5+
|
6+
16 | pub fn new(foo: Option<i32>, _: ()) -> Foo {
7+
| ------------------------------------------ takes 2 arguments
8+
...
9+
21 | self.foo.map(Foo::new)
10+
| ^^^ expected function that takes 1 argument
11+
12+
error: aborting due to 2 previous errors
13+

0 commit comments

Comments
 (0)