Skip to content

Commit

Permalink
Rollup merge of #95642 - lcnr:probe-smol, r=compiler-errors
Browse files Browse the repository at this point in the history
`CandidateSource::XCandidate` -> `CandidateSource::X`
  • Loading branch information
Dylan-DPC authored Apr 4, 2022
2 parents 5b8ac2d + 58dfe26 commit b3c3eda
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 33 deletions.
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 @@ -694,7 +694,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 @@ -848,7 +848,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 @@ -946,7 +946,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 @@ -1018,8 +1018,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 @@ -1417,8 +1417,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 @@ -1428,9 +1430,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

0 comments on commit b3c3eda

Please sign in to comment.