From b79af2fcde91abeae805c2dbccfdbf7114bfd47b Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Mon, 12 Apr 2021 16:03:53 -0700 Subject: [PATCH 1/9] Implement #[rustc_skip_array_during_method_dispatch] --- compiler/rustc_feature/src/builtin_attrs.rs | 5 +++++ compiler/rustc_metadata/src/rmeta/decoder.rs | 2 ++ compiler/rustc_metadata/src/rmeta/encoder.rs | 1 + compiler/rustc_metadata/src/rmeta/mod.rs | 1 + compiler/rustc_middle/src/ty/trait_def.rs | 7 +++++++ compiler/rustc_span/src/symbol.rs | 1 + compiler/rustc_typeck/src/check/method/probe.rs | 9 +++++++++ compiler/rustc_typeck/src/collect.rs | 13 ++++++++++++- 8 files changed, 38 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_feature/src/builtin_attrs.rs b/compiler/rustc_feature/src/builtin_attrs.rs index f286ea7cdecec..be3caf709c76d 100644 --- a/compiler/rustc_feature/src/builtin_attrs.rs +++ b/compiler/rustc_feature/src/builtin_attrs.rs @@ -540,6 +540,11 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[ rustc_main, Normal, template!(Word), "the `#[rustc_main]` attribute is used internally to specify test entry point function", ), + rustc_attr!( + rustc_skip_array_during_method_dispatch, Normal, template!(Word), + "the `#[rustc_skip_array_during_method_dispatch]` attribute is used to exclude a trait \ + from method dispatch when the receiver is an array, for compatibility in editions < 2021." + ), // ========================================================================== // Internal attributes, Testing: diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index 3d0a9d553b028..19ae5ce69c136 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -757,6 +757,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { data.paren_sugar, data.has_auto_impl, data.is_marker, + data.skip_array_during_method_dispatch, data.specialization_kind, self.def_path_hash(item_id), ) @@ -767,6 +768,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { false, false, false, + false, ty::trait_def::TraitSpecializationKind::None, self.def_path_hash(item_id), ), diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index a5157854e15c0..e8f02b8e66f0a 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -1422,6 +1422,7 @@ impl EncodeContext<'a, 'tcx> { paren_sugar: trait_def.paren_sugar, has_auto_impl: self.tcx.trait_is_auto(def_id), is_marker: trait_def.is_marker, + skip_array_during_method_dispatch: trait_def.skip_array_during_method_dispatch, specialization_kind: trait_def.specialization_kind, }; diff --git a/compiler/rustc_metadata/src/rmeta/mod.rs b/compiler/rustc_metadata/src/rmeta/mod.rs index 7cfb051e703c5..9f665d5daaa03 100644 --- a/compiler/rustc_metadata/src/rmeta/mod.rs +++ b/compiler/rustc_metadata/src/rmeta/mod.rs @@ -385,6 +385,7 @@ struct TraitData { paren_sugar: bool, has_auto_impl: bool, is_marker: bool, + skip_array_during_method_dispatch: bool, specialization_kind: ty::trait_def::TraitSpecializationKind, } diff --git a/compiler/rustc_middle/src/ty/trait_def.rs b/compiler/rustc_middle/src/ty/trait_def.rs index 33065bc3a7bd3..e9b8883f29a48 100644 --- a/compiler/rustc_middle/src/ty/trait_def.rs +++ b/compiler/rustc_middle/src/ty/trait_def.rs @@ -35,6 +35,11 @@ pub struct TraitDef { /// and thus `impl`s of it are allowed to overlap. pub is_marker: bool, + /// If `true`, then this trait has the `#[rustc_skip_array_during_method_dispatch]` + /// attribute, indicating that editions before 2021 should not consider this trait + /// during method dispatch if the receiver is an array. + pub skip_array_during_method_dispatch: bool, + /// Used to determine whether the standard library is allowed to specialize /// on this trait. pub specialization_kind: TraitSpecializationKind, @@ -82,6 +87,7 @@ impl<'tcx> TraitDef { paren_sugar: bool, has_auto_impl: bool, is_marker: bool, + skip_array_during_method_dispatch: bool, specialization_kind: TraitSpecializationKind, def_path_hash: DefPathHash, ) -> TraitDef { @@ -91,6 +97,7 @@ impl<'tcx> TraitDef { paren_sugar, has_auto_impl, is_marker, + skip_array_during_method_dispatch, specialization_kind, def_path_hash, } diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index 52270f0e6277b..a88bcc5c55881 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -1030,6 +1030,7 @@ symbols! { rustc_regions, rustc_reservation_impl, rustc_serialize, + rustc_skip_array_during_method_dispatch, rustc_specialization_trait, rustc_stable, rustc_std_internal_symbol, diff --git a/compiler/rustc_typeck/src/check/method/probe.rs b/compiler/rustc_typeck/src/check/method/probe.rs index 15342235d488a..88fd94931e1e0 100644 --- a/compiler/rustc_typeck/src/check/method/probe.rs +++ b/compiler/rustc_typeck/src/check/method/probe.rs @@ -1461,6 +1461,15 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> { } TraitCandidate(trait_ref) => { + if let Some(method_name) = self.method_name { + // Some trait methods are excluded for arrays before 2021. + if self_ty.is_array() && !method_name.span.rust_2021() { + let trait_def = self.tcx.trait_def(trait_ref.def_id); + if trait_def.skip_array_during_method_dispatch { + return ProbeResult::NoMatch; + } + } + } let predicate = trait_ref.without_const().to_predicate(self.tcx); let obligation = traits::Obligation::new(cause, self.param_env, predicate); if !self.predicate_may_hold(&obligation) { diff --git a/compiler/rustc_typeck/src/collect.rs b/compiler/rustc_typeck/src/collect.rs index 1477418d5d8cf..3692642b6e20d 100644 --- a/compiler/rustc_typeck/src/collect.rs +++ b/compiler/rustc_typeck/src/collect.rs @@ -1191,6 +1191,8 @@ fn trait_def(tcx: TyCtxt<'_>, def_id: DefId) -> ty::TraitDef { } let is_marker = tcx.has_attr(def_id, sym::marker); + let skip_array_during_method_dispatch = + tcx.has_attr(def_id, sym::rustc_skip_array_during_method_dispatch); let spec_kind = if tcx.has_attr(def_id, sym::rustc_unsafe_specialization_marker) { ty::trait_def::TraitSpecializationKind::Marker } else if tcx.has_attr(def_id, sym::rustc_specialization_trait) { @@ -1199,7 +1201,16 @@ fn trait_def(tcx: TyCtxt<'_>, def_id: DefId) -> ty::TraitDef { ty::trait_def::TraitSpecializationKind::None }; let def_path_hash = tcx.def_path_hash(def_id); - ty::TraitDef::new(def_id, unsafety, paren_sugar, is_auto, is_marker, spec_kind, def_path_hash) + ty::TraitDef::new( + def_id, + unsafety, + paren_sugar, + is_auto, + is_marker, + skip_array_during_method_dispatch, + spec_kind, + def_path_hash, + ) } fn has_late_bound_regions<'tcx>(tcx: TyCtxt<'tcx>, node: Node<'tcx>) -> Option { From 32aaea9c8fb99a94e6b289498a348459357c392f Mon Sep 17 00:00:00 2001 From: Lukas Kalbertodt Date: Fri, 25 Oct 2019 17:25:58 +0200 Subject: [PATCH 2/9] Add `IntoIterator` impl `for [T; N]` (arrays by value) --- library/core/src/array/mod.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/library/core/src/array/mod.rs b/library/core/src/array/mod.rs index b6ce825e2477a..bd6f35edfac50 100644 --- a/library/core/src/array/mod.rs +++ b/library/core/src/array/mod.rs @@ -155,6 +155,16 @@ impl fmt::Debug for [T; N] { } } +#[stable(feature = "array_into_iter_impl", since = "1.53.0")] +impl IntoIterator for [T; N] { + type Item = T; + type IntoIter = IntoIter; + + fn into_iter(self) -> Self::IntoIter { + IntoIter::new(self) + } +} + #[stable(feature = "rust1", since = "1.0.0")] impl<'a, T, const N: usize> IntoIterator for &'a [T; N] { type Item = &'a T; From 35b1590223b44a7da28c584c97967cc63f3e4e25 Mon Sep 17 00:00:00 2001 From: Lukas Kalbertodt Date: Tue, 28 Jul 2020 19:03:56 +0200 Subject: [PATCH 3/9] Adjust docs and tests for new `IntoIterator` impl for arrays --- library/std/src/primitive_docs.rs | 23 +---- src/test/ui/iterators/array-of-ranges.rs | 11 +- src/test/ui/iterators/array-of-ranges.stderr | 102 ------------------- src/test/ui/iterators/array.rs | 5 +- src/test/ui/iterators/array.stderr | 36 ------- 5 files changed, 8 insertions(+), 169 deletions(-) delete mode 100644 src/test/ui/iterators/array-of-ranges.stderr delete mode 100644 src/test/ui/iterators/array.stderr diff --git a/library/std/src/primitive_docs.rs b/library/std/src/primitive_docs.rs index 64b22b64f4bf1..ad4737ec2f618 100644 --- a/library/std/src/primitive_docs.rs +++ b/library/std/src/primitive_docs.rs @@ -498,7 +498,7 @@ mod prim_pointer {} /// - [`Copy`] /// - [`Clone`] /// - [`Debug`] -/// - [`IntoIterator`] (implemented for `&[T; N]` and `&mut [T; N]`) +/// - [`IntoIterator`] (implemented for `[T; N]`, `&[T; N]` and `&mut [T; N]`) /// - [`PartialEq`], [`PartialOrd`], [`Eq`], [`Ord`] /// - [`Hash`] /// - [`AsRef`], [`AsMut`] @@ -526,31 +526,16 @@ mod prim_pointer {} /// assert_eq!([1, 2], &array[1..]); /// /// // This loop prints: 0 1 2 -/// for x in &array { +/// for x in array { /// print!("{} ", x); /// } /// ``` /// -/// An array itself is not iterable: -/// -/// ```compile_fail,E0277 -/// let array: [i32; 3] = [0; 3]; -/// -/// for x in array { } -/// // error: the trait bound `[i32; 3]: std::iter::Iterator` is not satisfied -/// ``` -/// -/// The solution is to coerce the array to a slice by calling a slice method: +/// You can also iterate over reference to the array's elements: /// /// ``` -/// # let array: [i32; 3] = [0; 3]; -/// for x in array.iter() { } -/// ``` -/// -/// You can also use the array reference's [`IntoIterator`] implementation: +/// let array: [i32; 3] = [0; 3]; /// -/// ``` -/// # let array: [i32; 3] = [0; 3]; /// for x in &array { } /// ``` /// diff --git a/src/test/ui/iterators/array-of-ranges.rs b/src/test/ui/iterators/array-of-ranges.rs index d2dfc7ec327d3..037540a3e89e6 100644 --- a/src/test/ui/iterators/array-of-ranges.rs +++ b/src/test/ui/iterators/array-of-ranges.rs @@ -1,23 +1,16 @@ +// check-pass + fn main() { for _ in [0..1] {} -//~^ ERROR is not an iterator for _ in [0..=1] {} -//~^ ERROR is not an iterator for _ in [0..] {} -//~^ ERROR is not an iterator for _ in [..1] {} -//~^ ERROR is not an iterator for _ in [..=1] {} -//~^ ERROR is not an iterator let start = 0; let end = 0; for _ in [start..end] {} -//~^ ERROR is not an iterator let array_of_range = [start..end]; for _ in array_of_range {} -//~^ ERROR is not an iterator for _ in [0..1, 2..3] {} -//~^ ERROR is not an iterator for _ in [0..=1] {} -//~^ ERROR is not an iterator } diff --git a/src/test/ui/iterators/array-of-ranges.stderr b/src/test/ui/iterators/array-of-ranges.stderr deleted file mode 100644 index 7d58eb948ea81..0000000000000 --- a/src/test/ui/iterators/array-of-ranges.stderr +++ /dev/null @@ -1,102 +0,0 @@ -error[E0277]: `[std::ops::Range<{integer}>; 1]` is not an iterator - --> $DIR/array-of-ranges.rs:2:14 - | -LL | for _ in [0..1] {} - | ^^^^^^ if you meant to iterate between two values, remove the square brackets - | - = help: the trait `Iterator` is not implemented for `[std::ops::Range<{integer}>; 1]` - = note: `[start..end]` is an array of one `Range`; you might have meant to have a `Range` without the brackets: `start..end` - = note: required because of the requirements on the impl of `IntoIterator` for `[std::ops::Range<{integer}>; 1]` - = note: required by `into_iter` - -error[E0277]: `[RangeInclusive<{integer}>; 1]` is not an iterator - --> $DIR/array-of-ranges.rs:4:14 - | -LL | for _ in [0..=1] {} - | ^^^^^^^ if you meant to iterate between two values, remove the square brackets - | - = help: the trait `Iterator` is not implemented for `[RangeInclusive<{integer}>; 1]` - = note: `[start..=end]` is an array of one `RangeInclusive`; you might have meant to have a `RangeInclusive` without the brackets: `start..=end` - = note: required because of the requirements on the impl of `IntoIterator` for `[RangeInclusive<{integer}>; 1]` - = note: required by `into_iter` - -error[E0277]: `[RangeFrom<{integer}>; 1]` is not an iterator - --> $DIR/array-of-ranges.rs:6:14 - | -LL | for _ in [0..] {} - | ^^^^^ if you meant to iterate from a value onwards, remove the square brackets - | - = help: the trait `Iterator` is not implemented for `[RangeFrom<{integer}>; 1]` - = note: `[start..]` is an array of one `RangeFrom`; you might have meant to have a `RangeFrom` without the brackets: `start..`, keeping in mind that iterating over an unbounded iterator will run forever unless you `break` or `return` from within the loop - = note: required because of the requirements on the impl of `IntoIterator` for `[RangeFrom<{integer}>; 1]` - = note: required by `into_iter` - -error[E0277]: `[RangeTo<{integer}>; 1]` is not an iterator - --> $DIR/array-of-ranges.rs:8:14 - | -LL | for _ in [..1] {} - | ^^^^^ if you meant to iterate until a value, remove the square brackets and add a starting value - | - = help: the trait `Iterator` is not implemented for `[RangeTo<{integer}>; 1]` - = note: `[..end]` is an array of one `RangeTo`; you might have meant to have a bounded `Range` without the brackets: `0..end` - = note: required because of the requirements on the impl of `IntoIterator` for `[RangeTo<{integer}>; 1]` - = note: required by `into_iter` - -error[E0277]: `[RangeToInclusive<{integer}>; 1]` is not an iterator - --> $DIR/array-of-ranges.rs:10:14 - | -LL | for _ in [..=1] {} - | ^^^^^^ if you meant to iterate until a value (including it), remove the square brackets and add a starting value - | - = help: the trait `Iterator` is not implemented for `[RangeToInclusive<{integer}>; 1]` - = note: `[..=end]` is an array of one `RangeToInclusive`; you might have meant to have a bounded `RangeInclusive` without the brackets: `0..=end` - = note: required because of the requirements on the impl of `IntoIterator` for `[RangeToInclusive<{integer}>; 1]` - = note: required by `into_iter` - -error[E0277]: `[std::ops::Range<{integer}>; 1]` is not an iterator - --> $DIR/array-of-ranges.rs:14:14 - | -LL | for _ in [start..end] {} - | ^^^^^^^^^^^^ if you meant to iterate between two values, remove the square brackets - | - = help: the trait `Iterator` is not implemented for `[std::ops::Range<{integer}>; 1]` - = note: `[start..end]` is an array of one `Range`; you might have meant to have a `Range` without the brackets: `start..end` - = note: required because of the requirements on the impl of `IntoIterator` for `[std::ops::Range<{integer}>; 1]` - = note: required by `into_iter` - -error[E0277]: `[std::ops::Range<{integer}>; 1]` is not an iterator - --> $DIR/array-of-ranges.rs:17:14 - | -LL | for _ in array_of_range {} - | ^^^^^^^^^^^^^^ if you meant to iterate between two values, remove the square brackets - | - = help: the trait `Iterator` is not implemented for `[std::ops::Range<{integer}>; 1]` - = note: `[start..end]` is an array of one `Range`; you might have meant to have a `Range` without the brackets: `start..end` - = note: required because of the requirements on the impl of `IntoIterator` for `[std::ops::Range<{integer}>; 1]` - = note: required by `into_iter` - -error[E0277]: `[std::ops::Range<{integer}>; 2]` is not an iterator - --> $DIR/array-of-ranges.rs:19:14 - | -LL | for _ in [0..1, 2..3] {} - | ^^^^^^^^^^^^ arrays do not yet implement `IntoIterator`; try using `std::array::IntoIter::new(arr)` - | - = help: the trait `Iterator` is not implemented for `[std::ops::Range<{integer}>; 2]` - = note: see for more details - = note: required because of the requirements on the impl of `IntoIterator` for `[std::ops::Range<{integer}>; 2]` - = note: required by `into_iter` - -error[E0277]: `[RangeInclusive<{integer}>; 1]` is not an iterator - --> $DIR/array-of-ranges.rs:21:14 - | -LL | for _ in [0..=1] {} - | ^^^^^^^ if you meant to iterate between two values, remove the square brackets - | - = help: the trait `Iterator` is not implemented for `[RangeInclusive<{integer}>; 1]` - = note: `[start..=end]` is an array of one `RangeInclusive`; you might have meant to have a `RangeInclusive` without the brackets: `start..=end` - = note: required because of the requirements on the impl of `IntoIterator` for `[RangeInclusive<{integer}>; 1]` - = note: required by `into_iter` - -error: aborting due to 9 previous errors - -For more information about this error, try `rustc --explain E0277`. diff --git a/src/test/ui/iterators/array.rs b/src/test/ui/iterators/array.rs index 33c84f6fa3582..5985c74e11fdf 100644 --- a/src/test/ui/iterators/array.rs +++ b/src/test/ui/iterators/array.rs @@ -1,9 +1,8 @@ +// check-pass + fn main() { for _ in [1, 2] {} -//~^ ERROR is not an iterator let x = [1, 2]; for _ in x {} -//~^ ERROR is not an iterator for _ in [1.0, 2.0] {} -//~^ ERROR is not an iterator } diff --git a/src/test/ui/iterators/array.stderr b/src/test/ui/iterators/array.stderr deleted file mode 100644 index 7e2b600fb7af2..0000000000000 --- a/src/test/ui/iterators/array.stderr +++ /dev/null @@ -1,36 +0,0 @@ -error[E0277]: `[{integer}; 2]` is not an iterator - --> $DIR/array.rs:2:14 - | -LL | for _ in [1, 2] {} - | ^^^^^^ arrays do not yet implement `IntoIterator`; try using `std::array::IntoIter::new(arr)` - | - = help: the trait `Iterator` is not implemented for `[{integer}; 2]` - = note: see for more details - = note: required because of the requirements on the impl of `IntoIterator` for `[{integer}; 2]` - = note: required by `into_iter` - -error[E0277]: `[{integer}; 2]` is not an iterator - --> $DIR/array.rs:5:14 - | -LL | for _ in x {} - | ^ arrays do not yet implement `IntoIterator`; try using `std::array::IntoIter::new(arr)` - | - = help: the trait `Iterator` is not implemented for `[{integer}; 2]` - = note: see for more details - = note: required because of the requirements on the impl of `IntoIterator` for `[{integer}; 2]` - = note: required by `into_iter` - -error[E0277]: `[{float}; 2]` is not an iterator - --> $DIR/array.rs:7:14 - | -LL | for _ in [1.0, 2.0] {} - | ^^^^^^^^^^ arrays do not yet implement `IntoIterator`; try using `std::array::IntoIter::new(arr)` - | - = help: the trait `Iterator` is not implemented for `[{float}; 2]` - = note: see for more details - = note: required because of the requirements on the impl of `IntoIterator` for `[{float}; 2]` - = note: required by `into_iter` - -error: aborting due to 3 previous errors - -For more information about this error, try `rustc --explain E0277`. From 3dab4e22d43c1ec724b23d301c81bdc2b99d50e7 Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Mon, 12 Apr 2021 16:07:28 -0700 Subject: [PATCH 4/9] Skip into_iter() for arrays before 2021 --- library/core/src/array/mod.rs | 4 ++++ library/core/src/iter/traits/collect.rs | 1 + .../ui/iterators/into-iter-on-arrays-2018.rs | 17 ++++++++++++++ .../iterators/into-iter-on-arrays-2018.stderr | 23 +++++++++++++++++++ .../ui/iterators/into-iter-on-arrays-2021.rs | 15 ++++++++++++ 5 files changed, 60 insertions(+) create mode 100644 src/test/ui/iterators/into-iter-on-arrays-2018.rs create mode 100644 src/test/ui/iterators/into-iter-on-arrays-2018.stderr create mode 100644 src/test/ui/iterators/into-iter-on-arrays-2021.rs diff --git a/library/core/src/array/mod.rs b/library/core/src/array/mod.rs index bd6f35edfac50..ddbdc6a4acf3a 100644 --- a/library/core/src/array/mod.rs +++ b/library/core/src/array/mod.rs @@ -155,6 +155,10 @@ impl fmt::Debug for [T; N] { } } +// Note: the `#[rustc_skip_array_during_method_dispatch]` on `trait IntoIterator` +// hides this implementation from explicit `.into_iter()` calls on editions < 2021, +// so those calls will still resolve to the slice implementation, by reference. +#[cfg(not(bootstrap))] #[stable(feature = "array_into_iter_impl", since = "1.53.0")] impl IntoIterator for [T; N] { type Item = T; diff --git a/library/core/src/iter/traits/collect.rs b/library/core/src/iter/traits/collect.rs index 1ae6d15c12dd9..13a2e24cadd10 100644 --- a/library/core/src/iter/traits/collect.rs +++ b/library/core/src/iter/traits/collect.rs @@ -198,6 +198,7 @@ pub trait FromIterator: Sized { /// } /// ``` #[rustc_diagnostic_item = "IntoIterator"] +#[cfg_attr(not(bootstrap), rustc_skip_array_during_method_dispatch)] #[stable(feature = "rust1", since = "1.0.0")] pub trait IntoIterator { /// The type of the elements being iterated over. diff --git a/src/test/ui/iterators/into-iter-on-arrays-2018.rs b/src/test/ui/iterators/into-iter-on-arrays-2018.rs new file mode 100644 index 0000000000000..cbcfcf2512f5e --- /dev/null +++ b/src/test/ui/iterators/into-iter-on-arrays-2018.rs @@ -0,0 +1,17 @@ +// check-pass + +use std::array::IntoIter; +use std::slice::Iter; + +fn main() { + let array = [0; 10]; + + // Before 2021, the method dispatched to `IntoIterator for &[T]`, + // which we continue to support for compatibility. + let _: Iter<'_, i32> = array.into_iter(); + //~^ WARNING this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` + //~| WARNING this was previously accepted by the compiler but is being phased out + + // But you can always use the trait method explicitly as an array. + let _: IntoIter = IntoIterator::into_iter(array); +} diff --git a/src/test/ui/iterators/into-iter-on-arrays-2018.stderr b/src/test/ui/iterators/into-iter-on-arrays-2018.stderr new file mode 100644 index 0000000000000..7140c01ed0e92 --- /dev/null +++ b/src/test/ui/iterators/into-iter-on-arrays-2018.stderr @@ -0,0 +1,23 @@ +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. + --> $DIR/into-iter-on-arrays-2018.rs:11:34 + | +LL | let _: Iter<'_, i32> = array.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 + +warning: 1 warning emitted + +Future incompatibility report: Future breakage date: None, diagnostic: +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. + --> $DIR/into-iter-on-arrays-2018.rs:11:34 + | +LL | let _: Iter<'_, i32> = array.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 + diff --git a/src/test/ui/iterators/into-iter-on-arrays-2021.rs b/src/test/ui/iterators/into-iter-on-arrays-2021.rs new file mode 100644 index 0000000000000..731971484e82e --- /dev/null +++ b/src/test/ui/iterators/into-iter-on-arrays-2021.rs @@ -0,0 +1,15 @@ +// check-pass +// edition:2021 +// compile-flags: -Zunstable-options + +use std::array::IntoIter; + +fn main() { + let array = [0; 10]; + + // In 2021, the method dispatches to `IntoIterator for [T; N]`. + let _: IntoIter = array.into_iter(); + + // And you can always use the trait method explicitly as an array. + let _: IntoIter = IntoIterator::into_iter(array); +} From 25f02b276f9fb290318beb364c4d0c9e7079071f Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Tue, 13 Apr 2021 12:37:19 -0700 Subject: [PATCH 5/9] Test Box::new(array).into_iter() --- .../ui/iterators/into-iter-on-arrays-2018.rs | 7 +++++- .../iterators/into-iter-on-arrays-2018.stderr | 25 ++++++++++++++++--- .../ui/iterators/into-iter-on-arrays-2021.rs | 1 + 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/test/ui/iterators/into-iter-on-arrays-2018.rs b/src/test/ui/iterators/into-iter-on-arrays-2018.rs index cbcfcf2512f5e..75e0a58039fed 100644 --- a/src/test/ui/iterators/into-iter-on-arrays-2018.rs +++ b/src/test/ui/iterators/into-iter-on-arrays-2018.rs @@ -1,4 +1,5 @@ // check-pass +// edition:2018 use std::array::IntoIter; use std::slice::Iter; @@ -6,12 +7,16 @@ use std::slice::Iter; fn main() { let array = [0; 10]; - // Before 2021, the method dispatched to `IntoIterator for &[T]`, + // Before 2021, the method dispatched to `IntoIterator for &[T; N]`, // which we continue to support for compatibility. let _: Iter<'_, i32> = array.into_iter(); //~^ WARNING this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` //~| WARNING this was previously accepted by the compiler but is being phased out + let _: Iter<'_, i32> = Box::new(array).into_iter(); + //~^ WARNING this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` + //~| WARNING this was previously accepted by the compiler but is being phased out + // But you can always use the trait method explicitly as an array. let _: IntoIter = IntoIterator::into_iter(array); } diff --git a/src/test/ui/iterators/into-iter-on-arrays-2018.stderr b/src/test/ui/iterators/into-iter-on-arrays-2018.stderr index 7140c01ed0e92..b11941fe0e1b2 100644 --- a/src/test/ui/iterators/into-iter-on-arrays-2018.stderr +++ b/src/test/ui/iterators/into-iter-on-arrays-2018.stderr @@ -1,5 +1,5 @@ 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. - --> $DIR/into-iter-on-arrays-2018.rs:11:34 + --> $DIR/into-iter-on-arrays-2018.rs:12:34 | LL | let _: Iter<'_, i32> = array.into_iter(); | ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter` @@ -8,11 +8,20 @@ LL | let _: Iter<'_, i32> = array.into_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 -warning: 1 warning emitted +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. + --> $DIR/into-iter-on-arrays-2018.rs:16:44 + | +LL | let _: Iter<'_, i32> = Box::new(array).into_iter(); + | ^^^^^^^^^ 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 + +warning: 2 warnings emitted Future incompatibility report: Future breakage date: None, diagnostic: 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. - --> $DIR/into-iter-on-arrays-2018.rs:11:34 + --> $DIR/into-iter-on-arrays-2018.rs:12:34 | LL | let _: Iter<'_, i32> = array.into_iter(); | ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter` @@ -21,3 +30,13 @@ LL | let _: Iter<'_, i32> = array.into_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 +Future breakage date: None, diagnostic: +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. + --> $DIR/into-iter-on-arrays-2018.rs:16:44 + | +LL | let _: Iter<'_, i32> = Box::new(array).into_iter(); + | ^^^^^^^^^ 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 + diff --git a/src/test/ui/iterators/into-iter-on-arrays-2021.rs b/src/test/ui/iterators/into-iter-on-arrays-2021.rs index 731971484e82e..1ac31b23b59bd 100644 --- a/src/test/ui/iterators/into-iter-on-arrays-2021.rs +++ b/src/test/ui/iterators/into-iter-on-arrays-2021.rs @@ -9,6 +9,7 @@ fn main() { // In 2021, the method dispatches to `IntoIterator for [T; N]`. let _: IntoIter = array.into_iter(); + let _: IntoIter = Box::new(array).into_iter(); // And you can always use the trait method explicitly as an array. let _: IntoIter = IntoIterator::into_iter(array); From 4d089a41b1faec8b9d4f2a6cc3efcef72eb2d6fe Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Wed, 14 Apr 2021 12:05:24 -0700 Subject: [PATCH 6/9] Test array into_iter with more wrapper types --- .../ui/iterators/into-iter-on-arrays-2018.rs | 17 +++++++++++++++++ .../iterators/into-iter-on-arrays-2018.stderr | 8 ++++---- .../ui/iterators/into-iter-on-arrays-2021.rs | 19 ++++++++++++++++++- 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/src/test/ui/iterators/into-iter-on-arrays-2018.rs b/src/test/ui/iterators/into-iter-on-arrays-2018.rs index 75e0a58039fed..5661397b3c17b 100644 --- a/src/test/ui/iterators/into-iter-on-arrays-2018.rs +++ b/src/test/ui/iterators/into-iter-on-arrays-2018.rs @@ -2,6 +2,8 @@ // edition:2018 use std::array::IntoIter; +use std::ops::Deref; +use std::rc::Rc; use std::slice::Iter; fn main() { @@ -17,6 +19,21 @@ fn main() { //~^ WARNING this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` //~| WARNING this was previously accepted by the compiler but is being phased out + // The `array_into_iter` lint doesn't cover other wrappers that deref to an array. + let _: Iter<'_, i32> = Rc::new(array).into_iter(); + let _: Iter<'_, i32> = Array(array).into_iter(); + // But you can always use the trait method explicitly as an array. let _: IntoIter = IntoIterator::into_iter(array); } + +/// User type that dereferences to an array. +struct Array([i32; 10]); + +impl Deref for Array { + type Target = [i32; 10]; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} diff --git a/src/test/ui/iterators/into-iter-on-arrays-2018.stderr b/src/test/ui/iterators/into-iter-on-arrays-2018.stderr index b11941fe0e1b2..b43338382f20c 100644 --- a/src/test/ui/iterators/into-iter-on-arrays-2018.stderr +++ b/src/test/ui/iterators/into-iter-on-arrays-2018.stderr @@ -1,5 +1,5 @@ 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. - --> $DIR/into-iter-on-arrays-2018.rs:12:34 + --> $DIR/into-iter-on-arrays-2018.rs:14:34 | LL | let _: Iter<'_, i32> = array.into_iter(); | ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter` @@ -9,7 +9,7 @@ LL | let _: Iter<'_, i32> = array.into_iter(); = note: for more information, see issue #66145 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. - --> $DIR/into-iter-on-arrays-2018.rs:16:44 + --> $DIR/into-iter-on-arrays-2018.rs:18:44 | LL | let _: Iter<'_, i32> = Box::new(array).into_iter(); | ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter` @@ -21,7 +21,7 @@ warning: 2 warnings emitted Future incompatibility report: Future breakage date: None, diagnostic: 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. - --> $DIR/into-iter-on-arrays-2018.rs:12:34 + --> $DIR/into-iter-on-arrays-2018.rs:14:34 | LL | let _: Iter<'_, i32> = array.into_iter(); | ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter` @@ -32,7 +32,7 @@ LL | let _: Iter<'_, i32> = array.into_iter(); Future breakage date: None, diagnostic: 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. - --> $DIR/into-iter-on-arrays-2018.rs:16:44 + --> $DIR/into-iter-on-arrays-2018.rs:18:44 | LL | let _: Iter<'_, i32> = Box::new(array).into_iter(); | ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter` diff --git a/src/test/ui/iterators/into-iter-on-arrays-2021.rs b/src/test/ui/iterators/into-iter-on-arrays-2021.rs index 1ac31b23b59bd..ec54ed005173b 100644 --- a/src/test/ui/iterators/into-iter-on-arrays-2021.rs +++ b/src/test/ui/iterators/into-iter-on-arrays-2021.rs @@ -3,6 +3,8 @@ // compile-flags: -Zunstable-options use std::array::IntoIter; +use std::ops::Deref; +use std::rc::Rc; fn main() { let array = [0; 10]; @@ -11,6 +13,21 @@ fn main() { let _: IntoIter = array.into_iter(); let _: IntoIter = Box::new(array).into_iter(); - // And you can always use the trait method explicitly as an array. + // The `array_into_iter` lint doesn't cover other wrappers that deref to an array. + let _: IntoIter = Rc::new(array).into_iter(); + let _: IntoIter = Array(array).into_iter(); + + // You can always use the trait method explicitly as an array. let _: IntoIter = IntoIterator::into_iter(array); } + +/// User type that dereferences to an array. +struct Array([i32; 10]); + +impl Deref for Array { + type Target = [i32; 10]; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} From c020367b820b982a3c0ac86cedc5be6ed732b2fe Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Wed, 14 Apr 2021 12:05:56 -0700 Subject: [PATCH 7/9] Document the edition behavior for array.into_iter() --- library/core/src/array/mod.rs | 8 +++++ library/std/src/primitive_docs.rs | 50 +++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/library/core/src/array/mod.rs b/library/core/src/array/mod.rs index ddbdc6a4acf3a..d4e71f4ef75ee 100644 --- a/library/core/src/array/mod.rs +++ b/library/core/src/array/mod.rs @@ -164,6 +164,14 @@ impl IntoIterator for [T; N] { type Item = T; type IntoIter = IntoIter; + /// Creates a consuming iterator, that is, one that moves each value out of + /// the array (from start to end). The array cannot be used after calling + /// this unless `T` implements `Copy`, so the whole array is copied. + /// + /// Arrays have special behavior when calling `.into_iter()` prior to the + /// 2021 edition -- see the [array] Editions section for more information. + /// + /// [array]: prim@array fn into_iter(self) -> Self::IntoIter { IntoIter::new(self) } diff --git a/library/std/src/primitive_docs.rs b/library/std/src/primitive_docs.rs index ad4737ec2f618..65113bd71d96e 100644 --- a/library/std/src/primitive_docs.rs +++ b/library/std/src/primitive_docs.rs @@ -549,6 +549,56 @@ mod prim_pointer {} /// move_away(roa); /// ``` /// +/// # Editions +/// +/// Prior to Rust 1.53, arrays did not implement `IntoIterator` by value, so the method call +/// `array.into_iter()` auto-referenced into a slice iterator. That behavior is preserved in the +/// 2015 and 2018 editions of Rust for compatability, ignoring `IntoIterator` by value. +/// +/// ```rust,edition2018 +/// # #![allow(array_into_iter)] // override our `deny(warnings)` +/// let array: [i32; 3] = [0; 3]; +/// +/// // This creates a slice iterator, producing references to each value. +/// for item in array.into_iter().enumerate() { +/// let (i, x): (usize, &i32) = item; +/// println!("array[{}] = {}", i, x); +/// } +/// +/// // The `array_into_iter` lint suggests this change for future compatibility: +/// for item in array.iter().enumerate() { +/// let (i, x): (usize, &i32) = item; +/// println!("array[{}] = {}", i, x); +/// } +/// +/// // You can explicitly iterate an array by value using +/// // `IntoIterator::into_iter` or `std::array::IntoIter::new`: +/// for item in IntoIterator::into_iter(array).enumerate() { +/// let (i, x): (usize, i32) = item; +/// println!("array[{}] = {}", i, x); +/// } +/// ``` +/// +/// Starting in the 2021 edition, `array.into_iter()` will use `IntoIterator` normally to iterate +/// by value, and `iter()` should be used to iterate by reference like previous editions. +/// +/// ```rust,edition2021,ignore +/// # // FIXME: ignored because 2021 testing is still unstable +/// let array: [i32; 3] = [0; 3]; +/// +/// // This iterates by reference: +/// for item in array.iter().enumerate() { +/// let (i, x): (usize, &i32) = item; +/// println!("array[{}] = {}", i, x); +/// } +/// +/// // This iterates by value: +/// for item in array.into_iter().enumerate() { +/// let (i, x): (usize, i32) = item; +/// println!("array[{}] = {}", i, x); +/// } +/// ``` +/// /// [slice]: prim@slice /// [`Debug`]: fmt::Debug /// [`Hash`]: hash::Hash From 2a97c690bc56e7e20768d0ce632f0b719bf5ff37 Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Wed, 21 Apr 2021 12:13:55 -0700 Subject: [PATCH 8/9] Note array.into_iter() specifically in method probing --- compiler/rustc_typeck/src/check/method/probe.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/compiler/rustc_typeck/src/check/method/probe.rs b/compiler/rustc_typeck/src/check/method/probe.rs index 88fd94931e1e0..e65e66c32f6d8 100644 --- a/compiler/rustc_typeck/src/check/method/probe.rs +++ b/compiler/rustc_typeck/src/check/method/probe.rs @@ -1463,6 +1463,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> { TraitCandidate(trait_ref) => { if let Some(method_name) = self.method_name { // Some trait methods are excluded for arrays before 2021. + // (`array.into_iter()` wants a slice iterator for compatibility.) if self_ty.is_array() && !method_name.span.rust_2021() { let trait_def = self.tcx.trait_def(trait_ref.def_id); if trait_def.skip_array_during_method_dispatch { From f6a90ca168dfdf198b75bcdc0aba01915a5bd6fb Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Sat, 24 Apr 2021 22:33:51 -0700 Subject: [PATCH 9/9] Ignore array IntoIterator tests in bootstrap --- library/std/src/primitive_docs.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/library/std/src/primitive_docs.rs b/library/std/src/primitive_docs.rs index 65113bd71d96e..4e97fd487254a 100644 --- a/library/std/src/primitive_docs.rs +++ b/library/std/src/primitive_docs.rs @@ -517,7 +517,8 @@ mod prim_pointer {} /// /// # Examples /// -/// ``` +#[cfg_attr(bootstrap, doc = "```ignore")] +#[cfg_attr(not(bootstrap), doc = "```")] /// let mut array: [i32; 3] = [0; 3]; /// /// array[1] = 1; @@ -555,7 +556,8 @@ mod prim_pointer {} /// `array.into_iter()` auto-referenced into a slice iterator. That behavior is preserved in the /// 2015 and 2018 editions of Rust for compatability, ignoring `IntoIterator` by value. /// -/// ```rust,edition2018 +#[cfg_attr(bootstrap, doc = "```rust,edition2018,ignore")] +#[cfg_attr(not(bootstrap), doc = "```rust,edition2018")] /// # #![allow(array_into_iter)] // override our `deny(warnings)` /// let array: [i32; 3] = [0; 3]; ///