Skip to content

Commit 3bb3278

Browse files
nikomatsakisGuillaumeGomez
authored andcommitted
revert a lot of the changes to method probe internals
1 parent ec5847a commit 3bb3278

File tree

2 files changed

+47
-89
lines changed

2 files changed

+47
-89
lines changed

src/librustc_typeck/check/method/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
130130

131131
let mode = probe::Mode::MethodCall;
132132
let self_ty = self.resolve_type_vars_if_possible(&self_ty);
133-
let pick = self.probe_for_name(span, mode, method_name, self_ty, call_expr.id)?.remove(0);
133+
let pick = self.probe_for_name(span, mode, method_name, self_ty, call_expr.id)?;
134134

135135
if let Some(import_id) = pick.import_id {
136136
self.tcx.used_trait_imports.borrow_mut().insert(import_id);
@@ -328,8 +328,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
328328
expr_id: ast::NodeId)
329329
-> Result<Def, MethodError<'tcx>> {
330330
let mode = probe::Mode::Path;
331-
let picks = self.probe_for_name(span, mode, method_name, self_ty, expr_id)?;
332-
let pick = &picks[0];
331+
let pick = self.probe_for_name(span, mode, method_name, self_ty, expr_id)?;
333332

334333
if let Some(import_id) = pick.import_id {
335334
self.tcx.used_trait_imports.borrow_mut().insert(import_id);

src/librustc_typeck/check/method/probe.rs

Lines changed: 45 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ pub enum PickKind<'tcx> {
135135
ty::PolyTraitRef<'tcx>),
136136
}
137137

138-
pub type PickResult<'tcx> = Result<Vec<Pick<'tcx>>, MethodError<'tcx>>;
138+
pub type PickResult<'tcx> = Result<Pick<'tcx>, MethodError<'tcx>>;
139139

