about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--compiler/rustc_hir_analysis/src/astconv/errors.rs16
-rw-r--r--tests/ui/associated-type-bounds/overlaping-bound-suggestion.rs12
-rw-r--r--tests/ui/associated-type-bounds/overlaping-bound-suggestion.stderr24
3 files changed, 51 insertions, 1 deletions
diff --git a/compiler/rustc_hir_analysis/src/astconv/errors.rs b/compiler/rustc_hir_analysis/src/astconv/errors.rs
index bd311c98fac..6082d446979 100644
--- a/compiler/rustc_hir_analysis/src/astconv/errors.rs
+++ b/compiler/rustc_hir_analysis/src/astconv/errors.rs
@@ -597,7 +597,21 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
                 }
             }
         }
-        if !suggestions.is_empty() {
+        suggestions.sort_by_key(|&(span, _)| span);
+        // There are cases where one bound points to a span within another bound's span, like when
+        // you have code like the following (#115019), so we skip providing a suggestion in those
+        // cases to avoid having a malformed suggestion.
+        //
+        // pub struct Flatten<I> {
+        //     inner: <IntoIterator<Item: IntoIterator<Item: >>::IntoIterator as Item>::core,
+        //             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+        //             |                  ^^^^^^^^^^^^^^^^^^^^^
+        //             |                  |
+        //             |                  associated types `Item`, `IntoIter` must be specified
+        //             associated types `Item`, `IntoIter` must be specified
+        // }
+        let overlaps = suggestions.windows(2).any(|pair| pair[0].0.overlaps(pair[1].0));
+        if !suggestions.is_empty() && !overlaps {
             err.multipart_suggestion(
                 format!("specify the associated type{}", pluralize!(types_count)),
                 suggestions,
diff --git a/tests/ui/associated-type-bounds/overlaping-bound-suggestion.rs b/tests/ui/associated-type-bounds/overlaping-bound-suggestion.rs
new file mode 100644
index 00000000000..3853bc8594f
--- /dev/null
+++ b/tests/ui/associated-type-bounds/overlaping-bound-suggestion.rs
@@ -0,0 +1,12 @@
+#![allow(bare_trait_objects)]
+#![feature(associated_type_bounds)]
+trait Item {
+    type Core;
+}
+pub struct Flatten<I> {
+    inner: <IntoIterator<Item: IntoIterator<Item: >>::IntoIterator as Item>::Core,
+    //~^ ERROR E0191
+    //~| ERROR E0223
+}
+
+fn main() {}
diff --git a/tests/ui/associated-type-bounds/overlaping-bound-suggestion.stderr b/tests/ui/associated-type-bounds/overlaping-bound-suggestion.stderr
new file mode 100644
index 00000000000..61299550e98
--- /dev/null
+++ b/tests/ui/associated-type-bounds/overlaping-bound-suggestion.stderr
@@ -0,0 +1,24 @@
+error[E0191]: the value of the associated types `IntoIter` (from trait `IntoIterator`), `IntoIter` (from trait `IntoIterator`), `Item` (from trait `IntoIterator`), `Item` (from trait `IntoIterator`) must be specified
+  --> $DIR/overlaping-bound-suggestion.rs:7:13
+   |
+LL |     inner: <IntoIterator<Item: IntoIterator<Item: >>::IntoIterator as Item>::Core,
+   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |             |                  |
+   |             |                  associated types `Item`, `IntoIter` must be specified
+   |             associated types `Item`, `IntoIter` must be specified
+
+error[E0223]: ambiguous associated type
+  --> $DIR/overlaping-bound-suggestion.rs:7:13
+   |
+LL |     inner: <IntoIterator<Item: IntoIterator<Item: >>::IntoIterator as Item>::Core,
+   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |
+help: if there were a trait named `Example` with associated type `IntoIterator` implemented for `(dyn IntoIterator + 'static)`, you could use the fully-qualified path
+   |
+LL |     inner: <<(dyn IntoIterator + 'static) as Example>::IntoIterator as Item>::Core,
+   |             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+error: aborting due to 2 previous errors
+
+Some errors have detailed explanations: E0191, E0223.
+For more information about an error, try `rustc --explain E0191`.