From c92140e83831286c36882fa50cc9edc0ecbbc578 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Tue, 30 May 2023 20:35:10 +0000 Subject: [PATCH] Don't suggest cyclic associated type constraint --- .../src/infer/error_reporting/note_and_explain.rs | 6 ++++++ .../dont-suggest-cyclic-constraint.rs | 11 +++++++++++ .../dont-suggest-cyclic-constraint.stderr | 12 ++++++++++++ 3 files changed, 29 insertions(+) create mode 100644 tests/ui/associated-types/dont-suggest-cyclic-constraint.rs create mode 100644 tests/ui/associated-types/dont-suggest-cyclic-constraint.stderr diff --git a/compiler/rustc_infer/src/infer/error_reporting/note_and_explain.rs b/compiler/rustc_infer/src/infer/error_reporting/note_and_explain.rs index 9a8fac0fc1b..a723dc3f079 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/note_and_explain.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/note_and_explain.rs @@ -374,12 +374,18 @@ impl Trait 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); diff --git a/tests/ui/associated-types/dont-suggest-cyclic-constraint.rs b/tests/ui/associated-types/dont-suggest-cyclic-constraint.rs new file mode 100644 index 00000000000..6894f6b6cc4 --- /dev/null +++ b/tests/ui/associated-types/dont-suggest-cyclic-constraint.rs @@ -0,0 +1,11 @@ +use std::fmt::Debug; + +fn foo(mut iter: I, value: &I::Item) +where + I::Item: Eq + Debug, +{ + debug_assert_eq!(iter.next(), Some(value)); + //~^ ERROR mismatched types +} + +fn main() {} diff --git a/tests/ui/associated-types/dont-suggest-cyclic-constraint.stderr b/tests/ui/associated-types/dont-suggest-cyclic-constraint.stderr new file mode 100644 index 00000000000..3ecac9c83e5 --- /dev/null +++ b/tests/ui/associated-types/dont-suggest-cyclic-constraint.stderr @@ -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<::Item>`, found `Option<&::Item>` + | + = note: expected enum `Option<::Item>` + found enum `Option<&::Item>` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0308`.