Skip to content

Tracking issue for future-incompatibility lint array_into_iter #66145

Open
@LukasKalbertodt

Description

@LukasKalbertodt

What is this lint about?

Method resolution is responsible for finding a fitting method for a method call expression receiver.name(args). The expression array.into_iter() (where array has an array type [T; N]) currently resolves to either <&[T; N] as IntoIterator>::into_iter (for arrays smaller than 33) or <&[T] as IntoIterator>::into_iter (for larger arrays). In either way, an iterator over references to the array's elements is returned.

In the future, we might want to add impl IntoIterator for [T; N] (for arrays by value). In that case, method resolution would prioritize <[T;N] as IntoIterator>::into_iter as that method call would not require an autoref-coercion. In other words: the receiver expression (left of the dot in the method call) fits the receiver type of the method perfectly, so that method is preferred. In the &[T; N] or &[T] case, coercions are necessary to make the method call work.

Since the new method is prioritized over the old ones, some code can break. Usually that code looks somewhat like this:

[1, 2, 3].into_iter().for_each(|n| { *n; });

Currently this works, as into_iter returns an iterator over references to the array's values, meaning that n is indeed &{integer} and can be dereferenced. With the new impl, that code would stop compiling. The lint has been put in place to warn of this potentially upcoming breaking change.

How to fix this warning/error

Replace .into_iter() with .iter(). The latter is guaranteed to always resolve to an iterator over references to the elements.



Current status

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-arrayArea: `[T; N]`A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-future-incompatibilityCategory: Future-incompatibility lintsC-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    Status

    Idea

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions