Skip to content

Commit c92140e

Browse files
Don't suggest cyclic associated type constraint
1 parent acf257e commit c92140e

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

compiler/rustc_infer/src/infer/error_reporting/note_and_explain.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,12 +374,18 @@ impl<T> Trait<T> for X {
374374
) {
375375
let tcx = self.tcx;
376376

377+
// Don't suggest constraining a projection to something containing itself
378+
if self.tcx.erase_regions(values.found).contains(self.tcx.erase_regions(values.expected)) {
379+
return;
380+
}
381+
377382
let msg = || {
378383
format!(
379384
"consider constraining the associated type `{}` to `{}`",
380385
values.expected, values.found
381386
)
382387
};
388+
383389
let body_owner = tcx.hir().get_if_local(body_owner_def_id);
384390
let current_method_ident = body_owner.and_then(|n| n.ident()).map(|i| i.name);
385391

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
use std::fmt::Debug;
2+
3+
fn foo<I: Iterator>(mut iter: I, value: &I::Item)
4+
where
5+
I::Item: Eq + Debug,
6+
{
7+
debug_assert_eq!(iter.next(), Some(value));
8+
//~^ ERROR mismatched types
9+
}
10+
11+
fn main() {}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/dont-suggest-cyclic-constraint.rs:7:35
3+
|
4+
LL | debug_assert_eq!(iter.next(), Some(value));
5+
| ^^^^^^^^^^^ expected `Option<<I as Iterator>::Item>`, found `Option<&<I as Iterator>::Item>`
6+
|
7+
= note: expected enum `Option<<I as Iterator>::Item>`
8+
found enum `Option<&<I as Iterator>::Item>`
9+
10+
error: aborting due to previous error
11+
12+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)