about summary refs log tree commit diff
path: root/compiler/rustc_middle/src
diff options
context:
space:
mode:
authorEsteban Küber <esteban@kuber.com.ar>2024-11-28 18:43:28 +0000
committerEsteban Küber <esteban@kuber.com.ar>2024-12-07 21:26:20 +0000
commitd13c34828e4bc0924f7e27bae994f9afecaf0ab4 (patch)
tree16f36afad2817d252a1e6dc10c5b87f1a950718f /compiler/rustc_middle/src
parent68253e14ee3d0e81881bc908b07b5ba141daf226 (diff)
reword trait bound suggestion message to include the bounds
Diffstat (limited to 'compiler/rustc_middle/src')
-rw-r--r--compiler/rustc_middle/src/ty/diagnostics.rs45
1 files changed, 31 insertions, 14 deletions
diff --git a/compiler/rustc_middle/src/ty/diagnostics.rs b/compiler/rustc_middle/src/ty/diagnostics.rs
index b08e1632f65..a8500398082 100644
--- a/compiler/rustc_middle/src/ty/diagnostics.rs
+++ b/compiler/rustc_middle/src/ty/diagnostics.rs
@@ -171,7 +171,7 @@ enum SuggestChangingConstraintsMessage<'a> {
 
 fn suggest_changing_unsized_bound(
     generics: &hir::Generics<'_>,
-    suggestions: &mut Vec<(Span, String, SuggestChangingConstraintsMessage<'_>)>,
+    suggestions: &mut Vec<(Span, String, String, SuggestChangingConstraintsMessage<'_>)>,
     param: &hir::GenericParam<'_>,
     def_id: Option<DefId>,
 ) {
@@ -206,7 +206,8 @@ fn suggest_changing_unsized_bound(
             continue;
         }
 
-        let mut push_suggestion = |sp, msg| suggestions.push((sp, String::new(), msg));
+        let mut push_suggestion =
+            |sp, msg| suggestions.push((sp, "Sized".to_string(), String::new(), msg));
 
         if predicate.bounds.len() == unsized_bounds.len() {
             // All the bounds are unsized bounds, e.g.
@@ -348,10 +349,20 @@ pub fn suggest_constraining_type_params<'a>(
             use SuggestChangingConstraintsMessage::RestrictBoundFurther;
 
             if let Some(open_paren_sp) = open_paren_sp {
-                suggestions.push((open_paren_sp, "(".to_string(), RestrictBoundFurther));
-                suggestions.push((span, format!("){suggestion}"), RestrictBoundFurther));
+                suggestions.push((
+                    open_paren_sp,
+                    constraint.clone(),
+                    "(".to_string(),
+                    RestrictBoundFurther,
+                ));
+                suggestions.push((
+                    span,
+                    constraint.clone(),
+                    format!("){suggestion}"),
+                    RestrictBoundFurther,
+                ));
             } else {
-                suggestions.push((span, suggestion, RestrictBoundFurther));
+                suggestions.push((span, constraint.clone(), suggestion, RestrictBoundFurther));
             }
         };
 
@@ -409,6 +420,7 @@ pub fn suggest_constraining_type_params<'a>(
             //                                           - insert: `, X: Bar`
             suggestions.push((
                 generics.tail_span_for_predicate_suggestion(),
+                constraint.clone(),
                 constraints.iter().fold(String::new(), |mut string, &(constraint, _)| {
                     write!(string, ", {param_name}: {constraint}").unwrap();
                     string
@@ -438,6 +450,7 @@ pub fn suggest_constraining_type_params<'a>(
             // default (`<T=Foo>`), so we suggest adding `where T: Bar`.
             suggestions.push((
                 generics.tail_span_for_predicate_suggestion(),
+                constraint.clone(),
                 format!("{where_prefix} {param_name}: {constraint}"),
                 SuggestChangingConstraintsMessage::RestrictTypeFurther { ty: param_name },
             ));
@@ -451,6 +464,7 @@ pub fn suggest_constraining_type_params<'a>(
         if let Some(colon_span) = param.colon_span {
             suggestions.push((
                 colon_span.shrink_to_hi(),
+                constraint.clone(),
                 format!(" {constraint}"),
                 SuggestChangingConstraintsMessage::RestrictType { ty: param_name },
             ));
@@ -463,6 +477,7 @@ pub fn suggest_constraining_type_params<'a>(
         //          - help: consider restricting this type parameter with `T: Foo`
         suggestions.push((
             param.span.shrink_to_hi(),
+            constraint.clone(),
             format!(": {constraint}"),
             SuggestChangingConstraintsMessage::RestrictType { ty: param_name },
         ));
@@ -471,12 +486,16 @@ pub fn suggest_constraining_type_params<'a>(
     // FIXME: remove the suggestions that are from derive, as the span is not correct
     suggestions = suggestions
         .into_iter()
-        .filter(|(span, _, _)| !span.in_derive_expansion())
+        .filter(|(span, _, _, _)| !span.in_derive_expansion())
         .collect::<Vec<_>>();
 
     if suggestions.len() == 1 {
-        let (span, suggestion, msg) = suggestions.pop().unwrap();
-        let post = if unstable_suggestion { " but it is an `unstable` trait" } else { "" };
+        let (span, constraint, suggestion, msg) = suggestions.pop().unwrap();
+        let post = format!(
+            " with {}trait{} `{constraint}`",
+            if unstable_suggestion { "unstable " } else { "" },
+            if constraint.contains('+') { "s" } else { "" },
+        );
         let msg = match msg {
             SuggestChangingConstraintsMessage::RestrictBoundFurther => {
                 format!("consider further restricting this bound{post}")
@@ -488,21 +507,19 @@ pub fn suggest_constraining_type_params<'a>(
                 format!("consider further restricting type parameter `{ty}`{post}")
             }
             SuggestChangingConstraintsMessage::RemoveMaybeUnsized => {
-                format!(
-                    "consider removing the `?Sized` bound to make the type parameter `Sized`{post}"
-                )
+                format!("consider removing the `?Sized` bound to make the type parameter `Sized`")
             }
             SuggestChangingConstraintsMessage::ReplaceMaybeUnsizedWithSized => {
-                format!("consider replacing `?Sized` with `Sized`{post}")
+                format!("consider replacing `?Sized` with `Sized`")
             }
         };
 
         err.span_suggestion_verbose(span, msg, suggestion, applicability);
     } else if suggestions.len() > 1 {
-        let post = if unstable_suggestion { " but some of them are `unstable` traits" } else { "" };
+        let post = if unstable_suggestion { " (some of them are unstable traits)" } else { "" };
         err.multipart_suggestion_verbose(
             format!("consider restricting type parameters{post}"),
-            suggestions.into_iter().map(|(span, suggestion, _)| (span, suggestion)).collect(),
+            suggestions.into_iter().map(|(span, _, suggestion, _)| (span, suggestion)).collect(),
             applicability,
         );
     }