Skip to content

Commit 856027a

Browse files
authored
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.
2 parents 4fae589 + 5626df9 commit 856027a

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

library/core/src/iter/traits/accum.rs

+8
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ use crate::num::Wrapping;
1010
/// [`sum()`]: Iterator::sum
1111
/// [`FromIterator`]: iter::FromIterator
1212
#[stable(feature = "iter_arith_traits", since = "1.12.0")]
13+
#[rustc_on_unimplemented(
14+
message = "a value of type `{Self}` cannot be made by summing an iterator over elements of type `{A}`",
15+
label = "value of type `{Self}` cannot be made by summing a `std::iter::Iterator<Item={A}>`"
16+
)]
1317
pub trait Sum<A = Self>: Sized {
1418
/// Method which takes an iterator and generates `Self` from the elements by
1519
/// "summing up" the items.
@@ -27,6 +31,10 @@ pub trait Sum<A = Self>: Sized {
2731
/// [`product()`]: Iterator::product
2832
/// [`FromIterator`]: iter::FromIterator
2933
#[stable(feature = "iter_arith_traits", since = "1.12.0")]
34+
#[rustc_on_unimplemented(
35+
message = "a value of type `{Self}` cannot be made by multiplying all elements of type `{A}` from an iterator",
36+
label = "value of type `{Self}` cannot be made by multiplying all elements from a `std::iter::Iterator<Item={A}>`"
37+
)]
3038
pub trait Product<A = Self>: Sized {
3139
/// Method which takes an iterator and generates `Self` from the elements by
3240
/// multiplying the items.

src/test/ui/on-unimplemented/sum.rs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// <https://github.com/rust-lang/rust/issues/105184>
2+
3+
fn main() {
4+
vec![(), ()].iter().sum::<i32>();
5+
//~^ ERROR
6+
7+
vec![(), ()].iter().product::<i32>();
8+
//~^ ERROR
9+
}
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
error[E0277]: a value of type `i32` cannot be made by summing an iterator over elements of type `&()`
2+
--> $DIR/sum.rs:4:5
3+
|
4+
LL | vec![(), ()].iter().sum::<i32>();
5+
| ^^^^^^^^^^^^^^^^^^^ --- required by a bound introduced by this call
6+
| |
7+
| value of type `i32` cannot be made by summing a `std::iter::Iterator<Item=&()>`
8+
|
9+
= help: the trait `Sum<&()>` is not implemented for `i32`
10+
= help: the following other types implement trait `Sum<A>`:
11+
<i32 as Sum<&'a i32>>
12+
<i32 as Sum>
13+
note: required by a bound in `std::iter::Iterator::sum`
14+
--> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL
15+
|
16+
LL | S: Sum<Self::Item>,
17+
| ^^^^^^^^^^^^^^^ required by this bound in `std::iter::Iterator::sum`
18+
19+
error[E0277]: a value of type `i32` cannot be made by multiplying all elements of type `&()` from an iterator
20+
--> $DIR/sum.rs:7:5
21+
|
22+
LL | vec![(), ()].iter().product::<i32>();
23+
| ^^^^^^^^^^^^^^^^^^^ ------- required by a bound introduced by this call
24+
| |
25+
| value of type `i32` cannot be made by multiplying all elements from a `std::iter::Iterator<Item=&()>`
26+
|
27+
= help: the trait `Product<&()>` is not implemented for `i32`
28+
= help: the following other types implement trait `Product<A>`:
29+
<i32 as Product<&'a i32>>
30+
<i32 as Product>
31+
note: required by a bound in `std::iter::Iterator::product`
32+
--> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL
33+
|
34+
LL | P: Product<Self::Item>,
35+
| ^^^^^^^^^^^^^^^^^^^ required by this bound in `std::iter::Iterator::product`
36+
37+
error: aborting due to 2 previous errors
38+
39+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)