about summary refs log tree commit diff
path: root/tests/ui/span/dropck-object-cycle.rs
diff options
context:
space:
mode:
authorAlbert Larsan <74931857+albertlarsan68@users.noreply.github.com>2023-01-05 09:13:28 +0100
committerAlbert Larsan <74931857+albertlarsan68@users.noreply.github.com>2023-01-11 09:32:08 +0000
commitcf2dff2b1e3fa55fa5415d524200070d0d7aacfe (patch)
tree40a88d9a46aaf3e8870676eb2538378b75a263eb /tests/ui/span/dropck-object-cycle.rs
parentca855e6e42787ecd062d81d53336fe6788ef51a9 (diff)
downloadrust-cf2dff2b1e3fa55fa5415d524200070d0d7aacfe.tar.gz
rust-cf2dff2b1e3fa55fa5415d524200070d0d7aacfe.zip
Move /src/test to /tests
Diffstat (limited to 'tests/ui/span/dropck-object-cycle.rs')
-rw-r--r--tests/ui/span/dropck-object-cycle.rs47
1 files changed, 47 insertions, 0 deletions
diff --git a/tests/ui/span/dropck-object-cycle.rs b/tests/ui/span/dropck-object-cycle.rs
new file mode 100644
index 00000000000..a26123d5246
--- /dev/null
+++ b/tests/ui/span/dropck-object-cycle.rs
@@ -0,0 +1,47 @@
+// This test used to be part of a run-pass test, but revised outlives
+// rule means that it no longer compiles.
+
+#![allow(unused_variables)]
+
+trait Trait<'a> {
+    fn long(&'a self) -> isize;
+    fn short<'b>(&'b self) -> isize;
+}
+
+fn object_invoke1<'d>(x: &'d dyn Trait<'d>) -> (isize, isize) { loop { } }
+
+trait MakerTrait {
+    fn mk() -> Self;
+}
+
+fn make_val<T:MakerTrait>() -> T {
+    MakerTrait::mk()
+}
+
+impl<'t> MakerTrait for Box<dyn Trait<'t>+'static> {
+    fn mk() -> Box<dyn Trait<'t>+'static> { loop { } }
+}
+
+pub fn main() {
+    let m : Box<dyn Trait+'static> = make_val();
+    assert_eq!(object_invoke1(&*m), (4,5));
+    //~^ ERROR `*m` does not live long enough
+
+    // the problem here is that the full type of `m` is
+    //
+    //   Box<Trait<'m>+'static>
+    //
+    // Here `'m` must be exactly the lifetime of the variable `m`.
+    // This is because of two requirements:
+    // 1. First, the basic type rules require that the
+    //    type of `m`'s value outlives the lifetime of `m`. This puts a lower
+    //    bound `'m`.
+    //
+    // 2. Meanwhile, the signature of `object_invoke1` requires that
+    //    we create a reference of type `&'d Trait<'d>` for some `'d`.
+    //    `'d` cannot outlive `'m`, so that forces the lifetime to be `'m`.
+    //
+    // This then conflicts with the dropck rules, which require that
+    // the type of `m` *strictly outlives* `'m`. Hence we get an
+    // error.
+}