Skip to content

When a local binding shadows a fn, point at fn def in call failure #117924

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions compiler/rustc_hir_typeck/src/callee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,25 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
);
}

if let hir::ExprKind::Path(hir::QPath::Resolved(None, path)) = callee_expr.kind
&& let Res::Local(_) = path.res
&& let [segment] = &path.segments[..]
{
for id in self.tcx.hir().items() {
if let Some(node) = self.tcx.hir().get_if_local(id.owner_id.into())
&& let hir::Node::Item(item) = node
&& let hir::ItemKind::Fn(..) = item.kind
&& item.ident.name == segment.ident.name
{
err.span_label(
self.tcx.def_span(id.owner_id),
"this function of the same name is available here, but it's shadowed by \
the local binding",
);
}
}
}

let mut inner_callee_path = None;
let def = match callee_expr.kind {
hir::ExprKind::Path(ref qpath) => {
Expand Down
3 changes: 3 additions & 0 deletions tests/ui/issues/issue-22468.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ 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's shadowed by the local binding

error: aborting due to previous error

Expand Down