// Trait must have a method named `m_name` and it should not have
// type parameters or early-bound regions.
let tcx = self.tcx;
- let method_item = match self.associated_item(trait_def_id, m_name, Namespace::ValueNS) {
+ let method_item = match self.associated_value(trait_def_id, m_name) {
Some(method_item) => method_item,
None => {
tcx.sess.delay_span_bug(
/// Finds item with name `item_name` defined in impl/trait `def_id`
/// and return it, or `None`, if no such item was defined there.
- pub fn associated_item(
- &self,
- def_id: DefId,
- item_name: Ident,
- ns: Namespace,
- ) -> Option<ty::AssocItem> {
+ pub fn associated_value(&self, def_id: DefId, item_name: Ident) -> Option<ty::AssocItem> {
self.tcx
.associated_items(def_id)
- .find_by_name_and_namespace(self.tcx, item_name, ns, def_id)
+ .find_by_name_and_namespace(self.tcx, item_name, Namespace::ValueNS, def_id)
.copied()
}
}
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_errors::{pluralize, struct_span_err, Applicability, DiagnosticBuilder};
use rustc_hir as hir;
-use rustc_hir::def::Namespace;
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_hir::lang_items::LangItem;
use rustc_hir::{ExprKind, Node, QPath};
CandidateSource::ImplSource(impl_did) => {
// Provide the best span we can. Use the item, if local to crate, else
// the impl, if local to crate (item may be defaulted), else nothing.
- let item = match self
- .associated_item(impl_did, item_name, Namespace::ValueNS)
- .or_else(|| {
- let impl_trait_ref = self.tcx.impl_trait_ref(impl_did)?;
- self.associated_item(
- impl_trait_ref.def_id,
- item_name,
- Namespace::ValueNS,
- )
- }) {
+ let item = match self.associated_value(impl_did, item_name).or_else(|| {
+ let impl_trait_ref = self.tcx.impl_trait_ref(impl_did)?;
+ self.associated_value(impl_trait_ref.def_id, item_name)
+ }) {
Some(item) => item,
None => continue,
};
}
}
CandidateSource::TraitSource(trait_did) => {
- let item =
- match self.associated_item(trait_did, item_name, Namespace::ValueNS) {
- Some(item) => item,
- None => continue,
- };
+ let item = match self.associated_value(trait_did, item_name) {
+ Some(item) => item,
+ None => continue,
+ };
let item_span = self
.tcx
.sess
// Suggest clamping down the type if the method that is being attempted to
// be used exists at all, and the type is an ambiguous numeric type
// ({integer}/{float}).
- let mut candidates = all_traits(self.tcx).into_iter().filter_map(|info| {
- self.associated_item(info.def_id, item_name, Namespace::ValueNS)
- });
+ let mut candidates = all_traits(self.tcx)
+ .into_iter()
+ .filter_map(|info| self.associated_value(info.def_id, item_name));
// There are methods that are defined on the primitive types and won't be
// found when exploring `all_traits`, but we also need them to be acurate on
// our suggestions (#47759).
let fund_assoc = |opt_def_id: Option<DefId>| {
- opt_def_id
- .and_then(|id| self.associated_item(id, item_name, Namespace::ValueNS))
- .is_some()
+ opt_def_id.and_then(|id| self.associated_value(id, item_name)).is_some()
};
let lang_items = tcx.lang_items();
let found_candidate = candidates.next().is_some()
.inherent_impls(adt_deref.did)
.iter()
.filter_map(|def_id| {
- self.associated_item(
- *def_id,
- item_name,
- Namespace::ValueNS,
- )
+ self.associated_value(*def_id, item_name)
})
.count()
>= 1
.iter()
.copied()
.filter(|def_id| {
- if let Some(assoc) =
- self.associated_item(*def_id, item_name, Namespace::ValueNS)
- {
+ if let Some(assoc) = self.associated_value(*def_id, item_name) {
// Check for both mode is the same so we avoid suggesting
// incorrect associated item.
match (mode, assoc.fn_has_self_parameter, source) {
}
}) && (type_is_local || info.def_id.is_local())
&& self
- .associated_item(info.def_id, item_name, Namespace::ValueNS)
+ .associated_value(info.def_id, item_name)
.filter(|item| {
if let ty::AssocKind::Fn = item.kind {
let id = item