Suggest calling associated fn inside traits

When calling a function that doesn't exist inside of a trait's
associated `fn`, and another associated `fn` in that trait has that
name, suggest calling it with the appropriate fully-qualified path.

Expand the label to be more descriptive.

Prompted by the following user experience:
https://users.rust-lang.org/t/cannot-find-function/50663
This commit is contained in:
Esteban Küber
2020-10-26 16:28:56 -07:00
parent 9f6c670c4b
commit 9e16213610
9 changed files with 124 additions and 49 deletions

View File

@@ -353,8 +353,8 @@ impl<'a> PathSource<'a> {
#[derive(Default)]
struct DiagnosticMetadata<'ast> {
/// The current trait's associated types' ident, used for diagnostic suggestions.
current_trait_assoc_types: Vec<Ident>,
/// The current trait's associated items' ident, used for diagnostic suggestions.
current_trait_assoc_items: Option<&'ast [P<AssocItem>]>,
/// The current self type if inside an impl (used for better errors).
current_self_type: Option<Ty>,
@@ -1148,26 +1148,18 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
result
}
/// When evaluating a `trait` use its associated types' idents for suggestionsa in E0412.
/// When evaluating a `trait` use its associated types' idents for suggestions in E0412.
fn with_trait_items<T>(
&mut self,
trait_items: &Vec<P<AssocItem>>,
trait_items: &'ast Vec<P<AssocItem>>,
f: impl FnOnce(&mut Self) -> T,
) -> T {
let trait_assoc_types = replace(
&mut self.diagnostic_metadata.current_trait_assoc_types,
trait_items
.iter()
.filter_map(|item| match &item.kind {
AssocItemKind::TyAlias(_, _, bounds, _) if bounds.is_empty() => {
Some(item.ident)
}
_ => None,
})
.collect(),
let trait_assoc_items = replace(
&mut self.diagnostic_metadata.current_trait_assoc_items,
Some(&trait_items[..]),
);
let result = f(self);
self.diagnostic_metadata.current_trait_assoc_types = trait_assoc_types;
self.diagnostic_metadata.current_trait_assoc_items = trait_assoc_items;
result
}