mod suggest;
pub use self::suggest::SelfSource;
-pub use self::CandidateSource::*;
pub use self::MethodError::*;
use crate::check::FnCtxt;
// candidate can arise. Used for error reporting only.
#[derive(Copy, Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub enum CandidateSource {
- ImplSource(DefId),
- TraitSource(DefId /* trait id */),
+ Impl(DefId),
+ Trait(DefId /* trait id */),
}
impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
match *source {
// Note: this cannot come from an inherent impl,
// because the first probing succeeded.
- ImplSource(def) => self.tcx.trait_id_of_impl(def),
- TraitSource(_) => None,
+ CandidateSource::Impl(def) => self.tcx.trait_id_of_impl(def),
+ CandidateSource::Trait(_) => None,
}
})
.collect(),
use super::suggest;
+use super::CandidateSource;
use super::MethodError;
use super::NoMatchData;
-use super::{CandidateSource, ImplSource, TraitSource};
use crate::check::FnCtxt;
use crate::errors::MethodCallOnUnknownType;
for item in self.impl_or_trait_item(impl_def_id) {
if !self.has_applicable_self(&item) {
// No receiver declared. Not a candidate.
- self.record_static_candidate(ImplSource(impl_def_id));
+ self.record_static_candidate(CandidateSource::Impl(impl_def_id));
continue;
}
debug!("elaborate_bounds(bound_trait_ref={:?})", bound_trait_ref);
for item in self.impl_or_trait_item(bound_trait_ref.def_id()) {
if !self.has_applicable_self(&item) {
- self.record_static_candidate(TraitSource(bound_trait_ref.def_id()));
+ self.record_static_candidate(CandidateSource::Trait(bound_trait_ref.def_id()));
} else {
mk_cand(self, bound_trait_ref, item);
}
// Check whether `trait_def_id` defines a method with suitable name.
if !self.has_applicable_self(&item) {
debug!("method has inapplicable self");
- self.record_static_candidate(TraitSource(trait_def_id));
+ self.record_static_candidate(CandidateSource::Trait(trait_def_id));
continue;
}
Some(Err(MethodError::Ambiguity(v))) => v
.into_iter()
.map(|source| match source {
- TraitSource(id) => id,
- ImplSource(impl_id) => match tcx.trait_id_of_impl(impl_id) {
+ CandidateSource::Trait(id) => id,
+ CandidateSource::Impl(impl_id) => match tcx.trait_id_of_impl(impl_id) {
Some(id) => id,
None => span_bug!(span, "found inherent method when looking at traits"),
},
fn candidate_source(&self, candidate: &Candidate<'tcx>, self_ty: Ty<'tcx>) -> CandidateSource {
match candidate.kind {
- InherentImplCandidate(..) => ImplSource(candidate.item.container.id()),
- ObjectCandidate | WhereClauseCandidate(_) => TraitSource(candidate.item.container.id()),
+ InherentImplCandidate(..) => CandidateSource::Impl(candidate.item.container.id()),
+ ObjectCandidate | WhereClauseCandidate(_) => {
+ CandidateSource::Trait(candidate.item.container.id())
+ }
TraitCandidate(trait_ref) => self.probe(|_| {
let _ = self
.at(&ObligationCause::dummy(), self.param_env)
Ok(Some(traits::ImplSource::UserDefined(ref impl_data))) => {
// If only a single impl matches, make the error message point
// to that impl.
- ImplSource(impl_data.impl_def_id)
+ CandidateSource::Impl(impl_data.impl_def_id)
}
- _ => TraitSource(candidate.item.container.id()),
+ _ => CandidateSource::Trait(candidate.item.container.id()),
}
}),
}
for (idx, source) in sources.iter().take(limit).enumerate() {
match *source {
- CandidateSource::ImplSource(impl_did) => {
+ CandidateSource::Impl(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 Some(item) = self.associated_value(impl_did, item_name).or_else(|| {
);
}
}
- CandidateSource::TraitSource(trait_did) => {
+ CandidateSource::Trait(trait_did) => {
let Some(item) = self.associated_value(trait_did, item_name) else { continue };
let item_span = self
.tcx
custom_span_label = true;
}
if static_sources.len() == 1 {
- let ty_str = if let Some(CandidateSource::ImplSource(impl_did)) =
- static_sources.get(0)
- {
- // When the "method" is resolved through dereferencing, we really want the
- // original type that has the associated function for accurate suggestions.
- // (#61411)
- let ty = tcx.at(span).type_of(*impl_did);
- match (&ty.peel_refs().kind(), &actual.peel_refs().kind()) {
- (ty::Adt(def, _), ty::Adt(def_actual, _)) if def == def_actual => {
- // Use `actual` as it will have more `substs` filled in.
- self.ty_to_value_string(actual.peel_refs())
+ let ty_str =
+ if let Some(CandidateSource::Impl(impl_did)) = static_sources.get(0) {
+ // When the "method" is resolved through dereferencing, we really want the
+ // original type that has the associated function for accurate suggestions.
+ // (#61411)
+ let ty = tcx.at(span).type_of(*impl_did);
+ match (&ty.peel_refs().kind(), &actual.peel_refs().kind()) {
+ (ty::Adt(def, _), ty::Adt(def_actual, _)) if def == def_actual => {
+ // Use `actual` as it will have more `substs` filled in.
+ self.ty_to_value_string(actual.peel_refs())
+ }
+ _ => self.ty_to_value_string(ty.peel_refs()),
}
- _ => self.ty_to_value_string(ty.peel_refs()),
- }
- } else {
- self.ty_to_value_string(actual.peel_refs())
- };
+ } else {
+ self.ty_to_value_string(actual.peel_refs())
+ };
if let SelfSource::MethodCall(expr) = source {
err.span_suggestion(
expr.span.to(span),