Skip to content

Commit 0e84d42

Browse files
Rollup merge of #115077 - estebank:issue-115019, r=compiler-errors
Do not emit invalid suggestion in E0191 when spans overlap Fix #115019.
2 parents e9897c3 + b86285a commit 0e84d42

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
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,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#![allow(bare_trait_objects)]
2+
#![feature(associated_type_bounds)]
3+
trait Item {
4+
type Core;
5+
}
6+
pub struct Flatten<I> {
7+
inner: <IntoIterator<Item: IntoIterator<Item: >>::IntoIterator as Item>::Core,
8+
//~^ ERROR E0191
9+
//~| ERROR E0223
10+
}
11+
12+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
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
2+
--> $DIR/overlaping-bound-suggestion.rs:7:13
3+
|
4+
LL | inner: <IntoIterator<Item: IntoIterator<Item: >>::IntoIterator as Item>::Core,
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
| | |
7+
| | associated types `Item`, `IntoIter` must be specified
8+
| associated types `Item`, `IntoIter` must be specified
9+
10+
error[E0223]: ambiguous associated type
11+
--> $DIR/overlaping-bound-suggestion.rs:7:13
12+
|
13+
LL | inner: <IntoIterator<Item: IntoIterator<Item: >>::IntoIterator as Item>::Core,
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
15+
|
16+
help: if there were a trait named `Example` with associated type `IntoIterator` implemented for `(dyn IntoIterator + 'static)`, you could use the fully-qualified path
17+
|
18+
LL | inner: <<(dyn IntoIterator + 'static) as Example>::IntoIterator as Item>::Core,
19+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20+
21+
error: aborting due to 2 previous errors
22+
23+
Some errors have detailed explanations: E0191, E0223.
24+
For more information about an error, try `rustc --explain E0191`.

0 commit comments

Comments
 (0)