Skip to content

Commit fcff023

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 fcff023

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

compiler/rustc_hir_typeck/src/callee.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,26 @@ 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+
&& let hir::ItemKind::Fn(..) = item.kind
637+
&& item.ident.name == name
638+
{
639+
err.span_label(
640+
self.tcx.def_span(id.owner_id),
641+
"this function of the same name is available here, but it's shadowed by \
642+
the local binding of the same name",
643+
);
644+
}
645+
}
646+
}
647+
628648
let mut inner_callee_path = None;
629649
let def = match callee_expr.kind {
630650
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's shadowed by the local binding of the same name
1013

1114
error: aborting due to previous error
1215

0 commit comments

Comments
 (0)