Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CandidateSource::XCandidate -> CandidateSource::X #95642

Merged
merged 1 commit into from
Apr 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions compiler/rustc_typeck/src/check/method/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ pub mod probe;
mod suggest;

pub use self::suggest::SelfSource;
pub use self::CandidateSource::*;
pub use self::MethodError::*;

use crate::check::FnCtxt;
Expand Down Expand Up @@ -82,8 +81,8 @@ pub struct NoMatchData<'tcx> {
// 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> {
Expand Down Expand Up @@ -237,8 +236,8 @@ 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(),
Expand Down
22 changes: 12 additions & 10 deletions compiler/rustc_typeck/src/check/method/probe.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
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;
Expand Down Expand Up @@ -692,7 +692,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
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;
}

Expand Down Expand Up @@ -846,7 +846,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
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);
}
Expand Down Expand Up @@ -944,7 +944,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
// 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;
}

Expand Down Expand Up @@ -1016,8 +1016,8 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
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"),
},
Expand Down Expand Up @@ -1415,8 +1415,10 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {

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)
Expand All @@ -1426,9 +1428,9 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
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()),
}
}),
}
Expand Down
35 changes: 17 additions & 18 deletions compiler/rustc_typeck/src/check/method/suggest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {

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(|| {
Expand Down Expand Up @@ -193,7 +193,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
);
}
}
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
Expand Down Expand Up @@ -515,23 +515,22 @@ impl<'a, 'tcx> FnCtxt<'a, '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),
Expand Down