Skip to content

Suppress self-referential associated type constraint suggestion#153921

Merged
rust-bors[bot] merged 1 commit intorust-lang:mainfrom
arferreira:fix-bogus-assoc-type-constraint-suggestion
Mar 17, 2026
Merged

Suppress self-referential associated type constraint suggestion#153921
rust-bors[bot] merged 1 commit intorust-lang:mainfrom
arferreira:fix-bogus-assoc-type-constraint-suggestion

Conversation

@arferreira
Copy link
Contributor

@arferreira arferreira commented Mar 15, 2026

Fixes #112104

When comparing Option<&I::Item> against Option<I::Item>, the compiler decomposes the mismatch into expected = &<I as Iterator>::Item vs found = <I as Iterator>::Item. ARM 2 in note_and_explain_type_err matches because found is a projection, and would suggest Item = &<I as Iterator>::Item — constraining the associated type to something that contains itself.

ARM 1 (expected_projection) already guards against this with found.contains(expected). ARM 2 was missing the symmetric check. This adds expected.contains(found) to the match guard.

Before:

help: consider constraining the associated type `<I as Iterator>::Item` to `&<I as Iterator>::Item`
  |
1 | fn foo<I: Iterator<Item = &<I as Iterator>::Item>>(iter: &mut I) {
  |                   +++++++++++++++++++++++++++++++

After: bogus suggestion is gone, and the existing .as_ref() suggestion from hir_typeck still fires correctly.

r? @estebank

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 15, 2026
@estebank
Copy link
Contributor

@bors r+ rollup

@rust-bors
Copy link
Contributor

rust-bors bot commented Mar 16, 2026

📌 Commit 4ecc78d has been approved by estebank

It is now in the queue for this repository.

@rust-bors rust-bors bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 16, 2026
jhpratt added a commit to jhpratt/rust that referenced this pull request Mar 17, 2026
…nstraint-suggestion, r=estebank

Suppress self-referential associated type constraint suggestion

Fixes rust-lang#112104

When comparing `Option<&I::Item>` against `Option<I::Item>`, the compiler decomposes the mismatch into `expected = &<I as Iterator>::Item` vs `found = <I as Iterator>::Item`. ARM 2 in `note_and_explain_type_err` matches because `found` is a projection, and would suggest `Item = &<I as Iterator>::Item` — constraining the associated type to something that contains itself.

ARM 1 (`expected_projection`) already guards against this with `found.contains(expected)`. ARM 2 was missing the symmetric check. This adds `expected.contains(found)` to the match guard.

Before:
```rust
help: consider constraining the associated type `<I as Iterator>::Item` to `&<I as Iterator>::Item`
  |
1 | fn foo<I: Iterator<Item = &<I as Iterator>::Item>>(iter: &mut I) {
  |                   +++++++++++++++++++++++++++++++
```

After: bogus suggestion is gone, and the existing `.as_ref()` suggestion from `hir_typeck` still fires correctly.

r? @estebank
rust-bors bot pushed a commit that referenced this pull request Mar 17, 2026
Rollup of 5 pull requests

Successful merges:

 - #146013 (Add `From` impls for wrapper types)
 - #153966 (suggest valid features when target feature is invalid)
 - #153372 (Fix LegacyKeyValueFormat report from docker build: x86_64-gnu)
 - #153738 (Don't look for non-type-level assoc consts when checking trait object types)
 - #153921 (Suppress self-referential associated type constraint suggestion)
Zalathar added a commit to Zalathar/rust that referenced this pull request Mar 17, 2026
…nstraint-suggestion, r=estebank

Suppress self-referential associated type constraint suggestion

Fixes rust-lang#112104

When comparing `Option<&I::Item>` against `Option<I::Item>`, the compiler decomposes the mismatch into `expected = &<I as Iterator>::Item` vs `found = <I as Iterator>::Item`. ARM 2 in `note_and_explain_type_err` matches because `found` is a projection, and would suggest `Item = &<I as Iterator>::Item` — constraining the associated type to something that contains itself.

ARM 1 (`expected_projection`) already guards against this with `found.contains(expected)`. ARM 2 was missing the symmetric check. This adds `expected.contains(found)` to the match guard.

Before:
```rust
help: consider constraining the associated type `<I as Iterator>::Item` to `&<I as Iterator>::Item`
  |
1 | fn foo<I: Iterator<Item = &<I as Iterator>::Item>>(iter: &mut I) {
  |                   +++++++++++++++++++++++++++++++
```

After: bogus suggestion is gone, and the existing `.as_ref()` suggestion from `hir_typeck` still fires correctly.

r? @estebank
rust-bors bot pushed a commit that referenced this pull request Mar 17, 2026
Rollup of 5 pull requests

Successful merges:

 - #153966 (suggest valid features when target feature is invalid)
 - #153372 (Fix LegacyKeyValueFormat report from docker build: x86_64-gnu)
 - #153738 (Don't look for non-type-level assoc consts when checking trait object types)
 - #153907 (Remove redundant fields from `QueryStackFrame`)
 - #153921 (Suppress self-referential associated type constraint suggestion)
@rust-bors rust-bors bot merged commit 66c506d into rust-lang:main Mar 17, 2026
11 checks passed
@rustbot rustbot added this to the 1.96.0 milestone Mar 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bogus suggestion on I::Item vs &I::Item mismatch

3 participants