140140
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
141141
pub enum Mode {
@@ -175,8 +175,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
175175
.iter()
176176
.flat_map(|&method_name| {
177177
match self.probe_for_name(span, mode, method_name, self_ty, scope_expr_id) {
178-
Ok(picks) => picks.into_iter().map(move |pick| pick.item).collect(),
179-
Err(_) => vec![],
178+
Ok(pick) => Some(pick.item),
179+
Err(_) => None,
180180
}
181181
})
182182
.collect()
@@ -219,7 +219,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
219219
// think cause spurious errors. Really though this part should
220220
// take place in the `self.probe` below.
221221
let steps = if mode == Mode::MethodCall {
222-
match self.create_steps(span, self_ty, &looking_for) {
222+
match self.create_steps(span, self_ty) {
223223
Some(steps) => steps,
224224
None => {
225225
return Err(MethodError::NoMatch(NoMatchData::new(Vec::new(),
@@ -272,8 +272,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
272272

273273
fn create_steps(&self,
274274
span: Span,
275-
self_ty: Ty<'tcx>,
276-
looking_for: &LookingFor<'tcx>)
275+
self_ty: Ty<'tcx>)
277276
-> Option<Vec<CandidateStep<'tcx>>> {
278277
// FIXME: we don't need to create the entire steps in one pass
279278

@@ -288,12 +287,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
288287
})
289288
.collect();
290289

291-
let final_ty = match looking_for {
292-
&LookingFor::MethodName(_) => autoderef.unambiguous_final_ty(),
293-
// Since ReturnType case tries to coerce the returned type to the
294-
// expected one, we need all the information!
295-
&LookingFor::ReturnType(_) => self_ty,
296-
};
290+
let final_ty = autoderef.unambiguous_final_ty();
297291
match final_ty.sty {
298292
ty::TyArray(elem_ty, _) => {
299293
let dereferences = steps.len() - 1;
@@ -935,8 +929,8 @@ impl<'a, 'gcx, 'tcx> ProbeContext<'a, 'gcx, 'tcx> {
935929
LookingFor::ReturnType(_) => false,
936930
});
937931

938-
if let Some(ret) = self.pick_core() {
939-
return ret;
932+
if let Some(r) = self.pick_core() {
933+
return r;
940934
}
941935

942936
let static_candidates = mem::replace(&mut self.static_candidates, vec![]);
@@ -956,21 +950,21 @@ impl<'a, 'gcx, 'tcx> ProbeContext<'a, 'gcx, 'tcx> {
956950
//Some(Ok(p)) => p.iter().map(|p| p.item.container().id()).collect(),
957951
Some(Err(MethodError::Ambiguity(v))) => {
958952
v.into_iter()
959-
.map(|source| {
960-
match source {
961-
TraitSource(id) => id,
962-
ImplSource(impl_id) => {
963-
match tcx.trait_id_of_impl(impl_id) {
964-
Some(id) => id,
965-
None => {
966-
span_bug!(span,
967-
"found inherent method when looking at traits")
953+
.map(|source| {
954+
match source {
955+
TraitSource(id) => id,
956+
ImplSource(impl_id) => {
957+
match tcx.trait_id_of_impl(impl_id) {
958+
Some(id) => id,
959+
None => {
960+
span_bug!(span,
961+
"found inherent method when looking at traits")
962+
}
968963
}
969964
}
970965
}
971-
}
972-
})
973-
.collect()
966+
})
967+
.collect()
974968
}
975969
Some(Err(MethodError::NoMatch(NoMatchData { out_of_scope_traits: others, .. }))) => {
976970
assert!(others.is_empty());
@@ -997,9 +991,7 @@ impl<'a, 'gcx, 'tcx> ProbeContext<'a, 'gcx, 'tcx> {
997991
let steps = self.steps.clone();
998992

999993
// find the first step that works
1000-
steps.iter()
1001-
.filter_map(|step| self.pick_step(step))
1002-
.next()
994+
steps.iter().filter_map(|step| self.pick_step(step)).next()
1003995
}
1004996

1005997
fn pick_step(&mut self, step: &CandidateStep<'tcx>) -> Option<PickResult<'tcx>> {
@@ -1030,18 +1022,16 @@ impl<'a, 'gcx, 'tcx> ProbeContext<'a, 'gcx, 'tcx> {
10301022
}
10311023

10321024
self.pick_method(step.self_ty).map(|r| {
1033-
r.map(|mut picks| {
1034-
for pick in picks.iter_mut() {
1035-
pick.autoderefs = step.autoderefs;
1036-
1037-
// Insert a `&*` or `&mut *` if this is a reference type:
1038-
if let ty::TyRef(_, mt) = step.self_ty.sty {
1039-
pick.autoderefs += 1;
1040-
pick.autoref = Some(mt.mutbl);
1041-
}
1025+
r.map(|mut pick| {
1026+
pick.autoderefs = step.autoderefs;
1027+
1028+
// Insert a `&*` or `&mut *` if this is a reference type:
1029+
if let ty::TyRef(_, mt) = step.self_ty.sty {
1030+
pick.autoderefs += 1;
1031+
pick.autoref = Some(mt.mutbl);
10421032
}
10431033

1044-
picks
1034+
pick
10451035
})
10461036
})
10471037
}
@@ -1054,44 +1044,28 @@ impl<'a, 'gcx, 'tcx> ProbeContext<'a, 'gcx, 'tcx> {
10541044
let region = tcx.mk_region(ty::ReErased);
10551045

10561046
// Search through mutabilities in order to find one where pick works:
1057-
let mut elements = [hir::MutImmutable, hir::MutMutable];
1058-
let mut it = elements
1059-
.iter_mut()
1060-
.filter_map(|&mut m| {
1047+
[hir::MutImmutable, hir::MutMutable]
1048+
.iter()
1049+
.filter_map(|&m| {
10611050
let autoref_ty = tcx.mk_ref(region,
10621051
ty::TypeAndMut {
10631052
ty: step.self_ty,
10641053
mutbl: m,
10651054
});
10661055
self.pick_method(autoref_ty).map(|r| {
1067-
r.map(|mut picks| {
1068-
for pick in picks.iter_mut() {
1069-
pick.autoderefs = step.autoderefs;
1070-
pick.autoref = Some(m);
1071-
pick.unsize = if step.unsize {
1072-
Some(step.self_ty)
1073-
} else {
1074-
None
1075-
};
1076-
}
1077-
picks
1056+
r.map(|mut pick| {
1057+
pick.autoderefs = step.autoderefs;
1058+
pick.autoref = Some(m);
1059+
pick.unsize = if step.unsize {
1060+
Some(step.self_ty)
1061+
} else {
1062+
None
1063+
};
1064+
pick
10781065
})
10791066
})
1080-
});
1081-
match self.looking_for {
1082-
LookingFor::MethodName(_) => it.nth(0),
1083-
LookingFor::ReturnType(_) => {
1084-
let ret = it.filter_map(|entry| entry.ok())
1085-
.flat_map(|v| v)
1086-
.collect::<Vec<_>>();
1087-
1088-
if ret.len() < 1 {
1089-
None
1090-
} else {
1091-
Some(Ok(ret))
1092-
}
1093-
}
1094-
}
1067+
})
1068+
.nth(0)
10951069
}
10961070

10971071
fn pick_method(&mut self, self_ty: Ty<'tcx>) -> Option<PickResult<'tcx>> {
@@ -1130,7 +1104,7 @@ impl<'a, 'gcx, 'tcx> ProbeContext<'a, 'gcx, 'tcx> {
11301104
if applicable_candidates.len() > 1 {
11311105
match self.collapse_candidates_to_trait_pick(&applicable_candidates[..]) {
11321106
Some(pick) => {
1133-
return Some(Ok(vec![pick]));
1107+
return Some(Ok(pick));
11341108
}
11351109
None => {}
11361110
}
@@ -1141,22 +1115,7 @@ impl<'a, 'gcx, 'tcx> ProbeContext<'a, 'gcx, 'tcx> {
11411115
return Some(Err(MethodError::Ambiguity(sources)));
11421116
}
11431117

1144-
match self.looking_for {
1145-
LookingFor::MethodName(_) => applicable_candidates
1146-
.pop()
1147-
.map(|probe| Ok(vec![probe.to_unadjusted_pick()])),
1148-
LookingFor::ReturnType(_) => {
1149-
let ret: Vec<_> = applicable_candidates.iter()
1150-
.map(|probe| probe.to_unadjusted_pick())
1151-
.collect();
1152-
1153-
if ret.len() < 1 {
1154-
None
1155-
} else {
1156-
Some(Ok(ret))
1157-
}
1158-
}
1159-
}
1118+
applicable_candidates.pop().map(|probe| Ok(probe.to_unadjusted_pick()))
11601119
}
11611120

11621121
fn consider_probe(&self,

0 commit comments

Comments
 (0)