Skip to content
/ rust Public
forked from rust-lang/rust

Commit e1e60b6

Browse files
committed
Auto merge of rust-lang#117924 - estebank:issue-53841, r=petrochenkov
When a local binding shadows a fn, point at fn def in call failure When a local binding shadows a function that is then called, this local binding will cause an E0618 error. We now point not only at the binding definition, but also at the locally defined function of the same name. ``` error[E0618]: expected function, found `&str` --> $DIR/issue-22468.rs:3:13 | LL | let foo = "bar"; | --- `foo` has type `&str` LL | let x = foo("baz"); | ^^^------- | | | call expression requires function ... LL | fn foo(file: &str) -> bool { | -------------------------- this function of the same name is available here, but it shadowed by the local binding of the same name ``` Fix rust-lang#53841
2 parents 6416e2e + 7141399 commit e1e60b6

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

compiler/rustc_hir_typeck/src/callee.rs

+19
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,25 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
625625
);
626626
}
627627

628+
if let hir::ExprKind::Path(hir::QPath::Resolved(None, path)) = callee_expr.kind
629+
&& let Res::Local(_) = path.res
630+
&& let [segment] = &path.segments[..]
631+
{
632+
for id in self.tcx.hir().items() {
633+
if let Some(node) = self.tcx.hir().get_if_local(id.owner_id.into())
634+
&& let hir::Node::Item(item) = node
635+
&& let hir::ItemKind::Fn(..) = item.kind
636+
&& item.ident.name == segment.ident.name
637+
{
638+
err.span_label(
639+
self.tcx.def_span(id.owner_id),
640+
"this function of the same name is available here, but it's shadowed by \
641+
the local binding",
642+
);
643+
}
644+
}
645+
}
646+
628647
let mut inner_callee_path = None;
629648
let def = match callee_expr.kind {
630649
hir::ExprKind::Path(ref qpath) => {

tests/ui/issues/issue-22468.stderr

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ LL | let x = foo("baz");
77
| ^^^-------
88
| |
99
| call expression requires function
10+
...
11+
LL | fn foo(file: &str) -> bool {
12+
| -------------------------- this function of the same name is available here, but it's shadowed by the local binding
1013

1114
error: aborting due to previous error
1215

0 commit comments

Comments
 (0)