@@ -171,7 +171,7 @@ enum SuggestChangingConstraintsMessage<'a> {
171
171
172
172
fn suggest_changing_unsized_bound (
173
173
generics : & hir:: Generics < ' _ > ,
174
- suggestions : & mut Vec < ( Span , String , SuggestChangingConstraintsMessage < ' _ > ) > ,
174
+ suggestions : & mut Vec < ( Span , String , String , SuggestChangingConstraintsMessage < ' _ > ) > ,
175
175
param : & hir:: GenericParam < ' _ > ,
176
176
def_id : Option < DefId > ,
177
177
) {
@@ -206,7 +206,8 @@ fn suggest_changing_unsized_bound(
206
206
continue ;
207
207
}
208
208
209
- let mut push_suggestion = |sp, msg| suggestions. push ( ( sp, String :: new ( ) , msg) ) ;
209
+ let mut push_suggestion =
210
+ |sp, msg| suggestions. push ( ( sp, "Sized" . to_string ( ) , String :: new ( ) , msg) ) ;
210
211
211
212
if predicate. bounds . len ( ) == unsized_bounds. len ( ) {
212
213
// All the bounds are unsized bounds, e.g.
@@ -348,10 +349,20 @@ pub fn suggest_constraining_type_params<'a>(
348
349
use SuggestChangingConstraintsMessage :: RestrictBoundFurther ;
349
350
350
351
if let Some ( open_paren_sp) = open_paren_sp {
351
- suggestions. push ( ( open_paren_sp, "(" . to_string ( ) , RestrictBoundFurther ) ) ;
352
- suggestions. push ( ( span, format ! ( "){suggestion}" ) , RestrictBoundFurther ) ) ;
352
+ suggestions. push ( (
353
+ open_paren_sp,
354
+ constraint. clone ( ) ,
355
+ "(" . to_string ( ) ,
356
+ RestrictBoundFurther ,
357
+ ) ) ;
358
+ suggestions. push ( (
359
+ span,
360
+ constraint. clone ( ) ,
361
+ format ! ( "){suggestion}" ) ,
362
+ RestrictBoundFurther ,
363
+ ) ) ;
353
364
} else {
354
- suggestions. push ( ( span, suggestion, RestrictBoundFurther ) ) ;
365
+ suggestions. push ( ( span, constraint . clone ( ) , suggestion, RestrictBoundFurther ) ) ;
355
366
}
356
367
} ;
357
368
@@ -409,6 +420,7 @@ pub fn suggest_constraining_type_params<'a>(
409
420
// - insert: `, X: Bar`
410
421
suggestions. push ( (
411
422
generics. tail_span_for_predicate_suggestion ( ) ,
423
+ constraint. clone ( ) ,
412
424
constraints. iter ( ) . fold ( String :: new ( ) , |mut string, & ( constraint, _) | {
413
425
write ! ( string, ", {param_name}: {constraint}" ) . unwrap ( ) ;
414
426
string
@@ -438,6 +450,7 @@ pub fn suggest_constraining_type_params<'a>(
438
450
// default (`<T=Foo>`), so we suggest adding `where T: Bar`.
439
451
suggestions. push ( (
440
452
generics. tail_span_for_predicate_suggestion ( ) ,
453
+ constraint. clone ( ) ,
441
454
format ! ( "{where_prefix} {param_name}: {constraint}" ) ,
442
455
SuggestChangingConstraintsMessage :: RestrictTypeFurther { ty : param_name } ,
443
456
) ) ;
@@ -451,6 +464,7 @@ pub fn suggest_constraining_type_params<'a>(
451
464
if let Some ( colon_span) = param. colon_span {
452
465
suggestions. push ( (
453
466
colon_span. shrink_to_hi ( ) ,
467
+ constraint. clone ( ) ,
454
468
format ! ( " {constraint}" ) ,
455
469
SuggestChangingConstraintsMessage :: RestrictType { ty : param_name } ,
456
470
) ) ;
@@ -463,6 +477,7 @@ pub fn suggest_constraining_type_params<'a>(
463
477
// - help: consider restricting this type parameter with `T: Foo`
464
478
suggestions. push ( (
465
479
param. span . shrink_to_hi ( ) ,
480
+ constraint. clone ( ) ,
466
481
format ! ( ": {constraint}" ) ,
467
482
SuggestChangingConstraintsMessage :: RestrictType { ty : param_name } ,
468
483
) ) ;
@@ -471,12 +486,16 @@ pub fn suggest_constraining_type_params<'a>(
471
486
// FIXME: remove the suggestions that are from derive, as the span is not correct
472
487
suggestions = suggestions
473
488
. into_iter ( )
474
- . filter ( |( span, _, _) | !span. in_derive_expansion ( ) )
489
+ . filter ( |( span, _, _, _ ) | !span. in_derive_expansion ( ) )
475
490
. collect :: < Vec < _ > > ( ) ;
476
491
477
492
if suggestions. len ( ) == 1 {
478
- let ( span, suggestion, msg) = suggestions. pop ( ) . unwrap ( ) ;
479
- let post = if unstable_suggestion { " but it is an `unstable` trait" } else { "" } ;
493
+ let ( span, constraint, suggestion, msg) = suggestions. pop ( ) . unwrap ( ) ;
494
+ let post = format ! (
495
+ " with {}trait{} `{constraint}`" ,
496
+ if unstable_suggestion { "unstable " } else { "" } ,
497
+ if constraint. contains( '+' ) { "s" } else { "" } ,
498
+ ) ;
480
499
let msg = match msg {
481
500
SuggestChangingConstraintsMessage :: RestrictBoundFurther => {
482
501
format ! ( "consider further restricting this bound{post}" )
@@ -488,21 +507,19 @@ pub fn suggest_constraining_type_params<'a>(
488
507
format ! ( "consider further restricting type parameter `{ty}`{post}" )
489
508
}
490
509
SuggestChangingConstraintsMessage :: RemoveMaybeUnsized => {
491
- format ! (
492
- "consider removing the `?Sized` bound to make the type parameter `Sized`{post}"
493
- )
510
+ format ! ( "consider removing the `?Sized` bound to make the type parameter `Sized`" )
494
511
}
495
512
SuggestChangingConstraintsMessage :: ReplaceMaybeUnsizedWithSized => {
496
- format ! ( "consider replacing `?Sized` with `Sized`{post} " )
513
+ format ! ( "consider replacing `?Sized` with `Sized`" )
497
514
}
498
515
} ;
499
516
500
517
err. span_suggestion_verbose ( span, msg, suggestion, applicability) ;
501
518
} else if suggestions. len ( ) > 1 {
502
- let post = if unstable_suggestion { " but some of them are ` unstable` traits" } else { "" } ;
519
+ let post = if unstable_suggestion { " ( some of them are unstable traits) " } else { "" } ;
503
520
err. multipart_suggestion_verbose (
504
521
format ! ( "consider restricting type parameters{post}" ) ,
505
- suggestions. into_iter ( ) . map ( |( span, suggestion, _) | ( span, suggestion) ) . collect ( ) ,
522
+ suggestions. into_iter ( ) . map ( |( span, _ , suggestion, _) | ( span, suggestion) ) . collect ( ) ,
506
523
applicability,
507
524
) ;
508
525
}
0 commit comments