Skip to content

Commit be61f02

Browse files
committed
Add Span in TypoSuggestion and TypoCandidate
1 parent 6365e5a commit be61f02

File tree

2 files changed

+39
-30
lines changed

2 files changed

+39
-30
lines changed

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,32 @@ pub(crate) enum SuggestionTarget {
5858
#[derive(Debug)]
5959
pub(crate) struct TypoSuggestion {
6060
pub candidate: Symbol,
61+
/// The source location where the name is defined; None if the name is not defined
62+
/// in source e.g. primitives
63+
pub span: Option<Span>,
6164
pub res: Res,
6265
pub target: SuggestionTarget,
6366
}
6467

6568
impl TypoSuggestion {
66-
pub(crate) fn typo_from_res(candidate: Symbol, res: Res) -> TypoSuggestion {
67-
Self { candidate, res, target: SuggestionTarget::SimilarlyNamed }
69+
pub(crate) fn typo_from_ident(ident: Ident, res: Res) -> TypoSuggestion {
70+
Self {
71+
candidate: ident.name,
72+
span: Some(ident.span),
73+
res,
74+
target: SuggestionTarget::SimilarlyNamed,
75+
}
76+
}
77+
pub(crate) fn typo_from_name(candidate: Symbol, res: Res) -> TypoSuggestion {
78+
Self { candidate, span: None, res, target: SuggestionTarget::SimilarlyNamed }
6879
}
69-
pub(crate) fn single_item_from_res(candidate: Symbol, res: Res) -> TypoSuggestion {
70-
Self { candidate, res, target: SuggestionTarget::SingleItem }
80+
pub(crate) fn single_item_from_ident(ident: Ident, res: Res) -> TypoSuggestion {
81+
Self {
82+
candidate: ident.name,
83+
span: Some(ident.span),
84+
res,
85+
target: SuggestionTarget::SingleItem,
86+
}
7187
}
7288
}
7389

@@ -490,7 +506,7 @@ impl<'a> Resolver<'a> {
490506
if let Some(binding) = resolution.borrow().binding {
491507
let res = binding.res();
492508
if filter_fn(res) && ctxt.map_or(true, |ctxt| ctxt == key.ident.span.ctxt()) {
493-
names.push(TypoSuggestion::typo_from_res(key.ident.name, res));
509+
names.push(TypoSuggestion::typo_from_ident(key.ident, res));
494510
}
495511
}
496512
}
@@ -1145,7 +1161,7 @@ impl<'a> Resolver<'a> {
11451161
.get(&expn_id)
11461162
.into_iter()
11471163
.flatten()
1148-
.map(|ident| TypoSuggestion::typo_from_res(ident.name, res)),
1164+
.map(|ident| TypoSuggestion::typo_from_ident(*ident, res)),
11491165
);
11501166
}
11511167
}
@@ -1164,7 +1180,7 @@ impl<'a> Resolver<'a> {
11641180
suggestions.extend(
11651181
ext.helper_attrs
11661182
.iter()
1167-
.map(|name| TypoSuggestion::typo_from_res(*name, res)),
1183+
.map(|name| TypoSuggestion::typo_from_name(*name, res)),
11681184
);
11691185
}
11701186
}
@@ -1174,8 +1190,8 @@ impl<'a> Resolver<'a> {
11741190
if let MacroRulesScope::Binding(macro_rules_binding) = macro_rules_scope.get() {
11751191
let res = macro_rules_binding.binding.res();
11761192
if filter_fn(res) {
1177-
suggestions.push(TypoSuggestion::typo_from_res(
1178-
macro_rules_binding.ident.name,
1193+
suggestions.push(TypoSuggestion::typo_from_ident(
1194+
macro_rules_binding.ident,
11791195
res,
11801196
))
11811197
}
@@ -1193,7 +1209,7 @@ impl<'a> Resolver<'a> {
11931209
suggestions.extend(this.macro_use_prelude.iter().filter_map(
11941210
|(name, binding)| {
11951211
let res = binding.res();
1196-
filter_fn(res).then_some(TypoSuggestion::typo_from_res(*name, res))
1212+
filter_fn(res).then_some(TypoSuggestion::typo_from_name(*name, res))
11971213
},
11981214
));
11991215
}
@@ -1203,22 +1219,22 @@ impl<'a> Resolver<'a> {
12031219
suggestions.extend(
12041220
BUILTIN_ATTRIBUTES
12051221
.iter()
1206-
.map(|attr| TypoSuggestion::typo_from_res(attr.name, res)),
1222+
.map(|attr| TypoSuggestion::typo_from_name(attr.name, res)),
12071223
);
12081224
}
12091225
}
12101226
Scope::ExternPrelude => {
12111227
suggestions.extend(this.extern_prelude.iter().filter_map(|(ident, _)| {
12121228
let res = Res::Def(DefKind::Mod, CRATE_DEF_ID.to_def_id());
1213-
filter_fn(res).then_some(TypoSuggestion::typo_from_res(ident.name, res))
1229+
filter_fn(res).then_some(TypoSuggestion::typo_from_ident(*ident, res))
12141230
}));
12151231
}
12161232
Scope::ToolPrelude => {
12171233
let res = Res::NonMacroAttr(NonMacroAttrKind::Tool);
12181234
suggestions.extend(
12191235
this.registered_tools
12201236
.iter()
1221-
.map(|ident| TypoSuggestion::typo_from_res(ident.name, res)),
1237+
.map(|ident| TypoSuggestion::typo_from_ident(*ident, res)),
12221238
);
12231239
}
12241240
Scope::StdLibPrelude => {
@@ -1235,7 +1251,8 @@ impl<'a> Resolver<'a> {
12351251
Scope::BuiltinTypes => {
12361252
suggestions.extend(PrimTy::ALL.iter().filter_map(|prim_ty| {
12371253
let res = Res::PrimTy(*prim_ty);
1238-
filter_fn(res).then_some(TypoSuggestion::typo_from_res(prim_ty.name(), res))
1254+
filter_fn(res)
1255+
.then_some(TypoSuggestion::typo_from_name(prim_ty.name(), res))
12391256
}))
12401257
}
12411258
}

compiler/rustc_resolve/src/late/diagnostics.rs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -150,15 +150,15 @@ struct BaseError {
150150
#[derive(Debug)]
151151
enum TypoCandidate {
152152
Typo(TypoSuggestion),
153-
Shadowed(Res),
153+
Shadowed(Res, Option<Span>),
154154
None,
155155
}
156156

157157
impl TypoCandidate {
158158
fn to_opt_suggestion(self) -> Option<TypoSuggestion> {
159159
match self {
160160
TypoCandidate::Typo(sugg) => Some(sugg),
161-
TypoCandidate::Shadowed(_) | TypoCandidate::None => None,
161+
TypoCandidate::Shadowed(_, _) | TypoCandidate::None => None,
162162
}
163163
}
164164
}
@@ -691,10 +691,7 @@ 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-
if let TypoCandidate::Shadowed(res) = typo_sugg
695-
&& let Some(id) = res.opt_def_id()
696-
&& let Some(sugg_span) = self.r.opt_span(id)
697-
{
694+
if let TypoCandidate::Shadowed(res, Some(sugg_span)) = typo_sugg {
698695
err.span_label(
699696
sugg_span,
700697
format!("you might have meant to refer to this {}", res.descr()),
@@ -973,10 +970,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
973970
.collect();
974971
if targets.len() == 1 {
975972
let target = targets[0];
976-
return Some(TypoSuggestion::single_item_from_res(
977-
target.0.ident.name,
978-
target.1,
979-
));
973+
return Some(TypoSuggestion::single_item_from_ident(target.0.ident, target.1));
980974
}
981975
}
982976
}
@@ -1618,7 +1612,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
16181612
// Locals and type parameters
16191613
for (ident, &res) in &rib.bindings {
16201614
if filter_fn(res) && ident.span.ctxt() == rib_ctxt {
1621-
names.push(TypoSuggestion::typo_from_res(ident.name, res));
1615+
names.push(TypoSuggestion::typo_from_ident(*ident, res));
16221616
}
16231617
}
16241618

@@ -1647,9 +1641,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
16471641
Res::Def(DefKind::Mod, crate_id.as_def_id());
16481642

16491643
if filter_fn(crate_mod) {
1650-
Some(TypoSuggestion::typo_from_res(
1651-
ident.name, crate_mod,
1652-
))
1644+
Some(TypoSuggestion::typo_from_ident(*ident, crate_mod))
16531645
} else {
16541646
None
16551647
}
@@ -1668,7 +1660,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
16681660
// Add primitive types to the mix
16691661
if filter_fn(Res::PrimTy(PrimTy::Bool)) {
16701662
names.extend(PrimTy::ALL.iter().map(|prim_ty| {
1671-
TypoSuggestion::typo_from_res(prim_ty.name(), Res::PrimTy(*prim_ty))
1663+
TypoSuggestion::typo_from_name(prim_ty.name(), Res::PrimTy(*prim_ty))
16721664
}))
16731665
}
16741666
} else {
@@ -1695,7 +1687,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
16951687
return TypoCandidate::None;
16961688
};
16971689
if found == name {
1698-
TypoCandidate::Shadowed(sugg.res)
1690+
TypoCandidate::Shadowed(sugg.res, sugg.span)
16991691
} else {
17001692
TypoCandidate::Typo(sugg)
17011693
}

0 commit comments

Comments
 (0)