Skip to content

Commit aa64f56

Browse files
authored
Rollup merge of #147768 - chenyukang:yukang-refactor-report-method-error, r=nnethercote
Code refactoring on hir report_no_match_method_error While working on #147753, I found `report_no_match_method_error` now is too long for maintain, 1200 lines of code now: https://github.com/rust-lang/rust/blob/57ef8d642d21965304bde849bab4f389b4353e27/compiler/rustc_hir_typeck/src/method/suggest.rs#L589-L1736 this PR try to refactor it. I tried my best to group most related code into same places, but the logic here is still very complex, there are some variables across different functions, maybe we need more work to make it better understand. Maybe we could add a tidy check to avoid long spaghetti code. r? `@nnethercote`
2 parents f6d324f + 38e8066 commit aa64f56

File tree

4 files changed

+1020
-921
lines changed

4 files changed

+1020
-921
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1953,7 +1953,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
19531953
}
19541954
}
19551955
}
1956-
self.suggest_derive(diag, &[(trait_ref.upcast(self.tcx), None, None)]);
1956+
self.suggest_derive(diag, &vec![(trait_ref.upcast(self.tcx), None, None)]);
19571957
}
19581958
}
19591959
}

compiler/rustc_hir_typeck/src/method/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use tracing::{debug, instrument};
2626
pub(crate) use self::MethodError::*;
2727
use self::probe::{IsSuggestion, ProbeScope};
2828
use crate::FnCtxt;
29+
use crate::method::probe::UnsatisfiedPredicates;
2930

3031
#[derive(Clone, Copy, Debug)]
3132
pub(crate) struct MethodCallee<'tcx> {
@@ -71,8 +72,7 @@ pub(crate) enum MethodError<'tcx> {
7172
#[derive(Debug)]
7273
pub(crate) struct NoMatchData<'tcx> {
7374
pub static_candidates: Vec<CandidateSource>,
74-
pub unsatisfied_predicates:
75-
Vec<(ty::Predicate<'tcx>, Option<ty::Predicate<'tcx>>, Option<ObligationCause<'tcx>>)>,
75+
pub unsatisfied_predicates: UnsatisfiedPredicates<'tcx>,
7676
pub out_of_scope_traits: Vec<DefId>,
7777
pub similar_candidate: Option<ty::AssocItem>,
7878
pub mode: probe::Mode,

compiler/rustc_hir_typeck/src/method/probe.rs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -165,13 +165,12 @@ struct PickDiagHints<'a, 'tcx> {
165165

166166
/// Collects near misses when trait bounds for type parameters are unsatisfied and is only used
167167
/// for error reporting
168-
unsatisfied_predicates: &'a mut Vec<(
169-
ty::Predicate<'tcx>,
170-
Option<ty::Predicate<'tcx>>,
171-
Option<ObligationCause<'tcx>>,
172-
)>,
168+
unsatisfied_predicates: &'a mut UnsatisfiedPredicates<'tcx>,
173169
}
174170

171+
pub(crate) type UnsatisfiedPredicates<'tcx> =
172+
Vec<(ty::Predicate<'tcx>, Option<ty::Predicate<'tcx>>, Option<ObligationCause<'tcx>>)>;
173+
175174
/// Criteria to apply when searching for a given Pick. This is used during
176175
/// the search for potentially shadowed methods to ensure we don't search
177176
/// more candidates than strictly necessary.
@@ -1212,11 +1211,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
12121211

12131212
fn pick_core(
12141213
&self,
1215-
unsatisfied_predicates: &mut Vec<(
1216-
ty::Predicate<'tcx>,
1217-
Option<ty::Predicate<'tcx>>,
1218-
Option<ObligationCause<'tcx>>,
1219-
)>,
1214+
unsatisfied_predicates: &mut UnsatisfiedPredicates<'tcx>,
12201215
) -> Option<PickResult<'tcx>> {
12211216
// Pick stable methods only first, and consider unstable candidates if not found.
12221217
self.pick_all_method(&mut PickDiagHints {
@@ -1889,11 +1884,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
18891884
self_ty: Ty<'tcx>,
18901885
instantiate_self_ty_obligations: &[PredicateObligation<'tcx>],
18911886
probe: &Candidate<'tcx>,
1892-
possibly_unsatisfied_predicates: &mut Vec<(
1893-
ty::Predicate<'tcx>,
1894-
Option<ty::Predicate<'tcx>>,
1895-
Option<ObligationCause<'tcx>>,
1896-
)>,
1887+
possibly_unsatisfied_predicates: &mut UnsatisfiedPredicates<'tcx>,
18971888
) -> ProbeResult {
18981889
self.probe(|snapshot| {
18991890
let outer_universe = self.universe();

0 commit comments

Comments
 (0)