Rollup merge of #105265 - aDotInTheVoid:sum-product-on-unimplemented, r=estebank
Add `rustc_on_unimplemented` to `Sum` and `Product` trait. Helps with #105184, but I don't think it fully fixes it.
This commit is contained in:
commit
856027a73a
@ -10,6 +10,10 @@ use crate::num::Wrapping;
|
|||||||
/// [`sum()`]: Iterator::sum
|
/// [`sum()`]: Iterator::sum
|
||||||
/// [`FromIterator`]: iter::FromIterator
|
/// [`FromIterator`]: iter::FromIterator
|
||||||
#[stable(feature = "iter_arith_traits", since = "1.12.0")]
|
#[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<Item={A}>`"
|
||||||
|
)]
|
||||||
pub trait Sum<A = Self>: Sized {
|
pub trait Sum<A = Self>: Sized {
|
||||||
/// Method which takes an iterator and generates `Self` from the elements by
|
/// Method which takes an iterator and generates `Self` from the elements by
|
||||||
/// "summing up" the items.
|
/// "summing up" the items.
|
||||||
@ -27,6 +31,10 @@ pub trait Sum<A = Self>: Sized {
|
|||||||
/// [`product()`]: Iterator::product
|
/// [`product()`]: Iterator::product
|
||||||
/// [`FromIterator`]: iter::FromIterator
|
/// [`FromIterator`]: iter::FromIterator
|
||||||
#[stable(feature = "iter_arith_traits", since = "1.12.0")]
|
#[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<Item={A}>`"
|
||||||
|
)]
|
||||||
pub trait Product<A = Self>: Sized {
|
pub trait Product<A = Self>: Sized {
|
||||||
/// Method which takes an iterator and generates `Self` from the elements by
|
/// Method which takes an iterator and generates `Self` from the elements by
|
||||||
/// multiplying the items.
|
/// multiplying the items.
|
||||||
|
9
src/test/ui/on-unimplemented/sum.rs
Normal file
9
src/test/ui/on-unimplemented/sum.rs
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
// <https://github.com/rust-lang/rust/issues/105184>
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
vec![(), ()].iter().sum::<i32>();
|
||||||
|
//~^ ERROR
|
||||||
|
|
||||||
|
vec![(), ()].iter().product::<i32>();
|
||||||
|
//~^ ERROR
|
||||||
|
}
|
39
src/test/ui/on-unimplemented/sum.stderr
Normal file
39
src/test/ui/on-unimplemented/sum.stderr
Normal file
@ -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::<i32>();
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^ --- required by a bound introduced by this call
|
||||||
|
| |
|
||||||
|
| value of type `i32` cannot be made by summing a `std::iter::Iterator<Item=&()>`
|
||||||
|
|
|
||||||
|
= help: the trait `Sum<&()>` is not implemented for `i32`
|
||||||
|
= help: the following other types implement trait `Sum<A>`:
|
||||||
|
<i32 as Sum<&'a i32>>
|
||||||
|
<i32 as Sum>
|
||||||
|
note: required by a bound in `std::iter::Iterator::sum`
|
||||||
|
--> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL
|
||||||
|
|
|
||||||
|
LL | S: Sum<Self::Item>,
|
||||||
|
| ^^^^^^^^^^^^^^^ 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::<i32>();
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^ ------- required by a bound introduced by this call
|
||||||
|
| |
|
||||||
|
| value of type `i32` cannot be made by multiplying all elements from a `std::iter::Iterator<Item=&()>`
|
||||||
|
|
|
||||||
|
= help: the trait `Product<&()>` is not implemented for `i32`
|
||||||
|
= help: the following other types implement trait `Product<A>`:
|
||||||
|
<i32 as Product<&'a i32>>
|
||||||
|
<i32 as Product>
|
||||||
|
note: required by a bound in `std::iter::Iterator::product`
|
||||||
|
--> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL
|
||||||
|
|
|
||||||
|
LL | P: Product<Self::Item>,
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^ 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`.
|
Loading…
x
Reference in New Issue
Block a user