Skip to content

Commit fceab9f

Browse files
committed
make probe priority logic clearer
1 parent 6f82dea commit fceab9f

File tree

1 file changed

+28
-38
lines changed
  • src/librustc_typeck/check/method

1 file changed

+28
-38
lines changed

src/librustc_typeck/check/method/probe.rs

Lines changed: 28 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,21 +1116,17 @@ impl<'a, 'gcx, 'tcx> ProbeContext<'a, 'gcx, 'tcx> {
11161116
let steps = self.steps.clone();
11171117

11181118
// find the first step that works
1119-
steps.iter().filter_map(|step| self.pick_step(step)).next()
1120-
}
1121-
1122-
fn pick_step(&mut self, step: &CandidateStep<'tcx>) -> Option<PickResult<'tcx>> {
1123-
debug!("pick_step: step={:?}", step);
1124-
1125-
if step.self_ty.references_error() {
1126-
return None;
1127-
}
1128-
1129-
if let Some(result) = self.pick_by_value_method(step) {
1130-
return Some(result);
1131-
}
1132-
1133-
self.pick_autorefd_method(step)
1119+
steps
1120+
.iter()
1121+
.filter(|step| {
1122+
debug!("pick_core: step={:?}", step);
1123+
!step.self_ty.references_error()
1124+
}).flat_map(|step| {
1125+
self.pick_by_value_method(step).or_else(|| {
1126+
self.pick_autorefd_method(step, hir::MutImmutable).or_else(|| {
1127+
self.pick_autorefd_method(step, hir::MutMutable)
1128+
})})})
1129+
.next()
11341130
}
11351131

11361132
fn pick_by_value_method(&mut self, step: &CandidateStep<'tcx>) -> Option<PickResult<'tcx>> {
@@ -1161,36 +1157,30 @@ impl<'a, 'gcx, 'tcx> ProbeContext<'a, 'gcx, 'tcx> {
11611157
})
11621158
}
11631159

1164-
fn pick_autorefd_method(&mut self, step: &CandidateStep<'tcx>) -> Option<PickResult<'tcx>> {
1160+
fn pick_autorefd_method(&mut self, step: &CandidateStep<'tcx>, mutbl: hir::Mutability)
1161+
-> Option<PickResult<'tcx>> {
11651162
let tcx = self.tcx;
11661163

11671164
// In general, during probing we erase regions. See
11681165
// `impl_self_ty()` for an explanation.
11691166
let region = tcx.types.re_erased;
11701167

1171-
// Search through mutabilities in order to find one where pick works:
1172-
[hir::MutImmutable, hir::MutMutable]
1173-
.iter()
1174-
.filter_map(|&m| {
1175-
let autoref_ty = tcx.mk_ref(region,
1176-
ty::TypeAndMut {
1177-
ty: step.self_ty,
1178-
mutbl: m,
1179-
});
1180-
self.pick_method(autoref_ty).map(|r| {
1181-
r.map(|mut pick| {
1182-
pick.autoderefs = step.autoderefs;
1183-
pick.autoref = Some(m);
1184-
pick.unsize = if step.unsize {
1185-
Some(step.self_ty)
1186-
} else {
1187-
None
1188-
};
1189-
pick
1190-
})
1191-
})
1168+
let autoref_ty = tcx.mk_ref(region,
1169+
ty::TypeAndMut {
1170+
ty: step.self_ty, mutbl
1171+
});
1172+
self.pick_method(autoref_ty).map(|r| {
1173+
r.map(|mut pick| {
1174+
pick.autoderefs = step.autoderefs;
1175+
pick.autoref = Some(mutbl);
1176+
pick.unsize = if step.unsize {
1177+
Some(step.self_ty)
1178+
} else {
1179+
None
1180+
};
1181+
pick
11921182
})
1193-
.nth(0)
1183+
})
11941184
}
11951185

11961186
fn pick_method(&mut self, self_ty: Ty<'tcx>) -> Option<PickResult<'tcx>> {

0 commit comments

Comments
 (0)