From 959616ef44fe919f297bba1c3ddc65bd8f2432fc Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Sat, 31 Dec 2022 03:03:28 +0000 Subject: [PATCH] Handle inference variables in CollectAllMismatches correctly --- .../traits/error_reporting/method_chain.rs | 13 ++++++---- .../ct-var-in-collect_all_mismatches.rs | 20 ++++++++++++++++ .../ct-var-in-collect_all_mismatches.stderr | 22 +++++++++++++++++ .../invalid-iterator-chain-with-int-infer.rs | 4 ++++ ...valid-iterator-chain-with-int-infer.stderr | 24 +++++++++++++++++++ 5 files changed, 78 insertions(+), 5 deletions(-) create mode 100644 tests/ui/consts/ct-var-in-collect_all_mismatches.rs create mode 100644 tests/ui/consts/ct-var-in-collect_all_mismatches.stderr create mode 100644 tests/ui/iterators/invalid-iterator-chain-with-int-infer.rs create mode 100644 tests/ui/iterators/invalid-iterator-chain-with-int-infer.stderr diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/method_chain.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/method_chain.rs index 27c207528c7..ba9ee57d409 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/method_chain.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/method_chain.rs @@ -55,7 +55,7 @@ impl<'a, 'tcx> TypeRelation<'tcx> for CollectAllMismatches<'a, 'tcx> { fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> { self.infcx.probe(|_| { - if a.is_ty_infer() || b.is_ty_infer() { + if a.is_ty_var() || b.is_ty_var() { Ok(a) } else { self.infcx.super_combine_tys(self, a, b).or_else(|e| { @@ -71,10 +71,13 @@ impl<'a, 'tcx> TypeRelation<'tcx> for CollectAllMismatches<'a, 'tcx> { a: ty::Const<'tcx>, b: ty::Const<'tcx>, ) -> RelateResult<'tcx, ty::Const<'tcx>> { - if a == b { - return Ok(a); - } - relate::super_relate_consts(self, a, b) // could do something similar here for constants! + self.infcx.probe(|_| { + if a.is_ct_infer() || b.is_ct_infer() { + Ok(a) + } else { + relate::super_relate_consts(self, a, b) // could do something similar here for constants! + } + }) } fn binders>( diff --git a/tests/ui/consts/ct-var-in-collect_all_mismatches.rs b/tests/ui/consts/ct-var-in-collect_all_mismatches.rs new file mode 100644 index 00000000000..5fb633de983 --- /dev/null +++ b/tests/ui/consts/ct-var-in-collect_all_mismatches.rs @@ -0,0 +1,20 @@ +struct Foo { + array: [T; N], +} + +trait Bar {} + +impl Foo { + fn trigger(self) { + self.unsatisfied() + //~^ ERROR the trait bound `T: Bar` is not satisfied + } + + fn unsatisfied(self) + where + T: Bar, + { + } +} + +fn main() {} diff --git a/tests/ui/consts/ct-var-in-collect_all_mismatches.stderr b/tests/ui/consts/ct-var-in-collect_all_mismatches.stderr new file mode 100644 index 00000000000..43fba2573ff --- /dev/null +++ b/tests/ui/consts/ct-var-in-collect_all_mismatches.stderr @@ -0,0 +1,22 @@ +error[E0277]: the trait bound `T: Bar` is not satisfied + --> $DIR/ct-var-in-collect_all_mismatches.rs:9:14 + | +LL | self.unsatisfied() + | ^^^^^^^^^^^ the trait `Bar` is not implemented for `T` + | +note: required by a bound in `Foo::::unsatisfied` + --> $DIR/ct-var-in-collect_all_mismatches.rs:15:12 + | +LL | fn unsatisfied(self) + | ----------- required by a bound in this +LL | where +LL | T: Bar, + | ^^^^^^ required by this bound in `Foo::::unsatisfied` +help: consider restricting type parameter `T` + | +LL | impl, const N: usize> Foo { + | ++++++++ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0277`. diff --git a/tests/ui/iterators/invalid-iterator-chain-with-int-infer.rs b/tests/ui/iterators/invalid-iterator-chain-with-int-infer.rs new file mode 100644 index 00000000000..882a1d13954 --- /dev/null +++ b/tests/ui/iterators/invalid-iterator-chain-with-int-infer.rs @@ -0,0 +1,4 @@ +fn main() { + let x = Some(()).iter().map(|()| 1).sum::(); + //~^ ERROR a value of type `f32` cannot be made by summing an iterator over elements of type `{integer}` +} diff --git a/tests/ui/iterators/invalid-iterator-chain-with-int-infer.stderr b/tests/ui/iterators/invalid-iterator-chain-with-int-infer.stderr new file mode 100644 index 00000000000..3cb5e44c711 --- /dev/null +++ b/tests/ui/iterators/invalid-iterator-chain-with-int-infer.stderr @@ -0,0 +1,24 @@ +error[E0277]: a value of type `f32` cannot be made by summing an iterator over elements of type `{integer}` + --> $DIR/invalid-iterator-chain-with-int-infer.rs:2:41 + | +LL | let x = Some(()).iter().map(|()| 1).sum::(); + | ^^^ value of type `f32` cannot be made by summing a `std::iter::Iterator` + | + = help: the trait `Sum<{integer}>` is not implemented for `f32` + = help: the following other types implement trait `Sum`: + > + +note: the method call chain might not have had the expected associated types + --> $DIR/invalid-iterator-chain-with-int-infer.rs:2:29 + | +LL | let x = Some(()).iter().map(|()| 1).sum::(); + | -------- ------ ^^^^^^^^^^^ `Iterator::Item` changed to `{integer}` here + | | | + | | `Iterator::Item` is `&()` here + | this expression has type `Option<()>` +note: required by a bound in `std::iter::Iterator::sum` + --> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0277`.