Skip to content

Commit 8bcf2fb

Browse files
committed
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 avalable here, but it shadowed by the local binding of the same name ``` Fix #53841
1 parent d97bb19 commit 8bcf2fb

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

compiler/rustc_hir_typeck/src/callee.rs

Lines changed: 19 additions & 0 deletions
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+
&& path.segments.len() == 1
631+
{
632+
let name = path.segments[0].ident.name;
633+
for id in self.tcx.hir().items() {
634+
if let Some(node) = self.tcx.hir().get_if_local(id.owner_id.into())
635+
&& let hir::Node::Item(item) = node
636+
&& item.ident.name == 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 shadowed by the \
641+
local binding of the same name",
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

Lines changed: 3 additions & 0 deletions
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 shadowed by the local binding of the same name
1013

1114
error: aborting due to previous error
1215

0 commit comments

Comments
 (0)