Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,13 @@ impl<T> Trait<T> for X {
cause.code(),
);
}
// Don't suggest constraining a projection to something
// containing itself, e.g. `Item = &<I as Iterator>::Item`.
(_, ty::Alias(ty::Projection | ty::Inherent, proj_ty))
if !tcx.is_impl_trait_in_trait(proj_ty.def_id) =>
if !tcx.is_impl_trait_in_trait(proj_ty.def_id)
&& !tcx
.erase_and_anonymize_regions(values.expected)
.contains(tcx.erase_and_anonymize_regions(values.found)) =>
{
let msg = || {
format!(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Regression test for #112104.
//
// Don't suggest `Item = &<I as Iterator>::Item` when
// the expected type wraps the found projection.

fn option_of_ref_assoc<I: Iterator>(iter: &mut I) {
let _: Option<&I::Item> = iter.next();
//~^ ERROR mismatched types
}

// Valid constraint suggestions should still fire.
trait Foo {
type Assoc;
}

fn assoc_to_concrete<T: Foo>(x: T::Assoc) -> u32 {
x //~ ERROR mismatched types
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
error[E0308]: mismatched types
--> $DIR/dont-suggest-self-referential-constraint.rs:7:31
|
LL | let _: Option<&I::Item> = iter.next();
| ---------------- ^^^^^^^^^^^ expected `Option<&<I as Iterator>::Item>`, found `Option<<I as Iterator>::Item>`
| |
| expected due to this
|
= note: expected enum `Option<&_>`
found enum `Option<_>`
help: try using `.as_ref()` to convert `Option<<I as Iterator>::Item>` to `Option<&<I as Iterator>::Item>`
|
LL | let _: Option<&I::Item> = iter.next().as_ref();
| +++++++++

error[E0308]: mismatched types
--> $DIR/dont-suggest-self-referential-constraint.rs:17:5
|
LL | fn assoc_to_concrete<T: Foo>(x: T::Assoc) -> u32 {
| --- expected `u32` because of return type
LL | x
| ^ expected `u32`, found associated type
|
= note: expected type `u32`
found associated type `<T as Foo>::Assoc`
help: consider constraining the associated type `<T as Foo>::Assoc` to `u32`
|
LL | fn assoc_to_concrete<T: Foo<Assoc = u32>>(x: T::Assoc) -> u32 {
| +++++++++++++

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0308`.
Loading