summaryrefslogtreecommitdiffstats
path: root/tests/ui/polymorphization/predicates.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:19:03 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:19:03 +0000
commit64d98f8ee037282c35007b64c2649055c56af1db (patch)
tree5492bcf97fce41ee1c0b1cc2add283f3e66cdab0 /tests/ui/polymorphization/predicates.rs
parentAdding debian version 1.67.1+dfsg1-1. (diff)
downloadrustc-64d98f8ee037282c35007b64c2649055c56af1db.tar.xz
rustc-64d98f8ee037282c35007b64c2649055c56af1db.zip
Merging upstream version 1.68.2+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests/ui/polymorphization/predicates.rs')
-rw-r--r--tests/ui/polymorphization/predicates.rs95
1 files changed, 95 insertions, 0 deletions
diff --git a/tests/ui/polymorphization/predicates.rs b/tests/ui/polymorphization/predicates.rs
new file mode 100644
index 000000000..6a5fc2e33
--- /dev/null
+++ b/tests/ui/polymorphization/predicates.rs
@@ -0,0 +1,95 @@
+// build-fail
+// compile-flags: -Copt-level=0 -Zpolymorphize=on
+
+#![feature(rustc_attrs)]
+
+// This test checks that `T` is considered used in `foo`, because it is used in a predicate for
+// `I`, which is used.
+
+#[rustc_polymorphize_error]
+fn bar<I>() {
+ //~^ ERROR item has unused generic parameters
+}
+
+#[rustc_polymorphize_error]
+fn foo<I, T>(_: I)
+//~^ ERROR item has unused generic parameters
+where
+ I: Iterator<Item = T>,
+{
+ bar::<I>()
+}
+
+#[rustc_polymorphize_error]
+fn baz<I, T>(_: I)
+//~^ ERROR item has unused generic parameters
+where
+ std::iter::Repeat<I>: Iterator<Item = T>,
+{
+ bar::<I>()
+}
+
+// In addition, check that `I` is considered used in `next::{{closure}}`, because `T` is used and
+// `T` is really just `I::Item`. `E` is used due to the fixed-point marking of predicates.
+
+pub(crate) struct Foo<'a, I, E>(I, &'a E);
+
+impl<'a, I, T: 'a, E> Iterator for Foo<'a, I, E>
+where
+ I: Iterator<Item = &'a (T, E)>,
+{
+ type Item = T;
+
+ #[rustc_polymorphize_error]
+ fn next(&mut self) -> Option<Self::Item> {
+ self.find(|_| true)
+ //~^ ERROR item has unused generic parameters
+ }
+}
+
+// Furthermore, check that `B` is considered used because `C` is used, and that `A` is considered
+// used because `B` is now used.
+
+trait Baz<Z> {}
+
+impl Baz<u16> for u8 {}
+impl Baz<u32> for u16 {}
+
+#[rustc_polymorphize_error]
+fn quux<A, B, C: Default>() -> usize
+//~^ ERROR item has unused generic parameters
+where
+ A: Baz<B>,
+ B: Baz<C>,
+{
+ std::mem::size_of::<C>()
+}
+
+// Finally, check that `F` is considered used because `G` is used when neither are in the self-ty
+// of the predicate.
+
+trait Foobar<F, G> {}
+
+impl Foobar<u32, u32> for () {}
+
+#[rustc_polymorphize_error]
+fn foobar<F, G>() -> usize
+//~^ ERROR item has unused generic parameters
+where
+ (): Foobar<F, G>,
+{
+ std::mem::size_of::<G>()
+}
+
+fn main() {
+ let x = &[2u32];
+ foo(x.iter());
+ baz(x.iter());
+
+ let mut a = Foo([(1u32, 1u16)].iter(), &1u16);
+ let _ = a.next();
+
+ let _ = quux::<u8, u16, u32>();
+
+ let _ = foobar::<u32, u32>();
+}