Don't suggest cyclic associated type constraint

This commit is contained in:
Michael Goulet 2023-05-30 20:35:10 +00:00
parent acf257e62c
commit c92140e838
3 changed files with 29 additions and 0 deletions

View file

@ -374,12 +374,18 @@ impl<T> Trait<T> for X {
) {
let tcx = self.tcx;
// Don't suggest constraining a projection to something containing itself
if self.tcx.erase_regions(values.found).contains(self.tcx.erase_regions(values.expected)) {
return;
}
let msg = || {
format!(
"consider constraining the associated type `{}` to `{}`",
values.expected, values.found
)
};
let body_owner = tcx.hir().get_if_local(body_owner_def_id);
let current_method_ident = body_owner.and_then(|n| n.ident()).map(|i| i.name);

View file

@ -0,0 +1,11 @@
use std::fmt::Debug;
fn foo<I: Iterator>(mut iter: I, value: &I::Item)
where
I::Item: Eq + Debug,
{
debug_assert_eq!(iter.next(), Some(value));
//~^ ERROR mismatched types
}
fn main() {}

View file

@ -0,0 +1,12 @@
error[E0308]: mismatched types
--> $DIR/dont-suggest-cyclic-constraint.rs:7:35
|
LL | debug_assert_eq!(iter.next(), Some(value));
| ^^^^^^^^^^^ expected `Option<<I as Iterator>::Item>`, found `Option<&<I as Iterator>::Item>`
|
= note: expected enum `Option<<I as Iterator>::Item>`
found enum `Option<&<I as Iterator>::Item>`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0308`.