Skip to content

Commit 775328c

Browse files
committed
Modify check to output 'you might have meant' for indirect reference
1 parent 0b936d2 commit 775328c

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

compiler/rustc_resolve/src/late/diagnostics.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -691,8 +691,21 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
691691
let is_expected = &|res| source.is_expected(res);
692692
let ident_span = path.last().map_or(span, |ident| ident.ident.span);
693693
let typo_sugg = self.lookup_typo_candidate(path, source.namespace(), is_expected);
694-
let is_local = &|res: Res| res.opt_def_id().map_or(false, |id| id.is_local());
695-
if let TypoCandidate::Shadowed(res, Some(sugg_span)) = typo_sugg && is_local(res) {
694+
let is_in_same_file = &|sp1, sp2| {
695+
let source_map = self.r.session.source_map();
696+
let file1 = source_map.span_to_filename(sp1);
697+
let file2 = source_map.span_to_filename(sp2);
698+
file1 == file2
699+
};
700+
// print 'you might have meant' if the candidate is (1) is a shadowed name with
701+
// accessible definition and (2) either defined in the same crate as the typo
702+
// (could be in a different file) or introduced in the same file as the typo
703+
// (could belong to a different crate)
704+
if let TypoCandidate::Shadowed(res, Some(sugg_span)) = typo_sugg
705+
&& res
706+
.opt_def_id()
707+
.map_or(false, |id| id.is_local() || is_in_same_file(span, sugg_span))
708+
{
696709
err.span_label(
697710
sugg_span,
698711
format!("you might have meant to refer to this {}", res.descr()),

0 commit comments

Comments
 (0)