diff options
| author | Esteban Küber <esteban@kuber.com.ar> | 2023-08-21 21:01:27 +0000 |
|---|---|---|
| committer | Esteban Küber <esteban@kuber.com.ar> | 2023-08-22 15:51:12 +0000 |
| commit | b86285af1661abb0824ead4ea89db0e5c898c422 (patch) | |
| tree | eaefdb2a627c30a19349f3ce0742214aabdbe327 | |
| parent | bf766cd31bf045dfe7f37d4146f36aff001438ed (diff) | |
| download | rust-b86285af1661abb0824ead4ea89db0e5c898c422.tar.gz rust-b86285af1661abb0824ead4ea89db0e5c898c422.zip | |
Do not emit invalid suggestion in E0191 when spans overlap
Fix #115019.
| -rw-r--r-- | compiler/rustc_hir_analysis/src/astconv/errors.rs | 16 | ||||
| -rw-r--r-- | tests/ui/associated-type-bounds/overlaping-bound-suggestion.stderr | 5 |
2 files changed, 15 insertions, 6 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.stderr b/tests/ui/associated-type-bounds/overlaping-bound-suggestion.stderr index 8bea440ddba..61299550e98 100644 --- a/tests/ui/associated-type-bounds/overlaping-bound-suggestion.stderr +++ b/tests/ui/associated-type-bounds/overlaping-bound-suggestion.stderr @@ -6,11 +6,6 @@ LL | inner: <IntoIterator<Item: IntoIterator<Item: >>::IntoIterator as Item> | | | | | associated types `Item`, `IntoIter` must be specified | associated types `Item`, `IntoIter` must be specified - | -help: specify the associated types - | -LL | inner: <IntoIterator<Item: IntoIterator<Item: >, Item = Type, IntoIter = Type>IntoIterator<Item: , Item = Type, IntoIter = Type>>::IntoIterator as Item>::Core, - | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ error[E0223]: ambiguous associated type --> $DIR/overlaping-bound-suggestion.rs:7:13 |
