Skip to content

Prelude collision lint: invalid suggestion for T::from_iter when T is generic. #86940

Closed
@m-ou-se

Description

@m-ou-se

For generic types, the prelude collision lint can produce suggestions that do not compile:

Code:

struct Generic<T>(T);

trait Hey { fn from_iter(_: i32) -> Self; }

impl Hey for Generic<i32> { fn from_iter(x: i32) -> Self { Self(x) } }

impl std::iter::FromIterator<i32> for Generic<i32> { fn from_iter<T: IntoIterator<Item = i32>>(_: T) -> Self { todo!() } }

fn main() {
    Generic::from_iter(1);
}

Suggestion from the prelude collision lint:

warning: trait-associated function `from_iter` will become ambiguous in Rust 2021
  --> src/main.rs:10:5
   |
10 |     Generic::from_iter(1);
   |     ^^^^^^^^^^^^^^^^^^ help: disambiguate the associated function: `<Generic as Hey>::from_iter`
   |
   = note: `-W rust-2021-prelude-collisions` implied by `-W rust-2021-compatibility`
   = warning: this is accepted in the current edition (Rust 2018) but is a hard error in Rust 2021!
   = note: for more information, see issue #85684 <https://github.com/rust-lang/rust/issues/85684>

Error after applying the suggestion:

error[E0107]: missing generics for struct `Generic`
  --> src/main.rs:10:6
   |
10 |     <Generic as Hey>::from_iter(1);
   |      ^^^^^^^ expected 1 generic argument
   |

Metadata

Metadata

Assignees

Labels

A-edition-2021Area: The 2021 editionD-editionDiagnostics: An error or lint that should account for edition differences.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions