Skip to content

Commit 335298e

Browse files
committed
Show similar trait implementations if no matching impl is found
closes #21659
1 parent 88e819f commit 335298e

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

src/librustc/middle/traits/error_reporting.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,26 @@ pub fn report_selection_error<'a, 'tcx>(infcx: &InferCtxt<'a, 'tcx>,
225225
"the trait `{}` is not implemented for the type `{}`",
226226
trait_ref, trait_ref.self_ty());
227227

228+
let mut counter = 1;
229+
infcx.tcx.sess.fileline_help(
230+
obligation.cause.span,
231+
"the following implementations were found:");
232+
infcx.tcx.lookup_trait_def(trait_ref.def_id()).for_each_relevant_impl(
233+
infcx.tcx,
234+
trait_ref.self_ty(),
235+
|impl_def_id| {
236+
match infcx.tcx.impl_trait_ref(impl_def_id) {
237+
Some(ref imp) => {
238+
infcx.tcx.sess.fileline_help(
239+
obligation.cause.span,
240+
&format!("implementation {}: `{}`", counter, imp));
241+
counter += 1;
242+
},
243+
None => (),
244+
}
245+
}
246+
);
247+
228248
// Check if it has a custom "#[rustc_on_unimplemented]"
229249
// error message, report with that message if it does
230250
let custom_note = report_on_unimplemented(infcx, &trait_ref.0,
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
trait Foo<A> {
12+
fn foo(&self, a: A) -> A {
13+
a
14+
}
15+
}
16+
17+
trait NotRelevant<A> {
18+
fn nr(&self, a: A) -> A {
19+
a
20+
}
21+
}
22+
23+
struct Bar;
24+
25+
impl Foo<i32> for Bar {}
26+
27+
impl Foo<u8> for Bar {}
28+
29+
impl NotRelevant<usize> for Bar {}
30+
31+
fn main() {
32+
let f1 = Bar;
33+
34+
f1.foo(1usize);
35+
//~^ error: the trait `Foo<usize>` is not implemented for the type `Bar`
36+
// | help: the following implementations were found:
37+
// | help: implementation 1: `Foo<i32>`
38+
// | help: implementation 2: `Foo<u8>`
39+
}

0 commit comments

Comments
 (0)