@@ -1095,15 +1095,16 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
10951095 let sp = hir. span ( id) ;
10961096 // `sp` only covers `T`, change it so that it covers
10971097 // `T:` when appropriate
1098- let sp = if has_bounds {
1098+ let is_impl_trait = bound_kind. to_string ( ) . starts_with ( "impl " ) ;
1099+ let sp = if has_bounds && !is_impl_trait {
10991100 sp. to ( self . tcx
11001101 . sess
11011102 . source_map ( )
11021103 . next_point ( self . tcx . sess . source_map ( ) . next_point ( sp) ) )
11031104 } else {
11041105 sp
11051106 } ;
1106- ( sp, has_bounds)
1107+ ( sp, has_bounds, is_impl_trait )
11071108 } )
11081109 } else {
11091110 None
@@ -1136,25 +1137,33 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
11361137
11371138 fn binding_suggestion < ' tcx , S : fmt:: Display > (
11381139 err : & mut DiagnosticBuilder < ' tcx > ,
1139- type_param_span : Option < ( Span , bool ) > ,
1140+ type_param_span : Option < ( Span , bool , bool ) > ,
11401141 bound_kind : GenericKind < ' tcx > ,
11411142 sub : S ,
11421143 ) {
1143- let consider = & format ! (
1144- "consider adding an explicit lifetime bound `{}: {}`..." ,
1145- bound_kind, sub
1144+ let consider = format ! (
1145+ "consider adding an explicit lifetime bound {}" ,
1146+ if type_param_span. map( |( _, _, is_impl_trait) | is_impl_trait) . unwrap_or( false ) {
1147+ format!( " `{}` to `{}`..." , sub, bound_kind)
1148+ } else {
1149+ format!( "`{}: {}`..." , bound_kind, sub)
1150+ } ,
11461151 ) ;
1147- if let Some ( ( sp, has_lifetimes) ) = type_param_span {
1148- let tail = if has_lifetimes { " + " } else { "" } ;
1149- let suggestion = format ! ( "{}: {}{}" , bound_kind, sub, tail) ;
1152+ if let Some ( ( sp, has_lifetimes, is_impl_trait) ) = type_param_span {
1153+ let suggestion = if is_impl_trait {
1154+ format ! ( "{} + {}" , bound_kind, sub)
1155+ } else {
1156+ let tail = if has_lifetimes { " + " } else { "" } ;
1157+ format ! ( "{}: {}{}" , bound_kind, sub, tail)
1158+ } ;
11501159 err. span_suggestion_short_with_applicability (
11511160 sp,
1152- consider,
1161+ & consider,
11531162 suggestion,
11541163 Applicability :: MaybeIncorrect , // Issue #41966
11551164 ) ;
11561165 } else {
1157- err. help ( consider) ;
1166+ err. help ( & consider) ;
11581167 }
11591168 }
11601169
0 commit comments