Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b26ddb8

Browse files
committedOct 27, 2019
Point at local similarly named element and tweak references to variants
Point at the span for the definition of ADTs internal to the current crate. Look at the leading char of the ident to determine whether we're expecting a likely fn or any of a fn, a tuple struct or a tuple variant. Turn fn `add_typo_suggestion` into a `Resolver` method.
1 parent 0f677c6 commit b26ddb8

File tree

132 files changed

+600
-331
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

132 files changed

+600
-331
lines changed
 

‎src/librustc_resolve/build_reduced_graph.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -850,12 +850,14 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
850850
Res::Def(kind @ DefKind::Mod, def_id)
851851
| Res::Def(kind @ DefKind::Enum, def_id)
852852
| Res::Def(kind @ DefKind::Trait, def_id) => {
853-
let module = self.r.new_module(parent,
854-
ModuleKind::Def(kind, def_id, ident.name),
855-
def_id,
856-
expansion,
857-
span);
858-
self.r.define(parent, ident, TypeNS, (module, vis, DUMMY_SP, expansion));
853+
let module = self.r.new_module(
854+
parent,
855+
ModuleKind::Def(kind, def_id, ident.name),
856+
def_id,
857+
expansion,
858+
span,
859+
);
860+
self.r.define(parent, ident, TypeNS, (module, vis, span, expansion));
859861
}
860862
Res::Def(DefKind::Struct, _)
861863
| Res::Def(DefKind::Union, _)
@@ -868,17 +870,17 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
868870
| Res::Def(DefKind::AssocOpaqueTy, _)
869871
| Res::PrimTy(..)
870872
| Res::ToolMod =>
871-
self.r.define(parent, ident, TypeNS, (res, vis, DUMMY_SP, expansion)),
873+
self.r.define(parent, ident, TypeNS, (res, vis, span, expansion)),
872874
Res::Def(DefKind::Fn, _)
873875
| Res::Def(DefKind::Method, _)
874876
| Res::Def(DefKind::Static, _)
875877
| Res::Def(DefKind::Const, _)
876878
| Res::Def(DefKind::AssocConst, _)
877879
| Res::Def(DefKind::Ctor(..), _) =>
878-
self.r.define(parent, ident, ValueNS, (res, vis, DUMMY_SP, expansion)),
880+
self.r.define(parent, ident, ValueNS, (res, vis, span, expansion)),
879881
Res::Def(DefKind::Macro(..), _)
880882
| Res::NonMacroAttr(..) =>
881-
self.r.define(parent, ident, MacroNS, (res, vis, DUMMY_SP, expansion)),
883+
self.r.define(parent, ident, MacroNS, (res, vis, span, expansion)),
882884
Res::Def(DefKind::TyParam, _) | Res::Def(DefKind::ConstParam, _)
883885
| Res::Local(..) | Res::SelfTy(..) | Res::SelfCtor(..) | Res::Err =>
884886
bug!("unexpected resolution: {:?}", res)

‎src/librustc_resolve/diagnostics.rs

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -58,21 +58,6 @@ fn reduce_impl_span_to_impl_keyword(cm: &SourceMap, impl_span: Span) -> Span {
5858
impl_span
5959
}
6060

61-
crate fn add_typo_suggestion(
62-
err: &mut DiagnosticBuilder<'_>, suggestion: Option<TypoSuggestion>, span: Span
63-
) -> bool {
64-
if let Some(suggestion) = suggestion {
65-
let msg = format!(
66-
"{} {} with a similar name exists", suggestion.res.article(), suggestion.res.descr()
67-
);
68-
err.span_suggestion(
69-
span, &msg, suggestion.candidate.to_string(), Applicability::MaybeIncorrect
70-
);
71-
return true;
72-
}
73-
false
74-
}
75-
7661
impl<'a> Resolver<'a> {
7762
crate fn add_module_candidates(
7863
&mut self,
@@ -641,7 +626,7 @@ impl<'a> Resolver<'a> {
641626
let suggestion = self.early_lookup_typo_candidate(
642627
ScopeSet::Macro(macro_kind), parent_scope, ident, is_expected
643628
);
644-
add_typo_suggestion(err, suggestion, ident.span);
629+
self.add_typo_suggestion(err, suggestion, ident.span);
645630

646631
if macro_kind == MacroKind::Derive &&
647632
(ident.as_str() == "Send" || ident.as_str() == "Sync") {
@@ -652,6 +637,33 @@ impl<'a> Resolver<'a> {
652637
err.help("have you added the `#[macro_use]` on the module/import?");
653638
}
654639
}
640+
641+
crate fn add_typo_suggestion(
642+
&self,
643+
err: &mut DiagnosticBuilder<'_>,
644+
suggestion: Option<TypoSuggestion>,
645+
span: Span,
646+
) -> bool {
647+
if let Some(suggestion) = suggestion {
648+
let msg = format!(
649+
"{} {} with a similar name exists", suggestion.res.article(), suggestion.res.descr()
650+
);
651+
err.span_suggestion(
652+
span, &msg, suggestion.candidate.to_string(), Applicability::MaybeIncorrect
653+
);
654+
let def_span = suggestion.res.opt_def_id()
655+
.and_then(|def_id| self.definitions.opt_span(def_id));
656+
if let Some(span) = def_span {
657+
err.span_label(span, &format!(
658+
"similarly named {} `{}` defined here",
659+
suggestion.res.descr(),
660+
suggestion.candidate.as_str(),
661+
));
662+
}
663+
return true;
664+
}
665+
false
666+
}
655667
}
656668

657669
impl<'a, 'b> ImportResolver<'a, 'b> {

0 commit comments

Comments
 (0)