@@ -673,6 +673,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
673
673
if !self . report_similar_impl_candidates (
674
674
impl_candidates,
675
675
trait_ref,
676
+ obligation. cause . body_id ,
676
677
& mut err,
677
678
) {
678
679
// This is *almost* equivalent to
@@ -707,6 +708,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
707
708
self . report_similar_impl_candidates (
708
709
impl_candidates,
709
710
trait_ref,
711
+ obligation. cause . body_id ,
710
712
& mut err,
711
713
) ;
712
714
}
@@ -1353,6 +1355,7 @@ trait InferCtxtPrivExt<'hir, 'tcx> {
1353
1355
& self ,
1354
1356
impl_candidates : Vec < ImplCandidate < ' tcx > > ,
1355
1357
trait_ref : ty:: PolyTraitRef < ' tcx > ,
1358
+ body_id : hir:: HirId ,
1356
1359
err : & mut Diagnostic ,
1357
1360
) -> bool ;
1358
1361
@@ -1735,6 +1738,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
1735
1738
& self ,
1736
1739
impl_candidates : Vec < ImplCandidate < ' tcx > > ,
1737
1740
trait_ref : ty:: PolyTraitRef < ' tcx > ,
1741
+ body_id : hir:: HirId ,
1738
1742
err : & mut Diagnostic ,
1739
1743
) -> bool {
1740
1744
let report = |mut candidates : Vec < TraitRef < ' tcx > > , err : & mut Diagnostic | {
@@ -1805,8 +1809,24 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
1805
1809
|| self . tcx . is_builtin_derive ( def_id)
1806
1810
} )
1807
1811
. filter_map ( |def_id| self . tcx . impl_trait_ref ( def_id) )
1808
- // Avoid mentioning type parameters.
1809
- . filter ( |trait_ref| !matches ! ( trait_ref. self_ty( ) . kind( ) , ty:: Param ( _) ) )
1812
+ . filter ( |trait_ref| {
1813
+ let self_ty = trait_ref. self_ty ( ) ;
1814
+ // Avoid mentioning type parameters.
1815
+ if let ty:: Param ( _) = self_ty. kind ( ) {
1816
+ false
1817
+ }
1818
+ // Avoid mentioning types that are private to another crate
1819
+ else if let ty:: Adt ( def, _) = self_ty. peel_refs ( ) . kind ( ) {
1820
+ // FIXME(compiler-errors): This could be generalized, both to
1821
+ // be more granular, and probably look past other `#[fundamental]`
1822
+ // types, too.
1823
+ self . tcx
1824
+ . visibility ( def. did ( ) )
1825
+ . is_accessible_from ( body_id. owner . to_def_id ( ) , self . tcx )
1826
+ } else {
1827
+ true
1828
+ }
1829
+ } )
1810
1830
. collect ( ) ;
1811
1831
return report ( normalized_impl_candidates, err) ;
1812
1832
}
0 commit comments