about summary refs log tree commit diff
diff options
context:
space:
mode:
authorWill Crichton <wcrichto@cs.stanford.edu>2022-07-15 18:02:26 -0700
committerWill Crichton <wcrichto@cs.stanford.edu>2022-07-15 18:06:20 -0700
commit2f15dfab0b444e757dcb32d935a716a5785b2155 (patch)
tree5ef48e1a716180e6e3230b1ac1b8c6ed9f6de657
parente5bb7d80d698102175dbbb4094bec03b682e8399 (diff)
downloadrust-2f15dfab0b444e757dcb32d935a716a5785b2155.tar.gz
rust-2f15dfab0b444e757dcb32d935a716a5785b2155.zip
Fix suggestion regression with incorrect syntactic combination of trait bounds
-rw-r--r--compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs14
-rw-r--r--src/test/ui/suggestions/issue-97677.fixed8
-rw-r--r--src/test/ui/suggestions/issue-97677.rs4
-rw-r--r--src/test/ui/suggestions/issue-97677.stderr4
4 files changed, 26 insertions, 4 deletions
diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs
index 469436ff0db..bca80e7ab8a 100644
--- a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs
+++ b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs
@@ -599,7 +599,19 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
                     if let Some(proj_pred) = proj_pred {
                         let ProjectionPredicate { projection_ty, term } = proj_pred.skip_binder();
                         let item = self.tcx.associated_item(projection_ty.item_def_id);
-                        constraint.push_str(&format!("<{}={}>", item.name, term.to_string()));
+
+                        // FIXME: this case overlaps with code in TyCtxt::note_and_explain_type_err.
+                        // That should be extracted into a helper function.
+                        if constraint.ends_with('>') {
+                            constraint = format!(
+                                "{}, {}={}>",
+                                &constraint[..constraint.len() - 1],
+                                item.name,
+                                term.to_string()
+                            );
+                        } else {
+                            constraint.push_str(&format!("<{}={}>", item.name, term.to_string()));
+                        }
                     }
 
                     if suggest_constraining_type_param(
diff --git a/src/test/ui/suggestions/issue-97677.fixed b/src/test/ui/suggestions/issue-97677.fixed
new file mode 100644
index 00000000000..73ca9f97b43
--- /dev/null
+++ b/src/test/ui/suggestions/issue-97677.fixed
@@ -0,0 +1,8 @@
+// run-rustfix
+
+fn add_ten<N: std::ops::Add<i32, Output=N>>(n: N) -> N {
+    n + 10
+    //~^ ERROR cannot add `{integer}` to `N`
+}
+
+fn main() { add_ten(0); }
diff --git a/src/test/ui/suggestions/issue-97677.rs b/src/test/ui/suggestions/issue-97677.rs
index a4c3b1350b8..2abf2af3384 100644
--- a/src/test/ui/suggestions/issue-97677.rs
+++ b/src/test/ui/suggestions/issue-97677.rs
@@ -1,6 +1,8 @@
+// run-rustfix
+
 fn add_ten<N>(n: N) -> N {
     n + 10
     //~^ ERROR cannot add `{integer}` to `N`
 }
 
-fn main() {}
+fn main() { add_ten(0); }
diff --git a/src/test/ui/suggestions/issue-97677.stderr b/src/test/ui/suggestions/issue-97677.stderr
index cad6f858df1..069b184ac63 100644
--- a/src/test/ui/suggestions/issue-97677.stderr
+++ b/src/test/ui/suggestions/issue-97677.stderr
@@ -1,5 +1,5 @@
 error[E0369]: cannot add `{integer}` to `N`
-  --> $DIR/issue-97677.rs:2:7
+  --> $DIR/issue-97677.rs:4:7
    |
 LL |     n + 10
    |     - ^ -- {integer}
@@ -8,7 +8,7 @@ LL |     n + 10
    |
 help: consider restricting type parameter `N`
    |
-LL | fn add_ten<N: std::ops::Add<i32><Output=N>>(n: N) -> N {
+LL | fn add_ten<N: std::ops::Add<i32, Output=N>>(n: N) -> N {
    |             ++++++++++++++++++++++++++++++
 
 error: aborting due to previous error