summaryrefslogtreecommitdiffstats
path: root/tests/ui/higher-rank-trait-bounds/hrtb-perfect-forwarding.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:19:13 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:19:13 +0000
commit218caa410aa38c29984be31a5229b9fa717560ee (patch)
treec54bd55eeb6e4c508940a30e94c0032fbd45d677 /tests/ui/higher-rank-trait-bounds/hrtb-perfect-forwarding.rs
parentReleasing progress-linux version 1.67.1+dfsg1-1~progress7.99u1. (diff)
downloadrustc-218caa410aa38c29984be31a5229b9fa717560ee.tar.xz
rustc-218caa410aa38c29984be31a5229b9fa717560ee.zip
Merging upstream version 1.68.2+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests/ui/higher-rank-trait-bounds/hrtb-perfect-forwarding.rs')
-rw-r--r--tests/ui/higher-rank-trait-bounds/hrtb-perfect-forwarding.rs56
1 files changed, 56 insertions, 0 deletions
diff --git a/tests/ui/higher-rank-trait-bounds/hrtb-perfect-forwarding.rs b/tests/ui/higher-rank-trait-bounds/hrtb-perfect-forwarding.rs
new file mode 100644
index 000000000..d45fa183c
--- /dev/null
+++ b/tests/ui/higher-rank-trait-bounds/hrtb-perfect-forwarding.rs
@@ -0,0 +1,56 @@
+// Test a case where you have an impl of `Foo<X>` for all `X` that
+// is being applied to `for<'a> Foo<&'a mut X>`. Issue #19730.
+
+trait Foo<X> {
+ fn foo(&mut self, x: X) {}
+}
+
+trait Bar<X> {
+ fn bar(&mut self, x: X) {}
+}
+
+impl<'a, X, F> Foo<X> for &'a mut F where F: Foo<X> + Bar<X> {}
+
+impl<'a, X, F> Bar<X> for &'a mut F where F: Bar<X> {}
+
+fn no_hrtb<'b, T>(mut t: T) //~ WARN function cannot return
+where
+ T: Bar<&'b isize>,
+{
+ // OK -- `T : Bar<&'b isize>`, and thus the impl above ensures that
+ // `&mut T : Bar<&'b isize>`.
+ no_hrtb(&mut t);
+}
+
+fn bar_hrtb<T>(mut t: T) //~ WARN function cannot return
+where
+ T: for<'b> Bar<&'b isize>,
+{
+ // OK -- `T : for<'b> Bar<&'b isize>`, and thus the impl above
+ // ensures that `&mut T : for<'b> Bar<&'b isize>`. This is an
+ // example of a "perfect forwarding" impl.
+ bar_hrtb(&mut t);
+}
+
+fn foo_hrtb_bar_not<'b, T>(mut t: T) //~ WARN function cannot return
+where
+ T: for<'a> Foo<&'a isize> + Bar<&'b isize>,
+{
+ // Not OK -- The forwarding impl for `Foo` requires that `Bar` also
+ // be implemented. Thus to satisfy `&mut T : for<'a> Foo<&'a
+ // isize>`, we require `T : for<'a> Bar<&'a isize>`, but the where
+ // clause only specifies `T : Bar<&'b isize>`.
+ foo_hrtb_bar_not(&mut t);
+ //~^ ERROR implementation of `Bar` is not general enough
+ //~^^ ERROR lifetime may not live long enough
+}
+
+fn foo_hrtb_bar_hrtb<T>(mut t: T) //~ WARN function cannot return
+where
+ T: for<'a> Foo<&'a isize> + for<'b> Bar<&'b isize>,
+{
+ // OK -- now we have `T : for<'b> Bar<&'b isize>`.
+ foo_hrtb_bar_hrtb(&mut t);
+}
+
+fn main() {}