Skip to content

Commit b86285a

Browse files
committed
Do not emit invalid suggestion in E0191 when spans overlap
Fix #115019.
1 parent bf766cd commit b86285a

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

compiler/rustc_hir_analysis/src/astconv/errors.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,21 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
597597
}
598598
}
599599
}
600-
if !suggestions.is_empty() {
600+
suggestions.sort_by_key(|&(span, _)| span);
601+
// There are cases where one bound points to a span within another bound's span, like when
602+
// you have code like the following (#115019), so we skip providing a suggestion in those
603+
// cases to avoid having a malformed suggestion.
604+
//
605+
// pub struct Flatten<I> {
606+
// inner: <IntoIterator<Item: IntoIterator<Item: >>::IntoIterator as Item>::core,
607+
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
608+
// | ^^^^^^^^^^^^^^^^^^^^^
609+
// | |
610+
// | associated types `Item`, `IntoIter` must be specified
611+
// associated types `Item`, `IntoIter` must be specified
612+
// }
613+
let overlaps = suggestions.windows(2).any(|pair| pair[0].0.overlaps(pair[1].0));
614+
if !suggestions.is_empty() && !overlaps {
601615
err.multipart_suggestion(
602616
format!("specify the associated type{}", pluralize!(types_count)),
603617
suggestions,

tests/ui/associated-type-bounds/overlaping-bound-suggestion.stderr

-5
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@ LL | inner: <IntoIterator<Item: IntoIterator<Item: >>::IntoIterator as Item>
66
| | |
77
| | associated types `Item`, `IntoIter` must be specified
88
| associated types `Item`, `IntoIter` must be specified
9-
|
10-
help: specify the associated types
11-
|
12-
LL | inner: <IntoIterator<Item: IntoIterator<Item: >, Item = Type, IntoIter = Type>IntoIterator<Item: , Item = Type, IntoIter = Type>>::IntoIterator as Item>::Core,
13-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
149

1510
error[E0223]: ambiguous associated type
1611
--> $DIR/overlaping-bound-suggestion.rs:7:13

0 commit comments

Comments
 (0)