Skip to content

Commit 959616e

Browse files
Handle inference variables in CollectAllMismatches correctly
1 parent ef4046e commit 959616e

File tree

5 files changed

+78
-5
lines changed

5 files changed

+78
-5
lines changed

compiler/rustc_trait_selection/src/traits/error_reporting/method_chain.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl<'a, 'tcx> TypeRelation<'tcx> for CollectAllMismatches<'a, 'tcx> {
5555

5656
fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
5757
self.infcx.probe(|_| {
58-
if a.is_ty_infer() || b.is_ty_infer() {
58+
if a.is_ty_var() || b.is_ty_var() {
5959
Ok(a)
6060
} else {
6161
self.infcx.super_combine_tys(self, a, b).or_else(|e| {
@@ -71,10 +71,13 @@ impl<'a, 'tcx> TypeRelation<'tcx> for CollectAllMismatches<'a, 'tcx> {
7171
a: ty::Const<'tcx>,
7272
b: ty::Const<'tcx>,
7373
) -> RelateResult<'tcx, ty::Const<'tcx>> {
74-
if a == b {
75-
return Ok(a);
76-
}
77-
relate::super_relate_consts(self, a, b) // could do something similar here for constants!
74+
self.infcx.probe(|_| {
75+
if a.is_ct_infer() || b.is_ct_infer() {
76+
Ok(a)
77+
} else {
78+
relate::super_relate_consts(self, a, b) // could do something similar here for constants!
79+
}
80+
})
7881
}
7982

8083
fn binders<T: Relate<'tcx>>(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
struct Foo<T, const N: usize> {
2+
array: [T; N],
3+
}
4+
5+
trait Bar<const N: usize> {}
6+
7+
impl<T, const N: usize> Foo<T, N> {
8+
fn trigger(self) {
9+
self.unsatisfied()
10+
//~^ ERROR the trait bound `T: Bar<N>` is not satisfied
11+
}
12+
13+
fn unsatisfied(self)
14+
where
15+
T: Bar<N>,
16+
{
17+
}
18+
}
19+
20+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error[E0277]: the trait bound `T: Bar<N>` is not satisfied
2+
--> $DIR/ct-var-in-collect_all_mismatches.rs:9:14
3+
|
4+
LL | self.unsatisfied()
5+
| ^^^^^^^^^^^ the trait `Bar<N>` is not implemented for `T`
6+
|
7+
note: required by a bound in `Foo::<T, N>::unsatisfied`
8+
--> $DIR/ct-var-in-collect_all_mismatches.rs:15:12
9+
|
10+
LL | fn unsatisfied(self)
11+
| ----------- required by a bound in this
12+
LL | where
13+
LL | T: Bar<N>,
14+
| ^^^^^^ required by this bound in `Foo::<T, N>::unsatisfied`
15+
help: consider restricting type parameter `T`
16+
|
17+
LL | impl<T: Bar<N>, const N: usize> Foo<T, N> {
18+
| ++++++++
19+
20+
error: aborting due to previous error
21+
22+
For more information about this error, try `rustc --explain E0277`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
fn main() {
2+
let x = Some(()).iter().map(|()| 1).sum::<f32>();
3+
//~^ ERROR a value of type `f32` cannot be made by summing an iterator over elements of type `{integer}`
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
error[E0277]: a value of type `f32` cannot be made by summing an iterator over elements of type `{integer}`
2+
--> $DIR/invalid-iterator-chain-with-int-infer.rs:2:41
3+
|
4+
LL | let x = Some(()).iter().map(|()| 1).sum::<f32>();
5+
| ^^^ value of type `f32` cannot be made by summing a `std::iter::Iterator<Item={integer}>`
6+
|
7+
= help: the trait `Sum<{integer}>` is not implemented for `f32`
8+
= help: the following other types implement trait `Sum<A>`:
9+
<f32 as Sum<&'a f32>>
10+
<f32 as Sum>
11+
note: the method call chain might not have had the expected associated types
12+
--> $DIR/invalid-iterator-chain-with-int-infer.rs:2:29
13+
|
14+
LL | let x = Some(()).iter().map(|()| 1).sum::<f32>();
15+
| -------- ------ ^^^^^^^^^^^ `Iterator::Item` changed to `{integer}` here
16+
| | |
17+
| | `Iterator::Item` is `&()` here
18+
| this expression has type `Option<()>`
19+
note: required by a bound in `std::iter::Iterator::sum`
20+
--> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL
21+
22+
error: aborting due to previous error
23+
24+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)