Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tracking issue for array_into_iter future compatibility lint #66145

Open
2 tasks done
LukasKalbertodt opened this issue Nov 6, 2019 · 5 comments
Open
2 tasks done

Tracking issue for array_into_iter future compatibility lint #66145

LukasKalbertodt opened this issue Nov 6, 2019 · 5 comments
Labels
A-array Area: [T; N] A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-future-compatibility Category: Future-compatibility lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@LukasKalbertodt
Copy link
Member

LukasKalbertodt commented Nov 6, 2019

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

@jonas-schievink jonas-schievink added A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-future-compatibility Category: Future-compatibility lints labels Nov 6, 2019
@Centril Centril added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Nov 6, 2019
Centril added a commit to Centril/rust that referenced this issue Nov 7, 2019
…t, r=matthewjasper

Add future incompatibility lint for `array.into_iter()`

This is for rust-lang#65819. This lint warns when calling `into_iter` on an array directly. That's because today the method call resolves to `<&[T] as IntoIterator>::into_iter` but that would change when adding `IntoIterator` impls for arrays. This problem is discussed in detail in rust-lang#65819.

We still haven't decided how to proceed exactly, but it seems like adding a lint is a good idea regardless?

Also: this is the first time I implement a lint, so there are probably a lot of things I can improve. I used a different strategy than @scottmcm describes [here](rust-lang#65819 (comment)) since I already started implementing this before they commented.

### TODO

- [x] Decide if we want this lint -> apparently [we want](rust-lang#65819 (comment))
- [x] Open a lint-tracking-issue and add the correct issue number in the code -> rust-lang#66145
@est31
Copy link
Member

est31 commented Nov 8, 2019

Was hitting this in a private (currently not open source) project of mine. Thanks for introducing this slowly so that it can be caught in time instead of just breaking it!

bors bot added a commit to rust-lang/rust-analyzer that referenced this issue Nov 15, 2019
2259: Update smallvec and fix rustc warning r=matklad a=memoryruins

- Update smallvec in ra_mbe to [1.0](https://github.com/servo/rust-smallvec/releases/tag/v1.0.0)
- Heed rustc's `array_into_iter` lint rust-lang/rust#66145

Co-authored-by: memoryruins <[email protected]>
unleashed added a commit to 3scale-rs/threescalers that referenced this issue Nov 25, 2019
Such calls will likely create by-value iterators in the future.

See issue rust-lang/rust#66145.
unleashed added a commit to 3scale-rs/threescalers that referenced this issue Nov 25, 2019
Such calls will likely create by-value iterators in the future, and
raise warnings in 1.41.0 .

See issue rust-lang/rust#66145.
bors bot added a commit to 3scale-rs/threescalers that referenced this issue Nov 25, 2019
60: examples: use [T]::iter() rather than [T]::into_iter() r=davidor a=unleashed

Such calls will likely create by-value iterators in the future, and
raise warnings in 1.41.0 .

See issue rust-lang/rust#66145.

Co-authored-by: Alejandro Martinez Ruiz <[email protected]>
unleashed added a commit to 3scale-rs/threescalers that referenced this issue Nov 25, 2019
Such calls will likely create by-value iterators in the future, and
raise warnings in 1.41.0 .

See issue rust-lang/rust#66145.
sourcefrog added a commit to sourcefrog/conserve that referenced this issue Nov 29, 2019
jedisct1 added a commit to bytecodealliance/lucet that referenced this issue Dec 20, 2019
This was previously accepted by the compiler but is being phased out;
it will become a hard error in a future rustc release.

See rust-lang/rust#66145
>
jedisct1 added a commit to bytecodealliance/lucet that referenced this issue Dec 20, 2019
This was previously accepted by the compiler but is being phased out;
it will become a hard error in a future rustc release.

See rust-lang/rust#66145
>
@Skepfyr
Copy link
Contributor

Skepfyr commented Dec 20, 2019

This lint doesn't trigger for boxed slices (although the corresponding clippy lint does), #59878 suggests it probably should. Is this intentional?

@LukasKalbertodt
Copy link
Member Author

Currently it's intentional because this lint is only about arrays. But it seems reasonable to me to also lint for Box<[T]>, but probably as a new lint and not as array_into_iter. Furthermore, the lint doesn't fire for Box<[T; N]> which it should. I will try to prepare two PRs fixing those two things soon.

bors added a commit that referenced this issue Dec 25, 2019
…hewjasper

Generalize `array_into_iter` lint to also lint for boxed arrays

`Box` is special in that a method call on a box can move the value out
of the box. Thus, the same backwards-compatibility problem can arise
for boxed arrays as for simple arrays.

---

CC #66145
r? @matthewjasper  (as you reviewed the first PR)
Shirtpantswallet added a commit to Shirtpantswallet/proptest that referenced this issue Dec 31, 2019
`into_iter()` has been deprecated for arrays in 1.41 and replaced with
`iter()`. See rust-lang/rust#66145 .

Signed-off-by: Joshua Sorenson <[email protected]>
iceiix added a commit to iceiix/stevenarella that referenced this issue Jan 5, 2020
warning: this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` (due to autoref coercions), but that might change in the future when `IntoIterator` impls for arrays are added.
   --> src/entity/player.rs:363:11
    |
363 |         ].into_iter().enumerate() {
    |           ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter`
    |
    = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
    = note: for more information, see issue #66145 <rust-lang/rust#66145>
est31 added a commit to est31/mimas that referenced this issue Sep 3, 2020
dmitris added a commit to dmitris/itertools that referenced this issue Sep 20, 2020
Change into_iter() to iter() in impl_zip_iter macro
to avoid the compliation warning related to
s://github.com/rust-lang/rust/issues/66145.
dmitris added a commit to dmitris/itertools that referenced this issue Sep 20, 2020
Change into_iter() to iter() in impl_zip_iter macro to avoid
the compliation warning related to the potentially upcoming
incompatibility with array.into_iter()
rust-lang/rust#66145.
dmitris added a commit to dmitris/itertools that referenced this issue Sep 20, 2020
Change into_iter() to iter() in impl_zip_iter macro
to avoid the compliation warning related to
s://github.com/rust-lang/rust/issues/66145.
bors bot added a commit to rust-itertools/itertools that referenced this issue Sep 22, 2020
475: Trait impls for tuples of arity <= 12 r=jswrenn a=jswrenn

I stopped at 12, because that's where libcore stops.

fixes #428
fixes #398

476: Undeprecate and optimize `fold_while` r=jswrenn a=jswrenn

Prompted by #469.

479: fix compiler warning on array.into_iter() r=jswrenn a=dmitris

Fix the compile warnings listed below by changing `into_iter()` invocation to `iter()` 
in the `impl_zip_iter` macro as recommended in rust-lang/rust#66145.
For additional background, see also rust-lang/rust#65819 and rust-lang/rust#66017 (the latter is the linter change producing the warning).

```
$ cargo build
   Compiling itertools v0.9.0 (/Users/dsavints/dev/hack/github.com/rust-itertools/itertools)
warning: this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` (due to autoref coercions), but that might change in the future when `IntoIterator` impls for arrays are added.
   --> src/ziptuple.rs:111:47
    |
111 |                 let size = *[$( $B.len(), )*].into_iter().min().unwrap();
    |                                               ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter`
...
128 | impl_zip_iter!(A);
    | ------------------ in this macro invocation
    |
    = note: `#[warn(array_into_iter)]` on by default
    = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
    = note: for more information, see issue #66145 <rust-lang/rust#66145>
    = note: this warning originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

warning: this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` (due to autoref coercions), but that might change in the future when `IntoIterator` impls for arrays are added.
   --> src/ziptuple.rs:111:47
    |
111 |                 let size = *[$( $B.len(), )*].into_iter().min().unwrap();
    |                                               ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter`
...
129 | impl_zip_iter!(A, B);
    | --------------------- in this macro invocation
    |
    = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
    = note: for more information, see issue #66145 <rust-lang/rust#66145>
    = note: this warning originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
```

Co-authored-by: Jack Wrenn <[email protected]>
Co-authored-by: Dmitry Savintsev <[email protected]>
michaelcmartin added a commit to michaelcmartin/syzygy that referenced this issue Dec 22, 2020
As of Rust 1.48, array types do not actually implement IntoIterator, which
means that the borrow checker automatically introduces borrows with
surprising results. This behavior is now explicitly marked as unstable:

warning: this method call currently resolves to
`<&[T; N] as IntoIterator>::into_iter` (due to autoref coercions), but
that might change in the future when `IntoIterator` impls for arrays
are added.
warning: this was previously accepted by the compiler but is being
phased out; it will become a hard error in a future release!
note: for more information, see issue #66145
<rust-lang/rust#66145>

This commit changes the into_iter() calls into iter() calls that
match the current behavior in Rust.
@bstrie
Copy link
Contributor

bstrie commented Jan 24, 2021

With the edition coming up, has there been discussion about turning lint this into deny-by-default for users of the 2021 edition? Obviously adding a hypothetical impl IntoIterator for [T; N] would affect all editions, but having this pattern be denied in a new edition would be a gradual step toward making that transition gracefully. This is especially relevant now that min_const_generics will be stable in March, which will make fixed-size arrays more ergonomic than ever.

@SimonSapin
Copy link
Contributor

This won’t help with dependencies in the way #71249 would since current versions of these dependencies will still have a previous edition, but this still sounds like a good step.

dgreid pushed a commit to dgreid/crosvm that referenced this issue Mar 11, 2021
Convert into_iter() calls into iter() where appropriate:
rust-lang/rust#66145

BUG=b:181674168
TEST=cargo test -p disk --features=composite-disk

Change-Id: I9c82a7b956598628010a3dbb33db6e425bbc4e2c
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/ /2743402
Tested-by: kokoro <noreply [email protected]>
Commit-Queue: Daniel Verkamp <[email protected]>
Reviewed-by: Dennis Kempin <[email protected]>
Reviewed-by: Dylan Reid <[email protected]>
LukasKalbertodt added a commit to LukasKalbertodt/rust that referenced this issue Mar 29, 2021
This was lint's only purpose was to detect code that would break once
arrays would implement `IntoIterator`. Now that this impl exists, this
lint will never be triggered and thus is useless.
fogti added a commit to fogti/libnar that referenced this issue Apr 7, 2021
…into_iter)

e.g.:
warning: this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` (due to autoref coercions), but that might change in the future when `IntoIterator` impls for arrays are added.
  --> tests/serialize.rs:15:18
   |
15 |                 .into_iter()
   |                  ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter`
   |
   = note: `#[warn(array_into_iter)]` on by default
   = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
   = note: for more information, see issue #66145 <rust-lang/rust#66145>
fogti added a commit to fogti/libnar that referenced this issue Apr 7, 2021
…into_iter)

e.g.:
warning: this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` (due to autoref coercions), but that might change in the future when `IntoIterator` impls for arrays are added.
  --> tests/serialize.rs:15:18
   |
15 |                 .into_iter()
   |                  ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter`
   |
   = note: `#[warn(array_into_iter)]` on by default
   = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
   = note: for more information, see issue #66145 <rust-lang/rust#66145>
m-ou-se added a commit to m-ou-se/rust that referenced this issue Apr 28, 2021
Point out that behavior might be switched on 2015 and 2018 too one day

Reword documentation to make it clear that behaviour can be switched on older editions too, one day in the future. It doesn't *have* to be switched, but I think it's good to have it as an option and re-evaluate it a few months/years down the line when e.g. the crates that showed up in crater were broken by different changes in the language already.

cc rust-lang#25725, rust-lang#65819, rust-lang#66145, rust-lang#84147 , and rust-lang#84133 (comment)
jackh726 added a commit to jackh726/rust that referenced this issue Apr 28, 2021
Point out that behavior might be switched on 2015 and 2018 too one day

Reword documentation to make it clear that behaviour can be switched on older editions too, one day in the future. It doesn't *have* to be switched, but I think it's good to have it as an option and re-evaluate it a few months/years down the line when e.g. the crates that showed up in crater were broken by different changes in the language already.

cc rust-lang#25725, rust-lang#65819, rust-lang#66145, rust-lang#84147 , and rust-lang#84133 (comment)
jackh726 added a commit to jackh726/rust that referenced this issue Apr 29, 2021
Point out that behavior might be switched on 2015 and 2018 too one day

Reword documentation to make it clear that behaviour can be switched on older editions too, one day in the future. It doesn't *have* to be switched, but I think it's good to have it as an option and re-evaluate it a few months/years down the line when e.g. the crates that showed up in crater were broken by different changes in the language already.

cc rust-lang#25725, rust-lang#65819, rust-lang#66145, rust-lang#84147 , and rust-lang#84133 (comment)
jackh726 added a commit to jackh726/rust that referenced this issue Apr 29, 2021
Point out that behavior might be switched on 2015 and 2018 too one day

Reword documentation to make it clear that behaviour can be switched on older editions too, one day in the future. It doesn't *have* to be switched, but I think it's good to have it as an option and re-evaluate it a few months/years down the line when e.g. the crates that showed up in crater were broken by different changes in the language already.

cc rust-lang#25725, rust-lang#65819, rust-lang#66145, rust-lang#84147 , and rust-lang#84133 (comment)
jackh726 added a commit to jackh726/rust that referenced this issue Apr 29, 2021
Point out that behavior might be switched on 2015 and 2018 too one day

Reword documentation to make it clear that behaviour can be switched on older editions too, one day in the future. It doesn't *have* to be switched, but I think it's good to have it as an option and re-evaluate it a few months/years down the line when e.g. the crates that showed up in crater were broken by different changes in the language already.

cc rust-lang#25725, rust-lang#65819, rust-lang#66145, rust-lang#84147 , and rust-lang#84133 (comment)
jackh726 added a commit to jackh726/rust that referenced this issue Apr 29, 2021
Point out that behavior might be switched on 2015 and 2018 too one day

Reword documentation to make it clear that behaviour can be switched on older editions too, one day in the future. It doesn't *have* to be switched, but I think it's good to have it as an option and re-evaluate it a few months/years down the line when e.g. the crates that showed up in crater were broken by different changes in the language already.

cc rust-lang#25725, rust-lang#65819, rust-lang#66145, rust-lang#84147 , and rust-lang#84133 (comment)
jackh726 added a commit to jackh726/rust that referenced this issue Apr 29, 2021
Point out that behavior might be switched on 2015 and 2018 too one day

Reword documentation to make it clear that behaviour can be switched on older editions too, one day in the future. It doesn't *have* to be switched, but I think it's good to have it as an option and re-evaluate it a few months/years down the line when e.g. the crates that showed up in crater were broken by different changes in the language already.

cc rust-lang#25725, rust-lang#65819, rust-lang#66145, rust-lang#84147 , and rust-lang#84133 (comment)
nical added a commit to nical/euclid that referenced this issue May 28, 2021
See rust-lang/rust#66145. The removed checks aren't particularly useful and
might turn into build errors in the next rust edition.
nical added a commit to nical/euclid that referenced this issue May 28, 2021
See rust-lang/rust#66145. The removed checks aren't particularly useful and
might turn into build errors in the next rust edition.
nigelmegitt pushed a commit to nigelmegitt/wgmeeting-github-ircbot that referenced this issue Jul 21, 2022
This fixes the following warning issued by rustc 1.41.0:

warning: this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` (due to autoref coercions), but that might change in the future when `IntoIterator` impls for arrays are added.
   --> src/lib.rs:921:55
    |
921 |         ["github:", "github topic:", "github issue:"].into_iter(),
    |                                                       ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter`
    |
    = note: `#[warn(array_into_iter)]` on by default
    = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
    = note: for more information, see issue #66145 <rust-lang/rust#66145>
@workingjubilee workingjubilee added the A-array Area: [T; N] label Mar 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-array Area: [T; N] A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-future-compatibility Category: Future-compatibility lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
Archived in project
Development

No branches or pull requests

8 participants