From 5626df9c9098fc83518e3bc4cef06e449279ac3c Mon Sep 17 00:00:00 2001 From: Nixon Enraght-Moony Date: Sun, 4 Dec 2022 18:46:57 +0000 Subject: [PATCH] Add `rustc_on_unimplemented` to `Sum` and `Product` trait. --- library/core/src/iter/traits/accum.rs | 8 +++++ src/test/ui/on-unimplemented/sum.rs | 9 ++++++ src/test/ui/on-unimplemented/sum.stderr | 39 +++++++++++++++++++++++++ 3 files changed, 56 insertions(+) create mode 100644 src/test/ui/on-unimplemented/sum.rs create mode 100644 src/test/ui/on-unimplemented/sum.stderr diff --git a/library/core/src/iter/traits/accum.rs b/library/core/src/iter/traits/accum.rs index 84d83ee3969..e31669b3924 100644 --- a/library/core/src/iter/traits/accum.rs +++ b/library/core/src/iter/traits/accum.rs @@ -10,6 +10,10 @@ use crate::num::Wrapping; /// [`sum()`]: Iterator::sum /// [`FromIterator`]: iter::FromIterator #[stable(feature = "iter_arith_traits", since = "1.12.0")] +#[rustc_on_unimplemented( + message = "a value of type `{Self}` cannot be made by summing an iterator over elements of type `{A}`", + label = "value of type `{Self}` cannot be made by summing a `std::iter::Iterator`" +)] pub trait Sum: Sized { /// Method which takes an iterator and generates `Self` from the elements by /// "summing up" the items. @@ -27,6 +31,10 @@ pub trait Sum: Sized { /// [`product()`]: Iterator::product /// [`FromIterator`]: iter::FromIterator #[stable(feature = "iter_arith_traits", since = "1.12.0")] +#[rustc_on_unimplemented( + message = "a value of type `{Self}` cannot be made by multiplying all elements of type `{A}` from an iterator", + label = "value of type `{Self}` cannot be made by multiplying all elements from a `std::iter::Iterator`" +)] pub trait Product: Sized { /// Method which takes an iterator and generates `Self` from the elements by /// multiplying the items. diff --git a/src/test/ui/on-unimplemented/sum.rs b/src/test/ui/on-unimplemented/sum.rs new file mode 100644 index 00000000000..4f1c521d9fe --- /dev/null +++ b/src/test/ui/on-unimplemented/sum.rs @@ -0,0 +1,9 @@ +// + +fn main() { + vec![(), ()].iter().sum::(); + //~^ ERROR + + vec![(), ()].iter().product::(); + //~^ ERROR +} diff --git a/src/test/ui/on-unimplemented/sum.stderr b/src/test/ui/on-unimplemented/sum.stderr new file mode 100644 index 00000000000..0c99e63f0e5 --- /dev/null +++ b/src/test/ui/on-unimplemented/sum.stderr @@ -0,0 +1,39 @@ +error[E0277]: a value of type `i32` cannot be made by summing an iterator over elements of type `&()` + --> $DIR/sum.rs:4:5 + | +LL | vec![(), ()].iter().sum::(); + | ^^^^^^^^^^^^^^^^^^^ --- required by a bound introduced by this call + | | + | value of type `i32` cannot be made by summing a `std::iter::Iterator` + | + = help: the trait `Sum<&()>` is not implemented for `i32` + = help: the following other types implement trait `Sum`: + > + +note: required by a bound in `std::iter::Iterator::sum` + --> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL + | +LL | S: Sum, + | ^^^^^^^^^^^^^^^ required by this bound in `std::iter::Iterator::sum` + +error[E0277]: a value of type `i32` cannot be made by multiplying all elements of type `&()` from an iterator + --> $DIR/sum.rs:7:5 + | +LL | vec![(), ()].iter().product::(); + | ^^^^^^^^^^^^^^^^^^^ ------- required by a bound introduced by this call + | | + | value of type `i32` cannot be made by multiplying all elements from a `std::iter::Iterator` + | + = help: the trait `Product<&()>` is not implemented for `i32` + = help: the following other types implement trait `Product`: + > + +note: required by a bound in `std::iter::Iterator::product` + --> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL + | +LL | P: Product, + | ^^^^^^^^^^^^^^^^^^^ required by this bound in `std::iter::Iterator::product` + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0277`.