@@ -2776,97 +2776,115 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
27762776 let mut this = "this bound" ;
27772777 let mut note = None ;
27782778 let mut help = None ;
2779- if let ty:: PredicateKind :: Clause ( clause) = predicate. kind ( ) . skip_binder ( )
2780- && let ty:: ClauseKind :: Trait ( trait_pred) = clause
2781- {
2782- let def_id = trait_pred. def_id ( ) ;
2783- let visible_item = if let Some ( local) = def_id. as_local ( ) {
2784- // Check for local traits being reachable.
2785- let vis = & tcx. resolutions ( ( ) ) . effective_visibilities ;
2786- // Account for non-`pub` traits in the root of the local crate.
2787- let is_locally_reachable = tcx. parent ( def_id) . is_crate_root ( ) ;
2788- vis. is_reachable ( local) || is_locally_reachable
2789- } else {
2790- // Check for foreign traits being reachable.
2791- tcx. visible_parent_map ( ( ) ) . get ( & def_id) . is_some ( )
2792- } ;
2793- if tcx. is_lang_item ( def_id, LangItem :: Sized ) {
2794- // Check if this is an implicit bound, even in foreign crates.
2795- if tcx
2796- . generics_of ( item_def_id)
2797- . own_params
2798- . iter ( )
2799- . any ( |param| tcx. def_span ( param. def_id ) == span)
2800- {
2801- a = "an implicit `Sized`" ;
2802- this = "the implicit `Sized` requirement on this type parameter" ;
2803- }
2804- if let Some ( hir:: Node :: TraitItem ( hir:: TraitItem {
2805- generics,
2806- kind : hir:: TraitItemKind :: Type ( bounds, None ) ,
2807- ..
2808- } ) ) = tcx. hir ( ) . get_if_local ( item_def_id)
2809- // Do not suggest relaxing if there is an explicit `Sized` obligation.
2810- && !bounds. iter ( )
2811- . filter_map ( |bound| bound. trait_ref ( ) )
2812- . any ( |tr| tr. trait_def_id ( ) == tcx. lang_items ( ) . sized_trait ( ) )
2813- {
2814- let ( span, separator) = if let [ .., last] = bounds {
2815- ( last. span ( ) . shrink_to_hi ( ) , " +" )
2779+ if let ty:: PredicateKind :: Clause ( clause) = predicate. kind ( ) . skip_binder ( ) {
2780+ match clause {
2781+ ty:: ClauseKind :: Trait ( trait_pred) => {
2782+ let def_id = trait_pred. def_id ( ) ;
2783+ let visible_item = if let Some ( local) = def_id. as_local ( ) {
2784+ // Check for local traits being reachable.
2785+ let vis = & tcx. resolutions ( ( ) ) . effective_visibilities ;
2786+ // Account for non-`pub` traits in the root of the local crate.
2787+ let is_locally_reachable = tcx. parent ( def_id) . is_crate_root ( ) ;
2788+ vis. is_reachable ( local) || is_locally_reachable
28162789 } else {
2817- ( generics. span . shrink_to_hi ( ) , ":" )
2790+ // Check for foreign traits being reachable.
2791+ tcx. visible_parent_map ( ( ) ) . get ( & def_id) . is_some ( )
28182792 } ;
2819- err. span_suggestion_verbose (
2820- span,
2821- "consider relaxing the implicit `Sized` restriction" ,
2822- format ! ( "{separator} ?Sized" ) ,
2823- Applicability :: MachineApplicable ,
2824- ) ;
2793+ if tcx. is_lang_item ( def_id, LangItem :: Sized ) {
2794+ // Check if this is an implicit bound, even in foreign crates.
2795+ if tcx
2796+ . generics_of ( item_def_id)
2797+ . own_params
2798+ . iter ( )
2799+ . any ( |param| tcx. def_span ( param. def_id ) == span)
2800+ {
2801+ a = "an implicit `Sized`" ;
2802+ this =
2803+ "the implicit `Sized` requirement on this type parameter" ;
2804+ }
2805+ if let Some ( hir:: Node :: TraitItem ( hir:: TraitItem {
2806+ generics,
2807+ kind : hir:: TraitItemKind :: Type ( bounds, None ) ,
2808+ ..
2809+ } ) ) = tcx. hir ( ) . get_if_local ( item_def_id)
2810+ // Do not suggest relaxing if there is an explicit `Sized` obligation.
2811+ && !bounds. iter ( )
2812+ . filter_map ( |bound| bound. trait_ref ( ) )
2813+ . any ( |tr| tr. trait_def_id ( ) == tcx. lang_items ( ) . sized_trait ( ) )
2814+ {
2815+ let ( span, separator) = if let [ .., last] = bounds {
2816+ ( last. span ( ) . shrink_to_hi ( ) , " +" )
2817+ } else {
2818+ ( generics. span . shrink_to_hi ( ) , ":" )
2819+ } ;
2820+ err. span_suggestion_verbose (
2821+ span,
2822+ "consider relaxing the implicit `Sized` restriction" ,
2823+ format ! ( "{separator} ?Sized" ) ,
2824+ Applicability :: MachineApplicable ,
2825+ ) ;
2826+ }
2827+ }
2828+ if let DefKind :: Trait = tcx. def_kind ( item_def_id)
2829+ && !visible_item
2830+ {
2831+ note = Some ( format ! (
2832+ "`{short_item_name}` is a \" sealed trait\" , because to implement it \
2833+ you also need to implement `{}`, which is not accessible; this is \
2834+ usually done to force you to use one of the provided types that \
2835+ already implement it",
2836+ with_no_trimmed_paths!( tcx. def_path_str( def_id) ) ,
2837+ ) ) ;
2838+ let impls_of = tcx. trait_impls_of ( def_id) ;
2839+ let impls = impls_of
2840+ . non_blanket_impls ( )
2841+ . values ( )
2842+ . flatten ( )
2843+ . chain ( impls_of. blanket_impls ( ) . iter ( ) )
2844+ . collect :: < Vec < _ > > ( ) ;
2845+ if !impls. is_empty ( ) {
2846+ let len = impls. len ( ) ;
2847+ let mut types = impls
2848+ . iter ( )
2849+ . map ( |t| {
2850+ with_no_trimmed_paths ! ( format!(
2851+ " {}" ,
2852+ tcx. type_of( * t) . instantiate_identity( ) ,
2853+ ) )
2854+ } )
2855+ . collect :: < Vec < _ > > ( ) ;
2856+ let post = if types. len ( ) > 9 {
2857+ types. truncate ( 8 ) ;
2858+ format ! ( "\n and {} others" , len - 8 )
2859+ } else {
2860+ String :: new ( )
2861+ } ;
2862+ help = Some ( format ! (
2863+ "the following type{} implement{} the trait:\n {}{post}" ,
2864+ pluralize!( len) ,
2865+ if len == 1 { "s" } else { "" } ,
2866+ types. join( "\n " ) ,
2867+ ) ) ;
2868+ }
2869+ }
28252870 }
2826- }
2827- if let DefKind :: Trait = tcx. def_kind ( item_def_id)
2828- && !visible_item
2829- {
2830- note = Some ( format ! (
2831- "`{short_item_name}` is a \" sealed trait\" , because to implement it \
2832- you also need to implement `{}`, which is not accessible; this is \
2833- usually done to force you to use one of the provided types that \
2834- already implement it",
2835- with_no_trimmed_paths!( tcx. def_path_str( def_id) ) ,
2836- ) ) ;
2837- let impls_of = tcx. trait_impls_of ( def_id) ;
2838- let impls = impls_of
2839- . non_blanket_impls ( )
2840- . values ( )
2841- . flatten ( )
2842- . chain ( impls_of. blanket_impls ( ) . iter ( ) )
2843- . collect :: < Vec < _ > > ( ) ;
2844- if !impls. is_empty ( ) {
2845- let len = impls. len ( ) ;
2846- let mut types = impls
2847- . iter ( )
2848- . map ( |t| {
2849- with_no_trimmed_paths ! ( format!(
2850- " {}" ,
2851- tcx. type_of( * t) . instantiate_identity( ) ,
2852- ) )
2853- } )
2854- . collect :: < Vec < _ > > ( ) ;
2855- let post = if types. len ( ) > 9 {
2856- types. truncate ( 8 ) ;
2857- format ! ( "\n and {} others" , len - 8 )
2871+ ty:: ClauseKind :: ConstArgHasType ( ..) => {
2872+ let descr =
2873+ format ! ( "required by a const generic parameter in `{item_name}`" ) ;
2874+ if span. is_visible ( sm) {
2875+ let msg = format ! (
2876+ "required by this const generic parameter in `{short_item_name}`"
2877+ ) ;
2878+ multispan. push_span_label ( span, msg) ;
2879+ err. span_note ( multispan, descr) ;
28582880 } else {
2859- String :: new ( )
2860- } ;
2861- help = Some ( format ! (
2862- "the following type{} implement{} the trait:\n {}{post}" ,
2863- pluralize!( len) ,
2864- if len == 1 { "s" } else { "" } ,
2865- types. join( "\n " ) ,
2866- ) ) ;
2881+ err. span_note ( tcx. def_span ( item_def_id) , descr) ;
2882+ }
2883+ return ;
28672884 }
2885+ _ => ( ) ,
28682886 }
2869- } ;
2887+ }
28702888 let descr = format ! ( "required by {a} bound in `{item_name}`" ) ;
28712889 if span. is_visible ( sm) {
28722890 let msg = format ! ( "required by {this} in `{short_item_name}`" ) ;
0 commit comments