@@ -15,6 +15,7 @@ use crate::solve::{GenerateProofTree, InferCtxtEvalExt, UseGlobalCache};
1515use crate :: traits:: query:: evaluate_obligation:: InferCtxtExt as _;
1616use crate :: traits:: specialize:: to_pretty_impl_header;
1717use crate :: traits:: NormalizeExt ;
18+ use ambiguity:: Ambiguity :: * ;
1819use on_unimplemented:: { AppendConstMessage , OnUnimplementedNote , TypeErrCtxtExt as _} ;
1920use rustc_data_structures:: fx:: { FxHashMap , FxIndexMap } ;
2021use rustc_errors:: {
@@ -2281,14 +2282,28 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
22812282 )
22822283 } ;
22832284
2284- let ambiguities = ambiguity:: recompute_applicable_impls (
2285+ let mut ambiguities = ambiguity:: recompute_applicable_impls (
22852286 self . infcx ,
22862287 & obligation. with ( self . tcx , trait_ref) ,
22872288 ) ;
22882289 let has_non_region_infer =
22892290 trait_ref. skip_binder ( ) . args . types ( ) . any ( |t| !t. is_ty_or_numeric_infer ( ) ) ;
2290- // It doesn't make sense to talk about applicable impls if there are more
2291- // than a handful of them.
2291+ // It doesn't make sense to talk about applicable impls if there are more than a
2292+ // handful of them. If there are a lot of them, but only a few of them have no type
2293+ // params, we only show those, as they are more likely to be useful/intended.
2294+ if ambiguities. len ( ) > 20 {
2295+ let infcx = self . infcx ;
2296+ if !ambiguities. iter ( ) . all ( |option| match option {
2297+ DefId ( did) => infcx. fresh_args_for_item ( DUMMY_SP , * did) . is_empty ( ) ,
2298+ ParamEnv ( _) => true ,
2299+ } ) {
2300+ // If not all are blanket impls, we filter blanked impls out.
2301+ ambiguities. retain ( |option| match option {
2302+ DefId ( did) => infcx. fresh_args_for_item ( DUMMY_SP , * did) . is_empty ( ) ,
2303+ ParamEnv ( _) => true ,
2304+ } ) ;
2305+ }
2306+ }
22922307 if ambiguities. len ( ) > 1 && ambiguities. len ( ) < 10 && has_non_region_infer {
22932308 if self . tainted_by_errors ( ) . is_some ( ) && subst. is_none ( ) {
22942309 // If `subst.is_none()`, then this is probably two param-env
@@ -2307,7 +2322,7 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
23072322 err. note ( format ! ( "cannot satisfy `{predicate}`" ) ) ;
23082323 let impl_candidates = self
23092324 . find_similar_impl_candidates ( predicate. to_opt_poly_trait_pred ( ) . unwrap ( ) ) ;
2310- if impl_candidates. len ( ) < 10 {
2325+ if impl_candidates. len ( ) < 40 {
23112326 self . report_similar_impl_candidates (
23122327 impl_candidates. as_slice ( ) ,
23132328 trait_ref,
0 commit comments