Skip to content

Commit da12c4f

Browse files
committed
Handle ambiguous cases
1 parent ac919d5 commit da12c4f

File tree

1 file changed

+34
-21
lines changed
  • src/librustc_typeck/check/method

1 file changed

+34
-21
lines changed

src/librustc_typeck/check/method/mod.rs

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -167,27 +167,40 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
167167

168168
if result.rerun {
169169
// We probe again, taking all traits into account (not only those in scope).
170-
if let Ok(new_pick) = self.lookup_probe(span,
171-
segment.name,
172-
self_ty,
173-
call_expr,
174-
ProbeScope::AllTraits) {
175-
// If we find a different result, the caller probably forgot to import the trait.
176-
// We span an error with an appropriate help message.
177-
if new_pick != pick {
178-
let error = MethodError::NoMatch(
179-
NoMatchData::new(Vec::new(),
180-
Vec::new(),
181-
vec![new_pick.item.container.id()],
182-
probe::Mode::MethodCall)
183-
);
184-
self.report_method_error(span,
185-
self_ty,
186-
segment.name,
187-
Some(self_expr),
188-
error,
189-
None);
190-
}
170+
let candidates =
171+
match self.lookup_probe(span,
172+
segment.name,
173+
self_ty,
174+
call_expr,
175+
ProbeScope::AllTraits) {
176+
Ok(ref new_pick) if *new_pick != pick => vec![new_pick.item.container.id()],
177+
Err(MethodError::Ambiguity(ref sources)) => {
178+
sources.iter()
179+
.filter_map(|source| {
180+
match *source {
181+
// Note: this cannot come from an inherent impl,
182+
// because the first probe succeeded.
183+
ImplSource(def) => self.tcx.trait_id_of_impl(def),
184+
TraitSource(_) => None,
185+
}
186+
})
187+
.collect()
188+
}
189+
_ => Vec::new(),
190+
};
191+
192+
// If we find a different result, the caller probably forgot to import a trait.
193+
// We span an error with an appropriate help message.
194+
if !candidates.is_empty() {
195+
let error = MethodError::NoMatch(
196+
NoMatchData::new(Vec::new(), Vec::new(), candidates, probe::Mode::MethodCall)
197+
);
198+
self.report_method_error(span,
199+
self_ty,
200+
segment.name,
201+
Some(self_expr),
202+
error,
203+
None);
191204
}
192205
}
193206

0 commit comments

Comments
 (0)