diff --git a/src/librustc/middle/traits/error_reporting.rs b/src/librustc/middle/traits/error_reporting.rs index 9193b1a09f9f..94e017a6855a 100644 --- a/src/librustc/middle/traits/error_reporting.rs +++ b/src/librustc/middle/traits/error_reporting.rs @@ -225,6 +225,26 @@ pub fn report_selection_error<'a, 'tcx>(infcx: &InferCtxt<'a, 'tcx>, "the trait `{}` is not implemented for the type `{}`", trait_ref, trait_ref.self_ty()); + let mut counter = 1; + infcx.tcx.sess.fileline_help( + obligation.cause.span, + "the following implementations were found:"); + infcx.tcx.lookup_trait_def(trait_ref.def_id()).for_each_relevant_impl( + infcx.tcx, + trait_ref.self_ty(), + |impl_def_id| { + match infcx.tcx.impl_trait_ref(impl_def_id) { + Some(ref imp) => { + infcx.tcx.sess.fileline_help( + obligation.cause.span, + &format!("implementation {}: `{}`", counter, imp)); + counter += 1; + }, + None => (), + } + } + ); + // Check if it has a custom "#[rustc_on_unimplemented]" // error message, report with that message if it does let custom_note = report_on_unimplemented(infcx, &trait_ref.0, diff --git a/src/test/compile-fail/issue-21659-show-relevant-trait-impls.rs b/src/test/compile-fail/issue-21659-show-relevant-trait-impls.rs new file mode 100644 index 000000000000..416eef4ad258 --- /dev/null +++ b/src/test/compile-fail/issue-21659-show-relevant-trait-impls.rs @@ -0,0 +1,39 @@ +// Copyright 2015 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +trait Foo { + fn foo(&self, a: A) -> A { + a + } +} + +trait NotRelevant { + fn nr(&self, a: A) -> A { + a + } +} + +struct Bar; + +impl Foo for Bar {} + +impl Foo for Bar {} + +impl NotRelevant for Bar {} + +fn main() { + let f1 = Bar; + + f1.foo(1usize); + //~^ error: the trait `Foo` is not implemented for the type `Bar` + // | help: the following implementations were found: + // | help: implementation 1: `Foo` + // | help: implementation 2: `Foo` +}